Skip to content

Commit

Permalink
Added some functionality to PropertyManager.
Browse files Browse the repository at this point in the history
  • Loading branch information
Serg-Maximchuk committed Jan 12, 2016
1 parent b3bdbb2 commit 2965cea
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion src/main/java/imcode/util/PropertyManager.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package imcode.util;

import imcode.server.Imcms;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
Expand Down Expand Up @@ -56,6 +58,16 @@ public static void setConfigPathsByRoot(String rootPath) {
setServerPropertiesPath(new File(configPath, SERVER_PROPERTIES_FILENAME));
}

/**
* This static method must be called before any of the other static methods
*
* @param rootPath the root path of web application
*/
public static void setConfigPathsByRoot(File rootPath) {
setConfigPath(new File(rootPath, DEFAULT_PROPERTIES_CONFIG_PATH));
setServerPropertiesPath(new File(configPath, SERVER_PROPERTIES_FILENAME));
}

public static void setConfigPath(File confPath) {
configPath = confPath;
}
Expand Down Expand Up @@ -125,6 +137,9 @@ public static String getServerConfProperty(String property) throws IOException {
* @throws IOException Throws {@code IOException} if file not found.
*/
public static Properties getServerConfProperties() throws IOException {
if (null == configPath || null == serverPropertiesPath) {
setConfigPathsByRoot(Imcms.getPath());
}
return getProperties(serverPropertiesPath);
}

Expand All @@ -143,14 +158,19 @@ public static void flush() {
* @throws IOException Throws {@code IOException} if file not found.
*/
private static Properties getProperties(File file) throws IOException {
if (null == configPath) {
setConfigPathsByRoot(Imcms.getPath());
}
Properties properties = CACHE.get(file);
if (properties == null) {
try (FileInputStream in = new FileInputStream(file)) {
properties = new Properties();
properties.load(in);
CACHE.put(file, properties);
} catch (IOException ex) {
throw new IOException("PropertyManager: File not found: " + file.getAbsolutePath());
IOException newEx = new IOException("PropertyManager: File not found: " + file.getAbsolutePath());
newEx.initCause(ex);
throw newEx;
}
}
return properties;
Expand Down

0 comments on commit 2965cea

Please sign in to comment.