Hi, 

The code you gave works and does not introduce drawbacks. Currently, iPOJO does 
not provide an easier API to retrieve the contained objects. This is because 
iPOJO component type may not contain ‘objects’ such as composites (they 
contains other iPOJO instances). 

Depending ou your use case, you can change the perspective. Instead of creating 
an instance and retrieving the object, create an object and inject it in a 
‘container’. Have a look to:
http://felix.apache.org/documentation/subprojects/apache-felix-ipojo/apache-felix-ipojo-userguide/apache-felix-ipojo-api.html#singleton-component-type

You can also pass the object you want as content using the “instance.object” 
configuration property.

Regards,

Clement

On 8 octobre 2014 at 12:04:34, Milen Dyankov ([email protected]) wrote:

Hi everyone,  

I faced a use case where I don't really know what is the best way to solve.  
So here is oversimplified version of the problem:  
- I'm using a 3rd party framework (available as OSGI bundle) that does  
something with objects (what exactly is irrelevant)  
- The framework is configured with some class names and it will create new  
object instances as needed. However it allows you to hook into the object  
creation phase and create the object yourself as long as you return new  
object instance every time you are called.  
- I need to inject a OSGI services (iPOJO components) into the objects I  
create for the 3rd party framework.  

So I thought ideally those objects I create should be iPOJO components  
themselves so that IPOJO can auto wire the services they require. Thus I  
was looking for a way to manually create a new instance of IPOJO components  
on demand. What I managed to do is something like the code below in the  
hook for the 3rd party framework (error handling removed for simplicity):  

Factory factory = .... // get the relevant factory using ServiceTracker  
if (factory != null) {  
ComponentInstance componentInstance;  
try {  
componentInstance = factory.createComponentInstance(null);  
if (componentInstance instanceof InstanceManager) {  
InstanceManager instanceManager = (InstanceManager) componentInstance;  
Object pojoObject = instanceManager.getPojoObject();  
if (pojoObject instanceof MyOwnType) {  
return (MyOwnType) pojoObject;  
}  
}  
} catch (UnacceptableConfiguration | MissingHandlerException |  
ConfigurationException e) {  
}  
}  


This works but it feels somewhat too hacky. Is there a better way to do it?  
Are there any drawbacks (performance, cleanup, ...)?  

Regards,  
Milen  


--  
http://about.me/milen  

Reply via email to