Instead of using regular factory component, I solved my problem by using
configurationFactory and ConfigurationAdmin.
I used my first example to be able to create new instance of my class from
configuration tab in felix console
@Component(configurationFactory = true, policy =
ConfigurationPolicy.REQUIRE, metatype = true, immediate = true)
And then, to create my class instance from java code (actually I want to
allow user to create an instance from RESTful interface)
I used ConfigurationAdmin class as follows in the documentation
ServiceReference reference =
bundleContext.getServiceReference(ConfigurationAdmin.class.getName());
ConfigurationAdmin configAdmin = (ConfigurationAdmin)
bundleContext.getService(reference);
Configuration config =
configAdmin.createFactoryConfiguration(LDPathSemanticIndex.class.getName(),
null);
Dictionary props = new Hashtable();
props.put("key",value");
config.update(props);
Now, i can use both felix console and my restful interface to create my
class instance.
thanks for your answer.
Best regards,
Meric
2012/7/3 Felix Meschberger <[email protected]>
> Hi,
>
> Am 02.07.2012 um 10:14 schrieb Meriç Taze:
>
> > Hi all,
> >
> > I am trying to create a new instance of my class from felix console.
> Hence,
> > I'm using Component annotation like this:
> >
> > @Component(configurationFactory = true, policy =
> > ConfigurationPolicy.REQUIRE, metatype = true)
> >
> > and this works well. I can create new instance by using Configuration tab
> > of felix console. Now I also want to create
> > new instance of my class inside my java code. Thus, I tried to use
> >
> > @Component(factory = "org.apache.test.MyClassFactory")
> >
> > This is also work. However, I want both of these functionalities for my
> > class. Therefore, I add to top of my class definition
> >
> > @Component(configurationFactory = true, factory =
> > "org.apache.test.MyClassFactory", policy = ConfigurationPolicy.REQUIRE,
> > metatype = true)
> >
> > and it did not work as I expected. My question is how can achieve to
> create
> > my class instances using both felix
> > console and java code. I cannot succeed it by using
> > both configurationFactory and factory. Is there any other way?
>
> By the spec, this is not possible because factory configurations cannot be
> used to created instances of factory components. IIRC this is explicitly
> stated in the spec.
>
> The Felix implementation currently supports this non-compliant behaviour.
> But (a) you have to explicitly enable it and (b) you create a hidden
> implementation dependency. So I would discourage from using it.
>
> Rather I would solve the problem as follows:
>
> * Create a regular factory component (your second example)
> * Create another component having a service reference to the
> ComponentFactory service of the factory component. This
> second component has instances created for factory
> configuration. This component manages factory component
> instances for the factory configurations.
>
> HTH
>
> Regards
> Felix
>
> >
> > Thanks.
>
>