You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@felix.apache.org by Meriç Taze <me...@gmail.com> on 2012/07/02 10:14:36 UTC

Create a class instances using both felix console and java code

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?

Thanks.

Re: Create a class instances using both felix console and java code

Posted by Meriç Taze <me...@gmail.com>.
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 <fm...@adobe.com>

> 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.
>
>

Re: Create a class instances using both felix console and java code

Posted by Felix Meschberger <fm...@adobe.com>.
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.