org.apache.felix.ipojo
Class InstanceManager

java.lang.Object
  extended by org.apache.felix.ipojo.InstanceManager
All Implemented Interfaces:
ComponentInstance, InstanceStateListener
Direct Known Subclasses:
HandlerManager

public class InstanceManager
extends java.lang.Object
implements ComponentInstance, InstanceStateListener

The instance manager class manages one instance of a component type. It manages component lifecycle, component instance creation and handlers.

Author:
Felix Project Team

Field Summary
protected  java.lang.String m_className
          Name of the component type implementation class.
protected  HandlerManager[] m_handlers
          Handler list.
protected  java.util.List m_listeners
          Instance State Listener List.
protected  java.lang.String m_name
          Name of the component instance.
protected  int m_state
          Component state (STOPPED at the beginning).
 
Fields inherited from interface org.apache.felix.ipojo.ComponentInstance
DISPOSED, INVALID, STOPPED, VALID
 
Constructor Summary
InstanceManager(ComponentFactory factory, org.osgi.framework.BundleContext context, HandlerManager[] handlers)
          Construct a new Component Manager.
 
Method Summary
 void addInstanceStateListener(InstanceStateListener listener)
          Register an instance state listener.
 void configure(org.apache.felix.ipojo.metadata.Element metadata, java.util.Dictionary configuration)
          Configure the instance manager.
 java.lang.Object createPojoObject()
          Create an instance of the component.
 void dispose()
          Dispose the instance.
 java.lang.String getClassName()
          Get the implementation class of the component type.
 java.lang.Class getClazz()
          Get the manipulated class.
 org.osgi.framework.BundleContext getContext()
          Get the bundle context used by this component instance.
 ComponentFactory getFactory()
          Get the factory which create the current instance.
 java.lang.Object getFieldValue(java.lang.String fieldName)
          Give access to a field value to the first created pojo.
 java.lang.Object getFieldValue(java.lang.String fieldName, java.lang.Object pojo)
          Give access to a field value to the given created pojo.
 org.osgi.framework.BundleContext getGlobalContext()
           
 Handler getHandler(java.lang.String name)
          Return a specified handler.
 InstanceDescription getInstanceDescription()
          Get the description of the current instance.
 java.lang.String getInstanceName()
          Get the instance name.
 ServiceContext getLocalServiceContext()
           
 java.lang.Object getPojoObject()
          Get the first object created by the instance.
 java.lang.Object[] getPojoObjects()
          Get the array of object created by the instance.
 java.util.Set getRegistredFields()
          Get the list of registered fields.
 Handler[] getRegistredHandlers()
          Get the list of handlers plugged on the instance.
 java.util.Set getRegistredMethods()
          Get the list of registered methods.
 int getState()
          Get the actual state of the instance.
 boolean isStarted()
          Check if the instance if started.
 void onEntry(java.lang.Object pojo, java.lang.String methodId, java.lang.Object[] args)
          Dispatch entry method event on registered handler.
 void onError(java.lang.Object pojo, java.lang.String methodId, java.lang.Throwable error)
          Dispatch error method event on registered handler.
 void onExit(java.lang.Object pojo, java.lang.String methodId, java.lang.Object result)
          Dispatch exit method event on registered handler.
 java.lang.Object onGet(java.lang.Object pojo, java.lang.String fieldName)
          This method is called by the manipulated class each time that a GETFIELD instruction is found.
 void onSet(java.lang.Object pojo, java.lang.String fieldName, java.lang.Object objectValue)
          This method is called by the manipulated class each time that a PUTFILED instruction is found.
 void reconfigure(java.util.Dictionary configuration)
          Reconfigure the current instance.
 void register(FieldMetadata field, FieldInterceptor interceptor)
          Register a field interceptor.
 void register(MethodMetadata method, MethodInterceptor interceptor)
          Register a method interceptor.
 void register(PrimitiveHandler handler, FieldMetadata[] fields, MethodMetadata[] methods)
          Deprecated. use register(FieldMetadata fm, FieldInterceptor fi) and register(MethodMetadata mm, MethodInterceptor mi) instead.
 void removeInstanceStateListener(InstanceStateListener listener)
          Unregister an instance state listener.
 void setState(int state)
          Set the state of the component instance.
 void start()
          Start the instance manager.
 void stateChanged(ComponentInstance instance, int newState)
          State Change listener callback.
 void stop()
          Stop the instance manager.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

m_name

protected java.lang.String m_name
Name of the component instance.


m_className

protected java.lang.String m_className
Name of the component type implementation class.


m_handlers

protected HandlerManager[] m_handlers
Handler list.


m_state

protected int m_state
Component state (STOPPED at the beginning).


m_listeners

protected java.util.List m_listeners
Instance State Listener List.

Constructor Detail

InstanceManager

public InstanceManager(ComponentFactory factory,
                       org.osgi.framework.BundleContext context,
                       HandlerManager[] handlers)
Construct a new Component Manager.

Parameters:
factory - : the factory managing the instance manager
context - : the bundle context to give to the instance
handlers - : handlers array
Method Detail

configure

public void configure(org.apache.felix.ipojo.metadata.Element metadata,
                      java.util.Dictionary configuration)
               throws ConfigurationException
Configure the instance manager. Stop the existing handler, clear the handler list, change the metadata, recreate the handlers

Parameters:
metadata - : the component type metadata
configuration - : the configuration of the instance
Throws:
ConfigurationException - : occurs if the metadata are not correct

getInstanceDescription

public InstanceDescription getInstanceDescription()
Get the description of the current instance.

Specified by:
getInstanceDescription in interface ComponentInstance
Returns:
the instance description.
See Also:
ComponentInstance.getInstanceDescription()

getRegistredHandlers

public Handler[] getRegistredHandlers()
Get the list of handlers plugged on the instance.

Returns:
the handler array of plugged handlers.

getHandler

public Handler getHandler(java.lang.String name)
Return a specified handler.

Parameters:
name - : class name of the handler to find or its qualified name (namespace:name)
Returns:
: the handler, or null if not found

getFieldValue

public java.lang.Object getFieldValue(java.lang.String fieldName)
Give access to a field value to the first created pojo. This method process by analyzing both managed fields and pojo fields (by reflection). If no pojo were already created try only on managed fields.

Parameters:
fieldName - : field name.
Returns:
the field value, null is returned if the value is managed and not already set.

getFieldValue

public java.lang.Object getFieldValue(java.lang.String fieldName,
                                      java.lang.Object pojo)
Give access to a field value to the given created pojo. This method process by analyzing both managed fields and pojo fields (by reflection). If the given pojo is null, try only on managed fields.

Parameters:
fieldName - : field name.
pojo - : the pojo on which computing field value.
Returns:
the field value, null is returned if the value is managed and not already set.

start

public void start()
Start the instance manager.

Specified by:
start in interface ComponentInstance

stop

public void stop()
Stop the instance manager.

Specified by:
stop in interface ComponentInstance

dispose

public void dispose()
Dispose the instance.

Specified by:
dispose in interface ComponentInstance
See Also:
ComponentInstance.dispose()

setState

public void setState(int state)
Set the state of the component instance. if the state changed call the stateChanged(int) method on the handlers. This method has a reentrant mechanism. If in the flow of the first call the method is called another times, the second call is stored and executed after the first one is finished.

Parameters:
state - : the new state

getState

public int getState()
Get the actual state of the instance.

Specified by:
getState in interface ComponentInstance
Returns:
the actual state of the component instance.
See Also:
ComponentInstance.getState()

isStarted

public boolean isStarted()
Check if the instance if started.

Specified by:
isStarted in interface ComponentInstance
Returns:
true if the instance is started.
See Also:
ComponentInstance.isStarted()

addInstanceStateListener

public void addInstanceStateListener(InstanceStateListener listener)
Register an instance state listener.

Specified by:
addInstanceStateListener in interface ComponentInstance
Parameters:
listener - : listener to register.
See Also:
ComponentInstance.addInstanceStateListener(org.apache.felix.ipojo.InstanceStateListener)

removeInstanceStateListener

public void removeInstanceStateListener(InstanceStateListener listener)
Unregister an instance state listener.

Specified by:
removeInstanceStateListener in interface ComponentInstance
Parameters:
listener - : listener to unregister.
See Also:
ComponentInstance.removeInstanceStateListener(org.apache.felix.ipojo.InstanceStateListener)

getFactory

public ComponentFactory getFactory()
Get the factory which create the current instance.

Specified by:
getFactory in interface ComponentInstance
Returns:
the factory of the component
See Also:
ComponentInstance.getFactory()

getPojoObjects

public java.lang.Object[] getPojoObjects()
Get the array of object created by the instance.

Returns:
the created instance of the component instance.

createPojoObject

public java.lang.Object createPojoObject()
Create an instance of the component. This method need to be called one time only for singleton provided service

Returns:
a new instance

getPojoObject

public java.lang.Object getPojoObject()
Get the first object created by the instance. If no object created, create and return one object.

Returns:
the instance of the component instance to use for singleton component

getClazz

public java.lang.Class getClazz()
Get the manipulated class.

Returns:
the manipulated class

register

public void register(PrimitiveHandler handler,
                     FieldMetadata[] fields,
                     MethodMetadata[] methods)
Deprecated. use register(FieldMetadata fm, FieldInterceptor fi) and register(MethodMetadata mm, MethodInterceptor mi) instead.

Register an handler. The handler will be notified of event on each field given in the list.

Parameters:
handler - : the handler to register
fields - : the field metadata list
methods - : the method metadata list

register

public void register(FieldMetadata field,
                     FieldInterceptor interceptor)
Register a field interceptor.

Parameters:
field - : intercepted field
interceptor - : interceptor

register

public void register(MethodMetadata method,
                     MethodInterceptor interceptor)
Register a method interceptor.

Parameters:
method - : intercepted method
interceptor - : interceptor

onGet

public java.lang.Object onGet(java.lang.Object pojo,
                              java.lang.String fieldName)
This method is called by the manipulated class each time that a GETFIELD instruction is found. The method ask to each handler which value need to be returned.

Parameters:
pojo - : the pojo object on which the field was get
fieldName - : the field name on which the GETFIELD instruction is called
Returns:
the value decided by the last asked handler (throw a warning if two fields decide two different values)

onEntry

public void onEntry(java.lang.Object pojo,
                    java.lang.String methodId,
                    java.lang.Object[] args)
Dispatch entry method event on registered handler.

Parameters:
pojo - : the pojo object on which method is invoked.
methodId - : method id
args - : argument array

onExit

public void onExit(java.lang.Object pojo,
                   java.lang.String methodId,
                   java.lang.Object result)
Dispatch exit method event on registered handler. The given returned object is an instance of Exception if the method has launched an exception. If the given object is null, either the method returns void, either the method has returned null.

Parameters:
pojo - : the pojo object on which the method was invoked
methodId - : method id
result - : returned object.

onError

public void onError(java.lang.Object pojo,
                    java.lang.String methodId,
                    java.lang.Throwable error)
Dispatch error method event on registered handler. The given returned object is an instance of Exception if the method has thrown an exception. If the given object is null, either the method returns void, either the method has returned null.

Parameters:
pojo - : the pojo object on which the method was invoked
methodId - : method id
error - : throwable object.

onSet

public void onSet(java.lang.Object pojo,
                  java.lang.String fieldName,
                  java.lang.Object objectValue)
This method is called by the manipulated class each time that a PUTFILED instruction is found. the method send to each handler the new value.

Parameters:
pojo - : the pojo object on which the field was set
fieldName - : the field name on which the PUTFIELD instruction is called
objectValue - : the value of the field

getContext

public org.osgi.framework.BundleContext getContext()
Get the bundle context used by this component instance.

Specified by:
getContext in interface ComponentInstance
Returns:
the context of the component.
See Also:
ComponentInstance.getContext()

getGlobalContext

public org.osgi.framework.BundleContext getGlobalContext()

getLocalServiceContext

public ServiceContext getLocalServiceContext()

getInstanceName

public java.lang.String getInstanceName()
Get the instance name.

Specified by:
getInstanceName in interface ComponentInstance
Returns:
the instance name.
See Also:
ComponentInstance.getInstanceName()

reconfigure

public void reconfigure(java.util.Dictionary configuration)
Reconfigure the current instance.

Specified by:
reconfigure in interface ComponentInstance
Parameters:
configuration - : the new configuration to push
See Also:
ComponentInstance.reconfigure(java.util.Dictionary)

getClassName

public java.lang.String getClassName()
Get the implementation class of the component type.

Returns:
the class name of the component type.

stateChanged

public void stateChanged(ComponentInstance instance,
                         int newState)
State Change listener callback. This method is notified at each time a plugged handler becomes invalid.

Specified by:
stateChanged in interface InstanceStateListener
Parameters:
instance - : changing instance
newState - : new state
See Also:
InstanceStateListener.stateChanged(org.apache.felix.ipojo.ComponentInstance, int)

getRegistredFields

public java.util.Set getRegistredFields()
Get the list of registered fields. This method is invoked by the POJO itself.

Returns:
the set of registered fields.

getRegistredMethods

public java.util.Set getRegistredMethods()
Get the list of registered methods. This method is invoked by the POJO itself.

Returns:
the set of registered methods.