1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package com.geekologue.md4j.config;
17
18 import java.io.File;
19 import java.io.FileInputStream;
20 import java.io.IOException;
21 import java.io.InputStream;
22 import java.net.URL;
23 import javax.naming.InitialContext;
24 import org.apache.commons.configuration.PropertiesConfiguration;
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27
28 /***
29 * @author manos
30 */
31 public class Configuration {
32 private static Log log = LogFactory
33 .getLog(Configuration.class);
34
35 private static boolean initialized = false;
36
37 private static Configuration instance;
38
39 private PropertiesConfiguration config;
40
41 /***
42 * @return the unique instance of this class
43 * @throws Exception
44 */
45 public static Configuration getInstance() throws Exception {
46 if (instance == null) {
47 configure();
48 }
49 return instance;
50 }
51
52 /***
53 * @throws Exception
54 */
55 public static synchronized void configure() throws Exception {
56 configure("/md4j.properties");
57 }
58
59 /***
60 * @param resource
61 * @throws Exception
62 */
63 public static synchronized void configure(String resource) throws Exception {
64 System.out.println("configuring from resource: " + resource);
65 InputStream stream = getStream(resource);
66 configure(stream);
67 }
68
69 /***
70 * @param url
71 * @throws Exception
72 * @throws IOException
73 */
74 public static synchronized void configure(URL url) throws IOException,
75 Exception {
76 System.out.println("configuring from url: " + url.toString());
77 configure(url.openStream());
78 }
79
80 /***
81 * @param configFile
82 * @throws Exception
83 */
84 public static synchronized void configure(File configFile) throws Exception {
85 System.out.println("configuring from file: " + configFile.getName());
86 try {
87 configure(new FileInputStream(configFile));
88 } catch (Exception e) {
89 throw e;
90 }
91 }
92
93 protected static synchronized InputStream getStream(String resource) {
94 InputStream stream = Configuration.class.getResourceAsStream(resource);
95 if (stream == null)
96 stream = Thread.currentThread().getContextClassLoader()
97 .getResourceAsStream(resource);
98 if (stream == null) {
99 throw new RuntimeException(resource + " not found");
100 }
101 return stream;
102 }
103
104 /***
105 *
106 * @param is
107 * @throws Exception
108 */
109 public static synchronized void configure(InputStream is) throws Exception {
110 try {
111 System.out.println("configuring from stream: " + is);
112 if (!initialized) {
113 PropertiesConfiguration config = new PropertiesConfiguration();
114 config.load(is);
115 instance = new Configuration(config);
116 initialized = true;
117 if (true) {
118 System.out.println("Inititalized");
119 }
120 } else {
121 if (true) {
122 System.out.println("Already inititalized");
123 }
124 }
125 } catch (Exception e) {
126 e.printStackTrace();
127 }
128 }
129
130 private Configuration(PropertiesConfiguration config) {
131 this.config = config;
132 }
133
134 /***
135 * @param key
136 * @return the property identified by the given key, if any
137 */
138 public Object getProperty(String key) {
139 return this.config.getProperty(key);
140 }
141
142 /***
143 * @param packageName
144 * @return the namespace identified by the given package, if any
145 */
146 public String getNamespace(String packageName) {
147 return this.config.getString(packageName);
148 }
149 }