![](/img/trans.png)
[英]switching to gradle from maven to manage a osgi big project (>200 bundles)
[英]ClassCastException using OSGi bundles in a Maven project
我是OSGi的新手,並且在我的Maven項目中包含了捆綁包。
我使用mave-bundle-plugin創建了一個API包和一個實現包。 在我的主要項目(Maven項目)中,我嘗試使用Felix Framework從ServiceTracker獲取已實現捆綁的服務。 當我最終嘗試將獲得的服務轉換為正確的類型時,我收到ClassCastException。
API Maven項目:
public interface ParserTestService {
String validForStage();
}
API POM:
<artifactId>ParserTest</artifactId>
<version>0.0.2-SNAPSHOT</version>
<packaging>bundle</packaging>
<build>
<plugins>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<extensions>true</extensions>
<configuration>
<instructions>
<Bundle-SymbolicName>${pom.groupId}.${pom.artifactId}</Bundle-SymbolicName>
<Bundle-Vendor>Apache-Felix</Bundle-Vendor>
<Bundle-Version>0.0.1</Bundle-Version>
<Export-Service>de.ParserTestService</Export-Service>
</instructions>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.apache.felix</groupId>
<artifactId>org.apache.felix.framework</artifactId>
<version>2.0.4</version>
</dependency>
</dependencies>
Impl Maven項目:
public class ParserImplService implements ParserTestService {
public String validForStage() {
return "S";
}
}
Impl POM:
<artifactId>ParserTestVersion1</artifactId>
<version>0.0.2-SNAPSHOT</version>
<packaging>bundle</packaging>
<build>
<plugins>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<extensions>true</extensions>
<configuration>
<instructions>
<Bundle-SymbolicName>${pom.groupId}.${pom.artifactId}</Bundle-SymbolicName>
<Bundle-Vendor>Apache-Felix</Bundle-Vendor>
<Bundle-Version>0.0.1</Bundle-Version>
<Bundle-Activator>de.Activator</Bundle-Activator>
<Export-Package>de</Export-Package>
<Export-Service>de.ParserImplService</Export-Service>
<Import-Package>org.osgi.framework</Import-Package>
</instructions>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.apache.felix</groupId>
<artifactId>org.apache.felix.framework</artifactId>
<version>2.0.4</version>
<type>bundle</type>
</dependency>
<dependency>
<groupId>de</groupId>
<artifactId>ParserTest</artifactId>
<version>0.0.2-SNAPSHOT</version>
<type>bundle</type>
</dependency>
</dependencies>
Maven使用以下Manifest文件創建了兩個jar文件:
Manifest-Version: 1.0
Bnd-LastModified: 1340890655296
Build-Jdk: 1.6.0_24
Built-By: br_s1
Bundle-ManifestVersion: 2
Bundle-Name: Parser Test Interface
Bundle-SymbolicName: de.ParserTest
Bundle-Vendor: Apache-Felix
Bundle-Version: 0.0.1
Created-By: Apache Maven Bundle Plugin
Export-Package: de;version="0.0.1"
Export-Service: de.ParserTestService
Tool: Bnd-1.50.0
Manifest-Version: 1.0
Bnd-LastModified: 1340890661890
Build-Jdk: 1.6.0_24
Built-By: br_s1
Bundle-Activator: de.Activator
Bundle-ManifestVersion: 2
Bundle-Name: Parser Test
Bundle-SymbolicName: de.ParserTestVersion1
Bundle-Vendor: Apache-Felix
Bundle-Version: 0.0.1
Created-By: Apache Maven Bundle Plugin
Export-Package: de;version="0.0.1"
Export-Service: de.ParserImplService
Import-Package: org.osgi.framework;version="[1.5,2)"
Tool: Bnd-1.50.0
在我的Major Maven項目中,我設置並啟動了Felix框架。 在子文件夾“bundles”中,我復制了上面Maven部署過程創建的jar文件。 在安裝例程中,我嘗試安裝這些軟件包並啟動它們:
public class HostActivator implements BundleActivator {
...
public BundleContext getContext()
{
return m_context;
}
...
}
public class HostApplication
{
private HostActivator m_activator = null;
private Felix m_felix = null;
private ServiceTracker m_tracker = null;
public HostApplication()
{
Map<String, Object> configMap = new HashMap<String, Object>();
configMap.put(Constants.FRAMEWORK_SYSTEMPACKAGES_EXTRA, "de.test; version=1.0.0");
m_activator = new HostActivator();
List<BundleActivator> list = new ArrayList<BundleActivator>();
list.add(m_activator);
configMap.put(FelixConstants.SYSTEMBUNDLE_ACTIVATORS_PROP, list);
m_felix = new Felix(configMap);
m_felix.start();
m_tracker = new ServiceTracker(m_activator.getContext(), ParserTestService.class.getName(), null);
m_tracker.open();
}
public void installBundlesFromDir() {
File dir = new File("bundles");
for (File f : dir.listFiles()) {
try {
Bundle b = m_felix.getBundleContext().installBundle("file:"+f.getAbsolutePath());
b.start();
ServiceReference sr = m_felix.getBundleContext().getServiceReference(ParserTestService.class.getName());
ParserTestService service = (ParserTestService) m_felix.getBundleContext().getService(sr);
} catch (BundleException e) {
System.err.println("could not load bundle "+f.getAbsolutePath());
}
}
}
}
相應的POM:
<artifactId>parserUse</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.apache.felix</groupId>
<artifactId>org.apache.felix.framework</artifactId>
<version>2.0.4</version>
</dependency>
<dependency>
<groupId>de</groupId>
<artifactId>ParserTestVersion1</artifactId>
<version>0.0.2-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>de</groupId>
<artifactId>ParserTest</artifactId>
<version>0.0.2-SNAPSHOT</version>
</dependency>
</dependencies>
但是,當我嘗試調用安裝例程時,我收到一個ClassCastException:
java.lang.ClassCastException: de.ParserImplService cannot be cast to de.ParserTestService
我已經檢查了類加載器
ParserTestService.class.getClassLoader()
和
(m_felix.getBundleContext().getService(sr)).getClass().getClassLoader()
他們彼此不同。
我一直以為OSGi會照顧我的班級負載。
有誰知道我做錯了什么? 任何幫助表示贊賞!
在此先感謝塞巴斯蒂安
我終於找到了解決這個問題的方法,但只是避免使用Felix而是使用Equinox。
1.)API僅<Export-Package>de</Export-Package>
包<Export-Package>de</Export-Package>
2.)Impl什么都沒出口。 它只定義了激活劑及其進口
<Bundle-Activator>de.Activator</Bundle-Activator>
<Import-Package>org.osgi.framework;de</Import-Package>
3.)我使用Equinox替換了整個HostApplication:
public class HostApplication {
private BundleContext bundleContext;
public HostApplication(String profileName) {
BundleContext bc = null;
Properties frameworkProperties = readCustomProfile(profileName);
frameworkProperties.put("osgi.clean", "true");
frameworkProperties.put("osgi.console", "true");
Map<String, String> frameworkPropertiesMap = new HashMap<String, String>();
for (Object o : frameworkProperties.keySet()) {
frameworkPropertiesMap.put((String) o,
(String) frameworkProperties.getProperty((String) o));
}
EclipseStarter.setInitialProperties(frameworkPropertiesMap);
bc = EclipseStarter.startup(new String[] { "-console", "-dev", "bin" }, null);
}
public boolean containsIegm(String stage, byte[] msg) {
try {
ServiceReference serviceReference = bundleContext.getServiceReference(ParserTest.class.getName());
return ((ParserImplService) bundleContext.getService(serviceReference)).containsIegm(msg);
} catch (Exception ex) {
ex.printStackTrace();
}
return false;
}
public void installBundlesFromDir() {
File dir = new File("bundles");
int i = 0;
for (File f : dir.listFiles()) {
try {
Bundle b = bundleContext.installBundle("file:"+ f.getAbsolutePath());
b.start();
} catch (BundleException e) {
System.err.println("could not load bundle " + f.getAbsolutePath());
}
}
}
}
我通過使用m_felix的BundleContext(請參閱問題)嘗試與Felix完全相同,並完全刪除HostActivator(我知道在我的應用程序中沒有必要)。 但是,我無法使其發揮作用。
無論如何,使用Equinox,在非OGSi /非捆綁應用程序中嵌入OSGi框架同樣容易。
謝謝大家的幫助! 塞巴斯蒂安
兩個捆綁包都出口“de”。 這意味着將有兩個名為“de”的名稱空間。
bundle Bundle-SymbolicName:de.ParserTestVersion1應該導入 “de”並從另一個bundle獲取它
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.