[
https://issues.apache.org/jira/browse/FELIX-2563?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12904148#action_12904148
]
Christopher Brind commented on FELIX-2563:
------------------------------------------
Thanks Felix.
What you say makes sense though it makes the example I provided impossible to
implement with DS. That is, if I register a ServiceFactory I really do want to
be concerned with the construction of the object since that is the purpose of a
factory. Case in point, I might be trying to instantiate an object that does
not have a default constructor (like in my example). Am I right in saying that
SCR would require that class to have a default constructor?
Also, how do I get a reference to the bundle that is using this instance of the
service? Presumably I'd have to add the following method (and update my
component xml)?
public void activate(ComponentContext ctx)
{
Bundle b = ctx.getUsingBundle();
// ...
}
Thanks,
Chris
> 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.