KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > celtix > configuration > impl > DefaultConfigurationProviderFactory


1 package org.objectweb.celtix.configuration.impl;
2
3
4 import java.io.BufferedReader JavaDoc;
5 import java.io.IOException JavaDoc;
6 import java.io.InputStream JavaDoc;
7 import java.io.InputStreamReader JavaDoc;
8 import java.io.UnsupportedEncodingException JavaDoc;
9 import java.util.logging.Logger JavaDoc;
10 import org.objectweb.celtix.common.i18n.Message;
11 import org.objectweb.celtix.common.logging.LogUtils;
12 import org.objectweb.celtix.configuration.Configuration;
13 import org.objectweb.celtix.configuration.ConfigurationException;
14 import org.objectweb.celtix.configuration.ConfigurationProvider;
15 import org.objectweb.celtix.resource.DefaultResourceManager;
16
17
18 public class DefaultConfigurationProviderFactory {
19     
20     private static final Logger JavaDoc LOG = LogUtils.getL7dLogger(DefaultConfigurationProviderFactory.class);
21     
22     private static final String JavaDoc DEFAULT_CONFIGURATION_PROVIDER_CLASSNAME =
23         "org.objectweb.celtix.bus.configuration.spring.ConfigurationProviderImpl";
24     
25     private static final String JavaDoc DEFAULT_CONFIGURATION_PROVIDER_CLASSNAME_PROPERTY =
26         "org.objectweb.celtix.bus.configuration.ConfigurationProvider";
27     
28     private static DefaultConfigurationProviderFactory theInstance;
29     
30     
31     protected DefaultConfigurationProviderFactory() {
32     }
33     
34     public static DefaultConfigurationProviderFactory getInstance() {
35         if (null == theInstance) {
36             theInstance = new DefaultConfigurationProviderFactory();
37         }
38         return theInstance;
39     }
40     
41     public ConfigurationProvider createDefaultProvider(Configuration configuration) {
42         
43         String JavaDoc className = getDefaultProviderClassName();
44        
45         Class JavaDoc<? extends ConfigurationProvider> providerClass;
46         try {
47             providerClass = Class.forName(className).asSubclass(ConfigurationProvider.class);
48             ConfigurationProvider provider = providerClass.newInstance();
49             provider.init(configuration);
50             return provider;
51         } catch (ConfigurationException ex) {
52             throw ex;
53         } catch (Exception JavaDoc ex) {
54             throw new ConfigurationException(new Message("DEFAULT_PROVIDER_INSTANTIATION_EXC", LOG), ex);
55         }
56     }
57     
58     public String JavaDoc getDefaultProviderClassName() {
59         
60         String JavaDoc providerClass = null;
61         
62         // check system properties
63
providerClass = System.getProperty(DEFAULT_CONFIGURATION_PROVIDER_CLASSNAME_PROPERTY);
64         if (null != providerClass && !"".equals(providerClass)) {
65             return providerClass;
66         }
67     
68         // next, check for the services stuff in the jar file
69
String JavaDoc serviceId = "META-INF/services/" + DEFAULT_CONFIGURATION_PROVIDER_CLASSNAME_PROPERTY;
70         InputStream JavaDoc is = DefaultResourceManager.instance().getResourceAsStream(serviceId);
71   
72         if (is != null) {
73             try {
74                 BufferedReader JavaDoc rd = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(is, "UTF-8"));
75                 providerClass = rd.readLine();
76                 rd.close();
77             } catch (UnsupportedEncodingException JavaDoc ex) {
78                 //we're asking for UTF-8 which is supposed to always be supported,
79
//but we'll throw a ConfigurationException anyway
80
throw new ConfigurationException(new Message("DEFAULT_PROVIDER_INSTANTIATION_EXC", LOG), ex);
81             } catch (IOException JavaDoc ex) {
82                 throw new ConfigurationException(new Message("DEFAULT_PROVIDER_INSTANTIATION_EXC", LOG), ex);
83             }
84         }
85         
86         if (providerClass != null && !"".equals(providerClass)) {
87             return providerClass;
88         }
89         
90         // fallback to hardcoced default
91

92         return DEFAULT_CONFIGURATION_PROVIDER_CLASSNAME;
93     }
94 }
95
Popular Tags