6

OSGi ライブラリに実際に依存することなく、OSGi バンドルとして公開したいいくつかのモジュールを構築しています。これは、宣言型サービス オプションを使用して可能であるように思われます。

ただし、私は OSGi にかなり慣れていないため (少なくともバンドル作成側では)、すべてが正常に機能するかどうかをテストしたいと考えています。この目的のために、小さな組み込み OSGi 環境をセットアップしたいと考えています。

現在、API をエクスポートし、単一のインターフェイスのスタブ実装も提供する単一のバンドルがあります。

次のチュートリアルに従って、環境をセットアップしました。

組み込みの felix 実装は適切に動作しているように見えますが、2 つの問題があります。

Bundle bundle = felix.getBundleContext().installBundle("/path/to/bundle.jar")
bundle.start();
System.out.println(bundle.getRegisteredServices());

これnullは、バンドルが正常に開始されたように見えても、サービスを公開していないように見えるため、出力されます。

第二に、宣言型サービスを少しずつ稼働させるために何か特別なことをしなければならないかどうか疑問に思っています。私のMavenの依存関係は次のとおりです。

<dependencies>
    <dependency>
        <groupId>org.apache.felix</groupId>
        <artifactId>org.apache.felix.framework</artifactId>
        <version>4.2.1</version>
    </dependency>
    <dependency>
        <groupId>org.apache.felix</groupId>
        <artifactId>org.apache.felix.scr</artifactId>
        <version>1.6.2</version>
    </dependency>
</dependencies>

ここにある電子メール スレッドに基づく: http://mail-archives.apache.org/mod_mbox/felix-users/201111.mbox/%3CAE48C9B8172EFC48A028B60E8D6F96660143A5F336@sausexmbp02.amd.com%3E

バンドルを felix スタートアップ プロパティに追加しようとしました。

map.put(Constants.FRAMEWORK_SYSTEMPACKAGES_EXTRA, "org.apache.felix.scr; version=1.6.2");

しかし、これは一見すると少し楽観的に見えます。組み込みの Felix エンジンの宣言型サービスを有効にするにはどうすればよいですか?

4

2 に答える 2

7

両方の問題の解決策は、独自のバンドルをロードする前に、「scr」jar (宣言型サービスの解析に使用) をバンドルとしてロードすることでした。

jar は私の maven リポジトリにあり、クロス システムで動作するはずなので、次のコードは scr jar をどこからでもロードします。

    URL url = getClass().getClassLoader().getResource("org/apache/felix/scr/ScrService.class");
    String jarPath = url.toURI().getSchemeSpecificPart().replaceAll("!.*", "");
    framework.getBundleContext().installBundle(jarPath).start();

このビットの後、独自のバンドルをロードすると、その中のサービスが適切に検出されました。

ちなみに、初期マップにいくつかのプロパティを追加することで、ログを有効にすることができます。

    map.put("ds.showtrace", "true");
    map.put("ds.showerrors", "true");

その他のプロパティは、http://felix.apache.org/documentation/subprojects/apache-felix-service-component-runtime.htmlにあります。

今後の参考のために、これを起動して実行するために使用したすべてのコードを次に示します

private void initialize() throws BundleException, URISyntaxException {
    Map<String, String> map = new HashMap<String, String>();

    // make sure the cache is cleaned
    map.put(Constants.FRAMEWORK_STORAGE_CLEAN, Constants.FRAMEWORK_STORAGE_CLEAN_ONFIRSTINIT);

    // more properties available at: http://felix.apache.org/documentation/subprojects/apache-felix-service-component-runtime.html
    map.put("ds.showtrace", "true");
    map.put("ds.showerrors", "true");

    System.out.println("Building OSGi Framework");
    FrameworkFactory frameworkFactory = ServiceLoader.load(FrameworkFactory.class).iterator().next();
    Framework framework = frameworkFactory.newFramework(map);

    System.out.println("Starting OSGi Framework");
    framework.start();

    // declarative services dependency is necessary, otherwise they won't be picked up!
    loadScrBundle(framework);

    framework.getBundleContext().installBundle("file:/path/to/myBundle.jar").start();

    ServiceReference reference = framework.getBundleContext().getServiceReference("my.Interface");
    System.out.println(framework.getBundleContext().getService(reference));

    for (Bundle bundle : framework.getBundleContext().getBundles()) {
        System.out.println("Bundle: " + bundle.getSymbolicName());
        if (bundle.getRegisteredServices() != null) {
            for (ServiceReference serviceReference : bundle.getRegisteredServices())
                System.out.println("\tRegistered service: " + serviceReference);
        }
    }
}

private void loadScrBundle(Framework framework) throws URISyntaxException, BundleException {
    URL url = getClass().getClassLoader().getResource("org/apache/felix/scr/ScrService.class");
    if (url == null)
        throw new RuntimeException("Could not find the class org.apache.felix.scr.ScrService");
    String jarPath = url.toURI().getSchemeSpecificPart().replaceAll("!.*", "");
    System.out.println("Found declarative services implementation: " + jarPath);
    framework.getBundleContext().installBundle(jarPath).start();
}
于 2013-05-23T08:42:25.157 に答える