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.jar
跟org.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> _
然后就可以在命令行中启动我们的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
。
原文地址:http://blog.csdn.net/kisimple/article/details/45182773