KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.objectweb.celtix;
2
3
4 import java.lang.ref.WeakReference JavaDoc;
5 import java.util.HashMap JavaDoc;
6 import java.util.Map JavaDoc;
7 import java.util.concurrent.ConcurrentHashMap JavaDoc;
8
9 import javax.xml.ws.WebServiceException;
10
11 import org.objectweb.celtix.bindings.BindingManager;
12 import org.objectweb.celtix.buslifecycle.BusLifeCycleManager;
13 import org.objectweb.celtix.configuration.Configuration;
14 import org.objectweb.celtix.jaxws.EndpointRegistry;
15 import org.objectweb.celtix.management.InstrumentationManager;
16 import org.objectweb.celtix.plugins.PluginManager;
17 import org.objectweb.celtix.resource.ResourceManager;
18 import org.objectweb.celtix.transports.TransportFactoryManager;
19 import org.objectweb.celtix.workqueue.WorkQueueManager;
20 import org.objectweb.celtix.wsdl.WSDLManager;
21
22 /**
23  * The Bus class provides access to configuration, factories and managers
24  * for use by an application.
25  */

26 public abstract class Bus {
27     
28     public static final String JavaDoc BUS_CLASS_PROPERTY = "org.objectweb.celtix.BusClass";
29
30     private static ThreadLocal JavaDoc<Bus> current = new ThreadLocal JavaDoc<Bus>();
31     private static Map JavaDoc<String JavaDoc, WeakReference JavaDoc<Bus>> nameMap =
32         new ConcurrentHashMap JavaDoc<String JavaDoc, WeakReference JavaDoc<Bus>>();
33     private static Bus defaultBus;
34     
35     /**
36      * Returns a newly created and fully initialised <code>Bus</code>.
37      *
38      * @return Bus the newly created <code>Bus</code>.
39      * @throws BusException If there is an error initializing <code>Bus</code>.
40      */

41     public static synchronized Bus init() throws BusException {
42         return init(new String JavaDoc[0]);
43     }
44
45     /**
46      * Returns a newly created and fully initialised <code>Bus</code>.
47      *
48      * @param args any args, such as domain name, bus class, and other configuration
49      * options that can be used to initialize this <code>Bus</code>.
50      * @return Bus the newly created <code>Bus</code>.
51      * @throws BusException If there is an error initializing <code>Bus</code>.
52      */

53     public static synchronized Bus init(String JavaDoc[] args) throws BusException {
54         return init(args, new HashMap JavaDoc<String JavaDoc, Object JavaDoc>());
55     }
56     
57     /**
58      * Returns a newly created and fully initialised <code>Bus</code>.
59      *
60      * @param args any args, such as domain name, bus class, and other configuration
61      * options that can be used to initialize this <code>Bus</code>.
62      * @param properties any properties, such as bus identifier, bus class, and other configuration
63      * options that can be used to identify and initialize this <code>Bus</code>.
64      * The properties are superceded by the settings in the <code>args</code> parameter,
65      * and they in turn supercede system properties.
66      * @return Bus the newly created <code>Bus</code>.
67      * @throws BusException If there is an error initializing <code>Bus</code>.
68      */

69     public static synchronized Bus init(String JavaDoc[] args, Map JavaDoc<String JavaDoc, Object JavaDoc> properties) throws BusException {
70         return init(args, properties, null);
71     }
72     
73     /**
74      * Returns a newly created and fully initialised <code>Bus</code>.
75      *
76      * @param args any args, such as domain name, bus class, and other configuration
77      * options that can be used to initialize this <code>Bus</code>.
78      * @param properties any properties, such as domain name, bus class, and other configuration
79      * options that can be used to initialize this <code>Bus</code>.
80      * The properties are superceded by the settings in the <code>args</code> parameter,
81      * and they in turn supercede system properties.
82      * @param classLoader an optional classloader to use when instantiating a <code>Bus</code>
83      * needs to be instantiated (defaults to the current thread's context classloader).
84      * @return Bus the newly created <code>Bus</code>.
85      * @throws BusException If there is an error initializing <code>Bus</code>.
86      */

87     public static synchronized Bus init(String JavaDoc[] args,
88         Map JavaDoc<String JavaDoc, Object JavaDoc> properties,
89         ClassLoader JavaDoc classLoader) throws BusException {
90         
91         // delegate to the factory
92
BusFactory bf = BusFactory.getInstance();
93         Bus b = bf.getBus(args, properties, classLoader);
94         nameMap.put(b.getBusID(), new WeakReference JavaDoc<Bus>(b));
95         return b;
96     }
97     
98     /**
99     * Returns the current <code>Bus</code> on this thread. If no bus
100     * has been initialised on this thread, return the default bus.
101     *
102     * @return the current <code>Bus</code> on this thread.
103     */

104     public static Bus getCurrent() {
105         Bus ret = current.get();
106         if (ret == null) {
107             ret = getDefaultBus();
108         }
109         return ret;
110     }
111
112
113     /**
114     * Sets the current <code>Bus</code>. If a bus is explicitly
115     * initialised on a thread, this is the current bus. If no thread
116     * has been initialised (implicitly or explicitly), setting the
117     * current bus will set the default bus for all threads
118     *
119     * @param bus the current bus
120     */

121     public static void setCurrent(Bus bus) {
122         current.set(bus);
123         setDefaultBus(bus);
124     }
125
126     /**
127      * Returns the LAST Bus that was created with the given ID. If
128      * multiple buses are created with the same ID, only the last is
129      * saved for access later.
130      *
131      * The Bus objects are only held via a WeakReference. Thus, if
132      * something else doesn't hold onto it, it will be garbage collected
133      * and this method will return null.
134      *
135      * @param id
136      * @return The last bus by the given ID.
137      */

138     public static Bus getByID(String JavaDoc id) {
139         WeakReference JavaDoc<Bus> bus = nameMap.get(id);
140         if (bus != null) {
141             if (bus.get() == null) {
142                 nameMap.remove(id);
143             }
144             return bus.get();
145         }
146         return null;
147     }
148     
149     protected void removeByID(String JavaDoc id) {
150         if (nameMap.containsKey(id)) {
151             nameMap.remove(id);
152         }
153     }
154     
155     /**
156      * Sends the event specified to the <code>Bus</code>.
157      * @param event The <code>BusEvent</code> to send.
158      */

159     public abstract void sendEvent(BusEvent event);
160
161     /**
162      * Adds an event listener to the current <code>Bus</code>.
163      * @param l The <code>BusEvenetListener</code> to be added.
164      * @param filter A <code>BusEventFilter</code> to be applied to the listener.
165      * @throws BusException If there is an error adding listener.
166      */

167     public abstract void addListener(BusEventListener l, BusEventFilter filter)
168         throws BusException;
169
170     /**
171      * Removes the specified event listener from the <code>Bus</code>.
172      * @param l The <code>BusEventListener</code> to be removed.
173      * @throws BusException If there is an error removing the listener.
174      */

175     public abstract void removeListener(BusEventListener l) throws BusException;
176
177     /**
178      * Provides access to <code>BusEventCache</code> associated with the <code>Bus</code>.
179      * @return BusEventCache The <code>BusEventCache</code> object.
180      * @see BusEventCache
181      */

182     public abstract BusEventCache getEventCache();
183
184     /**
185      * Shuts down the <code>Bus</code>.
186      *
187      * @param wait If <code>true</code>, waits for the <code>Bus</code>
188      * to shutdown before returning, otherwise returns immediately.
189      * @throws BusException
190      */

191     public abstract void shutdown(boolean wait) throws BusException;
192
193     /**
194      * Returns the <code>Configuration</code> of this <code>Bus</code>.
195      *
196      * @return Configuration the configuration of this <code>bus</code>.
197      */

198     public abstract Configuration getConfiguration();
199
200     /**
201      * Returns the <code>TransportFactoryManager</code> of this <code>Bus</code>.
202      *
203      * @return TransportRegistry the servant registry of this <code>Bus</code>.
204      */

205     public abstract TransportFactoryManager getTransportFactoryManager();
206
207     /**
208      * Returns the <code>BindingManager</code> of this <code>Bus</code>.
209      *
210      * @return BindingManager the binding manager of this <code>Bus</code>.
211      */

212     public abstract BindingManager getBindingManager();
213
214     /**
215      * Returns the <code>ClientRegistry</code> of this <code>Bus</code>.
216      *
217      * @return WSDLManager the wsdl manager of this <code>Bus</code>.
218      */

219     public abstract WSDLManager getWSDLManager();
220
221     /**
222      * Returns the <code>PluginManager</code> of this <code>Bus</code>.
223      *
224      * @return PluginManager the plugin manager of this <code>Bus</code>.
225      */

226     public abstract PluginManager getPluginManager();
227
228     /**
229      * Returns the <code>BusLifeCycleManager</code> of this <code>Bus</code>.
230      *
231      * @return BusLifeCycleManager of this <code>Bus</code>.
232      */

233     public abstract BusLifeCycleManager getLifeCycleManager();
234
235     /**
236      * Returns the <code>WorkQueueManager</code> of this <code>Bus</code>.
237      *
238      * @return WorkQueueManager of this <code>Bus</code>.
239      */

240     public abstract WorkQueueManager getWorkQueueManager();
241     
242
243     /**
244      * Returns the <code>ResourceManager</code> of this <code>Bus</code>.
245      *
246      * @return ResourceManager of this <code>Bus</code>.
247      */

248     public abstract ResourceManager getResourceManager();
249     
250     /**
251      * Returns the <code> InstrumenatationManager </code> of this <code>Bus</code>
252      *
253      * @return InstrumentationManager of this <code>Bus</code>
254      */

255     public abstract InstrumentationManager getInstrumentationManager();
256     
257     /**
258      * Returns the BusID of this <code>Bus</code>
259      *
260      * @return String BusID of this <code>Bus</code>
261      */

262     public abstract String JavaDoc getBusID();
263     
264
265     /**
266      * Starts processing bus events, and returns only after the <code>Bus</code> has been shut down
267      * (from another thread).
268      *
269      */

270     public abstract void run();
271     
272     /**
273      * Get the Endpoint Registry from bus , which contains the jaxws endpoint reference
274      */

275     public abstract EndpointRegistry getEndpointRegistry();
276     
277     public abstract void initialize(String JavaDoc[] args,
278             Map JavaDoc<String JavaDoc, Object JavaDoc> properties) throws BusException;
279
280
281  
282     static void clearDefault() {
283         defaultBus = null;
284     }
285
286
287     /**
288      * Clear current for all threads. For use in unit testing
289      */

290     static void clearCurrent() {
291         current.remove();
292     }
293     /**
294      * Initialise a default bus.
295      */

296     private static synchronized Bus getDefaultBus() {
297         try {
298             if (defaultBus == null) {
299                 defaultBus = Bus.init();
300             }
301             return defaultBus;
302         } catch (BusException ex) {
303             throw new WebServiceException("unable to initialize default bus", ex);
304         }
305     }
306     
307     /**
308      * Set the default bus for all threads. If no bus has been
309      * already initialised, this bus will be used as the default bus
310      * that do not explicitly initialise a bus.
311      */

312     private static synchronized void setDefaultBus(Bus bus) {
313         if (defaultBus == null) {
314             defaultBus = bus;
315         }
316     }
317
318 }
319
Popular Tags