OSGi#1:Equinox 初探

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.0
Bundle-ManifestVersion: 2
Bundle-Name: HelloWorld
Bundle-SymbolicName: me.kisimple.just4fun.osgi.HelloWorld
Bundle-Version: 1.0.0
Bundle-Activator: me.kisimple.just4fun.osgi.IBundleActivator
Bundle-Vendor: KISIMPLE
Bundle-Localization: systembundle
Import-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 /F
E:\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 -console
osgi> _

org.eclipse.osgi_3.10.2.v20150203-1939.jar Main-Class org.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       Bundle
0       ACTIVE      org.eclipse.osgi_3.10.2.v20150203-1939
1       ACTIVE      org.eclipse.equinox.common_3.6.200.v20130402-1505
2       ACTIVE      org.eclipse.update.configurator_3.3.300.v20140518-1928
3       RESOLVED    me.kisimple.just4fun.osgi.HelloWorld_1.0.0
4       ACTIVE      org.apache.felix.gogo.command_0.10.0.v201209301215
5       ACTIVE      org.apache.felix.gogo.runtime_0.10.0.v201209301036
6       ACTIVE      org.apache.felix.gogo.shell_0.10.0.v201212101605
7       ACTIVE      org.eclipse.equinox.console_1.1.0.v20140131-1639

启动我们的bundle,
osgi> start 3
Hello,World.

这时候再用 ss 命令可以看到我们的bundle的状态变成了 ACTIVE 了,
3       ACTIVE      me.kisimple.just4fun.osgi.HelloWorld_1.0.0

停掉我们的bundle,
osgi> stop 3
Goodbye,World.

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

参考资料

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值