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