Welcome back to the EclipseZone OSGi tutorial series.
Last time we looked at a simple Hello World bundle that printed a message when starting and stopping. It did that by implementing the BundleActivator interface and providing start and stop methods. Take another look at the code now, in particular the method signature of start and stop , and you'll notice that we were passed a parameter, the BundleContext . In this installment of the tutorial we will be looking at BundleContext and what we can do with it.
BundleContext is a magic ticket that the OSGi framework passes to our bundle. When code needs to interact with the framework in any way, you will use the BundleContext . In fact this is the only way to interact with the OSGi API, and the framework issues one of these tickets to each bundle through its BundleActivator when the bundle is started.
If you still have Equinox running from last time then you don't need to restart it. If it's not running, then remember the command to start it is
> java -jar equinox.jar -console
Type ss at the prompt and you should see that the Hello World bundle from last time is still installed. That's the case even if you have shut down and restarted Equinox since then, because the OSGi framework persists its state between runs.
For this exercise we will write a bundle that searches out and uninstalls Hello World. We could do this easily from the console using the uninstall command, but we want to see how it can be done programmatically using the OSGi API. So, create a new file called HelloWorldKiller.java and copy in the following code:
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");
}
}
Now create the manifest. Again, remember the blank line at the end is very important. Copy the following into HelloWorldKiller.mf:
Manifest-Version: 1.0
Bundle-Name: HelloWorldKiller
Bundle-Activator: HelloWorldKiller
Bundle-SymbolicName: HelloWorldKiller
Bundle-Version: 1.0.0
Import-Package: org.osgi.framework
Now compile and build the Jar:
> javac -classpath equinox.jar HelloWorldKiller.java
> jar -cfm HelloWorldKiller.jar HelloWorldKiller.mf HelloWorldKiller.class
Back at the OSGi console, install the new bundle using install file:HelloWorldKiller.jar , and then type ss . The status listing should now look like this:
id State Bundle
0 ACTIVE system.bundle_3.2.1.R32x_v20060919
1 ACTIVE HelloWorld_1.0.0
2 INSTALLED HelloWorldKiller_1.0.0
Let's run the Hello World Killer by typing start 2 . You should see the following output:
HelloWorldKiller searching...
Hello World found, destroying!
Goodbye EclipseZone Readers!
Notice that the last line of output comes from our original Hello World bundle. Because it was in the active state before we ran the Killer, it had to be stopped before being uninstalled, and that caused the stop method of its BundleActivator to run.
Taking another look at the output of ss , Hello World has disappeared:
id State Bundle
0 ACTIVE system.bundle_3.2.1.R32x_v20060919
2 ACTIVE HelloWorldKiller_1.0.0
You might wonder if there is a security problem here. It appears that any bundle can uninstall any other bundle! Fortunately OSGi has a comprehensive security layer which gives fine-grained control over all interaction with the framework, so for example you could limit the right to uninstall bundles to a particular "management" bundle. However, getting security working is mostly a configuration issue, and in this series we're going to focus on the code.
That's it for this installment. Until next time, why not take a look at the BundleContext interface and see what else you can do with it? For example, you could try programmatically installing a new bundle using the installBundle method. Or you could get a list of all the currently installed bundles and print out the time and date they were last modified. To help you get started, check out the Javadocs for the OSGi Release 4 APIs .
分享到:
相关推荐
在标题“Getting Started with OSGi Part1”中,指明了这是一个关于OSGi入门的系列文章中的第一部分。描述部分虽然为“NULL”,但可以从给定的内容中提取出文章的重点信息。标签“源码工具”可能意味着在文章的系列...
OSGi(Open Services Gateway initiative)是一种Java框架,它定义了服务加载和模块化应用的标准方式。OSGi技术广泛应用于企业级应用开发中,尤其是在Eclipse插件开发和Java EE应用服务器中。OSGi规范定义了如何在...
《OSGi初识系列教程——第三部分:模块间的依赖关系》 在OSGi(Open Service Gateway Initiative)框架中,理解并管理模块间的依赖关系是至关重要的。本篇教程将深入探讨这一主题,帮助开发者们更好地掌握OSGi环境...
"Getting Started with OSGi 2 Interacting with the Framework.pdf"接着介绍了如何与OSGi框架交互,包括启动、停止、更新和安装捆绑包。此外,还会讨论如何使用OSGi框架API来管理和控制服务生命周期。 "Getting ...
本资源包括两部分:《深入理解OSGi:Equinox原理、应用与最佳实践》的源代码和equinox-SDK-3.8的源代码。 深入理解OSGi这本书提供了对OSGi,特别是Equinox实现的全面洞察。书中可能涵盖以下几个知识点: 1. **OSGi...
标题中的"SpringDM笔记28-Spring And OSGi:Layers of Integration"表明这是一篇关于Spring框架与OSGi(Open Service Gateway Initiative)集成的详细笔记。OSGi是一种模块化系统,它允许Java应用程序以模块化的方式...
《Eclipse RCP与Spring OSGi:技术详解与最佳实践》由资源的Eclipse专家亲自执笔,并得到了Eclipse官方技术社区的强烈推荐,权威性毋庸置疑!内容全面,系统讲解了利用Eclipse RCP和Spring OSGi开发大规模Java应用的...
OSGI原理与最佳实践的完整版,共12章 第1 章OSGi 简介 第2 章OSGi 框架简介 第3 章基于Spring-DM 实现Petstore 第4 章基于Apache CXF 实现分布式Petstore 第5 章构建OSGI Bundle Repositor'y 第6 章OSGi 规范解读 ...
标题中的"org.osgi.framework.BundleException-glassfish"指的是在使用Glassfish应用服务器时遇到的OSGi框架相关的异常。OSGi(Open Services Gateway Initiative)是一种Java模块化系统,它允许动态地管理和部署...
osgi的jar包,第一次上传,有需要的可以自取,联网下载很慢,希望对你们有帮助,偶然间遇到了
**OSGi服务详解** OSGi(Open Service Gateway Initiative)是一种Java模块化系统,它为创建、部署和管理软件组件提供了一种强大而灵活的方式。在Java世界中,OSGi被视为解决大型复杂应用系统中模块化问题的有效...
带有嵌入式OSGI的Spring Boot 这是一个嵌入了Felix OSGI框架的示例Spring Boot项目。 其他项目是API(接口和模型类)及其实现。 Spring Boot应用程序将这些程序包作为OSGI框架的额外程序包公开(以便能够使用公开的...
The "Instant OSGi Starter" is an essential guide for developers who want to get started with modular development using OSGi. Written by Johan Edstrom and Jamie Goodyear, this book covers the ...
教程-osgi OSGI 示例 设置 设置 Git、Ant 和 Maven 查看示例。 $ git clone https://github.com/danidemi/tutorial-osgi.git $ cd tutorial-osgi 只需在项目文件夹中运行ant ,这将指示 Ant 下载一些额外的库。 ...
标题:开始使用Equinox与OSGi 描述:在Dzone论坛上发现的一个快速入门卡片教程,为初学者提供了一瞥。 知识点: 1. **Equinox简介**:Equinox是一个高度模块化、动态的Java运行环境,基于OSGi框架规范构建。它...