Skip to content
Permalink
Browse files

[JENKINS-19990] Supports property files with non-ascii characters. Th…

…is feature only works properly in Java 1.6, but Java 1.5 is still supported.
  • Loading branch information...
ikedam committed Dec 5, 2013
1 parent 2b63546 commit 221df5e4ef878e4767ca4000bdb75b4b423c0421
@@ -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>
@@ -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;
@@ -25,11 +26,14 @@

import java.io.IOException;
import java.io.PrintStream;
import java.nio.charset.Charset;
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;


/**
@@ -77,14 +81,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);
}
@@ -93,6 +103,10 @@ public String getFilePattern() {
return filePattern;
}

public String getEncoding() {
return encoding;
}

public NoFilesFoundEnum getNoFilesFoundAction() {
return noFilesFoundAction;
}
@@ -109,7 +123,7 @@ public NoFilesFoundEnum getNoFilesFoundAction() {
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));
}
@@ -135,5 +149,19 @@ private FilePath getWorkspace(AbstractBuild build) {
public String getDisplayName() {
return Messages.FileBuildParameterFactory_FileBuildParameterFactory();
}

public FormValidation doCheckEncoding(@QueryParameter String encoding) {
if (!StringUtils.isBlank(encoding)) {
try {
Charset.forName(encoding.trim());
} catch(Exception e) {
return FormValidation.error(e, "Unsupported Encoding");
}
if(!ParameterizedTriggerUtils.isSupportNonAsciiPropertiesFile()) {
return FormValidation.warning("Non-ascii properties files are supported only since Java 1.6.");
}
}
return FormValidation.ok();
}
}
}
@@ -11,27 +11,36 @@
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.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);
}
@@ -65,10 +74,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(),
@@ -84,6 +92,10 @@ public String getPropertiesFile() {
return propertiesFile;
}

public String getEncoding() {
return encoding;
}

public boolean getFailTriggerOnMissing() {
return failTriggerOnMissing;
}
@@ -94,6 +106,20 @@ public boolean getFailTriggerOnMissing() {
public String getDisplayName() {
return "Parameters from properties file";
}

public FormValidation doCheckEncoding(@QueryParameter String encoding) {
if (!StringUtils.isBlank(encoding)) {
try {
Charset.forName(encoding.trim());
} catch(Exception e) {
return FormValidation.error(e, "Unsupported Encoding");
}
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(LinkageError _) {
// {@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(Exception _) {
return false;
}
return true;
}

}
@@ -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 {
@@ -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()) {
@@ -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>
@@ -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 221df5e

Please sign in to comment.
You can’t perform that action at this time.