Skip to content

Commit

Permalink
Merge pull request #55 from ikedam/feature/JENKINS-19990_SupportNonas…
Browse files Browse the repository at this point in the history
…ciiProperties

[JENKINS-19990] Support Non-ascii properties
  • Loading branch information
ikedam committed Jan 3, 2014
2 parents 4f7219e + e470582 commit fea0e12
Show file tree
Hide file tree
Showing 12 changed files with 488 additions and 11 deletions.
12 changes: 12 additions & 0 deletions pom.xml
Expand Up @@ -61,6 +61,13 @@
<version>1.3.1</version>
<optional>true</optional>
</dependency>
<dependency>
<!-- Used for @IgnoreJRERequirement -->
<groupId>org.jvnet</groupId>
<artifactId>animal-sniffer-annotation</artifactId>
<version>1.0</version>
<optional>true</optional>
</dependency>
</dependencies>

<build>
Expand Down Expand Up @@ -141,5 +148,10 @@
<url>http://repo.jenkins-ci.org/public/</url>
</pluginRepository>
</pluginRepositories>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>
</project>

Expand Up @@ -12,6 +12,7 @@
import hudson.model.AbstractBuild;
import hudson.model.TaskListener;
import hudson.remoting.VirtualChannel;
import hudson.util.FormValidation;
import hudson.util.LogTaskListener;
import hudson.util.StreamTaskListener;
import hudson.util.VariableResolver;
Expand All @@ -25,11 +26,16 @@

import java.io.IOException;
import java.io.PrintStream;
import java.nio.charset.Charset;
import java.nio.charset.IllegalCharsetNameException;
import java.nio.charset.UnsupportedCharsetException;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.lang.StringUtils;
import org.apache.tools.ant.DirectoryScanner;
import org.apache.tools.ant.types.FileSet;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.QueryParameter;


/**
Expand Down Expand Up @@ -77,14 +83,20 @@ public String getDescription() {
}

private final String filePattern;
private final String encoding;
private final NoFilesFoundEnum noFilesFoundAction;

@DataBoundConstructor
public FileBuildParameterFactory(String filePattern, NoFilesFoundEnum noFilesFoundAction) {
public FileBuildParameterFactory(String filePattern, String encoding, NoFilesFoundEnum noFilesFoundAction) {
this.filePattern = filePattern;
this.encoding = Util.fixEmptyAndTrim(encoding);
this.noFilesFoundAction = noFilesFoundAction;
}

public FileBuildParameterFactory(String filePattern, NoFilesFoundEnum noFilesFoundAction) {
this(filePattern, null, noFilesFoundAction);
}

public FileBuildParameterFactory(String filePattern) {
this(filePattern, NoFilesFoundEnum.SKIP);
}
Expand All @@ -93,6 +105,10 @@ public String getFilePattern() {
return filePattern;
}

public String getEncoding() {
return encoding;
}

public NoFilesFoundEnum getNoFilesFoundAction() {
return noFilesFoundAction;
}
Expand All @@ -109,7 +125,7 @@ public List<AbstractBuildParameters> getParameters(AbstractBuild<?, ?> build, Ta
noFilesFoundAction.failCheck(listener);
} else {
for(FilePath f: files) {
String parametersStr = f.readToString();
String parametersStr = ParameterizedTriggerUtils.readFileToString(f, getEncoding());
Logger.getLogger(FileBuildParameterFactory.class.getName()).log(Level.INFO, null, "Triggering build with " + f.getName());
result.add(new PredefinedBuildParameters(parametersStr));
}
Expand All @@ -135,5 +151,21 @@ public static class DescriptorImpl extends AbstractBuildParameterFactoryDescript
public String getDisplayName() {
return Messages.FileBuildParameterFactory_FileBuildParameterFactory();
}

public FormValidation doCheckEncoding(@QueryParameter String encoding) {
if (!StringUtils.isBlank(encoding)) {
try {
Charset.forName(encoding.trim());
} catch(UnsupportedCharsetException e) {
return FormValidation.error("Unsupported Encoding");
} catch(IllegalCharsetNameException e) {
return FormValidation.error("Bad Encoding Name");
}
if(!ParameterizedTriggerUtils.isSupportNonAsciiPropertiesFile()) {
return FormValidation.warning("Non-ascii properties files are supported only since Java 1.6.");
}
}
return FormValidation.ok();
}
}
}
Expand Up @@ -11,27 +11,38 @@
import hudson.model.ParametersAction;
import hudson.model.StringParameterValue;
import hudson.model.TaskListener;
import hudson.util.FormValidation;

import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.IllegalCharsetNameException;
import java.nio.charset.UnsupportedCharsetException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import org.apache.tools.ant.filters.StringInputStream;
import org.apache.commons.lang.StringUtils;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.QueryParameter;

public class FileBuildParameters extends AbstractBuildParameters {

private final String propertiesFile;
private final String encoding;
private final boolean failTriggerOnMissing;

@DataBoundConstructor
public FileBuildParameters(String propertiesFile, boolean failTriggerOnMissing) {
public FileBuildParameters(String propertiesFile, String encoding, boolean failTriggerOnMissing) {
this.propertiesFile = propertiesFile;
this.encoding = Util.fixEmptyAndTrim(encoding);
this.failTriggerOnMissing = failTriggerOnMissing;
}

public FileBuildParameters(String propertiesFile, boolean failTriggerOnMissing) {
this(propertiesFile, null, failTriggerOnMissing);
}

public FileBuildParameters(String propertiesFile) {
this(propertiesFile, false);
}
Expand Down Expand Up @@ -65,10 +76,9 @@ public Action getAction(AbstractBuild<?,?> build, TaskListener listener)
continue;
}

String s = f.readToString();
String s = ParameterizedTriggerUtils.readFileToString(f, getEncoding());
s = env.expand(s);
Properties p = new Properties();
p.load(new StringInputStream(s));
Properties p = ParameterizedTriggerUtils.loadProperties(s);

for (Map.Entry<Object, Object> entry : p.entrySet()) {
values.add(new StringParameterValue(entry.getKey().toString(),
Expand All @@ -84,6 +94,10 @@ public String getPropertiesFile() {
return propertiesFile;
}

public String getEncoding() {
return encoding;
}

public boolean getFailTriggerOnMissing() {
return failTriggerOnMissing;
}
Expand All @@ -94,6 +108,22 @@ public static class DescriptorImpl extends Descriptor<AbstractBuildParameters> {
public String getDisplayName() {
return "Parameters from properties file";
}

public FormValidation doCheckEncoding(@QueryParameter String encoding) {
if (!StringUtils.isBlank(encoding)) {
try {
Charset.forName(encoding.trim());
} catch(UnsupportedCharsetException e) {
return FormValidation.error("Unsupported Encoding");
} catch(IllegalCharsetNameException e) {
return FormValidation.error("Bad Encoding Name");
}
if(!ParameterizedTriggerUtils.isSupportNonAsciiPropertiesFile()) {
return FormValidation.warning("Non-ascii properties files are supported only since Java 1.6.");
}
}
return FormValidation.ok();
}
}

}
@@ -0,0 +1,93 @@
/*
* The MIT License
*
* Copyright (c) 2013 IKEDA Yasuyuki
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

package hudson.plugins.parameterizedtrigger;

import hudson.FilePath;

import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.io.StringReader;
import java.util.Properties;

import org.apache.commons.io.IOUtils;
import org.apache.tools.ant.filters.StringInputStream;
import org.jvnet.animal_sniffer.IgnoreJRERequirement;

/**
* Common utility methods.
*/
public class ParameterizedTriggerUtils {
/**
* Load properties from string.
* Extracted for sanitize JRE dependency.
*
* @param properties
* @return
* @throws IOException
*/
@IgnoreJRERequirement
public static Properties loadProperties(String properties) throws IOException {
Properties p = new Properties();
try {
p.load(new StringReader(properties));
} catch(NoSuchMethodError _) {
// {@link Properties#load(java.io.Reader)} is supported since Java 1.6
// When used with Java1.5, fall back to
// {@link Properties#load(java.io.InputStream)}, which does not support
// Non-ascii strings.
p.load(new StringInputStream(properties));
}
return p;
}

/**
* {@link FilePath#readToString()} with encoding.
*
* @param f file to read
* @param encoding null for platform default encoding.
* @return read string
* @throws IOException
*/
public static String readFileToString(FilePath f, String encoding) throws IOException {
InputStream in = f.read();
try {
return IOUtils.toString(in, encoding);
} finally {
in.close();
}
}

public static boolean isSupportNonAsciiPropertiesFile() {
try {
// Is {@link Properties#load(java.io.Reader)} supported?
Properties.class.getMethod("load", Reader.class);
} catch(NoSuchMethodException _) {
return false;
}
return true;
}

}
Expand Up @@ -16,7 +16,6 @@
import java.util.Map;
import java.util.Properties;

import org.apache.tools.ant.filters.StringInputStream;
import org.kohsuke.stapler.DataBoundConstructor;

public class PredefinedBuildParameters extends AbstractBuildParameters {
Expand All @@ -33,8 +32,7 @@ public Action getAction(AbstractBuild<?,?> build, TaskListener listener)

EnvVars env = getEnvironment(build, listener);

Properties p = new Properties();
p.load(new StringInputStream(properties));
Properties p = ParameterizedTriggerUtils.loadProperties(getProperties());

List<ParameterValue> values = new ArrayList<ParameterValue>();
for (Map.Entry<Object, Object> entry : p.entrySet()) {
Expand Down
Expand Up @@ -6,5 +6,10 @@
<f:entry field="noFilesFoundAction" title="${%Action to perform when no files found}">
<f:enum>${it.getDescription()}</f:enum>
</f:entry>
<f:advanced>
<f:entry field="encoding" title="${%File Encoding}">
<f:textbox />
</f:entry>
</f:advanced>

</j:jelly>
@@ -0,0 +1,5 @@
<div>
Specify the encoding of contents of the files.
If not specified, default encoding of the platform is used.
This does not work with Java 1.5, and you need write files with ISO 8859-1 character encoding.
</div>
Expand Up @@ -6,5 +6,10 @@
<f:entry field="failTriggerOnMissing" title="${%Don't trigger if any files are missing}">
<f:checkbox default="false" />
</f:entry>
<f:advanced>
<f:entry field="encoding" title="${%File Encoding}">
<f:textbox />
</f:entry>
</f:advanced>

</j:jelly>
@@ -0,0 +1,5 @@
<div>
Specify the encoding of contents of the files.
If not specified, default encoding of the platform is used.
This does not work with Java 1.5, and you need write files with ISO 8859-1 character encoding.
</div>

0 comments on commit fea0e12

Please sign in to comment.