Remove the redundant gss top-level directory.
[pithos] / src / gr / ebs / gss / server / configuration / GSSConfigurationFactory.java
1 /*\r
2  * Copyright 2006, 2007, 2008, 2009 Electronic Business Systems Ltd.\r
3  *\r
4  * This file is part of GSS.\r
5  *\r
6  * GSS is free software: you can redistribute it and/or modify\r
7  * it under the terms of the GNU General Public License as published by\r
8  * the Free Software Foundation, either version 3 of the License, or\r
9  * (at your option) any later version.\r
10  *\r
11  * GSS is distributed in the hope that it will be useful,\r
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
14  * GNU General Public License for more details.\r
15  *\r
16  * You should have received a copy of the GNU General Public License\r
17  * along with GSS.  If not, see <http://www.gnu.org/licenses/>.\r
18  */\r
19 package gr.ebs.gss.server.configuration;\r
20 \r
21 import java.io.File;\r
22 import java.net.MalformedURLException;\r
23 import java.net.URL;\r
24 \r
25 import org.apache.commons.configuration.BaseConfiguration;\r
26 import org.apache.commons.configuration.ConfigurationException;\r
27 import org.apache.commons.configuration.ConfigurationUtils;\r
28 import org.apache.commons.configuration.DataConfiguration;\r
29 import org.apache.commons.configuration.PropertiesConfiguration;\r
30 import org.apache.commons.configuration.reloading.FileChangedReloadingStrategy;\r
31 import org.apache.commons.logging.Log;\r
32 import org.apache.commons.logging.LogFactory;\r
33 \r
34 /**\r
35  * Singleton that loads properties from gss.properties and\r
36  * makes them available to all\r
37  *\r
38  * @author droutsis\r
39  */\r
40 public final class GSSConfigurationFactory {\r
41 \r
42         /**\r
43          * The logger.\r
44          */\r
45         private static Log logger = LogFactory.getLog(GSSConfigurationFactory.class);\r
46 \r
47         /**\r
48          * The filename of the file containing system properties.\r
49          */\r
50         private static String configFilename = "gss.properties";\r
51 \r
52         /**\r
53          * The (single) configuration object\r
54          */\r
55         private static DataConfiguration configuration = null;\r
56 \r
57         /**\r
58          * Exists only to defeat instantiation.\r
59          */\r
60         private GSSConfigurationFactory() {\r
61         }\r
62 \r
63         /**\r
64          * Because multiple classloaders are commonly used in many situations -\r
65          * including servlet containers - you can wind up with multiple singleton instances\r
66          * no matter how carefully you've implemented your singleton classes.\r
67          * If you want to make sure the same classloader loads your singletons,\r
68          * you must specify the classloader yourself; for example:\r
69          *\r
70          * @param classname\r
71          * @return\r
72          * @throws ClassNotFoundException\r
73          */\r
74         private static Class getClass(String classname) throws ClassNotFoundException {\r
75               ClassLoader classLoader = Thread.currentThread().getContextClassLoader();\r
76               if(classLoader == null)\r
77                  classLoader = GSSConfigurationFactory.class.getClassLoader();\r
78               return classLoader.loadClass(classname);\r
79         }\r
80 \r
81         /**\r
82          * @return the configuration object\r
83          */\r
84         public synchronized static DataConfiguration getConfiguration() {\r
85                 try {\r
86                         if (configuration==null) {\r
87                                 PropertiesConfiguration gssConfig = (PropertiesConfiguration) getClass(PropertiesConfiguration.class.getCanonicalName()).newInstance();\r
88                                 gssConfig.setBasePath("");\r
89                                 gssConfig.setFileName(configFilename);\r
90                                 gssConfig.setEncoding("ISO-8859-7");\r
91                                 // Set automatic reloading.\r
92                                 gssConfig.setReloadingStrategy(new VfsFileChangedReloadingStrategy());\r
93                                 gssConfig.load();\r
94                                 // Decorator.\r
95                                 configuration = new DataConfiguration(gssConfig);\r
96                         }\r
97                         return configuration;\r
98                 }\r
99                 catch (ClassNotFoundException e) {\r
100                         // Use empty configuration, so we get no NPE but default values\r
101                         configuration = new DataConfiguration(new BaseConfiguration());\r
102                         logger.error("Error in ExternalAPI initialization! GSS is running with default values!", e);\r
103                         return configuration;\r
104                 } catch (InstantiationException e) {\r
105                         // Use empty configuration, so we get no NPE but default values\r
106                         configuration = new DataConfiguration(new BaseConfiguration());\r
107                         logger.error("Error in ExternalAPI initialization! GSS is running with default values!", e);\r
108                         return configuration;\r
109                 } catch (IllegalAccessException e) {\r
110                         // Use empty configuration, so we get no NPE but default values\r
111                         configuration = new DataConfiguration(new BaseConfiguration());\r
112                         logger.error("Error in ExternalAPI initialization! GSS is running with default values!", e);\r
113                         return configuration;\r
114                 } catch (ConfigurationException e) {\r
115                         // Use empty configuration, so we get no NPE but default values\r
116                         configuration = new DataConfiguration(new BaseConfiguration());\r
117                         logger.error("Error in ExternalAPI initialization! GSS is running with default values!", e);\r
118                         return configuration;\r
119                 }\r
120         }\r
121 \r
122         /**\r
123          * Extends the FileChangedReloadingStrategy from Commons Configuration, adding\r
124          * support for files in JBoss MC VFS.\r
125          *\r
126          * @author past\r
127          */\r
128         private static class VfsFileChangedReloadingStrategy extends FileChangedReloadingStrategy {\r
129             /** Constant for the jar URL protocol.*/\r
130             private static final String JAR_PROTOCOL = "jar";\r
131             /** Constant for the JBoss MC VFSFile URL protocol.*/\r
132             private static final String VFSFILE_PROTOCOL = "vfsfile";\r
133 \r
134             @Override\r
135                 protected File getFile()\r
136             {\r
137                 return configuration.getURL() != null ? fileFromURL(configuration\r
138                         .getURL()) : configuration.getFile();\r
139             }\r
140 \r
141             /**\r
142              * Helper method for transforming a URL into a file object. This method\r
143              * handles file: and jar: URLs, as well as JBoss VFS-specific vfsfile:\r
144              * URLs.\r
145              *\r
146              * @param url the URL to be converted\r
147              * @return the resulting file or <b>null </b>\r
148              */\r
149             private File fileFromURL(URL url)\r
150             {\r
151                 if (VFSFILE_PROTOCOL.equals(url.getProtocol()))\r
152                 {\r
153                     String path = url.getPath();\r
154                     try\r
155                     {\r
156                         return ConfigurationUtils.fileFromURL(new URL("file:" + path));\r
157                     }\r
158                     catch (MalformedURLException mex)\r
159                     {\r
160                         return null;\r
161                     }\r
162                 }\r
163                 else if (JAR_PROTOCOL.equals(url.getProtocol()))\r
164                 {\r
165                     String path = url.getPath();\r
166                     try\r
167                     {\r
168                         return ConfigurationUtils.fileFromURL(new URL(path.substring(0,\r
169                                 path.indexOf('!'))));\r
170                     }\r
171                     catch (MalformedURLException mex)\r
172                     {\r
173                         return null;\r
174                     }\r
175                 } else\r
176                                 return ConfigurationUtils.fileFromURL(url);\r
177             }\r
178         }\r
179 }\r