OSGI入门:与OSGi框架进行交互

      上节课我们看了一个简单的Hello World Bundle,它在启动和停止的时候打印了一些信息。它继承了接口BundleActivator,并且实现了start和stop方法。我们来再看一下代码。特别是start方法和stop方法的实现,你是否注意到我们在方法里传入了一个参数——BundleContext。在这次的课程里,我们就来看一下BundleContext,以及我们可以用它来做什么。

 

      BundleContext是OSGi框架送给我们的Bundle的一张不可思议的门票。当我们需要同OSGi框架进行交互的时候,我们需要使用BundleContext。事实上,这是唯一同OSGi的API进行交互的途径。并且OSGi框架对每一个Bundle发放了门票,以便每一个Bundle在启动的可以通过BundleActivator与框架进行交互。

 

      如果你的Equinox也就是OSGi并没有停止的话,那你不需要启动它。如果它没有运行,请记住启动使用个这个命令将它启动(现将工作目录转移到OSGi测试目录里,详细请看第一部分)。

Cmd命令代码 复制代码
  1. java -jar equinox.jar -console  
java -jar equinox.jar -console

 

      根据提示符输入“ss”,然后你将会看到上次已经安装的Hello World Bundle。即使你已经关闭了OSGi服务并且又启动了它,你依然会看到这个情况。这是因为OSGi框架会自动将它的启动到运行的状态保留在配置文件中。为了证明这个,我们将写一个Bundle,这个Bundle将会自动寻找并且卸载Hello World Bundle。我们可以在控制台里使用“uninstall”命令轻松的实现这个功能。并且我们将学习如何利用OSGi的API进行编程。所以,现在建立一个名叫“HelloWorldKiller”的Java文件,并且将以下代码复制粘贴到里面。

Java代码 复制代码
  1. import org.osgi.framework.*;   
  2.   
  3. public class HelloWorldKiller implements BundleActivator {   
  4.     public void start(BundleContext context) {   
  5.         System.out.println("HelloWorldKiller searching...");   
  6.         Bundle[] bundles = context.getBundles();   
  7.         for (int i = 0; i < bundles.length; i++) {   
  8.             if ("HelloWorld".equals(bundles[i].getSymbolicName())) {   
  9.                 try {   
  10.                     System.out.println("Hello World found, " + "destroying!");   
  11.                     bundles[i].uninstall();   
  12.                     return;   
  13.                 } catch (BundleException e) {   
  14.                     System.err.println("Failed: " + e.getMessage());   
  15.                 }   
  16.             }   
  17.         }   
  18.         System.out.println("Hello World bundle not found");   
  19.     }   
  20.   
  21.     public void stop(BundleContext context) {   
  22.         System.out.println("HelloWorldKiller shutting down");   
  23.     }   
  24. }  
import org.osgi.framework.*;

public class HelloWorldKiller implements BundleActivator {
	public void start(BundleContext context) {
		System.out.println("HelloWorldKiller searching...");
		Bundle[] bundles = context.getBundles();
		for (int i = 0; i < bundles.length; i++) {
			if ("HelloWorld".equals(bundles[i].getSymbolicName())) {
				try {
					System.out.println("Hello World found, " + "destroying!");
					bundles[i].uninstall();
					return;
				} catch (BundleException e) {
					System.err.println("Failed: " + e.getMessage());
				}
			}
		}
		System.out.println("Hello World bundle not found");
	}

	public void stop(BundleContext context) {
		System.out.println("HelloWorldKiller shutting down");
	}
}

 

      然后我们同样创建一个名叫HelloWorldKiller.MF的manifest文件。并将以下代码粘贴到里面。再次强调一下,在结尾处一定要留一个空行,否则程序无法运行。

Helloworldkiller.mf代码 复制代码
  1. Manifest-Version: 1.0  
  2. Bundle-ManifestVersion: 2  
  3. Bundle-Name: DBLoginBundle Plug-in   
  4. Bundle-SymbolicName: Login_DBLoginBundle   
  5. Bundle-Version: 1.0.0  
  6. Bundle-Activator: HelloWorldKiller   
  7. Bundle-ActivationPolicy: lazy   
  8. Bundle-RequiredExecutionEnvironment: JavaSE-1.6  
  9. Import-Package: org.osgi.framework;version="1.3.0"  
Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: DBLoginBundle Plug-in
Bundle-SymbolicName: Login_DBLoginBundle
Bundle-Version: 1.0.0
Bundle-Activator: HelloWorldKiller
Bundle-ActivationPolicy: lazy
Bundle-RequiredExecutionEnvironment: JavaSE-1.6
Import-Package: org.osgi.framework;version="1.3.0"

 

      然后使用命令对这个Bundle进行打包。

Java代码 复制代码
  1. > javac -classpath equinox.jar HelloWorldKiller.java   
  2. > jar -cfm HelloWorldKiller.jar HelloWorldKiller.mf HelloWorldKiller.class  
> javac -classpath equinox.jar HelloWorldKiller.java
> jar -cfm HelloWorldKiller.jar HelloWorldKiller.mf HelloWorldKiller.class

 

       打包完后,我们回到OSGi控制台,使用命令“install file:HelloWorldKiller.jar”(“file”后是这个jar文件命)安装这个Bundle。然后使用“ss”查看目前的Bundle状态。大致情况如下:

Cmd控制台显示代码 复制代码
  1. id     State              Bundle   
  2. 0      ACTIVE           system.bundle_3.2.1.R32x_v20060919   
  3. 1      ACTIVE           HelloWorld_1.0.0  
  4. 2      INSTALLED     HelloWorldKiller_1.0.0  
id     State              Bundle
0      ACTIVE           system.bundle_3.2.1.R32x_v20060919
1      ACTIVE           HelloWorld_1.0.0
2      INSTALLED     HelloWorldKiller_1.0.0

 

      然后我们通过运行“start 2”这个命令来启动Hello World Bundle。你将会在控制台看到如下信息:

Cmd控制台输出代码 复制代码
  1. HelloWorldKiller searching...   
  2. Hello World found, destroying!   
  3. Goodbye EclipseZone Readers!  
HelloWorldKiller searching...
Hello World found, destroying!
Goodbye EclipseZone Readers!

 

      注意到了最后一行的输出吗?那是我们最初的Hello World Bundle。因为在我们运行Hello World Killer之前,它是处于运行状态的。当它被卸载的时候,先要被停止。因此,BundleActivator的stop方法会被执行。

然后我们再次输入“ss”查看一下Bundle的运行状态。HelloWorld的Bundle消失了。变成了下面的情况:

Cmd控制台输出代码 复制代码
  1. id            State             Bundle   
  2. 0             ACTIVE          system.bundle_3.2.1.R32x_v20060919   
  3. 2             ACTIVE          HelloWorldKiller_1.0.0  
id            State             Bundle
0             ACTIVE          system.bundle_3.2.1.R32x_v20060919
2             ACTIVE          HelloWorldKiller_1.0.0

 

      你可能通过上面的讲述惊奇的发现那里存在一个安全问题:我们可以通过任何一个Bundle卸载其他的Bundle。我们很幸运,OSGi有套全面的安全措施。这可以让我们在与OSGi框架进行交互的时候,灵活的控制每一个细节。简单的来说,你可以通过建立一个特殊的管理员Bundle来限制卸载其他Bundle的行为。然而,获得安全保障的工作最重要的是配置文件的定义。而我们这一系列的课程把焦点集中在代码上。

      以上就是这节课的全部内容。在下节课中,我们来看一下BundleContext接口并且看看我们能用它做什么。比如,我们是否可以用installBundle方法来进行一个Bundle的安装,或者查看运行中的Bundle的信息,并且打印出Bundle最近的修改时间和内容。为了更好的进行下堂课的学习,请先查阅一下OSGi Release 4 API的帮助文档。

  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值