Package org.apache.cxf

## Class BusFactory

- java.lang.Object
- - org.apache.cxf.BusFactory

- Direct Known Subclasses:
    `CXFBusFactory`, `SpringBusFactory`

      public abstract class BusFactory
      extends Object

  Factory to create CXF Bus objects.

  CXF includes a large number of components that provide services, such as WSDL parsing, and message processing. To avoid creating these objects over and over, and to allow them to be shared easily, they are associated with a data structure called a bus.

  You don't ever have to explicitly create or manipulate bus objects. If you simply use the CXF or JAX-WS APIs to create clients or servers, CXF will create a default bus for you. You can create a bus explicitly if you need to customize components on the bus or maintain several independent buses with independent configurations.

  This class maintains the default bus for the entire process and a set of thread-default buses. All CXF components that reference the bus, which is to say all CXF components, will obtain a default bus from this class if you do not set a specific bus.

  If you create a bus when there is no default bus in effect, that bus will become the default bus.

  This class holds a reference to the global default bus and a reference to each thread default bus. The thread references are weak with respect to the threads, but otherwise ordinary. Thus, so long as the thread remains alive there will be a strong reference to the bus, and it will not get garbage-collected. If you want to recover memory used CXF, you can set the default and per-thread default bus to null, explicitly.

- - ### Field Summary

    Fields
    Modifier and Type | Field | Description
    --- | --- | ---
    `static String` | `BUS_FACTORY_PROPERTY_NAME` |
    `static String` | `DEFAULT_BUS_FACTORY` |
    `protected static Bus` | `defaultBus` |
    `protected static ThreadLocal<org.apache.cxf.BusFactory.BusHolder>` | `THREAD_BUS` |
    `protected static Map<Thread,​org.apache.cxf.BusFactory.BusHolder>` | `THREAD_BUSSES` |

  - ### Constructor Summary

    Constructors
    Constructor | Description
    --- | ---
    `BusFactory()` |

  - ### Method Summary

    All Methods Static Methods Instance Methods Abstract Methods Concrete Methods
    Modifier and Type | Method | Description
    --- | --- | ---
    `static void` | `clearDefaultBusForAnyThread​(Bus bus)` | Removes a bus from being a thread default bus for any thread.
    `abstract Bus` | `createBus()` | Creates a new bus.
    `static Bus` | `getAndSetThreadDefaultBus​(Bus bus)` | Sets the default bus for the thread.
    `static Bus` | `getDefaultBus()` | Returns the default bus, creating it if necessary.
    `static Bus` | `getDefaultBus​(boolean createIfNeeded)` | Returns the default bus
    `static Bus` | `getThreadDefaultBus()` | Gets the default bus for the thread.
    `static Bus` | `getThreadDefaultBus​(boolean createIfNeeded)` | Gets the default bus for the thread, creating if needed
    `protected void` | `initializeBus​(Bus bus)` |
    `static BusFactory` | `newInstance()` | Create a new BusFactory The class of the BusFactory is determined by looking for the system propery: org.apache.cxf.bus.factory or by searching the classpath for: META-INF/services/org.apache.cxf.bus.factory
    `static BusFactory` | `newInstance​(String className)` | Create a new BusFactory
    `static boolean` | `possiblySetDefaultBus​(Bus bus)` | Sets the default bus if a default bus is not already set.
    `static void` | `setDefaultBus​(Bus bus)` | Sets the default bus.
    `static void` | `setThreadDefaultBus​(Bus bus)` | Sets the default bus for the thread.

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Field Detail

    - #### BUS_FACTORY_PROPERTY_NAME

          public static final String BUS_FACTORY_PROPERTY_NAME

      See Also:
        Constant Field Values

    - #### DEFAULT_BUS_FACTORY

          public static final String DEFAULT_BUS_FACTORY

      See Also:
        Constant Field Values

    - #### defaultBus

          protected static Bus defaultBus

    - #### THREAD_BUSSES

          protected static final Map<Thread,​org.apache.cxf.BusFactory.BusHolder> THREAD_BUSSES

    - #### THREAD_BUS

          protected static final ThreadLocal<org.apache.cxf.BusFactory.BusHolder> THREAD_BUS

  - ### Constructor Detail

    - #### BusFactory

          public BusFactory()

  - ### Method Detail

    - #### createBus

          public abstract Bus createBus()

      Creates a new bus. While concrete `BusFactory` may offer differently parameterized methods for creating a bus, all factories support this no-arg factory method.
      Returns:
        the newly created bus.

    - #### getDefaultBus

          public static Bus getDefaultBus()

      Returns the default bus, creating it if necessary.
      Returns:
        the default bus.

    - #### getDefaultBus

          public static Bus getDefaultBus​(boolean createIfNeeded)

      Returns the default bus
      Parameters:
        `createIfNeeded` - Set to true to create a default bus if one doesn't exist
      Returns:
        the default bus.

    - #### setDefaultBus

          public static void setDefaultBus​(Bus bus)

      Sets the default bus.
      Parameters:
        `bus` - the default bus.

    - #### setThreadDefaultBus

          public static void setThreadDefaultBus​(Bus bus)

      Sets the default bus for the thread.
      Parameters:
        `bus` - the default bus.

    - #### getAndSetThreadDefaultBus

          public static Bus getAndSetThreadDefaultBus​(Bus bus)

      Sets the default bus for the thread.
      Parameters:
        `bus` - the new thread default bus.
      Returns:
        the old thread default bus or null

    - #### getThreadDefaultBus

          public static Bus getThreadDefaultBus()

      Gets the default bus for the thread.
      Returns:
        the default bus.

    - #### getThreadDefaultBus

          public static Bus getThreadDefaultBus​(boolean createIfNeeded)

      Gets the default bus for the thread, creating if needed
      Parameters:
        `createIfNeeded` - Set to true to create a default bus if one doesn't exist
      Returns:
        the default bus.

    - #### clearDefaultBusForAnyThread

          public static void clearDefaultBusForAnyThread​(Bus bus)

      Removes a bus from being a thread default bus for any thread.

      This is typically done when a bus has ended its lifecycle (i.e.: a call to `Bus.shutdown(boolean)` was invoked) and it wants to remove any reference to itself for any thread.

      Parameters:
        `bus` - the bus to remove

    - #### possiblySetDefaultBus

          public static boolean possiblySetDefaultBus​(Bus bus)

      Sets the default bus if a default bus is not already set.
      Parameters:
        `bus` - the default bus.
      Returns:
        true if the bus was not set and is now set

    - #### newInstance

          public static BusFactory newInstance()

      Create a new BusFactory The class of the BusFactory is determined by looking for the system propery: org.apache.cxf.bus.factory or by searching the classpath for: META-INF/services/org.apache.cxf.bus.factory
      Returns:
        a new BusFactory to be used to create Bus objects

    - #### newInstance

          public static BusFactory newInstance​(String className)

      Create a new BusFactory
      Parameters:
        `className` - The class of the BusFactory to create. If null, uses the default search algorithm.
      Returns:
        a new BusFactory to be used to create Bus objects

    - #### initializeBus

          protected void initializeBus​(Bus bus)