[英]Felix OSGI Embedded application issue
如如何从代码启动和使用Apache Felix所述,我将Felix用作嵌入式应用程序。 。 我要做的是通过OSGi从主机应用程序动态加载jar文件,并调用实现类的方法。
所以我有以下三个Maven项目
1)一个具有接口的Maven项目。 并且此接口的包已导出。 ---> ProjA。
2)一个实现项目-> ProjB,这是另一个maven项目,它将ProjA作为maven依赖项导入,并使用具体的类在其上实现接口。 同样在此项目中,我为ProjA接口软件包执行OSGi导入软件包。 同样在这里,我通过激活器在OSGI上注册了我的实现。
3)然后是托管应用程序ProjC。 我在那里做的是
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();
一切正常,直到投射为止。 在那里我可以看到以下错误,
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)
谁能帮我这个忙,对我来说,这似乎是费利克斯的错误。
ProjA位于主项目的类路径中(打开嵌入式OSGi容器),它也作为捆绑包安装到嵌入式OSGi容器中。 解决ProjB后,它将连接到ProjA捆绑软件,因此它实现了来自已安装的projA捆绑软件的接口。
当您尝试投射结果对象时,您尝试投射到主项目的类路径上的接口。 这是ProjB捆绑包实现的另一个接口,因为它实现了projA捆绑包中的接口。
您不应将ProjA捆绑安装到OSGi容器中。 您应该确保ProjB捆绑包可以解决。 为此,您应该将projA作为系统软件包添加到嵌入式OSGi容器中。
解决此问题的另一种方法是在maven maven-bundle-plugin或清单文件中使用导出标记
<build>
<plugins>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<extensions>true</extensions>
<configuration>
<instructions>
<Embed-Dependency>*;scope=compile|runtime</Embed-Dependency>
<Export-Package>come.example.myInterface</Export-Package>
<Bundle-Activator>come.example.Activator</Bundle-Activator>
</instructions>
</configuration>
</plugin>
</plugins>
</build>
并没有忘记
map.put(Constants.FRAMEWORK_SYSTEMPACKAGES_EXTRA, "come.example.myInterface; version=0.0.1");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.