KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > celtix > BusFactory


1 package org.objectweb.celtix;
2
3
4 import java.io.BufferedReader JavaDoc;
5 import java.io.InputStream JavaDoc;
6 import java.io.InputStreamReader JavaDoc;
7 import java.util.Map JavaDoc;
8 import org.objectweb.celtix.configuration.CommandLineOption;
9
10 /**
11  * Manages the <code>Bus</code> instances in a process.
12  */

13 public final class BusFactory {
14
15     private static final CommandLineOption BUS_CLASS_OPT;
16     private static final String JavaDoc DEFAULT_BUS_CLASSNAME = "org.objectweb.celtix.bus.busimpl.CeltixBus";
17     private static BusFactory theInstance;
18     
19     static {
20         BUS_CLASS_OPT = new CommandLineOption("-BUSclass");
21     }
22     
23     private BusFactory() {
24     }
25     
26     public static BusFactory getInstance() {
27         synchronized (BusFactory.class) {
28             if (null == theInstance) {
29                 theInstance = new BusFactory();
30             }
31         }
32         return theInstance;
33     }
34     
35     public Bus getBus(String JavaDoc[] args,
36                       Map JavaDoc<String JavaDoc, Object JavaDoc> properties,
37                       ClassLoader JavaDoc classLoader) throws BusException {
38         
39         // check command line options and properties to
40
// determine bus class
41

42         String JavaDoc busClass = getBusClass(args, properties, classLoader);
43         
44         // create the bus
45

46         return createBus(busClass, selectClassLoader(classLoader), args, properties);
47     }
48     
49     private ClassLoader JavaDoc selectClassLoader(ClassLoader JavaDoc classLoader) {
50         ClassLoader JavaDoc ret = classLoader;
51         if (null == classLoader) {
52             ret = BusFactory.class.getClassLoader();
53         }
54         return ret;
55     }
56
57     private static Bus createBus(String JavaDoc className,
58                                  ClassLoader JavaDoc classLoader,
59                                  String JavaDoc[] args,
60                                  Map JavaDoc<String JavaDoc, Object JavaDoc> properties) throws BusException {
61
62         Class JavaDoc<? extends Bus> busClass;
63         try {
64             busClass = Class.forName(className, true, classLoader).asSubclass(Bus.class);
65             Bus bus = busClass.newInstance();
66             bus.initialize(args, properties);
67             return bus;
68         } catch (Exception JavaDoc ex) {
69             throw new BusException(ex);
70         }
71     }
72     
73     String JavaDoc getBusClass(String JavaDoc[] args, Map JavaDoc<String JavaDoc, Object JavaDoc> properties, ClassLoader JavaDoc classLoader)
74         throws BusException {
75         
76         String JavaDoc busClass = null;
77     
78         // first check command line arguments
79
BUS_CLASS_OPT.initialize(args);
80         busClass = (String JavaDoc)BUS_CLASS_OPT.getValue();
81         if (isValidBusClass(busClass)) {
82             return busClass;
83         }
84         
85         // next check properties
86
busClass = (String JavaDoc)properties.get(Bus.BUS_CLASS_PROPERTY);
87         if (isValidBusClass(busClass)) {
88             return busClass;
89         }
90         
91         // next check system properties
92
busClass = System.getProperty(Bus.BUS_CLASS_PROPERTY);
93         if (isValidBusClass(busClass)) {
94             return busClass;
95         }
96     
97         try {
98             // next, check for the services stuff in the jar file
99
String JavaDoc serviceId = "META-INF/services/" + Bus.BUS_CLASS_PROPERTY;
100             InputStream JavaDoc is = null;
101         
102             if (classLoader == null) {
103                 classLoader = Thread.currentThread().getContextClassLoader();
104             }
105         
106             if (classLoader == null) {
107                 is = ClassLoader.getSystemResourceAsStream(serviceId);
108             } else {
109                 is = classLoader.getResourceAsStream(serviceId);
110             }
111             if (is != null) {
112                 BufferedReader JavaDoc rd = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(is, "UTF-8"));
113                 busClass = rd.readLine();
114                 rd.close();
115             }
116             if (isValidBusClass(busClass)) {
117                 return busClass;
118             }
119
120             // otherwise use default
121
busClass = DEFAULT_BUS_CLASSNAME;
122             return busClass;
123         } catch (Exception JavaDoc ex) {
124             throw new BusException(ex);
125         }
126     }
127
128     private boolean isValidBusClass(String JavaDoc busClassName) {
129         return busClassName != null && !"".equals(busClassName);
130     }
131  
132 }
133
Popular Tags