public class InstanceManager extends java.lang.Object implements ComponentInstance, InstanceStateListener, InstanceBundleContextAware
Modifier and Type | Field and Description |
---|---|
protected java.lang.String |
m_className
The name of the component type implementation class.
|
protected HandlerManager[] |
m_handlers
The handler object list.
|
protected java.util.List |
m_listeners
The instance state listener list.
|
protected java.lang.String |
m_name
The name of the component instance.
|
protected java.util.List |
m_pojoObjects
The content of the current instance.
|
protected int |
m_state
The current instance state (
ComponentInstance.STOPPED at the beginning). |
DISPOSED, INVALID, STOPPED, VALID
Constructor and Description |
---|
InstanceManager(ComponentFactory factory,
org.osgi.framework.BundleContext context,
HandlerManager[] handlers)
Creates a new Component Manager.
|
Modifier and Type | Method and Description |
---|---|
void |
addInstanceStateListener(InstanceStateListener listener)
Registers an instance state listener.
|
void |
configure(org.apache.felix.ipojo.metadata.Element metadata,
java.util.Dictionary configuration)
Configures the instance manager.
|
protected java.lang.Object |
createObject()
Creates a POJO objects.
|
java.lang.Object |
createPojoObject()
Creates an instance of the content.
|
void |
deletePojoObject(java.lang.Object pojo)
Deletes a POJO object.
|
void |
dispose()
Disposes the instance.
|
java.lang.String |
getClassName()
Gets the implementation class of the component type.
|
java.lang.Class |
getClazz()
Gets the manipulated class.
|
org.osgi.framework.BundleContext |
getContext()
Gets the bundle context used by this component instance.
|
ComponentFactory |
getFactory()
Gets the factory which has created the current instance.
|
java.lang.Object |
getFieldValue(java.lang.String fieldName)
Gives access to a field value of the first created pojo.
|
java.lang.Object |
getFieldValue(java.lang.String fieldName,
java.lang.Object pojo)
Gives access to a field value to the given created pojo.
|
org.osgi.framework.BundleContext |
getGlobalContext()
Gets the global bundle context.
|
Handler |
getHandler(java.lang.String name)
Returns a specified handler.
|
org.osgi.framework.BundleContext |
getInstanceContext()
Gets the bundle context of the instance, i.e.
|
InstanceDescription |
getInstanceDescription()
Gets the description of the current instance.
|
java.lang.String |
getInstanceName()
Gets the instance name.
|
ServiceContext |
getLocalServiceContext()
Gets the local service context.
|
Logger |
getLogger()
The instance logger.
|
java.lang.Object |
getPojoObject()
Gets the first object created by the instance.
|
java.lang.Object[] |
getPojoObjects()
Gets the object array created by the instance.
|
Handler[] |
getRegisteredHandlers()
Gets the list of handlers plugged (i.e.
|
java.util.Set |
getRegistredFields()
Gets the list of registered fields (containing field names).
|
java.util.Set |
getRegistredMethods()
Gets the list of registered methods (containing method ids).
|
int |
getState()
Gets the actual state of the instance.
|
boolean |
isStarted()
Checks if the instance is started.
|
protected void |
load()
Loads the manipulated class.
|
void |
onEntry(java.lang.Object pojo,
java.lang.String methodId,
java.lang.Object[] args)
Dispatches entry method events on registered method interceptors.
|
void |
onError(java.lang.Object pojo,
java.lang.String methodId,
java.lang.Throwable error)
Dispatches error method events on registered method interceptors.
|
void |
onExit(java.lang.Object pojo,
java.lang.String methodId,
java.lang.Object result)
Dispatches exit method events on registered method interceptors.
|
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 executed.
|
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 PUTFIELD instruction is executed.
|
void |
reconfigure(java.util.Dictionary configuration)
Reconfigures the current instance.
|
void |
register(FieldMetadata field,
FieldInterceptor interceptor)
Registers a field interceptor.
|
void |
register(int index,
ConstructorInjector injector)
Registers a constructor injector.
|
void |
register(MethodMetadata method,
MethodInterceptor interceptor)
Registers a method interceptor.
|
void |
register(MethodMetadata method,
java.lang.String innerClass,
MethodInterceptor interceptor)
Registers a method interceptor on a methods from an inner class.
|
void |
register(PrimitiveHandler handler,
FieldMetadata[] fields,
MethodMetadata[] methods)
Deprecated.
|
void |
removeInstanceStateListener(InstanceStateListener listener)
Unregisters an instance state listener.
|
void |
setInstanceBundleContext(org.osgi.framework.BundleContext context)
Sets the instance bundle context.
|
void |
setState(int state)
Sets the state of the component instance.
|
void |
start()
Starts the instance manager.
|
void |
stateChanged(ComponentInstance instance,
int newState)
State Change listener callback.
|
void |
stop()
Stops the instance manager.
|
protected java.lang.String m_name
protected java.lang.String m_className
protected final HandlerManager[] m_handlers
protected int m_state
ComponentInstance.STOPPED
at the beginning).
Possible value are
ComponentInstance.INVALID
ComponentInstance.VALID
ComponentInstance.DISPOSED
ComponentInstance.STOPPED
protected java.util.List m_listeners
InstanceStateListener
protected java.util.List m_pojoObjects
public InstanceManager(ComponentFactory factory, org.osgi.framework.BundleContext context, HandlerManager[] handlers)
factory
- the factory managing the instance managercontext
- the bundle context to give to the instancehandlers
- handler object arraypublic void setInstanceBundleContext(org.osgi.framework.BundleContext context)
setInstanceBundleContext
in interface InstanceBundleContextAware
context
- the bundle context of the bundle having declared the instancepublic Logger getLogger()
public void configure(org.apache.felix.ipojo.metadata.Element metadata, java.util.Dictionary configuration) throws ConfigurationException
metadata
- the component type metadataconfiguration
- the configuration of the instanceConfigurationException
- if the metadata are not correctpublic InstanceDescription getInstanceDescription()
getInstanceDescription
in interface ComponentInstance
ComponentInstance.getInstanceDescription()
public Handler[] getRegisteredHandlers()
public Handler getHandler(java.lang.String name)
name
- the class name of the handler to find or its qualified name (namespace:name)public java.lang.Object getFieldValue(java.lang.String fieldName)
fieldName
- the field name.null
is returned if the value is managed and not already set.public java.lang.Object getFieldValue(java.lang.String fieldName, java.lang.Object pojo)
null
, tries only on managed fields.fieldName
- the field name.pojo
- the pojo on which computing field value.null
is returned if the value is managed and not already set.public void start()
start
in interface ComponentInstance
public void stop()
ComponentInstance.STOPPED
,
disables attached handlers, and notifies listeners (InstanceStateListener
)
of the instance stopping process.stop
in interface ComponentInstance
public void dispose()
ComponentInstance.STOPPED
InstanceStateListener
of the destructiondispose
in interface ComponentInstance
ComponentInstance.dispose()
public void setState(int state)
Handler.stateChanged(int)
method on the attached 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 finished.state
- the new statepublic int getState()
ComponentInstance.INVALID
ComponentInstance.VALID
ComponentInstance.DISPOSED
ComponentInstance.STOPPED
getState
in interface ComponentInstance
ComponentInstance.getState()
public boolean isStarted()
ComponentInstance.VALID
or ComponentInstance.INVALID
.isStarted
in interface ComponentInstance
true
if the instance is started.ComponentInstance.isStarted()
public void addInstanceStateListener(InstanceStateListener listener)
addInstanceStateListener
in interface ComponentInstance
listener
- the listener to register.ComponentInstance.addInstanceStateListener(org.apache.felix.ipojo.InstanceStateListener)
public void removeInstanceStateListener(InstanceStateListener listener)
removeInstanceStateListener
in interface ComponentInstance
listener
- the listener to unregister.ComponentInstance.removeInstanceStateListener(org.apache.felix.ipojo.InstanceStateListener)
public ComponentFactory getFactory()
getFactory
in interface ComponentInstance
ComponentInstance.getFactory()
protected void load()
public java.lang.Object[] getPojoObjects()
protected java.lang.Object createObject()
m_factoryMethod
is specified,
this method called this static method to creates the object.
Otherwise, the methods uses the regular constructor.
All those methods can receive the BundleContext
in
argument.null
if an error
occurs during the creation.public java.lang.Object createPojoObject()
createObject()
method, and adds
the created object to the m_pojoObjects
list. Then,
it calls the PrimitiveHandler.onCreation(Object)
methods on attached
handlers.null
if an error occurs during the
creation.public void deletePojoObject(java.lang.Object pojo)
pojo
- the pojo to remove from the list of created pojos.public java.lang.Object getPojoObject()
createObject()
method, and adds
the created object to the m_pojoObjects
list. Then,
it calls the PrimitiveHandler.onCreation(Object)
methods on attached
handlers.
TODO this method has a potential race condition if two threads require a pojo
object at the same time. Only one object will be created, but the second thread
can receive the created object before the PrimitiveHandler.onCreation(Object)
calls.
public java.lang.Class getClazz()
public void register(PrimitiveHandler handler, FieldMetadata[] fields, MethodMetadata[] methods)
register(FieldMetadata, FieldInterceptor)
and register(MethodMetadata, MethodInterceptor)
instead.handler
- the handler to registerfields
- the field metadata listmethods
- the method metadata listpublic void register(FieldMetadata field, FieldInterceptor interceptor)
field
- the field to monitorinterceptor
- the field interceptor objectpublic void register(MethodMetadata method, MethodInterceptor interceptor)
method
- the field to monitorinterceptor
- the field interceptor objectpublic void register(MethodMetadata method, java.lang.String innerClass, MethodInterceptor interceptor)
method
- the field to monitorinnerClass
- the inner class nameinterceptor
- the field interceptor objectpublic void register(int index, ConstructorInjector injector) throws ConfigurationException
index
- the index of the parameter. Only one injector per index can
be registered.injector
- the injector object.ConfigurationException
- if the given index is already injected by another
injectorpublic java.lang.Object onGet(java.lang.Object pojo, java.lang.String fieldName)
PrimitiveHandler.onGet(Object, String, Object)
method. If the field value changes, this method call the PrimitiveHandler.onSet(Object, String, Object)
method on each field interceptor monitoring the field in order to advertize the new value.pojo
- the pojo object on which the field was getfieldName
- the field name on which the GETFIELD instruction is calledpublic void onEntry(java.lang.Object pojo, java.lang.String methodId, java.lang.Object[] args)
MethodInterceptor.onEntry(Object, java.lang.reflect.Member, Object[])
methods on method interceptors monitoring the method.pojo
- the pojo object on which method is invoked.methodId
- the method id used to compute the Method
object.args
- the argument arraypublic void onExit(java.lang.Object pojo, java.lang.String methodId, java.lang.Object result)
Exception
if the method thrown an
exception. If the given object is null
, either the method returns void
,
or the method has returned null
This method calls the MethodInterceptor.onExit(Object, java.lang.reflect.Member, Object)
and the
MethodInterceptor.onFinally(Object, java.lang.reflect.Member)
methods on method interceptors
monitoring the method.pojo
- the pojo object on which method was invoked.methodId
- the method id used to compute the Method
object.result
- the returned object.public void onError(java.lang.Object pojo, java.lang.String methodId, java.lang.Throwable error)
null
This method calls the MethodInterceptor.onError(Object, java.lang.reflect.Member, Throwable)
and the
MethodInterceptor.onFinally(Object, java.lang.reflect.Member)
methods on method interceptors monitoring
the method.pojo
- the pojo object on which the method was invokedmethodId
- the method id used to compute the Method
object.error
- the Throwable object.public void onSet(java.lang.Object pojo, java.lang.String fieldName, java.lang.Object objectValue)
PrimitiveHandler.onSet(Object, String, Object)
method on each field
interceptors monitoring this field.
This method can be invoked with a null
pojo argument when the changes comes from another
handler.pojo
- the pojo object on which the field was setfieldName
- the field name on which the PUTFIELD instruction is calledobjectValue
- the new value of the fieldpublic org.osgi.framework.BundleContext getContext()
getContext
in interface ComponentInstance
ComponentInstance.getContext()
public org.osgi.framework.BundleContext getGlobalContext()
public ServiceContext getLocalServiceContext()
null
null
if the
instance doesn't live in a composite.public java.lang.String getInstanceName()
getInstanceName
in interface ComponentInstance
ComponentInstance.getInstanceName()
public void reconfigure(java.util.Dictionary configuration)
Handler.reconfigure(Dictionary)
methods on each attached handler, and then recompute the instance
state. Note that the reconfiguration process does not deactivate the
instance.reconfigure
in interface ComponentInstance
configuration
- the new configuration to pushComponentInstance.reconfigure(java.util.Dictionary)
public java.lang.String getClassName()
public void stateChanged(ComponentInstance instance, int newState)
setState(int)
method.stateChanged
in interface InstanceStateListener
instance
- the handler becoming valid or invalidnewState
- the new state of the handlerInstanceStateListener.stateChanged(org.apache.felix.ipojo.ComponentInstance, int)
public java.util.Set getRegistredFields()
public java.util.Set getRegistredMethods()
public org.osgi.framework.BundleContext getInstanceContext()
getInstanceContext
in interface InstanceBundleContextAware
Copyright © 2006-2014 The Apache Software Foundation. All Rights Reserved.