Skip to content
Permalink
Browse files
Fixed JENKINS-10916
  • Loading branch information
gboissinot committed Sep 7, 2011
1 parent 4bb05b7 commit 00f5fefacd740b0594f9bc87c1dfb5a257d6b74c
@@ -68,6 +68,7 @@ public Environment setUp(AbstractBuild build, final Launcher launcher, final Bui

} catch (Throwable throwable) {
build.setResult(Result.FAILURE);
return null;
}

return new Environment() {
@@ -5,6 +5,10 @@
*/
public class EnvInjectException extends Exception {

public EnvInjectException(String s) {
super(s);
}

public EnvInjectException(Throwable throwable) {
super(throwable);
}
@@ -18,4 +18,9 @@ public EnvInjectLogger(TaskListener listener) {
public void info(String message) {
listener.getLogger().println("[EnvInject] - " + message);
}

public void error(String message) {
listener.getLogger().println("[EnvInject] - [ERROR] - " + message);
}

}
@@ -16,26 +16,25 @@
/**
* Get a map environment variables from a properties file path
*
* @param filePath
* @return
* @param propertiesFile the properties file
* @return a map containing all the file properties content
* @throws EnvInjectException
*/
public Map<String, String> getVarsFromPropertiesFilePath(String filePath) throws EnvInjectException {
public Map<String, String> getVarsFromPropertiesFile(File propertiesFile) throws EnvInjectException {

if (filePath == null) {
throw new NullPointerException("The file path object must be set.");
if (propertiesFile == null) {
throw new NullPointerException("The properties file object must be set.");
}
if (!propertiesFile.exists()) {
throw new NullPointerException("The properties file object must be exist.");
}

Map<String, String> result = new HashMap<String, String>();

File f = new File(filePath);
if (!f.exists()) {
return result;
}
Properties properties = new Properties();
FileReader fileReader = null;
try {
fileReader = new FileReader(f);
fileReader = new FileReader(propertiesFile);
properties.load(fileReader);
} catch (IOException ioe) {
throw new EnvInjectException("Problem occurs on loading content", ioe);
@@ -1,17 +1,21 @@
package org.jenkinsci.plugins.envinject.service;

import hudson.FilePath;
import hudson.Util;
import hudson.remoting.Callable;
import hudson.remoting.VirtualChannel;
import org.jenkinsci.plugins.envinject.EnvInjectException;
import org.jenkinsci.plugins.envinject.EnvInjectInfo;
import org.jenkinsci.plugins.envinject.EnvInjectLogger;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
* @author Gregory Boissinot
*/
public class PropertiesVariablesRetriever implements Callable<Map<String, String>, Throwable> {
public class PropertiesVariablesRetriever implements FilePath.FileCallable<Map<String, String>> {

private EnvInjectInfo info;

@@ -25,29 +29,50 @@ public PropertiesVariablesRetriever(EnvInjectInfo info, Map<String, String> curr
this.logger = logger;
}

public Map<String, String> call() throws Throwable {

public Map<String, String> invoke(File base, VirtualChannel channel) throws IOException, InterruptedException {
Map<String, String> result = new HashMap<String, String>();

PropertiesFileService propertiesFileService = new PropertiesFileService();
try {

//Add the properties file
if (info.getPropertiesFilePath() != null) {
String scriptFilePath = Util.replaceMacro(info.getPropertiesFilePath(), currentEnvVars);
scriptFilePath = scriptFilePath.replace("\\", "/");
logger.info(String.format("Injecting as environment variables the properties file path '%s'", scriptFilePath));
result.putAll(propertiesFileService.getVarsFromPropertiesFilePath(scriptFilePath));
}
PropertiesFileService propertiesFileService = new PropertiesFileService();

//Add the properties file
if (info.getPropertiesFilePath() != null) {
String propertiesFilePath = Util.replaceMacro(info.getPropertiesFilePath(), currentEnvVars);
propertiesFilePath = propertiesFilePath.replace("\\", "/");
File propertiesFile = getFile(base, propertiesFilePath);
if (propertiesFile == null) {
throw new EnvInjectException(String.format("The given properties file path '%s' doesn't exist.", propertiesFilePath));
}
logger.info(String.format("Injecting as environment variables the properties file path '%s'", propertiesFilePath));
result.putAll(propertiesFileService.getVarsFromPropertiesFile(propertiesFile));
}

//Add the properties content
if (info.getPropertiesContent() != null) {
String content = Util.replaceMacro(info.getPropertiesContent(), currentEnvVars);
logger.info(String.format("Injecting as environment variables the properties content \n '%s' \n", content));
result.putAll(propertiesFileService.getVarsFromPropertiesContent(content));
//Add the properties content
if (info.getPropertiesContent() != null) {
String content = Util.replaceMacro(info.getPropertiesContent(), currentEnvVars);
logger.info(String.format("Injecting as environment variables the properties content \n '%s' \n", content));
result.putAll(propertiesFileService.getVarsFromPropertiesContent(content));
}

} catch (EnvInjectException envEx) {
logger.error("Exception occurs " + envEx.getMessage());
throw new IOException(envEx);
}

return result;
}

private File getFile(File base, String scriptFilePath) {

File file = new File(scriptFilePath);
if (file.exists()) {
return file;
}

file = new File(base, scriptFilePath);
return file.exists() ? file : null;
}


}

0 comments on commit 00f5fef

Please sign in to comment.