OSGiフレームワーク Apache Felix その3

前回の

blog1.mammb.com

では、辞書サービスを作成しました。今回は、このサービスを利用するクライアントバンドルを作成します。

クライアントバンドルの構成

前回までと同じ様に、以下の構成とします。

felix-framework-2.0.0
└─ work
     ├─ example1
     ├─ example2
     └─ example3
          ├─ MANIFEST.MF
          └─ tutorial
               └─ example3
                    └─ Activator.java

クライアントバンドルの作成

クライアントも BundleActivator を実装しstartメソッド内で前回作成のサービスを使用します。サービスの取得は、BundleContext からクラス名とプロパティを指定して行います。

package tutorial.example3;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;

import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;

import tutorial.example2.service.DictionaryService;

public class Activator implements BundleActivator {

    public void start(BundleContext context) throws Exception {
        ServiceReference[] refs = context.getServiceReferences(
            DictionaryService.class.getName(), "(Language=*)");

        if (refs != null) {
            try {
                System.out.println("Enter a blank line to exit.");
                BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
                String word = "";

                while (true) {
                    System.out.print("Enter word: ");
                    word = in.readLine();

                    if (word.length() == 0) break;

                    DictionaryService dictionary =
                        (DictionaryService) context.getService(refs[0]);
                    if (dictionary.checkWord(word)) {
                        System.out.println("Correct.");
                    } else {
                        System.out.println("Incorrect.");
                    }

                    context.ungetService(refs[0]);
                }
            } catch (IOException ex) { }
        } else {
            System.out.println("Couldn't find any dictionary service...");
        }
    }

    public void stop(BundleContext context) { }
}

MANIFEST.MFの作成

Import-Package にて前回作成のサービスインターフェースを指定します。

Bundle-Name: Dictionary client
Bundle-Description: A bundle that uses the dictionary service if it finds it at startup
Bundle-Vendor: Apache Felix
Bundle-Version: 1.0.0
Bundle-Activator: tutorial.example3.Activator
Import-Package: org.osgi.framework,
 tutorial.example2.service

バンドルの作成

クライアントバンドルは、example2のサービスインターフェースに依存しているため、クラスパスに追加してコンパイルを行います。

> javac -classpath ..\..\bin\felix.jar;..\example2\example2.jar tutorial\example3\*.java
> jar cfm example3.jar MANIFEST.MF tutorial\example3

クライアントバンドルの使用

作成したクライアントバンドルを動作させてみます。

-> start file:work\example3\example3.jar
Enter a blank line to exit.
Enter word: osgi
Correct.
Enter word: osgg
Incorrect.
Enter word:

入力した word が辞書サービスを利用して、正しく動作していることが確認できます。
このときのバンドルのステータスは以下となっています。

-> ps
START LEVEL 1
   ID   State         Level  Name
[   0] [Active     ] [    0] System Bundle (2.0.0)
[   1] [Active     ] [    1] Apache Felix Bundle Repository (1.4.1)
[   2] [Active     ] [    1] Apache Felix Shell Service (1.4.0)
[   3] [Active     ] [    1] Apache Felix Shell TUI (1.4.0)
[   4] [Active     ] [    1] Service listener example (1.0.0)
[   5] [Active     ] [    1] English dictionary (1.0.0)
[   7] [Active     ] [    1] Dictionary client (1.0.0)
->