1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35 package org.ogf.graap.wsag.api.configuration;
36
37 import java.io.File;
38 import java.io.FileNotFoundException;
39 import java.io.IOException;
40 import java.io.InputStream;
41 import java.net.URL;
42 import java.text.MessageFormat;
43 import java.util.Properties;
44
45 import org.apache.commons.discovery.log.SimpleLog;
46 import org.apache.commons.discovery.resource.ClassLoaders;
47 import org.apache.commons.discovery.tools.DefaultClassHolder;
48 import org.apache.commons.discovery.tools.DiscoverClass;
49 import org.apache.commons.discovery.tools.DiscoverSingleton;
50 import org.apache.commons.discovery.tools.PropertiesHolder;
51 import org.apache.commons.discovery.tools.ResourceUtils;
52 import org.apache.commons.discovery.tools.SPInterface;
53 import org.apache.log4j.Logger;
54 import org.apache.xmlbeans.XmlException;
55 import org.ogf.graap.wsag.api.logging.LogMessage;
56 import org.ogf.graap.wsag4j.types.configuration.ConfigurationDocument;
57 import org.ogf.graap.wsag4j.types.configuration.ConfigurationType;
58
59
60
61
62
63
64 public class WSAG4JConfiguration
65 {
66
67 private static final Logger LOG = Logger.getLogger( WSAG4JConfiguration.class );
68
69 private static WSAG4JConfigurationEnvironment instance = null;
70
71
72
73
74 private static synchronized WSAG4JConfigurationEnvironment getEnvironment()
75 {
76
77 SimpleLog.setLevel( SimpleLog.LOG_LEVEL_INFO );
78
79 if ( instance == null )
80 {
81
82 SPInterface mssConfigSP = getWSAG4JSPI();
83 PropertiesHolder pHolder = new PropertiesHolder( WSAG4JEnvironment.DEFAULT_CONFIGURATION_FILE );
84 DefaultClassHolder cHolder = new DefaultClassHolder( WSAG4JEnvironment.class.getName() );
85
86 instance =
87 (WSAG4JConfigurationEnvironment) DiscoverSingleton.find( null, mssConfigSP, pHolder, cHolder );
88 }
89
90 return instance;
91 }
92
93 private static SPInterface getWSAG4JSPI()
94 {
95
96 ClassLoaders loaders =
97 ClassLoaders.getLibLoaders( WSAG4JConfigurationEnvironment.class, DiscoverClass.class, true );
98
99
100 Properties properties =
101 ResourceUtils.loadProperties( WSAG4JConfigurationEnvironment.class,
102 WSAG4JEnvironment.DEFAULT_CONFIGURATION_FILE, loaders );
103
104 if ( properties == null )
105 {
106 properties = new Properties();
107 }
108
109
110 String cPath =
111 properties.getProperty( WSAG4JEnvironment.DEFAULT_CONFIGURATION_PATH_KEY,
112 WSAG4JEnvironment.DEFAULT_CONFIGURATION_PATH );
113
114
115
116
117 SPInterface mssConfigSP =
118 new SPInterface( WSAG4JConfigurationEnvironment.class, new Class[] { String.class },
119 new Object[] { cPath } );
120
121 return mssConfigSP;
122 }
123
124
125
126
127 public static String getConfigurationPath()
128 {
129 return getEnvironment().getConfigurationPath();
130 }
131
132
133
134
135
136
137
138
139
140
141
142
143 public static Object findImplementation( Class<?> interfaceDef, String configFile, String defaultImpl )
144 {
145 DiscoverClass discovery = new DiscoverClass();
146 try
147 {
148 try
149 {
150 Properties properties = new Properties();
151
152 InputStream resource = findResource( configFile );
153 properties.load( resource );
154
155 Class<?> theClass = discovery.find( interfaceDef, properties, defaultImpl );
156
157 return theClass.newInstance();
158 }
159 catch ( IOException e )
160 {
161 String message = "Could not load resource {0}. Try default discovery.";
162 LOG.debug( LogMessage.getMessage( message, configFile ) );
163
164 Class<?> theClass = discovery.find( interfaceDef, configFile, defaultImpl );
165
166 return theClass.newInstance();
167 }
168 }
169 catch ( InstantiationException e )
170 {
171 final String message = "Could not instantiate class for interface {0}.";
172 LOG.debug( LogMessage.getMessage( message, interfaceDef.getName() ), e );
173 }
174 catch ( IllegalAccessException e )
175 {
176 final String message = "Could not instantiate class for interface {0}.";
177 LOG.debug( LogMessage.getMessage( message, interfaceDef.getName() ), e );
178 }
179
180 return null;
181 }
182
183
184
185
186
187
188
189
190
191
192
193
194 public static Object findSingeltonImplementation( Class<?> interfaceDef, String configFile,
195 String defaultImpl )
196 {
197 try
198 {
199 Properties properties = new Properties();
200
201 InputStream resource = findResource( configFile );
202 properties.load( resource );
203
204 return DiscoverSingleton.find( interfaceDef, properties, defaultImpl );
205 }
206 catch ( IOException e )
207 {
208 final String message = "Could not load resource {0}. Try default discovery.";
209 LOG.debug( LogMessage.getMessage( message, configFile ) );
210 LOG.trace( e );
211
212 return DiscoverSingleton.find( interfaceDef, configFile, defaultImpl );
213 }
214 }
215
216
217
218
219
220
221
222
223 public static InputStream findResource( String resourceName ) throws IOException
224 {
225 return findResource( getEnvironment().getConfigurationPath(), resourceName );
226 }
227
228
229
230
231
232
233
234
235
236
237
238 public static InputStream findResource( String path, String resourceName ) throws IOException
239 {
240 return findResourceURL( path, resourceName ).openStream();
241 }
242
243
244
245
246
247
248
249
250 public static URL findResourceURL( String resourceName ) throws IOException
251 {
252 return findResourceURL( getEnvironment().getConfigurationPath(), resourceName );
253 }
254
255
256
257
258
259
260
261
262
263
264
265 public static URL findResourceURL( String path, String resourceName ) throws IOException
266 {
267 if ( path == null )
268 {
269 path = "";
270 }
271
272 if ( resourceName == null )
273 {
274 throw new IOException( "Could not find resource. No resource name specified (null)." );
275 }
276
277 File file = null;
278
279
280
281
282 file = new File( path, resourceName );
283 if ( file.exists() )
284 {
285 try
286 {
287 Object[] filler = new Object[] { resourceName, path };
288 String message = MessageFormat.format( "Found resource {0} in directory {1}.", filler );
289
290 if ( LOG.isTraceEnabled() )
291 {
292 LOG.trace( message );
293 }
294
295 return file.toURL();
296 }
297 catch ( IOException e )
298 {
299 Object[] filler = new Object[] { resourceName };
300 String message = MessageFormat.format( "Could not read resource {0}.", filler );
301 LOG.error( message, e );
302 }
303 }
304
305
306 file = new File( resourceName );
307 if ( file.exists() )
308 {
309 try
310 {
311 if ( LOG.isTraceEnabled() )
312 {
313 LOG.trace( MessageFormat.format( "Found resource [{0}].", new Object[] { resourceName } ) );
314 }
315
316 return file.toURL();
317 }
318 catch ( IOException e )
319 {
320 LOG.error(
321 MessageFormat.format( "Could not read resource {0}.", new Object[] { resourceName } ), e );
322 }
323 }
324
325
326 String qualifiedResourceName = path + System.getProperty( "file.separator" ) + resourceName;
327
328
329 if ( path.equals( "" ) )
330 {
331 qualifiedResourceName = "/" + resourceName;
332 }
333
334 URL resourceUrl = WSAG4JConfiguration.class.getResource( qualifiedResourceName );
335 if ( resourceUrl != null )
336 {
337 if ( LOG.isTraceEnabled() )
338 {
339 LOG.trace( MessageFormat.format(
340 "Found resource {0} by the classloader [external name: {1}]", new Object[] {
341 qualifiedResourceName, resourceUrl.toExternalForm() } ) );
342 }
343
344 return resourceUrl;
345 }
346
347
348
349 if ( resourceName.startsWith( "/" ) || resourceName.startsWith( "\\" ) )
350 {
351 resourceUrl = WSAG4JConfiguration.class.getResource( resourceName );
352 }
353 else
354 {
355
356 resourceUrl = WSAG4JConfiguration.class.getResource( "/" + resourceName );
357 }
358
359 if ( resourceUrl != null )
360 {
361 if ( LOG.isTraceEnabled() )
362 {
363 LOG.trace( MessageFormat.format(
364 "Found resource {0} by the classloader [external name: {1}]", new Object[] {
365 resourceName, resourceUrl.toExternalForm() } ) );
366 }
367
368 return resourceUrl;
369 }
370
371 LOG.error( LogMessage.getMessage( "The resource {0} was not found.", resourceName ) );
372 LOG.error( LogMessage.getMessage( "Tried the following directories: [{0}] [{1}]", path,
373 System.getProperty( "java.class.path" ) ) );
374
375 throw new FileNotFoundException( LogMessage.format(
376 "The resource [{0}] was not found at the system.", resourceName ) );
377 }
378
379
380
381
382
383
384
385
386 public static ConfigurationType findWSAG4JConfiguration( String fileName ) throws IOException
387 {
388 try
389 {
390
391 InputStream resourceInput = findResource( fileName );
392
393 ConfigurationType result = null;
394
395 if ( resourceInput != null )
396 {
397 try
398 {
399 result = ConfigurationDocument.Factory.parse( resourceInput ).getConfiguration();
400 }
401 catch ( IOException e )
402 {
403 String msgText = "Error reading the configuration file {0}. Error: {1}";
404 String message = LogMessage.format( msgText, fileName, e.getMessage() );
405
406 LOG.error( message );
407 throw new IOException( message );
408
409 }
410 catch ( XmlException e )
411 {
412 String msgText = "Error reading the configuration file {0}. Description: {1}";
413 String message = LogMessage.format( msgText, fileName, e.getMessage() );
414
415 LOG.error( message );
416 throw new IOException( message );
417 }
418 }
419
420 return result;
421 }
422 catch ( Exception ex )
423 {
424 String msgText = "Error loading configuration file [{0}]. Message: {1}";
425 String message = LogMessage.format( msgText, fileName, ex.getMessage() );
426 LOG.error( message );
427
428 return null;
429 }
430 }
431
432 }