API Server changes needed to read a properties file present within a jar provided in classpath.

This commit is contained in:
prachi 2011-11-29 17:59:15 -08:00
parent 7de9c06aee
commit 0769a3b3f3
2 changed files with 34 additions and 1 deletions

View File

@ -23,6 +23,7 @@ import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InterruptedIOException;
import java.io.UnsupportedEncodingException;
import java.net.InetAddress;
@ -201,7 +202,19 @@ public class ApiServer implements HttpRequestHandler {
if (apiConfig != null) {
for (String configFile : apiConfig) {
File commandsFile = PropertiesUtil.findConfigFile(configFile);
preProcessedCommands.load(new FileInputStream(commandsFile));
if(commandsFile != null){
try{
preProcessedCommands.load(new FileInputStream(commandsFile));
}catch (FileNotFoundException fnfex) {
//in case of a file within a jar in classpath, try to open stream using url
InputStream stream = PropertiesUtil.openStreamFromURL(configFile);
if(stream != null){
preProcessedCommands.load(stream);
}else{
s_logger.error("Unable to find properites file", fnfex);
}
}
}
}
for (Object key : preProcessedCommands.keySet()) {
String preProcessedCommand = preProcessedCommands.getProperty((String) key);

View File

@ -18,6 +18,8 @@
package com.cloud.utils;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
@ -94,4 +96,22 @@ public class PropertiesUtil {
return map;
}
/*
* Returns an InputStream for the given resource
* This is needed to read the files within a jar in classpath.
*/
public static InputStream openStreamFromURL(String path){
ClassLoader cl = PropertiesUtil.class.getClassLoader();
URL url = cl.getResource(path);
if (url != null) {
try{
InputStream stream = url.openStream();
return stream;
} catch (IOException ioex) {
return null;
}
}
return null;
}
}