mirror of
https://github.com/apache/cloudstack.git
synced 2025-10-26 08:42:29 +01:00
Allow PropertiesUtil to read from jar files.
PropertiesUtil has code for reading from jar files, but the findConfigFile method will prevent it from ever returning a file in a jar on the classpath since it always wants to have a "file:" URL and use the File class. This commit moves the jar file loading attempt from a catch block to an else clause, executed if a config file:// URL could not be found. Signed-off-by: Daan Hoogland <daan.hoogland@gmail.com> This closes #358
This commit is contained in:
parent
0326fb3b5c
commit
db69c8e82b
@ -21,7 +21,6 @@ package com.cloud.utils;
|
|||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileInputStream;
|
import java.io.FileInputStream;
|
||||||
import java.io.FileNotFoundException;
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
@ -44,6 +43,7 @@ public class PropertiesUtil {
|
|||||||
public static File findConfigFile(String path) {
|
public static File findConfigFile(String path) {
|
||||||
ClassLoader cl = PropertiesUtil.class.getClassLoader();
|
ClassLoader cl = PropertiesUtil.class.getClassLoader();
|
||||||
URL url = cl.getResource(path);
|
URL url = cl.getResource(path);
|
||||||
|
|
||||||
if (url != null && "file".equals(url.getProtocol())) {
|
if (url != null && "file".equals(url.getProtocol())) {
|
||||||
return new File(url.getFile());
|
return new File(url.getFile());
|
||||||
}
|
}
|
||||||
@ -124,6 +124,15 @@ public class PropertiesUtil {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void loadFromJar(Properties properties, String configFile) throws IOException {
|
||||||
|
InputStream stream = PropertiesUtil.openStreamFromURL(configFile);
|
||||||
|
if (stream != null) {
|
||||||
|
properties.load(stream);
|
||||||
|
} else {
|
||||||
|
s_logger.error("Unable to find properties file: " + configFile);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Returns key=value pairs by parsing a commands.properties/config file
|
// Returns key=value pairs by parsing a commands.properties/config file
|
||||||
// with syntax; key=cmd;value (with this syntax cmd is stripped) and key=value
|
// with syntax; key=cmd;value (with this syntax cmd is stripped) and key=value
|
||||||
public static Map<String, String> processConfigFile(String[] configFiles) {
|
public static Map<String, String> processConfigFile(String[] configFiles) {
|
||||||
@ -134,22 +143,18 @@ public class PropertiesUtil {
|
|||||||
if (commandsFile != null) {
|
if (commandsFile != null) {
|
||||||
try {
|
try {
|
||||||
loadFromFile(preProcessedCommands, commandsFile);
|
loadFromFile(preProcessedCommands, 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) {
|
|
||||||
try {
|
|
||||||
preProcessedCommands.load(stream);
|
|
||||||
} catch (IOException e) {
|
|
||||||
s_logger.error("IO Exception, unable to find properties file:", fnfex);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
s_logger.error("Unable to find properites file", fnfex);
|
|
||||||
}
|
|
||||||
} catch (IOException ioe) {
|
} catch (IOException ioe) {
|
||||||
s_logger.error("IO Exception loading properties file", ioe);
|
s_logger.error("IO Exception loading properties file", ioe);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
else {
|
||||||
|
// in case of a file within a jar in classpath, try to open stream using url
|
||||||
|
try {
|
||||||
|
loadFromJar(preProcessedCommands, configFile);
|
||||||
|
} catch (IOException e) {
|
||||||
|
s_logger.error("IO Exception loading properties file from jar", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for (Object key : preProcessedCommands.keySet()) {
|
for (Object key : preProcessedCommands.keySet()) {
|
||||||
@ -158,6 +163,7 @@ public class PropertiesUtil {
|
|||||||
String value = preProcessedCommand.substring(splitIndex + 1);
|
String value = preProcessedCommand.substring(splitIndex + 1);
|
||||||
configMap.put((String)key, value);
|
configMap.put((String)key, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
return configMap;
|
return configMap;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user