OSGi#1:Equinox 初探

来源:互联网 发布:淘宝直通车 如何出价 编辑:程序博客网 时间:2024/05/19 06:50

From a code point of view, Equinox is an implementation of the OSGi core framework specification, a set of bundles that implement various optional OSGi services and other infrastructure for running OSGi-based systems.

上面是Equinox官网的描述,Equinox是一个OSGi实现,具体就不赘述了,下面来看看Equinox该怎么玩,直接上代码。

按照惯例,来个Hello World的栗子。先打出一个输出Hello World的bundle,

package me.kisimple.just4fun.osgi;import org.osgi.framework.BundleActivator;import org.osgi.framework.BundleContext;public class IBundleActivator implements BundleActivator {    @Override    public void start(BundleContext bundleContext) throws Exception {        System.out.println("Hello,World.");    }    @Override    public void stop(BundleContext bundleContext) throws Exception {        System.out.println("Goodbye,World.");    }}

MANIFEST.MF这么写,

Manifest-Version: 1.0Bundle-ManifestVersion: 2Bundle-Name: HelloWorldBundle-SymbolicName: me.kisimple.just4fun.osgi.HelloWorldBundle-Version: 1.0.0Bundle-Activator: me.kisimple.just4fun.osgi.IBundleActivatorBundle-Vendor: KISIMPLEBundle-Localization: systembundleImport-Package: org.osgi.framework

然后打包,

> jar cvfm me.kisimple.just4fun_1.0.0.jar MANIFEST.MF me\kisimple\just4fun\osgi\IBundleActivator.class

接下来需要去官网下载Equinox的SDK,我下的是equinox-SDK-LunaSR2.zip。然后参考官网的quick start文档,我们新建一个container目录,该目录有如下文件,

E:\Projects> tree container /FE:\PROJECTS\CONTAINER│  org.eclipse.equinox.common_3.6.200.v20130402-1505.jar│  org.eclipse.osgi_3.10.2.v20150203-1939.jar│  org.eclipse.update.configurator_3.3.300.v20140518-1928.jar│├─configuration│      config.ini│└─plugins        me.kisimple.just4fun_1.0.0.jar        org.apache.felix.gogo.command_0.10.0.v201209301215.jar        org.apache.felix.gogo.runtime_0.10.0.v201209301036.jar        org.apache.felix.gogo.shell_0.10.0.v201212101605.jar        org.eclipse.equinox.console_1.1.0.v20140131-1639.jar

除了me.kisimple.just4fun_1.0.0.jarorg.eclipse.update.configurator_3.3.300.v20140518-1928.jar,其他jar包都是从下载下来的SDK中可以拿到的,me.kisimple.just4fun_1.0.0.jar是上面我们打的输出Hello World的bundle,而org.eclipse.update.configurator_3.3.300.v20140518-1928.jar则是从eclipse(忘记安装的是哪个版本了>_<)的安装目录下的plugins目录中获取(本身eclipse也使用了Equinox,所以也有configuration/config.ini文件和plugins目录)。

configuration/config.ini如下,

osgi.bundles=org.eclipse.equinox.common@2:start, org.eclipse.update.configurator@3:start

alright,接下来就可以启动Equinox了,

E:\Projects\container>java -jar org.eclipse.osgi_3.10.2.v20150203-1939.jar -consoleosgi> _

org.eclipse.osgi_3.10.2.v20150203-1939.jarMain-Classorg.eclipse.core.runtime.adaptor.EclipseStarter,除了-console,还支持一些其他选项。源码可以使用git获得,

> git clone http://git.eclipse.org/gitroot/equinox/rt.equinox.framework.git

接下来可以在命令行中启动我们的bundle了。先用ss命令看下bundle的情况,

osgi> ss"Framework is launched."id      State       Bundle0       ACTIVE      org.eclipse.osgi_3.10.2.v20150203-19391       ACTIVE      org.eclipse.equinox.common_3.6.200.v20130402-15052       ACTIVE      org.eclipse.update.configurator_3.3.300.v20140518-19283       RESOLVED    me.kisimple.just4fun.osgi.HelloWorld_1.0.04       ACTIVE      org.apache.felix.gogo.command_0.10.0.v2012093012155       ACTIVE      org.apache.felix.gogo.runtime_0.10.0.v2012093010366       ACTIVE      org.apache.felix.gogo.shell_0.10.0.v2012121016057       ACTIVE      org.eclipse.equinox.console_1.1.0.v20140131-1639

启动我们的bundle,

osgi> start 3Hello,World.

这时候再用ss命令可以看到我们的bundle的状态变成了ACTIVE了,

3       ACTIVE      me.kisimple.just4fun.osgi.HelloWorld_1.0.0

停掉我们的bundle,

osgi> stop 3Goodbye,World.

这时候bundle的状态就会又回到了RESOLVED

参考资料

  • http://eclipse.org/equinox/resources.php
  • http://eclipse.org/equinox/documents/quickstart-framework.php
  • http://www.javaworld.com/article/2077837/java-se/hello–osgi–part-1–bundles-for-beginners.html
  • http://developer.51cto.com/art/200909/152209.htm
  • http://moi.vonos.net/java/osgi-classloaders/
0 1
原创粉丝点击