带有(嵌入式OSGI)/外部启动程序的ClassCastException

时间:2012-09-10 21:11:31

标签: osgi apache-felix

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

捆绑1

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

--------- impl ----------

捆绑2

Bundle Activator

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;
}
}

1 个答案:

答案 0 :(得分: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);
}