001: package org.objectweb.celtix;
002:
003: import java.lang.ref.WeakReference;
004: import java.util.HashMap;
005: import java.util.Map;
006: import java.util.concurrent.ConcurrentHashMap;
007:
008: import javax.xml.ws.WebServiceException;
009:
010: import org.objectweb.celtix.bindings.BindingManager;
011: import org.objectweb.celtix.buslifecycle.BusLifeCycleManager;
012: import org.objectweb.celtix.configuration.Configuration;
013: import org.objectweb.celtix.jaxws.EndpointRegistry;
014: import org.objectweb.celtix.management.InstrumentationManager;
015: import org.objectweb.celtix.plugins.PluginManager;
016: import org.objectweb.celtix.resource.ResourceManager;
017: import org.objectweb.celtix.transports.TransportFactoryManager;
018: import org.objectweb.celtix.workqueue.WorkQueueManager;
019: import org.objectweb.celtix.wsdl.WSDLManager;
020:
021: /**
022: * The Bus class provides access to configuration, factories and managers
023: * for use by an application.
024: */
025: public abstract class Bus {
026:
027: public static final String BUS_CLASS_PROPERTY = "org.objectweb.celtix.BusClass";
028:
029: private static ThreadLocal<Bus> current = new ThreadLocal<Bus>();
030: private static Map<String, WeakReference<Bus>> nameMap = new ConcurrentHashMap<String, WeakReference<Bus>>();
031: private static Bus defaultBus;
032:
033: /**
034: * Returns a newly created and fully initialised <code>Bus</code>.
035: *
036: * @return Bus the newly created <code>Bus</code>.
037: * @throws BusException If there is an error initializing <code>Bus</code>.
038: */
039: public static synchronized Bus init() throws BusException {
040: return init(new String[0]);
041: }
042:
043: /**
044: * Returns a newly created and fully initialised <code>Bus</code>.
045: *
046: * @param args any args, such as domain name, bus class, and other configuration
047: * options that can be used to initialize this <code>Bus</code>.
048: * @return Bus the newly created <code>Bus</code>.
049: * @throws BusException If there is an error initializing <code>Bus</code>.
050: */
051: public static synchronized Bus init(String[] args)
052: throws BusException {
053: return init(args, new HashMap<String, Object>());
054: }
055:
056: /**
057: * Returns a newly created and fully initialised <code>Bus</code>.
058: *
059: * @param args any args, such as domain name, bus class, and other configuration
060: * options that can be used to initialize this <code>Bus</code>.
061: * @param properties any properties, such as bus identifier, bus class, and other configuration
062: * options that can be used to identify and initialize this <code>Bus</code>.
063: * The properties are superceded by the settings in the <code>args</code> parameter,
064: * and they in turn supercede system properties.
065: * @return Bus the newly created <code>Bus</code>.
066: * @throws BusException If there is an error initializing <code>Bus</code>.
067: */
068: public static synchronized Bus init(String[] args,
069: Map<String, Object> properties) throws BusException {
070: return init(args, properties, null);
071: }
072:
073: /**
074: * Returns a newly created and fully initialised <code>Bus</code>.
075: *
076: * @param args any args, such as domain name, bus class, and other configuration
077: * options that can be used to initialize this <code>Bus</code>.
078: * @param properties any properties, such as domain name, bus class, and other configuration
079: * options that can be used to initialize this <code>Bus</code>.
080: * The properties are superceded by the settings in the <code>args</code> parameter,
081: * and they in turn supercede system properties.
082: * @param classLoader an optional classloader to use when instantiating a <code>Bus</code>
083: * needs to be instantiated (defaults to the current thread's context classloader).
084: * @return Bus the newly created <code>Bus</code>.
085: * @throws BusException If there is an error initializing <code>Bus</code>.
086: */
087: public static synchronized Bus init(String[] args,
088: Map<String, Object> properties, ClassLoader classLoader)
089: throws BusException {
090:
091: // delegate to the factory
092: BusFactory bf = BusFactory.getInstance();
093: Bus b = bf.getBus(args, properties, classLoader);
094: nameMap.put(b.getBusID(), new WeakReference<Bus>(b));
095: return b;
096: }
097:
098: /**
099: * 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: * Sets the current <code>Bus</code>. If a bus is explicitly
114: * initialised on a thread, this is the current bus. If no thread
115: * has been initialised (implicitly or explicitly), setting the
116: * current bus will set the default bus for all threads
117: *
118: * @param bus the current bus
119: */
120: public static void setCurrent(Bus bus) {
121: current.set(bus);
122: setDefaultBus(bus);
123: }
124:
125: /**
126: * Returns the LAST Bus that was created with the given ID. If
127: * multiple buses are created with the same ID, only the last is
128: * saved for access later.
129: *
130: * The Bus objects are only held via a WeakReference. Thus, if
131: * something else doesn't hold onto it, it will be garbage collected
132: * and this method will return null.
133: *
134: * @param id
135: * @return The last bus by the given ID.
136: */
137: public static Bus getByID(String id) {
138: WeakReference<Bus> bus = nameMap.get(id);
139: if (bus != null) {
140: if (bus.get() == null) {
141: nameMap.remove(id);
142: }
143: return bus.get();
144: }
145: return null;
146: }
147:
148: protected void removeByID(String id) {
149: if (nameMap.containsKey(id)) {
150: nameMap.remove(id);
151: }
152: }
153:
154: /**
155: * Sends the event specified to the <code>Bus</code>.
156: * @param event The <code>BusEvent</code> to send.
157: */
158: public abstract void sendEvent(BusEvent event);
159:
160: /**
161: * Adds an event listener to the current <code>Bus</code>.
162: * @param l The <code>BusEvenetListener</code> to be added.
163: * @param filter A <code>BusEventFilter</code> to be applied to the listener.
164: * @throws BusException If there is an error adding listener.
165: */
166: public abstract void addListener(BusEventListener l,
167: BusEventFilter filter) throws BusException;
168:
169: /**
170: * Removes the specified event listener from the <code>Bus</code>.
171: * @param l The <code>BusEventListener</code> to be removed.
172: * @throws BusException If there is an error removing the listener.
173: */
174: public abstract void removeListener(BusEventListener l)
175: 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: * Returns the <code>ResourceManager</code> of this <code>Bus</code>.
244: *
245: * @return ResourceManager of this <code>Bus</code>.
246: */
247: public abstract ResourceManager getResourceManager();
248:
249: /**
250: * Returns the <code> InstrumenatationManager </code> of this <code>Bus</code>
251: *
252: * @return InstrumentationManager of this <code>Bus</code>
253: */
254: public abstract InstrumentationManager getInstrumentationManager();
255:
256: /**
257: * Returns the BusID of this <code>Bus</code>
258: *
259: * @return String BusID of this <code>Bus</code>
260: */
261: public abstract String getBusID();
262:
263: /**
264: * Starts processing bus events, and returns only after the <code>Bus</code> has been shut down
265: * (from another thread).
266: *
267: */
268: public abstract void run();
269:
270: /**
271: * Get the Endpoint Registry from bus , which contains the jaxws endpoint reference
272: */
273: public abstract EndpointRegistry getEndpointRegistry();
274:
275: public abstract void initialize(String[] args,
276: Map<String, Object> properties) throws BusException;
277:
278: static void clearDefault() {
279: defaultBus = null;
280: }
281:
282: /**
283: * Clear current for all threads. For use in unit testing
284: */
285: static void clearCurrent() {
286: current.remove();
287: }
288:
289: /**
290: * Initialise a default bus.
291: */
292: private static synchronized Bus getDefaultBus() {
293: try {
294: if (defaultBus == null) {
295: defaultBus = Bus.init();
296: }
297: return defaultBus;
298: } catch (BusException ex) {
299: throw new WebServiceException(
300: "unable to initialize default bus", ex);
301: }
302: }
303:
304: /**
305: * Set the default bus for all threads. If no bus has been
306: * already initialised, this bus will be used as the default bus
307: * that do not explicitly initialise a bus.
308: */
309: private static synchronized void setDefaultBus(Bus bus) {
310: if (defaultBus == null) {
311: defaultBus = bus;
312: }
313: }
314:
315: }
|