【问题标题】:ClassCastException with (embedded OSGI)/External launcher带有(嵌入式 OSGI)/外部启动器的 ClassCastException
【发布时间】:2012-09-10 21:11:30
【问题描述】:

我正在为此使用 felix。对于我的测试,我刚刚创建了一个简单的 HelloWorld。

捆绑 1

public interface HelloWorldService {
    public void sayHello(String name);
}

--------- 实现 ----------

捆绑包 2

捆绑激活器

public class HelloWorldActivator implements BundleActivator {
private static BundleContext bc;

@Override
public void start(BundleContext bundleContext) throws Exception {
    bc = bundleContext;
    System.out.println(bc.getBundle().getHeaders().get(Constants.BUNDLE_NAME) + " starting...");
    HelloWorldService helloWorldService = new HelloWorldImpl();
    bc.registerService(HelloWorldService.class.getName(),helloWorldService,null);
    System.out.println("registered...");
}

@Override
public void stop(BundleContext bundleContext) throws Exception {
    System.out.println(bc.getBundle().getHeaders().get(Constants.BUNDLE_NAME) + " stopping...");
    bc = null;
}

}

实现---

public class HelloWorldImpl implements HelloWorldService {

@Override

public void sayHello(String name) {
    System.out.println("Hello "+name);
}
}

当我使用客户端时,它也是一个 OSGI 包,它工作正常。对于使用 OSGI 服务的外部客户端,我得到 ClassCastException。有什么想法吗?

public class ExternalClient {
private static final String BUNDLE_DIRECTORY = System.getProperty("auto.deploy.dir");
private static final ExternalClient externalClient = new ExternalClient();
private static Framework framework;
private ExternalClient() {
    start();
}
public static void main(String[] args) throws Exception{
    //For Some reason ServiceTracker was not returning the service, nor was getServiceReference. 
    ServiceReference[] serviceReferences = framework.getBundleContext().getAllServiceReferences(HelloWorldService.class.getName(),null);
    for(ServiceReference serviceReference : serviceReferences) {
        System.out.println(framework.getBundleContext().getService(serviceReference));
    }
    HelloWorldService helloWorldService = (HelloWorldService)framework.getBundleContext().getService(serviceReferences[0]); // Gives CLassCastException saying java.lang.ClassCastException: com.test.helloworld.impl.HelloWorldImpl cannot be cast to com.test.helloworld.HelloWorldService

    helloWorldService.sayHello("Blah");
    Thread.sleep(Long.MAX_VALUE);
}

public void start() {
    try {
        ServiceLoader<FrameworkFactory> factoryServiceLoader = ServiceLoader.load(FrameworkFactory.class);
        FrameworkFactory frameworkFactory = factoryServiceLoader.iterator().next();
        Map config = new HashMap();
        config.putAll(System.getProperties());
        config.put(Constants.FRAMEWORK_STORAGE_CLEAN, Constants.FRAMEWORK_STORAGE_CLEAN);
        config.put(Constants.FRAMEWORK_STORAGE_CLEAN, Constants.FRAMEWORK_STORAGE_CLEAN_ONFIRSTINIT);

        config.put(Constants.FRAMEWORK_SYSTEMPACKAGES_EXTRA, "com.test.helloworld");
        framework = frameworkFactory.newFramework(config);
        framework.start();

        List<Bundle> bundleList = new ArrayList<Bundle>();

        BundleContext bundleContext = framework.getBundleContext();
        File[] bundleJars = new File(BUNDLE_DIRECTORY).listFiles();
        Arrays.sort(bundleJars);
        for (File bundleJar : bundleJars) {
            if(bundleJar.getName().endsWith(".jar")) {
                Bundle b = bundleContext.installBundle(bundleJar.toURI().toString());
                bundleList.add(b);
            }
        }

        // Start all installed bundles.
        for (Bundle bundle : bundleList) {
            if(!isFragment(bundle))
                bundle.start();
        }

    } catch (BundleException bundleException) {
        throw new RuntimeException(bundleException);
    }
}

private static boolean isFragment(Bundle bundle) {
    return bundle.getHeaders().get(Constants.FRAGMENT_HOST) != null;
}
}

【问题讨论】:

    标签: osgi apache-felix


    【解决方案1】:

    没关系,我想我必须休息一下。我注意到,我在指定包时错过了版本

    *config.put(Constants.FRAMEWORK_SYSTEMPACKAGES_EXTRA, "com.test.helloworld;version=\"1.0.0\"");*

    以及访问代码:

    public static void main(String[] args) throws Exception{
        ServiceTracker helloWorldTracker;
        helloWorldTracker = new ServiceTracker(framework.getBundleContext(),HelloWorldService.class.getName(),null);
        helloWorldTracker.open();
        HelloWorldService helloWorldService = (HelloWorldService)helloWorldTracker.waitForService(5000);
        helloWorldService.sayHello((String) framework.getBundleContext().getBundle().getHeaders().get(Constants.BUNDLE_NAME));
        Thread.sleep(Long.MAX_VALUE);
    }
    

    【讨论】:

      猜你喜欢
      • 2016-04-12
      • 1970-01-01
      • 2021-07-29
      • 2013-12-19
      • 2018-06-03
      • 1970-01-01
      • 1970-01-01
      • 2020-09-22
      • 2015-03-31
      相关资源
      最近更新 更多