带有(嵌入式OSGI)/外部启动器的ClassCastException
我用felix来做这个。为了进行测试,我刚刚创建了一个简单的HelloWorld 捆绑包1带有(嵌入式OSGI)/外部启动器的ClassCastException,osgi,apache-felix,Osgi,Apache Felix,我用felix来做这个。为了进行测试,我刚刚创建了一个简单的HelloWorld 捆绑包1 public interface HelloWorldService { public void sayHello(String name); } ------impl------ 捆绑包2 束激活剂 public class HelloWorldActivator implements BundleActivator { private static BundleContext bc; @O
public interface HelloWorldService {
public void sayHello(String name);
}
------impl------
捆绑包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;
}
}
实施---
当我使用一个客户端时,它也是一个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;
}
}
公共类外部客户端{
私有静态最终字符串BUNDLE_DIRECTORY=System.getProperty(“auto.deploy.dir”);
私有静态final ExternalClient ExternalClient=新的ExternalClient();
私有静态框架;
私有外部客户端(){
start();
}
公共静态void main(字符串[]args)引发异常{
//出于某种原因,ServiceTracker没有返回服务,getServiceReference也没有返回。
ServiceReference[]serviceReferences=framework.getBundleContext().getAllServiceReferences(HelloWorldService.class.getName(),null);
对于(ServiceReference ServiceReference:serviceReferences){
System.out.println(framework.getBundleContext().getService(serviceReference));
}
HelloWorldService HelloWorldService=(HelloWorldService)framework.getBundleContext().getService(serviceReferences[0]);//给出CLassCastException,表示java.lang.CLassCastException:com.test.helloworld.impl.HelloWorldImpl不能强制转换为com.test.helloworld.HelloWorldService
helloWorldService.sayHello(“废话”);
线程睡眠(长.MAX_值);
}
公开作废开始(){
试一试{
ServiceLoader factoryServiceLoader=ServiceLoader.load(FrameworkFactory.class);
FrameworkFactory FrameworkFactory=factoryServiceLoader.iterator().next();
Map config=newhashmap();
config.putAll(System.getProperties());
config.put(Constants.FRAMEWORK\u STORAGE\u CLEAN,Constants.FRAMEWORK\u STORAGE\u CLEAN);
config.put(Constants.FRAMEWORK\u STORAGE\u CLEAN,Constants.FRAMEWORK\u STORAGE\u CLEAN\u ONFIRSTINIT);
config.put(Constants.FRAMEWORK\u SYSTEMPACKAGES\u EXTRA,“com.test.helloworld”);
frameworkFactory.newFramework(配置);
framework.start();
List bundleList=new ArrayList();
BundleContext BundleContext=framework.getBundleContext();
File[]bundleJars=新文件(BUNDLE_目录).listFiles();
数组.sort(bundleJars);
用于(文件bundleJar:bundleJars){
if(bundleJar.getName().endsWith(“.jar”)){
Bundle b=bundleContext.installBundle(bundleJar.toURI().toString());
捆绑列表。添加(b);
}
}
//启动所有已安装的捆绑包。
用于(捆绑包:捆绑列表){
如果(!isFragment(bundle))
bundle.start();
}
}捕获(BundleException BundleException){
抛出新的RuntimeException(bundleException);
}
}
私有静态布尔isFragment(Bundle Bundle){
返回bundle.getHeaders().get(Constants.FRAGMENT\u HOST)!=null;
}
}
没关系,我想我必须休息一下。我注意到,在指定包时,我错过了版本
*config.put(Constants.FRAMEWORK\u SYSTEMPACKAGES\u EXTRA,“com.test.helloworld;版本=\“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);
}
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);
}