1

私は 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";
    }
}

実装 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 は、次のマニフェスト ファイルを含む 2 つの 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 がクラスのロードを処理してくれると思っていました。

誰かが私が間違っていることの手がかりを持っていますか? どんな助けでも大歓迎です!

前もって感謝します、セバスチャン

4

2 に答える 2

2

私は最終的にこの問題を解決する方法を見つけましたが、それは Felix を避け、代わりに Equinox を使用することだけでした。

1.) API はパッケージのみをエクスポートします。<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 フレームワークを埋め込むことも同様に簡単です。

みんな助けてくれてありがとう!セバスチャン

于 2012-06-29T12:53:39.100 に答える
1

どちらのバンドルも「de」をエクスポートしています。これは、「de」と呼ばれる 2 つの名前空間が存在することを意味します。
バンドル Bundle-SymbolicName: de.ParserTestVersion1 は「de」をインポートし、他のバンドルから取得する必要があります

于 2012-06-28T19:03:57.933 に答える