001: package org.objectweb.celtix.configuration;
002:
003: import java.io.BufferedReader;
004: import java.io.InputStream;
005: import java.io.InputStreamReader;
006:
007: public final class ConfigurationBuilderFactory {
008: private static final String DEFAULT_CONFIGURATION_BUILDER_CLASSNAME = "org.objectweb.celtix.bus.configuration.CeltixConfigurationBuilder";
009: private static ConfigurationBuilder theBuilder;
010:
011: private ConfigurationBuilderFactory() {
012: }
013:
014: public static ConfigurationBuilder getBuilder() {
015: return getBuilder(null);
016: }
017:
018: public static void clearBuilder() {
019: theBuilder = null;
020: }
021:
022: public static ConfigurationBuilder getBuilder(
023: ClassLoader classLoader) {
024:
025: if (null == theBuilder) {
026: String builderClass = getBuilderClass(classLoader);
027:
028: // create the builder
029:
030: theBuilder = createBuilder(builderClass,
031: selectClassLoader(classLoader));
032: }
033: return theBuilder;
034: }
035:
036: private static ConfigurationBuilder createBuilder(String className,
037: ClassLoader classLoader) {
038:
039: Class<? extends ConfigurationBuilder> builderClass;
040: try {
041: builderClass = Class.forName(className, true, classLoader)
042: .asSubclass(ConfigurationBuilder.class);
043: return builderClass.newInstance();
044: } catch (Exception ex) {
045: throw new ConfigurationException(ex);
046: }
047: }
048:
049: private static ClassLoader selectClassLoader(ClassLoader classLoader) {
050: ClassLoader ret = classLoader;
051: if (null == classLoader) {
052: ret = ConfigurationBuilderFactory.class.getClassLoader();
053: }
054: return ret;
055: }
056:
057: private static String getBuilderClass(ClassLoader classLoader)
058: throws ConfigurationException {
059:
060: String builderClass = null;
061:
062: // check system properties
063: builderClass = System
064: .getProperty(ConfigurationBuilder.CONFIGURATION_BUILDER_CLASS_PROPERTY);
065: if (isValidBuilderClass(builderClass)) {
066: return builderClass;
067: }
068:
069: try {
070: // next, check for the services stuff in the jar file
071: String serviceId = "META-INF/services/"
072: + ConfigurationBuilder.CONFIGURATION_BUILDER_CLASS_PROPERTY;
073: InputStream is = null;
074:
075: if (classLoader == null) {
076: classLoader = Thread.currentThread()
077: .getContextClassLoader();
078: }
079:
080: if (classLoader == null) {
081: is = ClassLoader.getSystemResourceAsStream(serviceId);
082: } else {
083: is = classLoader.getResourceAsStream(serviceId);
084: }
085: if (is != null) {
086: BufferedReader rd = new BufferedReader(
087: new InputStreamReader(is, "UTF-8"));
088: builderClass = rd.readLine();
089: rd.close();
090: }
091: if (isValidBuilderClass(builderClass)) {
092: return builderClass;
093: }
094:
095: // otherwise use default
096: builderClass = DEFAULT_CONFIGURATION_BUILDER_CLASSNAME;
097: return builderClass;
098: } catch (Exception ex) {
099: throw new ConfigurationException(ex);
100: }
101: }
102:
103: private static boolean isValidBuilderClass(String builderClassName) {
104: return builderClassName != null && !"".equals(builderClassName);
105: }
106:
107: }
|