All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.objectweb.celtix.Bus Maven / Gradle / Ivy

The newest version!
package org.objectweb.celtix;


import java.lang.ref.WeakReference;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import javax.xml.ws.WebServiceException;

import org.objectweb.celtix.bindings.BindingManager;
import org.objectweb.celtix.buslifecycle.BusLifeCycleManager;
import org.objectweb.celtix.configuration.Configuration;
import org.objectweb.celtix.jaxws.EndpointRegistry;
import org.objectweb.celtix.management.InstrumentationManager;
import org.objectweb.celtix.plugins.PluginManager;
import org.objectweb.celtix.resource.ResourceManager;
import org.objectweb.celtix.transports.TransportFactoryManager;
import org.objectweb.celtix.workqueue.WorkQueueManager;
import org.objectweb.celtix.wsdl.WSDLManager;

/**
 * The Bus class provides access to configuration, factories and managers
 * for use by an application.
 */
public abstract class Bus {
    
    public static final String BUS_CLASS_PROPERTY = "org.objectweb.celtix.BusClass";

    private static ThreadLocal current = new ThreadLocal();
    private static Map> nameMap = 
        new ConcurrentHashMap>();
    private static Bus defaultBus; 
    
    /**
     * Returns a newly created and fully initialised Bus.
     * 
     * @return Bus the newly created Bus.
     * @throws BusException If there is an error initializing Bus.
     */
    public static synchronized Bus init() throws BusException {
        return init(new String[0]);
    }

    /**
     * Returns a newly created and fully initialised Bus.
     * 
     * @param args any args, such as domain name, bus class, and other configuration
     * options that can be used to initialize this Bus.
     * @return Bus the newly created Bus.
     * @throws BusException If there is an error initializing Bus.
     */
    public static synchronized Bus init(String[] args) throws BusException {
        return init(args, new HashMap());
    }
    
    /**
     * Returns a newly created and fully initialised Bus.
     * 
     * @param args any args, such as domain name, bus class, and other configuration
     * options that can be used to initialize this Bus.
     * @param properties any properties, such as bus identifier, bus class, and other configuration
     * options that can be used to identify and initialize this Bus. 
     * The properties are superceded by the settings in the args parameter,
     * and they in turn supercede system properties.
     * @return Bus the newly created Bus.
     * @throws BusException If there is an error initializing Bus.
     */
    public static synchronized Bus init(String[] args, Map properties) throws BusException {
        return init(args, properties, null);
    }
    
    /**
     * Returns a newly created and fully initialised Bus.
     * 
     * @param args any args, such as domain name, bus class, and other configuration
     * options that can be used to initialize this Bus.
     * @param properties any properties, such as domain name, bus class, and other configuration
     * options that can be used to initialize this Bus. 
     * The properties are superceded by the settings in the args parameter,
     * and they in turn supercede system properties.
     * @param classLoader an optional classloader to use when instantiating a Bus
     * needs to be instantiated (defaults to the current thread's context classloader).
     * @return Bus the newly created Bus.
     * @throws BusException If there is an error initializing Bus.
     */
    public static synchronized Bus init(String[] args, 
        Map properties, 
        ClassLoader classLoader) throws BusException {
        
        // delegate to the factory 
        BusFactory bf = BusFactory.getInstance();
        Bus b = bf.getBus(args, properties, classLoader);
        nameMap.put(b.getBusID(), new WeakReference(b));
        return b;
    }
    
    /** 
    * Returns the current Bus on this thread.  If no bus
    * has been initialised on this thread, return the default bus.
    * 
    * @return the current Bus on this thread.
    */
    public static Bus getCurrent() {        
        Bus ret = current.get();
        if (ret == null) { 
            ret = getDefaultBus(); 
        } 
        return ret; 
    }


    /** 
    * Sets the current Bus.  If a bus is explicitly
    * initialised on a thread, this is the current bus.  If no thread
    * has been initialised (implicitly or explicitly), setting the
    * current bus will set the default bus for all threads
    *
    * @param bus the current bus
    */
    public static void setCurrent(Bus bus) {
        current.set(bus);
        setDefaultBus(bus);
    }

    /**
     * Returns the LAST Bus that was created with the given ID.  If 
     * multiple buses are created with the same ID, only the last is
     * saved for access later.
     * 
     * The Bus objects are only held via a WeakReference.   Thus, if
     * something else doesn't hold onto it, it will be garbage collected 
     * and this method will return null.
     * 
     * @param id
     * @return The last bus by the given ID.
     */
    public static Bus getByID(String id) {
        WeakReference bus = nameMap.get(id);
        if (bus != null) {
            if (bus.get() == null) {
                nameMap.remove(id);
            }
            return bus.get();
        }
        return null;
    }
    
    protected void removeByID(String id) {
        if (nameMap.containsKey(id)) {
            nameMap.remove(id);
        }
    }
    
    /**
     * Sends the event specified to the Bus.
     * @param event The BusEvent to send.     
     */
    public abstract void sendEvent(BusEvent event);

    /**
     * Adds an event listener to the current Bus.
     * @param l The BusEvenetListener to be added.
     * @param filter A BusEventFilter to be applied to the listener.
     * @throws BusException If there is an error adding listener.
     */
    public abstract void addListener(BusEventListener l, BusEventFilter filter)
        throws BusException;

    /**
     * Removes the specified event listener from the Bus.
     * @param l The BusEventListener to be removed.
     * @throws BusException If there is an error removing the listener.
     */
    public abstract void removeListener(BusEventListener l) throws BusException;

    /**
     * Provides access to BusEventCache associated with the Bus.
     * @return BusEventCache The BusEventCache object.
     * @see BusEventCache
     */
    public abstract BusEventCache getEventCache();

    /**
     * Shuts down the Bus.
     * 
     * @param wait If true, waits for the Bus
     * to shutdown before returning, otherwise returns immediately.
     * @throws BusException
     */
    public abstract void shutdown(boolean wait) throws BusException;

    /** 
     * Returns the Configuration of this Bus.
     * 
     * @return Configuration the configuration of this bus.
     */
    public abstract Configuration getConfiguration();

    /** 
     * Returns the TransportFactoryManager of this Bus.
     * 
     * @return TransportRegistry the servant registry of this Bus.
     */
    public abstract TransportFactoryManager getTransportFactoryManager();

    /** 
     * Returns the BindingManager of this Bus.
     * 
     * @return BindingManager the binding manager of this Bus.
     */
    public abstract BindingManager getBindingManager();

    /** 
     * Returns the ClientRegistry of this Bus.
     * 
     * @return WSDLManager the wsdl manager of this Bus.
     */
    public abstract WSDLManager getWSDLManager();

    /** 
     * Returns the PluginManager of this Bus.
     * 
     * @return PluginManager the plugin manager of this Bus.
     */
    public abstract PluginManager getPluginManager();

    /** 
     * Returns the BusLifeCycleManager of this Bus.
     * 
     * @return BusLifeCycleManager of this Bus.
     */
    public abstract BusLifeCycleManager getLifeCycleManager();

    /** 
     * Returns the WorkQueueManager of this Bus.
     * 
     * @return WorkQueueManager of this Bus.
     */
    public abstract WorkQueueManager getWorkQueueManager();
    

    /** 
     * Returns the ResourceManager of this Bus.
     * 
     * @return ResourceManager of this Bus.
     */
    public abstract ResourceManager getResourceManager();
    
    /**
     * Returns the  InstrumenatationManager  of this Bus
     * 
     * @return InstrumentationManager of this Bus
     */
    public abstract InstrumentationManager getInstrumentationManager();
    
    /**
     * Returns the BusID  of this Bus
     * 
     * @return String BusID of this Bus
     */
    public abstract String getBusID();
    

    /**
     * Starts processing bus events, and returns only after the Bus has been shut down
     * (from another thread).
     *
     */
    public abstract void run();
    
    /**
     * Get the Endpoint Registry from bus , which contains the jaxws endpoint reference
     */
    public abstract EndpointRegistry getEndpointRegistry();
    
    public abstract void initialize(String[] args,
            Map properties) throws BusException;


 
    static void clearDefault() { 
        defaultBus = null; 
    } 


    /**
     * Clear current for all threads.  For use in unit testing
     */
    static void clearCurrent() { 
        current.remove(); 
    } 
    /**
     * Initialise a default bus.
     */
    private static synchronized Bus getDefaultBus() { 
        try { 
            if (defaultBus == null) { 
                defaultBus = Bus.init(); 
            } 
            return defaultBus;
        } catch (BusException ex) { 
            throw new WebServiceException("unable to initialize default bus", ex);
        } 
    } 
    
    /** 
     * Set the default bus for all threads.  If no bus has been
     * already initialised, this bus will be used as the default bus
     * that do not explicitly initialise a bus.
     */
    private static synchronized void setDefaultBus(Bus bus) { 
        if (defaultBus == null) { 
            defaultBus = bus;
        } 
    } 

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy