OSGi Bundleはじめの一歩

2013年10月 3日 Java OSGi

OSGi Bundleはじめの一歩です。

New ProjectでPlug-in Projectを選択します。
ちなみにeclipseです。



Target Platformはan OSGi frameworkでEquinoxを選択します。

Finish!

生成されたActivatorにstartとstopを出力するようにします。

java
package com.misfrog.osgi.example1;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
 
public class Activator implements BundleActivator {
 
    private static BundleContext context;
 
    static BundleContext getContext() {
        return context;
    }
 
    public void start(BundleContext bundleContext) throws Exception {
        Activator.context = bundleContext;
        System.out.println("start");
    }
 
    public void stop(BundleContext bundleContext) throws Exception {
        System.out.println("stop");
        Activator.context = null;
    }
}

プロジェクトを右クリックしRun Configurationを選択して作成したBundleのみを選択します。

Add Required Bundlesボタンで依存しているBundleを追加します。

どうやら作成したBundleを含めて4つのBundleが選択されたようです。

Runボタンで実行!

start
!SESSION 2013-10-03 12:02:55.289 -----------------------------------------------
eclipse.buildId=unknown
java.version=1.7.0_07
java.vendor=Oracle Corporation
BootLoader constants: OS=win32, ARCH=x86_64, WS=win32, NL=ja_JP
Command-line arguments:  -dev file:C:/devel/eclipse/eclipse/.metadata/.plugins/org.eclipse.pde.core/com.misfrog.osgi.example1/dev.properties -os win32 -ws win32 -arch x86_64 -consoleLog -console!ENTRY org.eclipse.osgi 4 0 2013-10-03 12:02:56.014
!MESSAGE Could not find bundle: org.eclipse.equinox.console
!STACK 0
org.osgi.framework.BundleException: Could not find bundle: org.eclipse.equinox.console
at org.eclipse.osgi.framework.internal.core.ConsoleManager.checkForConsoleBundle(ConsoleManager.java:211)
at org.eclipse.core.runtime.adaptor.EclipseStarter.startup(EclipseStarter.java:298)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:177)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:636)
at org.eclipse.equinox.launcher.Main.basicRun(Main.java:591)
at org.eclipse.equinox.launcher.Main.run(Main.java:1450)
at org.eclipse.equinox.launcher.Main.main(Main.java:1426)

エラーになりました‥
どうもorg.eclipse.equinox.consoleがみつからないようなのでConfigurationで追加します。
追加してAdd Required Bundlesしたところ合計8個のBundleになりました。
javax.servletとか必要なのでしょうか‥

Run!

start
!SESSION 2013-10-03 12:07:45.552 -----------------------------------------------
eclipse.buildId=unknown
java.version=1.7.0_07
java.vendor=Oracle Corporation
BootLoader constants: OS=win32, ARCH=x86_64, WS=win32, NL=ja_JP
Command-line arguments:  -dev file:C:/devel/eclipse/eclipse/.metadata/.plugins/org.eclipse.pde.core/com.misfrog.osgi.example1/dev.properties -os win32 -ws win32 -arch x86_64 -consoleLog -console!ENTRY org.eclipse.equinox.console 4 0 2013-10-03 12:07:46.349
!MESSAGE FrameworkEvent ERROR
!STACK 0
org.osgi.framework.BundleException: Exception in org.eclipse.equinox.console.command.adapter.Activator.start() of bundle org.eclipse.equinox.console.
at org.eclipse.osgi.framework.internal.core.BundleContextImpl.startActivator(BundleContextImpl.java:734)
at org.eclipse.osgi.framework.internal.core.BundleContextImpl.start(BundleContextImpl.java:683)
at org.eclipse.osgi.framework.internal.core.BundleHost.startWorker(BundleHost.java:381)
at org.eclipse.osgi.framework.internal.core.AbstractBundle.resume(AbstractBundle.java:390)
at org.eclipse.osgi.framework.internal.core.Framework.resumeBundle(Framework.java:1176)
at org.eclipse.osgi.framework.internal.core.StartLevelManager.resumeBundles(StartLevelManager.java:559)
at org.eclipse.osgi.framework.internal.core.StartLevelManager.resumeBundles(StartLevelManager.java:544)
at org.eclipse.osgi.framework.internal.core.StartLevelManager.incFWSL(StartLevelManager.java:457)
at org.eclipse.osgi.framework.internal.core.StartLevelManager.doSetStartLevel(StartLevelManager.java:243)
at org.eclipse.osgi.framework.internal.core.StartLevelManager.dispatchEvent(StartLevelManager.java:438)
at org.eclipse.osgi.framework.internal.core.StartLevelManager.dispatchEvent(StartLevelManager.java:1)
at org.eclipse.osgi.framework.eventmgr.EventManager.dispatchEvent(EventManager.java:230)
at org.eclipse.osgi.framework.eventmgr.EventManager$EventThread.run(EventManager.java:340)
Caused by: org.osgi.framework.BundleException: Missing required bundle: org.apache.felix.gogo.shell
at org.eclipse.equinox.console.command.adapter.Activator.startBundle(Activator.java:254)
at org.eclipse.equinox.console.command.adapter.Activator.start(Activator.java:242)
at org.eclipse.osgi.framework.internal.core.BundleContextImpl$1.run(BundleContextImpl.java:711)
at java.security.AccessController.doPrivileged(Native Method)
at org.eclipse.osgi.framework.internal.core.BundleContextImpl.startActivator(BundleContextImpl.java:702)
... 12 more
Root exception:
org.osgi.framework.BundleException: Missing required bundle: org.apache.felix.gogo.shell
at org.eclipse.equinox.console.command.adapter.Activator.startBundle(Activator.java:254)
at org.eclipse.equinox.console.command.adapter.Activator.start(Activator.java:242)
at org.eclipse.osgi.framework.internal.core.BundleContextImpl$1.run(BundleContextImpl.java:711)
at java.security.AccessController.doPrivileged(Native Method)
at org.eclipse.osgi.framework.internal.core.BundleContextImpl.startActivator(BundleContextImpl.java:702)
at org.eclipse.osgi.framework.internal.core.BundleContextImpl.start(BundleContextImpl.java:683)
at org.eclipse.osgi.framework.internal.core.BundleHost.startWorker(BundleHost.java:381)
at org.eclipse.osgi.framework.internal.core.AbstractBundle.resume(AbstractBundle.java:390)
at org.eclipse.osgi.framework.internal.core.Framework.resumeBundle(Framework.java:1176)
at org.eclipse.osgi.framework.internal.core.StartLevelManager.resumeBundles(StartLevelManager.java:559)
at org.eclipse.osgi.framework.internal.core.StartLevelManager.resumeBundles(StartLevelManager.java:544)
at org.eclipse.osgi.framework.internal.core.StartLevelManager.incFWSL(StartLevelManager.java:457)
at org.eclipse.osgi.framework.internal.core.StartLevelManager.doSetStartLevel(StartLevelManager.java:243)
at org.eclipse.osgi.framework.internal.core.StartLevelManager.dispatchEvent(StartLevelManager.java:438)
at org.eclipse.osgi.framework.internal.core.StartLevelManager.dispatchEvent(StartLevelManager.java:1)
at org.eclipse.osgi.framework.eventmgr.EventManager.dispatchEvent(EventManager.java:230)
at org.eclipse.osgi.framework.eventmgr.EventManager$EventThread.run(EventManager.java:340)!ENTRY org.eclipse.osgi 4 0 2013-10-03 12:07:46.356
!MESSAGE Bundle org.eclipse.equinox.console_1.0.100.v20130429-0953 [6] is not active.</small>!ENTRY org.eclipse.osgi 4 0 2013-10-03 12:07:46.361
!MESSAGE Could not start bundle: org.eclipse.equinox.console
!STACK 0
org.osgi.framework.BundleException: Could not start bundle: org.eclipse.equinox.console
at org.eclipse.osgi.framework.internal.core.ConsoleManager.checkForConsoleBundle(ConsoleManager.java:217)
at org.eclipse.core.runtime.adaptor.EclipseStarter.startup(EclipseStarter.java:298)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:177)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:636)
at org.eclipse.equinox.launcher.Main.basicRun(Main.java:591)
at org.eclipse.equinox.launcher.Main.run(Main.java:1450)
at org.eclipse.equinox.launcher.Main.main(Main.java:1426)
Caused by: org.osgi.framework.BundleException: Exception in org.eclipse.equinox.console.command.adapter.Activator.start() of bundle org.eclipse.equinox.console.
at org.eclipse.osgi.framework.internal.core.BundleContextImpl.startActivator(BundleContextImpl.java:734)
at org.eclipse.osgi.framework.internal.core.BundleContextImpl.start(BundleContextImpl.java:683)
at org.eclipse.osgi.framework.internal.core.BundleHost.startWorker(BundleHost.java:381)
at org.eclipse.osgi.framework.internal.core.AbstractBundle.start(AbstractBundle.java:300)
at org.eclipse.osgi.framework.internal.core.ConsoleManager.checkForConsoleBundle(ConsoleManager.java:215)
... 10 more
Caused by: org.osgi.framework.BundleException: Missing required bundle: org.apache.felix.gogo.shell
at org.eclipse.equinox.console.command.adapter.Activator.startBundle(Activator.java:254)
at org.eclipse.equinox.console.command.adapter.Activator.start(Activator.java:242)
at org.eclipse.osgi.framework.internal.core.BundleContextImpl$1.run(BundleContextImpl.java:711)
at java.security.AccessController.doPrivileged(Native Method)
at org.eclipse.osgi.framework.internal.core.BundleContextImpl.startActivator(BundleContextImpl.java:702)
... 14 more
Root exception:
org.osgi.framework.BundleException: Missing required bundle: org.apache.felix.gogo.shell
at org.eclipse.equinox.console.command.adapter.Activator.startBundle(Activator.java:254)
at org.eclipse.equinox.console.command.adapter.Activator.start(Activator.java:242)
at org.eclipse.osgi.framework.internal.core.BundleContextImpl$1.run(BundleContextImpl.java:711)
at java.security.AccessController.doPrivileged(Native Method)
at org.eclipse.osgi.framework.internal.core.BundleContextImpl.startActivator(BundleContextImpl.java:702)
at org.eclipse.osgi.framework.internal.core.BundleContextImpl.start(BundleContextImpl.java:683)
at org.eclipse.osgi.framework.internal.core.BundleHost.startWorker(BundleHost.java:381)
at org.eclipse.osgi.framework.internal.core.AbstractBundle.start(AbstractBundle.java:300)
at org.eclipse.osgi.framework.internal.core.ConsoleManager.checkForConsoleBundle(ConsoleManager.java:215)
at org.eclipse.core.runtime.adaptor.EclipseStarter.startup(EclipseStarter.java:298)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:177)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:636)
at org.eclipse.equinox.launcher.Main.basicRun(Main.java:591)
at org.eclipse.equinox.launcher.Main.run(Main.java:1450)
at org.eclipse.equinox.launcher.Main.main(Main.java:1426)

またエラーです。
今度はorg.apache.felix.gogo.shellが必要と言われてます。
先ほどと同じようにConfigurationから追加し、org.apache.felix.gogo.shellが依存しているBundleも追加します。
そしてRun!

start
osgi>

今度は実行できました!

ssと入力するとBundleの状態を見れるようです。

start
osgi> ss
"Framework is launched."id     State       Bundle
0     ACTIVE      org.eclipse.osgi_3.9.0.v20130529-1710
Fragments=1, 2
1     RESOLVED    org.eclipse.equinox.transforms.hook_1.0.401.v20130327-1442
Master=0
2     RESOLVED    org.eclipse.equinox.weaving.hook_1.0.200.v20130327-1442
Master=0
3     ACTIVE      com.misfrog.osgi.example1_1.0.0.qualifier
4     ACTIVE      org.apache.felix.gogo.runtime_0.10.0.v201209301036
5     ACTIVE      javax.servlet_3.0.0.v201112011016
6     ACTIVE      org.eclipse.equinox.console_1.0.100.v20130429-0953
7     ACTIVE      org.eclipse.osgi.services_3.3.100.v20130513-1956
8     ACTIVE      org.apache.felix.gogo.shell_0.10.0.v201212101605
osgi>

Add Required Bundlesしたのにエラーになったり、すんなりいかないですね。



次へ