org.apache.felix.ipojo

## Class ComponentFactory

- java.lang.Object
- - org.apache.felix.ipojo.IPojoFactory
  - - org.apache.felix.ipojo.ComponentFactory

All Implemented Interfaces:
  Factory, TrackerCustomizer

Direct Known Subclasses:
  HandlerManagerFactory

    public class ComponentFactory
    extends IPojoFactory
    implements TrackerCustomizer

The component factory manages component instance objects. This management consists to create and manage component instances build with the current component factory. If the factory is public a `Factory` service is exposed.

Author:
  Felix Project Team
See Also:
  `Factory`, `IPojoFactory`, `TrackerCustomizer`

### Nested Class Summary

### Nested classes/interfaces inherited from class org.apache.felix.ipojo.IPojoFactory

`IPojoFactory.DefaultNameGenerator, IPojoFactory.NameGenerator, IPojoFactory.RequiredHandler, IPojoFactory.RetryNameGenerator`

### Field Summary

Fields
`static java.lang.String` `HANDLER_AUTO_PRIMITIVE` System property set to automatically attach primitive handlers to primitive component types.
`protected Tracker` `m_tracker` The tracker used to track required handler factories.

### Fields inherited from class org.apache.felix.ipojo.IPojoFactory

`INSTANCE_NAME, m_componentDesc, m_componentInstances, m_componentMetadata, m_context, m_factoryName, m_isPublic, m_listeners, m_logger, m_requiredHandlers, m_sr, m_state, m_version`

### Fields inherited from interface org.apache.felix.ipojo.Factory

`FACTORY_VERSION_PROPERTY, INSTANCE_NAME_PROPERTY, INVALID, VALID`

### Constructor Summary

Constructors
Constructor and Description
`ComponentFactory(org.osgi.framework.BundleContext context, byte[] clazz, org.apache.felix.ipojo.metadata.Element element)` Creates a instance manager factory.
`ComponentFactory(org.osgi.framework.BundleContext context, org.apache.felix.ipojo.metadata.Element element)` Creates a instance manager factory.

### Method Summary

All Methods Instance Methods Concrete Methods
`void` `addedService(org.osgi.framework.ServiceReference reference)` This method is called when a matching service has been added to the tracker, we can no compute the factory state.
`boolean` `addingService(org.osgi.framework.ServiceReference reference)` This method is called when a new handler factory is detected.
`void` `check(org.apache.felix.ipojo.metadata.Element element)` Allows a factory to check if the given element is well-formed.
`ComponentInstance` `createInstance(java.util.Dictionary config, IPojoContext context, HandlerManager[] handlers)` Creates a primitive instance.
`java.lang.Class<? extends java.lang.Object>` `defineClass(java.lang.String name, byte[] clazz, java.security.ProtectionDomain domain)` Defines a class.
`java.lang.ClassLoader` `getBundleClassLoader()`
`java.lang.String` `getClassName()` Gets the class name.
`ComponentTypeDescription` `getComponentTypeDescription()` Gets the component type description of the current factory.
`java.lang.String` `getFactoryName()` Computes the factory name.
`PojoMetadata` `getPojoMetadata()` Returns manipulation metadata of this component type.
`java.util.List<IPojoFactory.RequiredHandler>` `getRequiredHandlerList()` Computes required handlers.
`java.lang.String` `getVersion()` Gets the version of the component type.
`java.lang.Class` `loadClass(java.lang.String className)` Loads a class.
`void` `modifiedService(org.osgi.framework.ServiceReference reference, java.lang.Object service)` This method is called when a used handler factory is modified.
`void` `removedService(org.osgi.framework.ServiceReference reference, java.lang.Object service)` This method is called when a used handler factory disappears.
`void` `setUseFactoryClassloader(boolean use)` Sets the flag enabling / disabling the factory classloader.
`void` `starting()` Starts the factory.
`void` `stopping()` Stops all the instance managers.

### Methods inherited from class org.apache.felix.ipojo.IPojoFactory

`addFactoryStateListener, checkAcceptability, computeDescription, computeFactoryState, createComponentInstance, createComponentInstance, deleted, dispose, disposed, getBundleContext, getComponentDescription, getComponentMetadata, getDescription, getHandler, getInstanceByName, getInstances, getInstancesNames, getIPOJOBundleContext, getLogger, getMissingHandlers, getName, getRequiredHandlers, getState, isAcceptable, match, reconfigure, removeFactoryStateListener, restart, start, stop, updated`

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

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

### Field Detail

#### HANDLER_AUTO_PRIMITIVE

    public static final java.lang.String HANDLER_AUTO_PRIMITIVE

System property set to automatically attach primitive handlers to primitive component types. The value is a String parsed as a list (comma separated). Each element is the fully qualified name of the handler `namespace:name`.

See Also:
  Constant Field Values

#### m_tracker

    protected Tracker m_tracker

The tracker used to track required handler factories. Immutable once set.

### Constructor Detail

#### ComponentFactory

    public ComponentFactory(org.osgi.framework.BundleContext context,
                            byte[] clazz,
                            org.apache.felix.ipojo.metadata.Element element)
                     throws ConfigurationException

Creates a instance manager factory. The class is given in parameter. The component type is not a composite.

Parameters:
  `context` - the bundle context
  `clazz` - the component class
  `element` - the metadata of the component
Throws:
  `ConfigurationException` - if the element describing the factory is malformed.

#### ComponentFactory

    public ComponentFactory(org.osgi.framework.BundleContext context,
                            org.apache.felix.ipojo.metadata.Element element)
                     throws ConfigurationException

Creates a instance manager factory.

Parameters:
  `context` - the bundle context
  `element` - the metadata of the component to create
Throws:
  `ConfigurationException` - if element describing the factory is malformed.

### Method Detail

#### setUseFactoryClassloader

    public void setUseFactoryClassloader(boolean use)

Sets the flag enabling / disabling the factory classloader.

Parameters:
  `use` - `true` enables the factory classloader.

#### getComponentTypeDescription

    public ComponentTypeDescription getComponentTypeDescription()

Gets the component type description of the current factory.

Overrides:
  `getComponentTypeDescription` in class `IPojoFactory`
Returns:
  the description of the component type attached to this factory.
See Also:
  `IPojoFactory.getComponentTypeDescription()`

#### check

    public void check(org.apache.felix.ipojo.metadata.Element element)
               throws ConfigurationException

Allows a factory to check if the given element is well-formed. A component factory metadata is correct if they contain the 'classname' attribute. As this method is called from the (single-threaded) constructor, no synchronization is needed.

Parameters:
  `element` - the metadata describing the component
Throws:
  `ConfigurationException` - if the element describing the factory is malformed.

#### getClassName

    public java.lang.String getClassName()

Gets the class name. No synchronization needed, the classname is immutable.

Specified by:
  `getClassName` in interface `Factory`
Specified by:
  `getClassName` in class `IPojoFactory`
Returns:
  the class name.
See Also:
  `IPojoFactory.getClassName()`

#### createInstance

    public ComponentInstance createInstance(java.util.Dictionary config,
                                            IPojoContext context,
                                            HandlerManager[] handlers)
                                     throws ConfigurationException

Creates a primitive instance. This method is called when holding the lock.

Specified by:
  `createInstance` in class `IPojoFactory`
Parameters:
  `config` - the instance configuration
  `context` - the service context (null if the instance has to be created in the global space).
  `handlers` - the handlers to attach to the instance
Returns:
  the created instance
Throws:
  `ConfigurationException` - if the configuration process failed.
See Also:
  `IPojoFactory.createInstance(java.util.Dictionary, org.apache.felix.ipojo.IPojoContext, org.apache.felix.ipojo.HandlerManager[])`

#### defineClass

    public java.lang.Class<? extends java.lang.Object> defineClass(java.lang.String name,
                                                                   byte[] clazz,
                                                                   java.security.ProtectionDomain domain)

Defines a class. This method needs to be synchronized to avoid that the classloader is created twice. This method delegates the `define` method invocation to the factory classloader.

Parameters:
  `name` - the qualified name of the class
  `clazz` - the byte array of the class
  `domain` - the protection domain of the class
Returns:
  the defined class object

#### loadClass

    public java.lang.Class loadClass(java.lang.String className)
                              throws java.lang.ClassNotFoundException

Loads a class. This method checks if the class to load is the implementation class or not. If it is, the factory classloader is used, else the `Bundle.loadClass(String)` is called. The implementation class is loaded using the factory classloader only if the factory classloader was enabled

Parameters:
  `className` - the name of the class to load
Returns:
  the resulting Class object
Throws:
  `java.lang.ClassNotFoundException` - if the class is not found
See Also:
  `setUseFactoryClassloader(boolean)`

#### starting

    public void starting()

Starts the factory. This method is not called when holding the monitor lock.

Specified by:
  `starting` in class `IPojoFactory`

#### stopping

    public void stopping()

Stops all the instance managers. This method is called when holding the lock.

Specified by:
  `stopping` in class `IPojoFactory`

#### getFactoryName

    public java.lang.String getFactoryName()

Computes the factory name. The factory name is computed from the 'name' and 'classname' attributes. This method does not manipulate any non-immutable fields, so does not need to be synchronized.

Specified by:
  `getFactoryName` in class `IPojoFactory`
Returns:
  the factory name.

#### getRequiredHandlerList

    public java.util.List<IPojoFactory.RequiredHandler> getRequiredHandlerList()

Computes required handlers. This method does not manipulate any non-immutable fields, so does not need to be synchronized. This method checks the `HANDLER_AUTO_PRIMITIVE` system property to add the listed handlers to the required handler set.

Specified by:
  `getRequiredHandlerList` in class `IPojoFactory`
Returns:
  the required handler list.

#### addingService

    public boolean addingService(org.osgi.framework.ServiceReference reference)

This method is called when a new handler factory is detected. Test if the factory can be used or not. This method need to be synchronized as it accesses to the content of required handlers.

Specified by:
  `addingService` in interface `TrackerCustomizer`
Parameters:
  `reference` - the new service reference.
Returns:
  `true` if the given factory reference matches with a required handler.
See Also:
  `TrackerCustomizer.addingService(org.osgi.framework.ServiceReference)`

#### addedService

    public void addedService(org.osgi.framework.ServiceReference reference)

This method is called when a matching service has been added to the tracker, we can no compute the factory state. This method is synchronized to avoid concurrent calls to method modifying the factory state.

Specified by:
  `addedService` in interface `TrackerCustomizer`
Parameters:
  `reference` - the added service reference.
See Also:
  `TrackerCustomizer.addedService(org.osgi.framework.ServiceReference)`

#### removedService

    public void removedService(org.osgi.framework.ServiceReference reference,
                               java.lang.Object service)

This method is called when a used handler factory disappears. This method is synchronized to avoid concurrent calls to method modifying the factory state.

Specified by:
  `removedService` in interface `TrackerCustomizer`
Parameters:
  `reference` - the leaving service reference.
  `service` - the handler factory object.
See Also:
  `TrackerCustomizer.removedService(org.osgi.framework.ServiceReference, java.lang.Object)`

#### modifiedService

    public void modifiedService(org.osgi.framework.ServiceReference reference,
                                java.lang.Object service)

This method is called when a used handler factory is modified. However, handler factory modification is not possible, so this method is never called.

Specified by:
  `modifiedService` in interface `TrackerCustomizer`
Parameters:
  `reference` - the service reference
  `service` - the Factory object (if already get)
See Also:
  `TrackerCustomizer.modifiedService(org.osgi.framework.ServiceReference, java.lang.Object)`

#### getPojoMetadata

    public PojoMetadata getPojoMetadata()

Returns manipulation metadata of this component type.

Returns:
  manipulation metadata of this component type.

#### getVersion

    public java.lang.String getVersion()

Gets the version of the component type.

Specified by:
  `getVersion` in interface `Factory`
Returns:
  the version of `null` if not set.
See Also:
  `Factory.getVersion()`

#### getBundleClassLoader

    public java.lang.ClassLoader getBundleClassLoader()