Pax Constructの使い方

Pax ConstructはOSGi bundleの作成や、ビルド等々を助けてくれるスイスアーミーナイフと紹介されています。これからOSGi Bundleを作成するのに便利そうですね!ということで、試してみました。Pax系のツールはMaven2と組み合わせる事で有効にプロジェクトの補助をしてくれるそうなので、Maven2のインストールは忘れずに。MavenizeされたOSGiプロジェクトが作られていく感じなんでしょうね。
プロジェクトのページはこちら。最新は1.4です。
をまずやってみます。
Pax Constructはスクリプトなので、Pax Constructをダウンロード後、忘れずにPATHを通しましょう。

まずプロジェクト作ってみよう。

それじゃプロジェクトを作ってみます。pax-create-projectを実行します。パラメータを指定すると初期値の指定になるようですが、そのまま実行してみたら、対話シェルになったよ。

$ pax-create-project 

pax-create-project -g groupId -a artifactId [-v version] [-o] [-- mvnOpts ...]

groupId (examples) ? kompiro
artifactId (myProject) ? hello-pax
version (1.0-SNAPSHOT) ? 

[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Building Maven Default Project
[INFO]    task-segment: [org.ops4j:maven-pax-plugin:1.4:create-project] (aggregator-style)
[INFO] ------------------------------------------------------------------------

...ライブラリのダウンロード

[INFO] ----------------------------------------------------------------------------
[INFO] Parameter: packageName, Value: kompiro.hello-pax
[INFO] Parameter: archetypeVersion, Value: 1.0.3
[INFO] Parameter: groupId, Value: kompiro
[INFO] Parameter: archetypeArtifactId, Value: maven-archetype-osgi-project
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] Parameter: archetypeGroupId, Value: org.ops4j.pax.construct
[INFO] Parameter: basedir, Value: /home/kompiro/codes/java/osgi-demo
[INFO] Parameter: package, Value: kompiro.hello-pax
[INFO] Parameter: artifactId, Value: hello-pax
[INFO] ********************* End of debug info from resources from generated POM ***********************
[INFO] Archetype created in dir: /home/kompiro/codes/java/osgi-demo/hello-pax
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 13 seconds
[INFO] Finished at: Sat Jan 09 17:23:49 JST 2010
[INFO] Final Memory: 10M/18M
[INFO] ------------------------------------------------------------------------

どうなったのか、中を見てみる。

$ ls
pom.xml pomse provisione

すっきりしたもんですね。

Bundleを作ろう

続いてBundleを作ってみますか。

$ pax-create-bundle -p org.kompiro.osgi.hello -n hello
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Building kompiro.hello-pax (OSGi project)
[INFO]    task-segment: [org.ops4j:maven-pax-plugin:1.4:create-bundle] (aggregator-style)
[INFO] ------------------------------------------------------------------------

...
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 11 seconds
[INFO] Finished at: Sat Jan 09 17:28:41 JST 2010
[INFO] Final Memory: 10M/18M
[INFO] ------------------------------------------------------------------------

OSGiコンテナへデプロイ

Quick Startよろしく、felixにデプロイしてみる。

$ mvn clean install pax:provision
...
[INFO] [pax:provision]
[INFO] Installing /home/kompiro/codes/java/osgi-demo/hello-pax/runner/deploy-pom.xml to /home/kompiro/.m2/repository/kompiro/hello-pax/build/deployment/1.0-SNAPSHOT/deployment-1.0-SNAPSHOT.pom
__________                 __________                                 
\______   \_____  ___  ___ \______   \__ __  ____   ____   ___________
|     ___/\__  \ \  \/  /  |       _/  |  \/    \ /    \_/ __ \_  __ \
|    |     / __ \_>    <   |    |   \  |  /   |  \   |  \  ___/|  | \/
|____|    (____  /__/\_ \  |____|_  /____/|___|  /___|  /\___  >__|   
               \/      \/         \/           \/     \/     \/       

Pax Runner (1.3.0) from OPS4J - http://www.ops4j.org
----------------------------------------------------

うぉ。Pax Runnerじゃないですか。

... ダウンロード等
Welcome to Felix
================

-> STARTING org.kompiro.osgi.hello
REGISTER org.kompiro.osgi.hello.ExampleService

お!felixさん、こんにちは。helpって打ち込めばコマンドわかるかな。

help
bundlelevel
cd
find
headers
help
inspect
install
log
ps
refresh
resolve
shutdown
start
startlevel
stop
sysprop
uninstall
update
version

じゃ、この中からBundleのリストを見るpsを実行してみる。

-> ps
START LEVEL 6
   ID   State         Level  Name
[   0] [Active     ] [    0] System Bundle (2.0.1)
[   1] [Active     ] [    5] kompiro.hello-pax.hello [org.kompiro.osgi.hello] (1.0.0.SNAPSHOT)
[   2] [Active     ] [    1] Apache Felix Shell Service (1.4.1)
[   3] [Active     ] [    1] Apache Felix Shell TUI (1.4.1)

よし、終了。

-> shutdown
-> STOPPING org.kompiro.osgi.hello

 -> Platform has been shutdown.
...
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3 minutes 27 seconds
[INFO] Finished at: Sat Jan 09 17:34:49 JST 2010
[INFO] Final Memory: 20M/36M
[INFO] ------------------------------------------------------------------------

OSGiコンテナを切り替える

ん?よくよくみたら、pax-provisionなんてコマンドがある。こっちのヘルプを見てみると、こんな感じ。。デフォルトはfelixかな?起動してみる。

$ pax-provision
...中略
Welcome to Felix
================

-> STARTING org.kompiro.osgi.hello
REGISTER org.kompiro.osgi.hello.ExampleService

やっぱりfelixだった。じゃ、equinoxもいってみっか。

$ pax-provision -f equinox
...中略
osgi> STARTING org.kompiro.osgi.hello
REGISTER org.kompiro.osgi.hello.ExampleService
ss

Framework is launched.

id	State       Bundle
0	ACTIVE      org.eclipse.osgi_3.5.1.R35x_v20090827
1	ACTIVE      kompiro.hello-pax.hello_1.0.0.SNAPSHOT

osgi> exit
 -> Platform has been shutdown.
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 28 seconds
[INFO] Finished at: Sat Jan 09 17:38:41 JST 2010
[INFO] Final Memory: 9M/18M
[INFO] ------------------------------------------------------------------------

おー。簡単にコンテナを切り替えられる。さすがPax Runner。ついでにKnopflerfish

$ pax-provision -f kf
...中略
Knopflerfish OSGi framework, version 4.1.10
Copyright 2003-2009 Knopflerfish. All Rights Reserved.

See http://www.knopflerfish.org for more information.
Loading xargs url file:knopflerfish/config.ini
Installed and started: file:bundles/org.knopflerfish.log-API_2.0.1.jar (id#1)
Installed and started: file:bundles/org.knopflerfish.bundle.cm-API_2.0.1.jar (id#2)
Installed and started: file:bundles/org.knopflerfish.bundle.console_2.0.1.jar (id#3)
Installed and started: file:bundles/org.knopflerfish.bundle.consoletty-IMPL_2.0.1.jar (id#4)
Installed and started: file:bundles/org.knopflerfish.bundle.frameworkcommands-IMPL_2.0.5.jar (id#5)
Installed and started: file:bundles/kompiro.hello-pax.hello_1.0.0.SNAPSHOT.jar (id#6)
> STARTING org.kompiro.osgi.hello
REGISTER org.kompiro.osgi.hello.ExampleService
Framework launched
help
Available command groups (type 'enter' to enter a group):
session - Session commands built into the console
framework - Framework commands
> 
> session
Available session commands:
  alias [<alias>] [<val>] ...  - Set or show aliases
  echo [-n] ...                - Echo command arguments
  enter <command group>        - Enter command group mode
  help [<command group> | all] - Help about specific command group or all command groups
  leave                        - Leave command group mode
  prompt <command prompt>      - Set command prompt
  quit                         - Exit this session
  save [<file>]                - Save current aliases as a property file
  restore [<file>]             - Restore aliases from a property file or from default aliases
  source <URL>                 - Source commands at URL
  unalias <alias name>         - Remove an alias

ヘルプの見方が・・・。つらい。

> help all
Available command groups (type 'enter' to enter a group):
session - Session commands built into the console
framework - Framework commands
> help session
Available session commands:
  alias [<alias>] [<val>] ...  - Set or show aliases
  echo [-n] ...                - Echo command arguments
  enter <command group>        - Enter command group mode
  help [<command group> | all] - Help about specific command group or all command groups
  leave                        - Leave command group mode
  prompt <command prompt>      - Set command prompt
  quit                         - Exit this session
  save [<file>]                - Save current aliases as a property file
  restore [<file>]             - Restore aliases from a property file or from default aliases
  source <URL>                 - Source commands at URL
  unalias <alias name>         - Remove an alias

なるほど。ヘルプがsessionとframeworkでグループ化されてるようなイメージかな。じゃ、frameworkのヘルプを見てみたらできるか?

> help framework
Available framework commands:
  addpermission [-help] -b #bundle# | -d | -l #location# <type> [<name> [<actions>]] - Add permissions to bundle
  bundles [-help] [-1] [-i] [-l] [-s] [-t] [<bundle>] ... - List bundles
  call [-help] [-f #filter#] <interface> <method> [<args>] ... - Call a method with zero or more java.lang.String
  closure [-help] <bundle> - Display the closure for a bundle
  deletepermission [-help] [-r] -b #bundle# | -d | -l #location# <type> <name> <actions> - Delete permissions from a bundle
  findbundles [-help] <symbolic name> - Find bundles with a given symbolic name
  headers [-help] [-i] [-l #locale#] <bundle> ... - Show bundle header values
  install [-help] [-s] <location> ... - Install one or more bundles
  package [-help] [-l] -b | -p [<selection>] ... - Show java package information
  permissions [-help] [-d] [<selection>] ... - Show permission information
  refresh [-help] [<bundle>] ... - Refresh all exported java packages belong to specified bundle
  resolve [-help] [<bundle>] ... - Resolve one or more bundles
  services [-help] [-i] [-l] [-f #filter#] [-r] [-u] [<bundle>] ... - List registered services
  start [-help] <bundle> ... - Start one or more bundles
  stop [-help] <bundle> ... - Stop one or more bundles
  showstate [-help] [<pid>] ... - Show the state of a service, if the service provides state information
  shutdown [-help] [<exit code>] - Shutdown framework
  uninstall [-help] <bundle> ... - Uninstall one or more bundles
  update [-help] <bundle> ... - Update one or more bundles
  fromupdate [-help] <bundle> <url> - Update a bundle from a specific URL
  frominstall [-help] <url> [<location>] - Install a bundle with a specific location from an URL
  startlevel [-help] [<level>] - Shows or sets the global startlevel
  cd [-help] [-reset] [<prefix>] ... - Shows or sets URL prefix
  bundlelevel [-help] <level> [<bundle>] ... - Set startlevel(s) for bundles

bundlesっぽい。

> bundles
   id  level/state name
   --------------------
    3  1/active    Console                  5  1/active    FW-Commands-IMPL
    1  1/active    LogService-API           0  0/active    System Bundle
    4  1/active    TTY-Console-IMPL         2  1/active    cm-API
    6  5/active    kompiro.hello-pax.hello [org.kompiro.osgi.hello]

でけた。なんか、くせある感じ。終了しよう。

> shutdown
> STOPPING org.kompiro.osgi.hello

 -> Platform has been shutdown.
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2 minutes 17 seconds
[INFO] Finished at: Sat Jan 09 17:44:14 JST 2010
[INFO] Final Memory: 9M/17M
[INFO] ------------------------------------------------------------------------

Bundleをいくつか作ってみよう。

よっしゃ、もしかしてBundleと言ってもEclipse Pluginと異なり、一つのプロジェクトで複数のBundleいけるのか?もやってみた。

$ pax-create-bundle -n test

ん?・・・よくよくみたら、hello-paxの下にプロジェクトができておる!どちらかというと、さっき作ったプロジェクトはワークスペースに近い性質っぽい。じゃ、testするにはどうしたらいいんだろう。とりあえず作成したtest bundleは削除しておこう。

$ pax-remove-bundle -n org.kompiro.hello-pax.test
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Building kompiro.hello-pax (OSGi project)
[INFO]    task-segment: [org.ops4j:maven-pax-plugin:1.4:remove-bundle] (aggregator-style)
[INFO] ------------------------------------------------------------------------
[INFO] [pax:remove-bundle]
[INFO] Removing kompiro.hello-pax:test:bundle:1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 10 seconds
[INFO] Finished at: Sat Jan 09 18:19:14 JST 2010
[INFO] Final Memory: 8M/17M
[INFO] ------------------------------------------------------------------------

testはPax Examでやるのね。ちょい小休止。