[ 
https://issues.apache.org/jira/browse/FELIX-2563?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12904145#action_12904145
 ] 

Felix Meschberger commented on FELIX-2563:
------------------------------------------

I think this is related to a misunderstanding.

If you declare a service to be a service factory, this is an indication to the 
Declarative Services how to cope with the component. As for the class of your 
component: You just implement the actual service class and don't care for 
registering and implementing the ServiceFactory interface.

For example:

   <?xml version="1.0" encoding="UTF-8"?>
   <scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0"; 
name="dsbug.LongFactory">
     <implementation class="Test"/>
     <service servicefactory="true">
       <provide interface="Test />
     </service>
   </scr:component>

Your implementation would be :

   public class Test  {
      ... do something ....
   }

DS will create a ServiceFactory object, register it under the name "Test" and 
implement required creation and drop implementations to create your Test 
instances. Your component does not have to care for this and does not have to 
maintain these instances.

> service factory causes cast error
> ---------------------------------
>
>                 Key: FELIX-2563
>                 URL: https://issues.apache.org/jira/browse/FELIX-2563
>             Project: Felix
>          Issue Type: Bug
>          Components: Declarative Services (SCR)
>    Affects Versions:  scr-1.6.0
>            Reporter: Christopher Brind
>            Priority: Minor
>         Attachments: dsbug.jar
>
>
> Given this component XML:
> <?xml version="1.0" encoding="UTF-8"?> 
> <scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0"; 
> name="dsbug.LongFactory">
>       <implementation class="dsbug.LongFactory"/> 
>       
>       <service servicefactory="true">
>               <provide interface="java.lang.Long" />
>       </service>
> </scr:component>
> And given this component class:
> package dsbug;
> import org.osgi.framework.Bundle;
> import org.osgi.framework.ServiceFactory;
> import org.osgi.framework.ServiceRegistration;
> public class LongFactory implements ServiceFactory {
>       public Object getService(Bundle bundle, ServiceRegistration 
> registration) {
>               return new Long(bundle.getBundleId());
>       }
>       public void ungetService(Bundle bundle, ServiceRegistration 
> registration,
>                       Object service) {
>       }
>       
> }
> Causes this error when referencing the service factory (resulting in 
> unsatisfied dependencies):
> org.apache.felix.log.LogException: org.osgi.framework.ServiceException: 
> Service cannot be cast: java.lang.Long
> However, when using this Activator *instead* of DS, all is well:
> package dsbug;
> import org.osgi.framework.BundleActivator;
> import org.osgi.framework.BundleContext;
> public class Activator implements BundleActivator {
>       public void start(BundleContext context) throws Exception {
>               context.registerService(Long.class.getName(), new 
> LongFactory(), null);
>       }
>       public void stop(BundleContext context) throws Exception {
>       }
> }
> Workaround (with DS) is to create a simple component with an 
> activate(BundleContext ctx) method and manually register the service. 

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.

Reply via email to