Java Reference
In-Depth Information
instance.reconfigure(props);
instances.put(newName, instance);
}
}
Configures previously
created instances
D
public void dispose(String name) {
ComponentInstance instance = instances.remove(name);
if (instance != null) {
instance.dispose();
System.out.println(name + " says: Eeek!");
}
}
}
In this example, you define a component with a dependency B on a component fac-
tory service for the previous trivial Hello component implementation. You specify the
desired factory using the filter attribute of @Requires ; in this case, you previously
named the component type hello . Like any normal service dependency, the Creator
component becomes valid only if a matching factory service is available.
In the create() method, you prepare a new Hello instance configuration by set-
ting the name property to the passed-in value and then use the factory to create the
instance C . In the rename() method D , you use the ComponentInstance object
returned from the factory service to configure previously created instances. When
you're finished with the instance you dispose of it in dispose() .
This approach is well-suited to pooling, allowing you to programmatically create
and release component instances. If you swapped your Hello implementation for a
database connection pool or a thread pool, for example, instances could be program-
matically created as other components in the framework noticed degradation in appli-
cation performance. Although this mechanism lets you dynamically create instances
at execution time, it ties components to the i POJO API . But this effect can be mini-
mized: i POJO provides another approach to eliminate this coupling.
CONFIGURATION ADMIN INSTANCE CREATION
The final option for creating component instances uses the ManagedServiceFactory
interface from the OSG i Configuration Admin specification. This approach is fairly
similar to the i POJO factory service, except that it uses the standard OSG i interface
rather than an i POJO -specific one. To illustrate, the next listing shows the previous
Creator component refactored to use the ConfigurationAdmin service instead.
Listing 12.8 Creating components using Configuration Admin
@Component(immediate=true)
public class Creator {
@Requires
private ConfigurationAdmin ca;
Configuration object B
Creates
public void create(String name) throws IOException {
Configuration config = ca.createFactoryConfiguration("hello");
Hashtable props = new Hashtable();
props.put("name", name);
 
Search WWH ::




Custom Search