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