2 * Copyright 2006, 2007, 2008, 2009 Electronic Business Systems Ltd.
\r
4 * This file is part of GSS.
\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
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
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
19 package gr.ebs.gss.server.configuration;
\r
21 import java.io.File;
\r
22 import java.net.MalformedURLException;
\r
23 import java.net.URL;
\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
35 * Singleton that loads properties from gss.properties and
\r
36 * makes them available to all
\r
40 public final class GSSConfigurationFactory {
\r
45 private static Log logger = LogFactory.getLog(GSSConfigurationFactory.class);
\r
48 * The filename of the file containing system properties.
\r
50 private static String configFilename = "gss.properties";
\r
53 * The (single) configuration object
\r
55 private static DataConfiguration configuration = null;
\r
58 * Exists only to defeat instantiation.
\r
60 private GSSConfigurationFactory() {
\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
72 * @throws ClassNotFoundException
\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
82 * @return the configuration object
\r
84 public synchronized static DataConfiguration getConfiguration() {
\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
95 configuration = new DataConfiguration(gssConfig);
\r
97 return configuration;
\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
123 * Extends the FileChangedReloadingStrategy from Commons Configuration, adding
\r
124 * support for files in JBoss MC VFS.
\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
135 protected File getFile()
\r
137 return configuration.getURL() != null ? fileFromURL(configuration
\r
138 .getURL()) : configuration.getFile();
\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
146 * @param url the URL to be converted
\r
147 * @return the resulting file or <b>null </b>
\r
149 private File fileFromURL(URL url)
\r
151 if (VFSFILE_PROTOCOL.equals(url.getProtocol()))
\r
153 String path = url.getPath();
\r
156 return ConfigurationUtils.fileFromURL(new URL("file:" + path));
\r
158 catch (MalformedURLException mex)
\r
163 else if (JAR_PROTOCOL.equals(url.getProtocol()))
\r
165 String path = url.getPath();
\r
168 return ConfigurationUtils.fileFromURL(new URL(path.substring(0,
\r
169 path.indexOf('!'))));
\r
171 catch (MalformedURLException mex)
\r
176 return ConfigurationUtils.fileFromURL(url);
\r