Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Equinox » ConfigAdmin and DS Component Factory
ConfigAdmin and DS Component Factory [message #520010] Wed, 10 March 2010 13:52 Go to next message
Philipp Kursawe is currently offline Philipp KursaweFriend
Messages: 135
Registered: July 2009
Senior Member
Hello,

someone out there that can explain me how the ConfigAdmin and DS
Component Factories work together?

I was under the impression that each factory configuration created will
invoke the newInstance method of the DS component factory?

Or do I have to create the instances myself programmatically?
What properties have to be set in the factory configuration to provoke a
creation of a new instance with that factory? Do I need to set the
service.factoryPid to the id of the factory?

There seem to be no tutorials out there when or how to use the DS
component factory. Only for the deprecated ManagedServiceFactory.

Thanks,
Phil
Re: ConfigAdmin and DS Component Factory [message #522038 is a reply to message #520010] Fri, 19 March 2010 13:30 Go to previous messageGo to next message
Jens Muehlenhoff is currently offline Jens MuehlenhoffFriend
Messages: 20
Registered: July 2009
Junior Member
Hello Philipp,

I'm working on the same issue. As far as I know should it work like this:

<?xml version="1.0" encoding="UTF-8"?>
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" enabled="true" immediate="false"
name="exampleService" factory="exampleServiceFactory" modified="modified">
<implementation class="de.kue.example.scr.ExampleServiceFactory" />
<service>
<provide interface="de.kue.example.scr.IExampleService" />
</service>
</scr:component>

The ComponantFactory will be created by the SCR-Manager. You do not have to implement it by yourself
(IMO).

Now you can create new configurations with

Configuration config = configurationAdmin.createFactoryConfiguration("exampleService ");
Properties props = new Properties();
props.put("name", name);
config.update(props);

Thats what I have taken from the OSGi spec -- but unfortunately I can't get it run :(

Currenlty I can't get the point where I'm wrong. Comments are welcome.

Philipp Kursawe wrote:
> Hello,
>
> someone out there that can explain me how the ConfigAdmin and DS
> Component Factories work together?
>
> I was under the impression that each factory configuration created will
> invoke the newInstance method of the DS component factory?
>
> Or do I have to create the instances myself programmatically?
> What properties have to be set in the factory configuration to provoke a
> creation of a new instance with that factory? Do I need to set the
> service.factoryPid to the id of the factory?
>
> There seem to be no tutorials out there when or how to use the DS
> component factory. Only for the deprecated ManagedServiceFactory.
>
> Thanks,
> Phil
> From - Fri
Re: ConfigAdmin and DS Component Factory [message #522082 is a reply to message #522038] Fri, 19 March 2010 20:25 Go to previous messageGo to next message
Oliver Vesper is currently offline Oliver VesperFriend
Messages: 42
Registered: July 2009
Member
Hi Jens,

I was also quite confused at the beginning as I did not realize that the
actual "factory" needs to implement the interface for which you would
like the factory to create instances of. In your example I guess you'd
like the ExampleServiceFactory to create components implementing
IExampleService. In fact your ESFactory has to implement IEService.

So when you're querying (or injecting via DS) a component-factory which
is able to create instances implementing IEService, you will receive an
o.o.s.c.ComponentFactory which can create instances of type ESFactory.
In turn your ESFactory should have an activate method accepting a
Map<?,?> parameter holding the information to configure your instance.

For example I have three classes:

public interface IFoo {
public String getName();
}

public class TheFoo implements IFoo {
private String name;
protected void activate(final Map<?, ?> properties) {
this.name = (String) properties.get("name");
}
@Override
public String getName() {
return this.name;
}
}

public class SomeService {
private ComponentFactory factory;
protected void setFactory(final ComponentFactory factory) {
this.factory = factory;
}
protected void activate(final ComponentContext ctx) {
final Properties props = new Properties();
props.setProperty("name", "customized name");
final ComponentInstance instance = this.factory.newInstance(props);
final IFoo foo = (IFoo) instance.getInstance();
System.out.println(foo.getName());
}
}

Furthermore there are two component definitions:
- TheFoo.xml:
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0"
factory="sandbox.IFoo" immediate="false" name="sandbox.FooFactory">
<implementation class="sandbox.TheFoo"/>
<property name="name" type="String" value="default name"/>>
</scr:component>
- SomeService.xml
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0"
immediate="true" name="sandbox.SomeService">
<implementation class="sandbox.SomeService"/>
<reference bind="setFactory" cardinality="1..1"
interface="org.osgi.service.component.ComponentFactory" policy="static"
target="(component.factory=sandbox.IFoo)"/>
</scr:component>

As I mentioned at the beginning, IMHO the actual factory seen here is
not a classic factory in terms of the factory pattern but more like a
configurable instance of a given type.

Hope this helps,
Oliver

Jens schrieb:
> Hello Philipp,
>
> I'm working on the same issue. As far as I know should it work like this:
>
> <?xml version="1.0" encoding="UTF-8"?>
> <scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" enabled="true" immediate="false"
> name="exampleService" factory="exampleServiceFactory" modified="modified">
> <implementation class="de.kue.example.scr.ExampleServiceFactory" />
> <service>
> <provide interface="de.kue.example.scr.IExampleService" />
> </service>
> </scr:component>
>
> The ComponantFactory will be created by the SCR-Manager. You do not have to implement it by yourself
> (IMO).
>
> Now you can create new configurations with
>
> Configuration config = configurationAdmin.createFactoryConfiguration("exampleService ");
> Properties props = new Properties();
> props.put("name", name);
> config.update(props);
>
> Thats what I have taken from the OSGi spec -- but unfortunately I can't get it run :(
>
> Currenlty I can't get the point where I'm wrong. Comments are welcome.
>
> Philipp Kursawe wrote:
>> Hello,
>>
>> someone out there that can explain me how the ConfigAdmin and DS
>> Component Factories work together?
>>
>> I was under the impression that each factory configuration created will
>> invoke the newInstance method of the DS component factory?
>>
>> Or do I have to create the instances myself programmatically?
>> What properties have to be set in the factory configuration to provoke a
>> creation of a new instance with that factory? Do I need to set the
>> service.factoryPid to the id of the factory?
>>
>> There seem to be no tutorials out there when or how to use the DS
>> component factory. Only for the deprecated ManagedServiceFactory.
>>
>> Thanks,
>> Phil
>> From - Fri
Re: ConfigAdmin and DS Component Factory [message #522090 is a reply to message #522082] Fri, 19 March 2010 21:38 Go to previous messageGo to next message
Jens Muehlenhoff is currently offline Jens MuehlenhoffFriend
Messages: 20
Registered: July 2009
Junior Member
Hello Oliver,

thanks for the quick answer. Meanwhile I found the same thing our write.
That works pretty fine, but without the Configuration Admin Service.

I'm looking for a solution, where entries in the configuration create the services, like the
ManagedServiceFactory.

I thought I could create an entry with the ConfigurationAdmin-Service. The serivce stores the data
in the config database and the SCR-Service receives the config-create-event and creates the new
service with the ComponentFactory.

The code:

Configuration config = configurationAdmin.createFactoryConfiguration("exampleService ");
ci.println("config.getFactoryPid()=" + config.getFactoryPid());
ci.println("config.getPid()=" + config.getPid());

Properties props = new Properties();
props.put("name", "hello world");
config.update(props);

leads to the following:

config.getFactoryPid()=exampleService
config.getPid()=exampleService-1269034185441-0

!SESSION 2010-03-19 22:29:41.234 -----------------------------------------------
eclipse.buildId=unknown
java.version=1.6.0_18
java.vendor=Sun Microsystems Inc.
BootLoader constants: OS=linux, ARCH=x86, WS=gtk, NL=en_DK
Command-line arguments: -dev
file:/media/data/home/Prog/osgi-projekt/workspace/de.kue.exa mple.scr/runtime/dev.properties
-os linux -ws gtk -arch x86 -consoleLog -console

!ENTRY de.kue.example.scr 4 0 2010-03-19 22:29:45.483
!MESSAGE [SCR - SCRManager] ComponentFactory exampleService cannot be managed using factory
configuration!

Very strange. I cant find some thing by google, at eclipse.org, wiki.eclipse.org nor in this
newsgroup. Looks like nobody had tried it before or I'm really confused.

Jens

Oliver Vesper wrote:
> Hi Jens,
>
> I was also quite confused at the beginning as I did not realize that the
> actual "factory" needs to implement the interface for which you would
> like the factory to create instances of. In your example I guess you'd
> like the ExampleServiceFactory to create components implementing
> IExampleService. In fact your ESFactory has to implement IEService.
>
> So when you're querying (or injecting via DS) a component-factory which
> is able to create instances implementing IEService, you will receive an
> o.o.s.c.ComponentFactory which can create instances of type ESFactory.
> In turn your ESFactory should have an activate method accepting a
> Map<?,?> parameter holding the information to configure your instance.
>
> For example I have three classes:
>
> public interface IFoo {
> public String getName();
> }
>
> public class TheFoo implements IFoo {
> private String name;
> protected void activate(final Map<?, ?> properties) {
> this.name = (String) properties.get("name");
> }
> @Override
> public String getName() {
> return this.name;
> }
> }
>
> public class SomeService {
> private ComponentFactory factory;
> protected void setFactory(final ComponentFactory factory) {
> this.factory = factory;
> }
> protected void activate(final ComponentContext ctx) {
> final Properties props = new Properties();
> props.setProperty("name", "customized name");
> final ComponentInstance instance = this.factory.newInstance(props);
> final IFoo foo = (IFoo) instance.getInstance();
> System.out.println(foo.getName());
> }
> }
>
> Furthermore there are two component definitions:
> - TheFoo.xml:
> <scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0"
> factory="sandbox.IFoo" immediate="false" name="sandbox.FooFactory">
> <implementation class="sandbox.TheFoo"/>
> <property name="name" type="String" value="default name"/>>
> </scr:component>
> - SomeService.xml
> <scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0"
> immediate="true" name="sandbox.SomeService">
> <implementation class="sandbox.SomeService"/>
> <reference bind="setFactory" cardinality="1..1"
> interface="org.osgi.service.component.ComponentFactory" policy="static"
> target="(component.factory=sandbox.IFoo)"/>
> </scr:component>
>
> As I mentioned at the beginning, IMHO the actual factory seen here is
> not a classic factory in terms of the factory pattern but more like a
> configurable instance of a given type.
>
> Hope this helps,
> Oliver
>
> Jens schrieb:
>> Hello Philipp,
>>
>> I'm working on the same issue. As far as I know should it work like this:
>>
>> <?xml version="1.0" encoding="UTF-8"?>
>> <scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0"
>> enabled="true" immediate="false"
>> name="exampleService" factory="exampleServiceFactory"
>> modified="modified">
>> <implementation class="de.kue.example.scr.ExampleServiceFactory" />
>> <service>
>> <provide interface="de.kue.example.scr.IExampleService" />
>> </service>
>> </scr:component>
>>
>> The ComponantFactory will be created by the SCR-Manager. You do not
>> have to implement it by yourself
>> (IMO).
>>
>> Now you can create new configurations with
>>
>> Configuration config =
>> configurationAdmin.createFactoryConfiguration("exampleService ");
>> Properties props = new Properties();
>> props.put("name", name);
>> config.update(props);
>>
>> Thats what I have taken from the OSGi spec -- but unfortunately I
>> can't get it run :(
>>
>> Currenlty I can't get the point where I'm wrong. Comments are welcome.
>>
>> Philipp Kursawe wrote:
>>> Hello,
>>>
>>> someone out there that can explain me how the ConfigAdmin and DS
>>> Component Factories work together?
>>>
>>> I was under the impression that each factory configuration created will
>>> invoke the newInstance method of the DS component factory?
>>>
>>> Or do I have to create the instances myself programmatically?
>>> What properties have to be set in the factory configuration to provoke a
>>> creation of a new instance with that factory? Do I need to set the
>>> service.factoryPid to the id of the factory?
>>>
>>> There seem to be no tutorials out there when or how to use the DS
>>> component factory. Only for the deprecated ManagedServiceFactory.
>>>
>>> Thanks,
>>> Phil
>>> From - Fri
Re: ConfigAdmin and DS Component Factory [message #522097 is a reply to message #522090] Fri, 19 March 2010 23:03 Go to previous message
Jens Muehlenhoff is currently offline Jens MuehlenhoffFriend
Messages: 20
Registered: July 2009
Junior Member
Okay,

after reading the spec more than twice (their should be more examples I guess). I come around with this:

-----<snipp>-----<snipp>-----<snipp>-----<snipp>-----
<?xml version="1.0" encoding="UTF-8"?>
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0"
configuration-policy="require"
enabled="true" immediate="false" name="exampleService">
<implementation class="de.kue.example.scr.ExampleService" />
<service>
<provide interface="de.kue.example.scr.IExampleService" />
</service>
</scr:component>
-----<snipp>-----<snipp>-----<snipp>-----<snipp>-----

-----<snipp>-----<snipp>-----<snipp>-----<snipp>-----
Configuration config = configurationAdmin.createFactoryConfiguration("exampleService ");
Properties props = new Properties();
props.put("name", name);
config.update(props);
-----<snipp>-----<snipp>-----<snipp>-----<snipp>-----

Works fine, if
1) you have always one initial configuration available and
2) if you do not restart your equinox.

1) I need the situation in which their is no configuration avaliable

2) I get a
java.lang.NullPointerException
at org.eclipse.equinox.internal.ds.SCRManager.configAdminRegist ered(SCRManager.java:824)
at org.eclipse.equinox.internal.ds.Activator.serviceChanged(Act ivator.java:422)
at
org.eclipse.osgi.internal.serviceregistry.FilteredServiceLis tener.serviceChanged(FilteredServiceListener.java:104)


Look like that theirs was no testing around with this feature yet. ;-) *smile*


Jens wrote:
> Hello Oliver,
>
> thanks for the quick answer. Meanwhile I found the same thing our write.
> That works pretty fine, but without the Configuration Admin Service.
>
> I'm looking for a solution, where entries in the configuration create the services, like the
> ManagedServiceFactory.
>
> I thought I could create an entry with the ConfigurationAdmin-Service. The serivce stores the data
> in the config database and the SCR-Service receives the config-create-event and creates the new
> service with the ComponentFactory.
>
> The code:
>
> Configuration config = configurationAdmin.createFactoryConfiguration("exampleService ");
> ci.println("config.getFactoryPid()=" + config.getFactoryPid());
> ci.println("config.getPid()=" + config.getPid());
>
> Properties props = new Properties();
> props.put("name", "hello world");
> config.update(props);
>
> leads to the following:
>
> config.getFactoryPid()=exampleService
> config.getPid()=exampleService-1269034185441-0
>
> !SESSION 2010-03-19 22:29:41.234 -----------------------------------------------
> eclipse.buildId=unknown
> java.version=1.6.0_18
> java.vendor=Sun Microsystems Inc.
> BootLoader constants: OS=linux, ARCH=x86, WS=gtk, NL=en_DK
> Command-line arguments: -dev
> file:/media/data/home/Prog/osgi-projekt/workspace/de.kue.exa mple.scr/runtime/dev.properties
> -os linux -ws gtk -arch x86 -consoleLog -console
>
> !ENTRY de.kue.example.scr 4 0 2010-03-19 22:29:45.483
> !MESSAGE [SCR - SCRManager] ComponentFactory exampleService cannot be managed using factory
> configuration!
>
> Very strange. I cant find some thing by google, at eclipse.org, wiki.eclipse.org nor in this
> newsgroup. Looks like nobody had tried it before or I'm really confused.
>
> Jens
Previous Topic:Equinox Classes Apr. 12-May 28
Next Topic:Generic p2 mirror
Goto Forum:
  


Current Time: Fri Apr 26 23:58:55 GMT 2024

Powered by FUDForum. Page generated in 0.03970 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top