org.apache.felix.ipojo
Class ComponentFactory

java.lang.Object
  extended by org.apache.felix.ipojo.IPojoFactory
      extended by org.apache.felix.ipojo.ComponentFactory
All Implemented Interfaces:
Factory, TrackerCustomizer, ManagedServiceFactory
Direct Known Subclasses:
CompositeFactory, 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. This class could export Factory and ManagedServiceFactory services.

Author:
Felix Project Team
See Also:
IPojoFactory, TrackerCustomizer

Nested Class Summary
 
Nested classes/interfaces inherited from class org.apache.felix.ipojo.IPojoFactory
IPojoFactory.RequiredHandler
 
Field Summary
static 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
INVALID, VALID
 
Constructor Summary
ComponentFactory(BundleContext context, byte[] clazz, Element element)
          Creates a instance manager factory.
ComponentFactory(BundleContext context, Element element)
          Creates a instance manager factory.
 
Method Summary
 void addedService(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(ServiceReference reference)
          This method is called when a new handler factory is detected.
 void check(Element element)
          Allows a factory to check if the given element is well-formed.
 ComponentInstance createInstance(Dictionary config, IPojoContext context, HandlerManager[] handlers)
          Creates a primitive instance.
 Class defineClass(String name, byte[] clazz, ProtectionDomain domain)
          Defines a class.
 ClassLoader getBundleClassLoader()
           
 String getClassName()
          Gets the class name.
 ComponentTypeDescription getComponentTypeDescription()
          Gets the component type description of the current factory.
 String getFactoryName()
          Computes the factory name.
 PojoMetadata getPojoMetadata()
          Returns manipulation metadata of this component type.
 List getRequiredHandlerList()
          Computes required handlers.
 URL getResource(String resName)
          Returns the URL of a resource.
 String getVersion()
          Gets the version of the component type.
 Class loadClass(String className)
          Loads a class.
 void modifiedService(ServiceReference reference, Object service)
          This method is called when a used handler factory is modified.
 void removedService(ServiceReference reference, Object service)
          This method is called when a used handler factory disappears.
 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, disposed, generateName, getBundleContext, getComponentDescription, getDescription, getHandler, 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 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(BundleContext context,
                        byte[] clazz,
                        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(BundleContext context,
                        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

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(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 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(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 Class defineClass(String name,
                         byte[] clazz,
                         ProtectionDomain domain)
Defines a class. This method needs to be synchronized to avoid that the classloader is created twice. This method delegate 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

getResource

public URL getResource(String resName)
Returns the URL of a resource. This methods delegates the invocation to the Bundle.getResource(String) method.

Parameters:
resName - the resource name
Returns:
the URL of the resource

loadClass

public Class loadClass(String className)
                throws 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.

Parameters:
className - the name of the class to load
Returns:
the resulting Class object
Throws:
ClassNotFoundException - if the class is not found

starting

public void starting()
Starts the factory. This method is 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 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 List 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(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(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(ServiceReference reference,
                           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(ServiceReference reference,
                            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 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 ClassLoader getBundleClassLoader()


Copyright © 2013 The Apache Software Foundation. All Rights Reserved.