1
4 package nl.justobjects.pushlet.core;
5
6 import nl.justobjects.pushlet.util.Log;
7 import nl.justobjects.pushlet.util.PushletException;
8 import nl.justobjects.pushlet.util.Sys;
9
10import java.io.File;
11import java.util.Properties;
12
13
19public class Config implements ConfigDefs {
20 private static final String PROPERTIES_FILE = "pushlet.properties";
21 private static Properties properties;
22
23
30 public static Object createObject(String aClassNameProp, String aDefault) throws PushletException {
31 Class clazz = getClass(aClassNameProp, aDefault);
32 try {
33 return clazz.newInstance();
34 } catch (Throwable t) {
35 throw new PushletException("Cannot instantiate class for " + aClassNameProp + "=" + clazz, t);
37 }
38 }
39
40
47 public static Class getClass(String aClassNameProp, String aDefault) throws PushletException {
48 String clazz = (aDefault == null ? getProperty(aClassNameProp) : getProperty(aClassNameProp, aDefault));
51
52 try {
53 return Class.forName(clazz);
54 } catch (ClassNotFoundException t) {
55 throw new PushletException("Cannot find class for " + aClassNameProp + "=" + clazz, t);
57 }
58 }
59
60
63 public static void load(String aDirPath) {
64 try {
66 Log.info("Config: loading " + PROPERTIES_FILE + " from classpath");
68 properties = Sys.loadPropertiesResource(PROPERTIES_FILE);
69 } catch (Throwable t) {
70 String filePath = aDirPath + File.separator + PROPERTIES_FILE;
72 Log.info("Config: cannot load " + PROPERTIES_FILE + " from classpath, will try from " + filePath);
73
74 try {
75 properties = Sys.loadPropertiesFile(filePath);
76 } catch (Throwable t2) {
77 Log.fatal("Config: cannot load properties file from " + filePath, t);
78
79 return;
81 }
82 }
83
84 Log.info("Config: loaded values=" + properties);
85 }
86
87 public static String getProperty(String aName, String aDefault) {
88 return properties.getProperty(aName, aDefault);
89 }
90
91 public static String getProperty(String aName) {
92 String value = properties.getProperty(aName);
93 if (value == null) {
94 throw new IllegalArgumentException("Unknown property: " + aName);
95 }
96 return value;
97 }
98
99 public static boolean getBoolProperty(String aName) {
00 String value = getProperty(aName);
01 try {
02 return value.equals("true");
03 } catch (Throwable t) {
04 throw new IllegalArgumentException("Illegal property value: " + aName + " val=" + value);
05 }
06 }
07
08 public static int getIntProperty(String aName) {
09 String value = getProperty(aName);
10 try {
11 return Integer.parseInt(value);
12 } catch (Throwable t) {
13 throw new IllegalArgumentException("Illegal property value: " + aName + " val=" + value);
14 }
15 }
16
17 public static long getLongProperty(String aName) {
18 String value = getProperty(aName);
19 try {
20 return Long.parseLong(value);
21 } catch (Throwable t) {
22 throw new IllegalArgumentException("Illegal property value: " + aName + " val=" + value);
23 }
24 }
25
26 public static boolean hasProperty(String aName) {
27 return properties.containsKey(aName);
28 }
29
30
31}
32
33
53