OSGi ServiceFactory返回一个无法解析的Object

时间:2015-07-04 07:31:26

标签: java osgi apache-felix service-factory embedded-osgi

我一直试图让这一整天工作,这是令人沮丧的原因我没有看到我的代码有任何问题。

以下是我的课程:

DisplayerServiceFactory.java

package com.lincesoft.imperator.displayer.provider;

import org.osgi.framework.Bundle;
import org.osgi.framework.ServiceFactory;
import org.osgi.framework.ServiceRegistration;

import com.lincesoft.imperator.displayer.api.Displayer;

public class DisplayerServiceFactory implements ServiceFactory<Displayer> {

    public static final String NAME = "displayer_service_factory";

    @Override
    public void ungetService(Bundle bundle, ServiceRegistration<Displayer> registration, Displayer service) {
        // TODO Auto-generated method stub
    }
    @Override
    public Displayer getService(Bundle bundle, ServiceRegistration<Displayer> registration) {
        return new DisplayerImplementation();
    }
}

这是包含DisplayerServiceFactory

的包的Activator.java
package com.lincesoft.imperator.displayer.launcher;

import java.util.Hashtable;

import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceFactory;
import org.osgi.framework.ServiceRegistration;

import com.lincesoft.imperator.displayer.api.Displayer;
import com.lincesoft.imperator.displayer.provider.DisplayerServiceFactory;

public class Activator implements BundleActivator {

    private ServiceRegistration<?> DisplayerFactoryRegistration;

    @Override
    public void start(BundleContext bundle_context) throws Exception {
        System.out.println("Starting the Displayer Provider Bundle");
        ServiceFactory<Displayer> displayer_service_factory = new DisplayerServiceFactory();
        Hashtable<String,String> properties = new Hashtable<String,String>();
        properties.put("service.vendor", DisplayerServiceFactory.NAME);
        DisplayerFactoryRegistration = bundle_context.registerService(Displayer.class.getName(), displayer_service_factory,properties);
    }

    @Override
    public void stop(BundleContext context) throws Exception {
        System.out.println("Stopping the Displayer Provider Bundle");
        DisplayerFactoryRegistration.unregister();
    }
}

这里是运行previus bundle的嵌入式osgi容器的代码

/* Get a displayer instance from the service factory provided by the displayer provider bundle */
        ServiceReference<?>[] service_references = null;
        try {
            service_references = my_bundle_context.getServiceReferences(Displayer.class.getName(),"(service.vendor=displayer_service_factory)");
        } catch (InvalidSyntaxException e) {
            System.out.println("Sintaxis para obtener displayer_service_factory_reference invalida.");
            e.printStackTrace();
        }
        ServiceReference<?> displayer_service_factory_reference = null;
        if (service_references!=null && service_references.length==1) {
             displayer_service_factory_reference = service_references[0];
        } else {
            //Throw new NoDisplayerFactoryException();
        }
        Displayer bundle_displayer_instance = (Displayer) my_bundle_context.getService(displayer_service_factory_reference);

当我运行此代码时,它给了我这个例外

Exception in thread "main" java.lang.ClassCastException: com.lincesoft.imperator.displayer.provider.DisplayerImplementation cannot be cast to com.lincesoft.imperator.displayer.api.Displayer
at com.lincesoft.imperator.procurator.launcher.Procurator.main(Procurator.java:94)

为什么会这样?很明显,DisplayerImplementation是一个显示器的实例。

我也做了一些调试,在ServiceFactory类中(DisplayerImplementation instanceof Displayer)返回true,但是,当我从注册为服务的ServiceFactory获取显示器实现时,(DisplayerImplementation instanceof Displayer)返回false。

这可能是我使用的框架实现中的一个错误吗?我正在使用felix btw。

如果你到了这里,谢谢你的阅读!如果你试图帮助我,我真的会感到沮丧。祝你今天愉快!或者晚上!

1 个答案:

答案 0 :(得分:1)

您有一个特殊情况,因为您想从OSGi容器外部访问OSGi服务。这只能通过容器外部的API包来完成,因为您无法从包中访问包。

您已在容器外的类路径中包含com.lincesoft.imperator.displayer.api包。所以你只需要将这个包添加到框架属性org.osgi.framework.system.packages.extra中。这样容器在osgi中发布api包。

就像Balazs写的那样,你也应该确保你所部署的一个软件包中没有api软件包。这可以确保OSGi不会意外选择错误的api包。