Differences
This shows you the differences between two versions of the page.
component_factory [2015/03/16 14:01] |
component_factory [2021/04/05 11:23] (current) |
||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====== Component Factory ====== | ||
+ | Most of the time it is quite sufficient and probably should be so to use Singletons. In an OSGi environment this is the standard type of beans/ | ||
+ | But sometimes you need several instances of a component. The default pattern for this is a [[http:// | ||
+ | |||
+ | OSGi supports this in several ways. | ||
+ | |||
+ | ===== Declarative Services ===== | ||
+ | This service can be used to let the system create instances of components for you. These instances can be retrieve programmatically or via method injection. | ||
+ | |||
+ | Declarative Service also lets you define a factory for a component in a very simple way. You don't have to write the factory yourself. The system will create it for you. | ||
+ | |||
+ | One of the very good parts of OSGi Declarative Services is that your component class can be totally free of OSGi classes. | ||
+ | |||
+ | <sxh java; title: Component Class> | ||
+ | public class RestServiceClient { | ||
+ | |||
+ | public Object get() { | ||
+ | // retrieve object from server | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | <sxh xml; title: Component Definition XML> | ||
+ | <?xml version=" | ||
+ | < | ||
+ | | ||
+ | < | ||
+ | </ | ||
+ | </ | ||
+ | |||
+ | Just by specifying a factory attribute in the component definition the system will create a factory for it. | ||
+ | |||
+ | ==== Retrieve Component Instance ==== | ||
+ | Most tutorials and articles describe how to write/ | ||
+ | |||
+ | The system will create the factory for you. It is registered as a component itself and implements '' | ||
+ | |||
+ | === Programmatically Retrieve Component Instance === | ||
+ | These are the necessary steps to manually get an instance of a component from a factory. | ||
+ | |||
+ | Getting the service reference for the component factory: | ||
+ | ServiceReference[] serviceReferences = context.getServiceReferences(ComponentFactory.class.getName(), | ||
+ | |||
+ | Getting the component factory: | ||
+ | ComponentFactory factory = (ComponentFactory) context.getService(serviceReferences[0]); | ||
+ | | ||
+ | Getting an instance from the factory: | ||
+ | ComponentInstance instance = factory.newInstance(null); | ||
+ | | ||
+ | In this example '' | ||
+ | |||
+ | <sxh java> | ||
+ | void activate(ComponentContext context) { | ||
+ | Dictionary properties = context.getProperties(); | ||
+ | ... | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | < | ||
+ | |||
+ | |||
+ | ==== Component Instances with Properties ==== | ||
+ | There are several ways to add properties to an OSGi component. | ||
+ | |||
+ | === Static Properties === | ||
+ | One way to add static properties to an OSGi component is to define them in the component definition xml file. | ||
+ | |||
+ | <sxh xml> | ||
+ | < | ||
+ | ... | ||
+ | < | ||
+ | </ | ||
+ | </ | ||
+ | |||
+ | |||
+ | ==== Component Instances with Service References ==== | ||
+ | Anything specified in the component definition xml file will be used on the component instance, not the factory, f. e. properties and service references. | ||
+ | |||
+ | So every service reference entered in the component definition will be used on each component instance. | ||
+ | |||
+ | <sxh xml> | ||
+ | <?xml version=" | ||
+ | < | ||
+ | | ||
+ | ... | ||
+ | < | ||
+ | ... | ||
+ | </ | ||
+ | </ | ||
+ | |||
+ | ==== Component Instances with Managed Configuration ==== | ||
+ | In an OSGi environment components can be provisioned with a configuration. This is also true for component instances created by a component factory. | ||
+ | |||
+ | <sxh java> | ||
+ | public class RestServiceClient implements ManagedService { | ||
+ | |||
+ | void activate(ComponentContext context) { | ||
+ | Dictionary properties = context.getProperties(); | ||
+ | System.out.println(" | ||
+ | } | ||
+ | |||
+ | @Override | ||
+ | public void updated(Dictionary properties) throws ConfigurationException { | ||
+ | if (properties == null) { | ||
+ | System.out.println(" | ||
+ | } | ||
+ | else { | ||
+ | System.out.println(" | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | <sxh xml> | ||
+ | <?xml version=" | ||
+ | < | ||
+ | ... | ||
+ | < | ||
+ | <provide interface=" | ||
+ | </ | ||
+ | ... | ||
+ | </ | ||
+ | </ | ||
+ | |||
+ | <note important> | ||
+ | |||
+ | |||
+ | ==== Disposing Component Instances ==== | ||
+ | The created component instances are managed by the OSGi framework. To unregister a component instance the // | ||
+ | |||
+ | <sxh java> | ||
+ | // get factory | ||
+ | ComponentFactory factory = (ComponentFactory) context.getService(serviceReferences[0]); | ||
+ | |||
+ | // get instance | ||
+ | ComponentInstance instance = factory.newInstance(null); | ||
+ | | ||
+ | ... | ||
+ | // do something | ||
+ | ... | ||
+ | |||
+ | // unregister component instance | ||
+ | instance.dispose(); | ||
+ | </ | ||
+ | |||
+ | |||
+ | ===== Links ===== | ||
+ | * [[http:// | ||
+ | * [[http:// | ||
+ | * [[http:// | ||
+ | |||
+ | {{tag> |