当前位置: 首页 > 知识库问答 >
问题:

在Maven项目中使用OSGi包的ClassCastException

巫马昆杰
2023-03-14

我是OSGi的新手,很难将包包含到我的Maven项目中。

我使用mave-bundle-plugin创建了一个API包和一个实现包。在我的主要项目(一个Maven项目)中,我试图使用Felix框架从ServiceTracker获得已实现包的服务。当我最终尝试将获得的服务强制转换为正确的类型时,我会收到一个ClassCastException。

public interface ParserTestService {
    String validForStage();
}
<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>
public class ParserImplService implements ParserTestService {
    public String validForStage() {
        return "S";
    }
}
<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使用以下清单文件创建了两个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

在我主要的Maven项目中,我设置并启动了一个Felix框架。在子文件夹“bundles”中,我复制了由上面的Maven部署过程创建的jar文件。在install例程中,我尝试安装这些包并启动它们:

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>
java.lang.ClassCastException: de.ParserImplService cannot be cast to de.ParserTestService
ParserTestService.class.getClassLoader()
(m_felix.getBundleContext().getService(sr)).getClass().getClassLoader()

有人知道我做错了什么吗?任何帮助都很感激!

提前谢谢你,塞巴斯蒂安

共有1个答案

龚振濂
2023-03-14

我终于找到了解决这个问题的方法,但只能避开Felix而改用Equinox。

1.)API只导出包 de

2.)Impl不导出任何东西。它只定义激活器及其导入

<Bundle-Activator>de.Activator</Bundle-Activator>
<Import-Package>org.osgi.framework;de</Import-Package>
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());
            }
        }

    }
}

感谢大家的帮助!莎贝之圣

 类似资料:
  • 问题内容: 我做了一个maven-osgi项目,其中激活程序应该发送一个osgi事件,但是由于某种原因EventAdmin始终为null。 这是我的java课 这是我的pom.xml: EventAdmin为空的原因可能是什么? 问题答案: 您已经在OSGi中实现了经典的反模式:您假设在捆绑包启动时EventAdmin服务将已经可用。这本质上是不安全的假设,因为您的捆绑包实际上可能在提供Event

  • 我们面临的最大问题是,我们无法找出如何构造项目C,以使其在项目A和B中都能正常工作。在项目A中,我们希望maven能像往常一样工作,并根据需要降低依赖项。但我们也希望在项目B(也许结合Nexus+Tycho??)中实现该功能。 我们应该如何解决这个问题。我还没有找到一个好的解决办法如何做这件事。我已经在maven中尝试了apache felix捆绑插件,但不能让它像我想要的那样工作。这是正确的解决

  • 在Netbeans 8.0中,我使用Maven开发JavaFX项目。我的java版本是1.7,升级到Java8有问题。我尝试将POM文件中的和更改为1.8 当我尝试创建新的JavaFX项目(如文件-new project-maven category-JavaFX Application)时,我仍然得到了使用JDK1.7的项目。如何强制使用JDK1.8?

  • 我在尝试创建一个简单的GWTMaven项目时遇到了很多问题,这个项目可以在Eclipse中使用。以下是我正在做的: > 在Eclipse:File中打开项目= 但是,我发现以下错误: 我不明白这个错误信息。我发现了一个关于SO的相关问题,但是在我的pom.xml中添加建议的剪报似乎没有任何作用。 有人能解释一下吗?

  • 在感觉自己已经掌握了如何使用OSGi之后,我尝试向我的应用程序添加第三方依赖,特别是log4j2,该应用程序使用ApacheFelix并与maven捆绑插件捆绑。不幸的是,我似乎陷入了依赖地狱。 我尝试过使用许多maven捆绑包策略,如导入包,嵌入依赖,wrapImportPackage,Embed-Transitive,以及设置特定的版本号,仅举几例。以下是我的pom在这个插件中的样子: 我觉得