001: package org.objectweb.celtix.configuration.impl;
002:
003: import java.io.BufferedReader;
004: import java.io.IOException;
005: import java.io.InputStream;
006: import java.io.InputStreamReader;
007: import java.io.UnsupportedEncodingException;
008: import java.util.logging.Logger;
009: import org.objectweb.celtix.common.i18n.Message;
010: import org.objectweb.celtix.common.logging.LogUtils;
011: import org.objectweb.celtix.configuration.Configuration;
012: import org.objectweb.celtix.configuration.ConfigurationException;
013: import org.objectweb.celtix.configuration.ConfigurationProvider;
014: import org.objectweb.celtix.resource.DefaultResourceManager;
015:
016: public class DefaultConfigurationProviderFactory {
017:
018: private static final Logger LOG = LogUtils
019: .getL7dLogger(DefaultConfigurationProviderFactory.class);
020:
021: private static final String DEFAULT_CONFIGURATION_PROVIDER_CLASSNAME = "org.objectweb.celtix.bus.configuration.spring.ConfigurationProviderImpl";
022:
023: private static final String DEFAULT_CONFIGURATION_PROVIDER_CLASSNAME_PROPERTY = "org.objectweb.celtix.bus.configuration.ConfigurationProvider";
024:
025: private static DefaultConfigurationProviderFactory theInstance;
026:
027: protected DefaultConfigurationProviderFactory() {
028: }
029:
030: public static DefaultConfigurationProviderFactory getInstance() {
031: if (null == theInstance) {
032: theInstance = new DefaultConfigurationProviderFactory();
033: }
034: return theInstance;
035: }
036:
037: public ConfigurationProvider createDefaultProvider(
038: Configuration configuration) {
039:
040: String className = getDefaultProviderClassName();
041:
042: Class<? extends ConfigurationProvider> providerClass;
043: try {
044: providerClass = Class.forName(className).asSubclass(
045: ConfigurationProvider.class);
046: ConfigurationProvider provider = providerClass
047: .newInstance();
048: provider.init(configuration);
049: return provider;
050: } catch (ConfigurationException ex) {
051: throw ex;
052: } catch (Exception ex) {
053: throw new ConfigurationException(new Message(
054: "DEFAULT_PROVIDER_INSTANTIATION_EXC", LOG), ex);
055: }
056: }
057:
058: public String getDefaultProviderClassName() {
059:
060: String providerClass = null;
061:
062: // check system properties
063: providerClass = System
064: .getProperty(DEFAULT_CONFIGURATION_PROVIDER_CLASSNAME_PROPERTY);
065: if (null != providerClass && !"".equals(providerClass)) {
066: return providerClass;
067: }
068:
069: // next, check for the services stuff in the jar file
070: String serviceId = "META-INF/services/"
071: + DEFAULT_CONFIGURATION_PROVIDER_CLASSNAME_PROPERTY;
072: InputStream is = DefaultResourceManager.instance()
073: .getResourceAsStream(serviceId);
074:
075: if (is != null) {
076: try {
077: BufferedReader rd = new BufferedReader(
078: new InputStreamReader(is, "UTF-8"));
079: providerClass = rd.readLine();
080: rd.close();
081: } catch (UnsupportedEncodingException ex) {
082: //we're asking for UTF-8 which is supposed to always be supported,
083: //but we'll throw a ConfigurationException anyway
084: throw new ConfigurationException(new Message(
085: "DEFAULT_PROVIDER_INSTANTIATION_EXC", LOG), ex);
086: } catch (IOException ex) {
087: throw new ConfigurationException(new Message(
088: "DEFAULT_PROVIDER_INSTANTIATION_EXC", LOG), ex);
089: }
090: }
091:
092: if (providerClass != null && !"".equals(providerClass)) {
093: return providerClass;
094: }
095:
096: // fallback to hardcoced default
097:
098: return DEFAULT_CONFIGURATION_PROVIDER_CLASSNAME;
099: }
100: }
|