Felix OSGI嵌入式应用程序问题 [英] Felix OSGI Embedded application issue
问题描述
我将Felix用作嵌入式应用程序,如所述, 如何通过代码启动和使用Apache Felix? .我想做的是通过OSGi从主机应用程序动态加载jar文件并调用实现类的方法.
I was using Felix as a embedded application as explained in, How to start and use Apache Felix from code?. What I want to do is dynamically load jar files from my host application via OSGi and invoke methods of implementation classes.
所以我有以下三个Maven项目
So I have following three maven projects
1)一个具有接口的Maven项目.并且此接口的包已导出. ---> ProjA.
1) A maven project which has an interface. And the package of this interface is exported. ---> ProjA .
2)一个实现项目-> ProjB,这是另一个maven项目,它将ProjA作为maven依赖项导入,并使用具体的类在其上实现接口.同样在这个项目中,我为ProjA接口软件包提供OSGi导入软件包.同样在这里,我通过激活器在OSGI上注册了我的实现.
2) A implementation project --> ProjB, another maven project which import ProjA as a maven dependency and implement interface on it with a concrete class. Also in this project I do OSGi import-package for ProjA interface package. Also here I register my implementation on OSGI via activator.
3)然后是托管应用程序ProjC.我在那里做的,
3) Then ProjC which is the hosted application. What I do there is,
HostActivator activator = new HostActivator();
List<Object> list = new LinkedList<Object>();
list.add(activator);
map.put(FelixConstants.SYSTEMBUNDLE_ACTIVATORS_PROP, list);
Felix f = new Felix(map);
f.start();
Bundle a = f.getBundleContext().installBundle("file:C:/ProjA.jar");
Bundle b = f.getBundleContext().installBundle("file:C:/ProjB.jar"); ); // dirty path ;)
b.start();
ServiceReference sr = activator.getContext().getAllServiceReferences(MyInterface.class.getName(), "(" + "osgi-device-name" + "=*)")[0];
MyInterface dictionary = (MyInterface) activator.getContext().getService(sr);
dictionary.doAction();
一切正常,直到投放为止.在那里,我可以看到以下错误,
Everything works fine until cast. There I can see following error,
Exception in thread "main" java.lang.ClassCastException: projB.MyImplementation cannot be cast to projA.MyInterface
at MyHostApplication.MyMainClass.main(MyMainClass.java:70)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
任何人都可以帮我这个忙,对我来说,这似乎是felix上的一个错误.
Can anyone help me on this, for me this seems like a bug on felix.
推荐答案
ProjA位于主项目(打开嵌入式OSGi容器)的类路径中,并且也捆绑安装在嵌入式OSGi容器中.解决ProjB后,它会连接到ProjA捆绑包,因此它实现了来自已安装的projA捆绑包的接口.
ProjA is on the classpath of your main project (that opens the embedded OSGi container) and it is also installed into the embedded OSGi container as a bundle. When ProjB is resolved, it wires to the ProjA bundle, so it implements the interface that is coming from the installed projA bundle.
当您尝试转换结果对象时,您尝试转换到主项目的类路径上的接口.这是ProjB捆绑包实现的另一个接口,因为它实现了projA捆绑包中的接口.
When you try to cast the result object, you try to cast to the interface that is on the classpath of the main project. That is a different interface that the ProjB bundle implements as it implements the interface from projA bundle.
您不应将ProjA作为捆绑软件安装到OSGi容器中.您应该确保ProjB捆绑包可以解决.为此,您应该将 projA 作为系统软件包添加到嵌入式OSGi容器中.
You should not install ProjA as a bundle into the OSGi container. You should be sure that ProjB bundle can resolve. To do that, you should add projA as a system package to the embedded OSGi container.
这篇关于Felix OSGI嵌入式应用程序问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!