|
Page Edited :
FELIX :
How-to use iPOJO factories
How-to use iPOJO factories has been edited by Clement Escoffier (Jan 27, 2007). Content:iPOJO Factories PrinciplesClement Escoffier - [EMAIL PROTECTED] IPOJO defines a factory for each declared component type. These factories are used to create component instances. This document presents component type and component instance concepts, and how-to use factories is a metadata file and with the API. Preliminary ConceptsComponent TypeA component type is a kind of instance template. If we compare component concepts with object oriented programming, component types are classes and component instances are objects. A component type is declared inside a metadata file (metadata.xml). The next snippet shows you a component type declaration: <component className="..." factory="MyFactory"> ... <!—handler configuration - -> ... </component>
The factory attribute contains the factory name. This name is used to refer to the factory (and consequently to the component type). The factory attribute configuration can varies
A private factory is no accessible outside the metadata file. To refer to the type, you need to use the class name. If the factory is public (not private), the factory is accessible outside the metadata file. Indeed, iPOJO will publish two services to access to the factory:
The factory name will be used a service.pid property for these services. The service.pid is unique and persists between frmaework restarts. Component InstanceA component instance is an instance of a component type. For example, if a component type declares providing and requiring services, the component instances, will expose and require services. Each factory can create several instances, but all these instances will be different entities, and will be independent. A component instance is characterized by:
A factory keeps a reference on each instance it creates. If the factory stops or goes away, all created instances stops and will be destroyed.
How-to declare instances inside metadata filesA metadata file can declare instances from private (and contained) component type and from public (contained or not) factories. If a component type is private (factory attribute of the component type is "no"), the metadata file is the only way to create instances for this type. It is generally used when the component instance must be the unique instance of the type. Else, component instance can be declared either in the metadata file declaring the type, either in an external metadata file. If a declared instance targets an outside factory, the instance will be created only when the factory is available. Then, if the factory goes away, the instance stops, but if the factory comes back, the instance will be recreated. The next snippet shows how to declare an instance in the metadata: <instance component="component factory name" name = "instance name" > <property name="a property name" value="a string form of the value"/> <property name="another property name" value="the string form of the wanted value"/> </instance> The component type attribute contains the targeted factory name. The name attribute declares the instance name. The instance configuration is set by declaring a set of properties. A property is composed by a name and a value. The value is the string form of the wanted value. The iPOJO runtime will create the property object with this string. How-to use iPOJO factory serviceA public factory is exposed as an org.apache.felix.ipojo.Factory service. This service is accessible as any other OSGiâ„¢ service, and could be an iPOJO dependency too. This service used the following interfaces: Factory.java public interface Factory { /** * Create an instance manager (i.e. component type instance). * @param configuration : the configuration properties for this component. * @return the created instance manager. * @throw UnacceptableConfiguration if the given configuration is not consistent for the factory */ ComponentInstance createComponentInstance(Dictionary configuration) throw UnacceptableConfiguration; /** * Create an instance manager (i.e. component type instance). * This has these service interaction in the scope given in argument. * @param configuration : the configuration properties for this component. * @param serviceContext : the service context of the component. * @return the created instance manager. * @throw UnacceptableConfiguration if the given configuration is not consistent for the factory */ ComponentInstance createComponentInstance(Dictionary configuration, ServiceContext serviceContext) throw UnacceptableConfiguration; /** * Get the component type information containing provided service, configuration properties ... * @return the component type information. */ ComponentDescription getComponentDescription(); /** * Check if the given configuration is acceptable as a configuration of a component instance. * @param conf : the configuration to test * @return true if the configuration is acceptable */ boolean isAcceptable(Dictionary conf); /** * @return the name of the factory. */ String getName(); } The factory service contains two methods to create instances:
An instance is started automatically when created. However, the instance can be invalid, if handlers are not valid). To destroy an instance, invoke the stop method on the instance. How to use the ManagedServiceFactory to create instancesThe principle of the ManagedServiceFactory is the same as the iPOJO Factory Service. To have further information on it, read the OSGi R4 Compendium - Configuration Admin chapter. ConclusionThis document has presented how to use iPOJO factories. If you have remarks or comments, do not hesitate do send me en email to [EMAIL PROTECTED] |
Unsubscribe or edit your notifications preferences
