001: package org.objectweb.celtix;
002:
003: import java.io.BufferedReader;
004: import java.io.InputStream;
005: import java.io.InputStreamReader;
006: import java.util.Map;
007: import org.objectweb.celtix.configuration.CommandLineOption;
008:
009: /**
010: * Manages the <code>Bus</code> instances in a process.
011: */
012: public final class BusFactory {
013:
014: private static final CommandLineOption BUS_CLASS_OPT;
015: private static final String DEFAULT_BUS_CLASSNAME = "org.objectweb.celtix.bus.busimpl.CeltixBus";
016: private static BusFactory theInstance;
017:
018: static {
019: BUS_CLASS_OPT = new CommandLineOption("-BUSclass");
020: }
021:
022: private BusFactory() {
023: }
024:
025: public static BusFactory getInstance() {
026: synchronized (BusFactory.class) {
027: if (null == theInstance) {
028: theInstance = new BusFactory();
029: }
030: }
031: return theInstance;
032: }
033:
034: public Bus getBus(String[] args, Map<String, Object> properties,
035: ClassLoader classLoader) throws BusException {
036:
037: // check command line options and properties to
038: // determine bus class
039:
040: String busClass = getBusClass(args, properties, classLoader);
041:
042: // create the bus
043:
044: return createBus(busClass, selectClassLoader(classLoader),
045: args, properties);
046: }
047:
048: private ClassLoader selectClassLoader(ClassLoader classLoader) {
049: ClassLoader ret = classLoader;
050: if (null == classLoader) {
051: ret = BusFactory.class.getClassLoader();
052: }
053: return ret;
054: }
055:
056: private static Bus createBus(String className,
057: ClassLoader classLoader, String[] args,
058: Map<String, Object> properties) throws BusException {
059:
060: Class<? extends Bus> busClass;
061: try {
062: busClass = Class.forName(className, true, classLoader)
063: .asSubclass(Bus.class);
064: Bus bus = busClass.newInstance();
065: bus.initialize(args, properties);
066: return bus;
067: } catch (Exception ex) {
068: throw new BusException(ex);
069: }
070: }
071:
072: String getBusClass(String[] args, Map<String, Object> properties,
073: ClassLoader classLoader) throws BusException {
074:
075: String busClass = null;
076:
077: // first check command line arguments
078: BUS_CLASS_OPT.initialize(args);
079: busClass = (String) BUS_CLASS_OPT.getValue();
080: if (isValidBusClass(busClass)) {
081: return busClass;
082: }
083:
084: // next check properties
085: busClass = (String) properties.get(Bus.BUS_CLASS_PROPERTY);
086: if (isValidBusClass(busClass)) {
087: return busClass;
088: }
089:
090: // next check system properties
091: busClass = System.getProperty(Bus.BUS_CLASS_PROPERTY);
092: if (isValidBusClass(busClass)) {
093: return busClass;
094: }
095:
096: try {
097: // next, check for the services stuff in the jar file
098: String serviceId = "META-INF/services/"
099: + Bus.BUS_CLASS_PROPERTY;
100: InputStream is = null;
101:
102: if (classLoader == null) {
103: classLoader = Thread.currentThread()
104: .getContextClassLoader();
105: }
106:
107: if (classLoader == null) {
108: is = ClassLoader.getSystemResourceAsStream(serviceId);
109: } else {
110: is = classLoader.getResourceAsStream(serviceId);
111: }
112: if (is != null) {
113: BufferedReader rd = new BufferedReader(
114: new InputStreamReader(is, "UTF-8"));
115: busClass = rd.readLine();
116: rd.close();
117: }
118: if (isValidBusClass(busClass)) {
119: return busClass;
120: }
121:
122: // otherwise use default
123: busClass = DEFAULT_BUS_CLASSNAME;
124: return busClass;
125: } catch (Exception ex) {
126: throw new BusException(ex);
127: }
128: }
129:
130: private boolean isValidBusClass(String busClassName) {
131: return busClassName != null && !"".equals(busClassName);
132: }
133:
134: }
|