Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Equinox » Service Factory with Declared Service
Service Factory with Declared Service [message #1181219] Mon, 11 November 2013 13:05 Go to next message
Richard Meyer is currently offline Richard MeyerFriend
Messages: 40
Registered: June 2012
Member
Hy,

I use a Service Factory to control the creation of my Service Components.
Simply something like this:
public class LoggerServiceFactory implements ServiceFactory<ILogger>
{
	@Override
	public ILogger getService(Bundle bundle,
			ServiceRegistration<ILogger> registration) 
	{
		return new LoggerImpl();
	}

	@Override
	public void ungetService(Bundle bundle,
			ServiceRegistration<ILogger> registration, ILogger service) {
		
	}
}

now I can register this factory at the bundle startup:
Hashtable<String, Object> props = new Hashtable<>();
bundleContext.registerService(ILogger.class.getName(), new LoggerFactory(), props);


My question now is:
How can I exchange the programmaticly registration on bundle startup with declarative services?
I have tried to declare it in the XML file like a normal Component but it does not work:
 <implementation class="org.my.LoggerServiceFactory"/>
   <service>
      <provide interface="org.my.ILogger"/>
   </service>


Thanks in advance.
Richard

Re: Service Factory with Declared Service [message #1181298 is a reply to message #1181219] Mon, 11 November 2013 14:06 Go to previous messageGo to next message
Thomas Watson is currently offline Thomas WatsonFriend
Messages: 503
Registered: July 2009
Senior Member
Try using the following instead which has the attribute servicefactory

 <implementation class="org.my.LoggerServiceFactory"/>
   <service servicefactory="true">
      <provide interface="org.my.ILogger"/>
   </service>
Re: Service Factory with Declared Service [message #1181329 is a reply to message #1181298] Mon, 11 November 2013 14:35 Go to previous messageGo to next message
Richard Meyer is currently offline Richard MeyerFriend
Messages: 40
Registered: June 2012
Member
Hy Thomas,

many thanks for your quick reply.
But if I set the servicefactory="true" I received the following error:
!MESSAGE FrameworkEvent ERROR
!STACK 0
org.osgi.framework.ServiceException: org.eclipse.equinox.internal.ds.ServiceReg.getService() returned a service object that is not an instance of the service class org.my.ILogger
Re: Service Factory with Declared Service [message #1181380 is a reply to message #1181329] Mon, 11 November 2013 15:13 Go to previous messageGo to next message
Thomas Watson is currently offline Thomas WatsonFriend
Messages: 503
Registered: July 2009
Senior Member
Hmmm, now that is strange, because your code you posted before seems to indicate that it DID return ILogger instances. Is there any chance multiple versions of the package org.my are available in the system?
Re: Service Factory with Declared Service [message #1181554 is a reply to message #1181380] Mon, 11 November 2013 17:27 Go to previous messageGo to next message
BJ Hargrave is currently offline BJ HargraveFriend
Messages: 60
Registered: July 2009
Member
When you use servicefactory=true in DS, your implementation class must NOT implement ServiceFactory. DS takes care of creating the instances of your implementation class. So make your implementation class be LoggerImpl and not LoggerServiceFactory.

<implementation class="org.my.impl.LoggerImpl"/>
   <service servicefactory="true">
      <provide interface="org.my.ILogger"/>
   </service>


Also, make sure to place your service types (e.g. ILogger) in a different package than your impl types (e.g. LoggerImpl).

[Updated on: Mon, 11 November 2013 17:29]

Report message to a moderator

Re: Service Factory with Declared Service [message #1182849 is a reply to message #1181554] Tue, 12 November 2013 13:02 Go to previous messageGo to next message
Richard Meyer is currently offline Richard MeyerFriend
Messages: 40
Registered: June 2012
Member
Hy BJ,

thanks for your reply.
But my aim is to use my own service factory to create the instances and not to use the default implementation of DS!
(The point is that the real LoggerImpl is an EMF class and therefore I have to call the EMF Factory to create the class)

[Updated on: Tue, 12 November 2013 13:04]

Report message to a moderator

Re: Service Factory with Declared Service [message #1183129 is a reply to message #1182849] Tue, 12 November 2013 17:02 Go to previous message
BJ Hargrave is currently offline BJ HargraveFriend
Messages: 60
Registered: July 2009
Member
Then you cannot use DS for that. DS will not delegate to your ServiceFactory implementation. In this case you need to register your service using the OSGi API.
Previous Topic:Downgrade feature with multiple installed versions
Next Topic:Missing Configuration Admin (org.eclipse.equinox.cm) Bundle/Plugin in Kepler
Goto Forum:
  


Current Time: Fri Apr 19 00:50:07 GMT 2024

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

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

Back to the top