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 .
分享到:
相关推荐
深入理解OSGi:Equinox原理、应用与最佳实践.pdfOSGi应用开发
NULL 博文链接:https://zhouwendong006.iteye.com/blog/381680
NULL 博文链接:https://zhouwendong006.iteye.com/blog/408415
NULL 博文链接:https://zhouwendong006.iteye.com/blog/382882
深入理解OSGi:Equinox原理、应用与最佳实践源代码,同时附带equinox-SDK-3.8源代码。
深入理解OSGi:Equinox原理、应用与最佳实践,书本源代码
深入理解OSGi:Equinox原理、应用与最佳实践源码
《深入理解OSGi:Equinox原理、应用与最佳实践》一书的附赠光盘
《深入理解OSGi:Equinox原理、应用与最佳实践》自从1999年OSGi联盟成立以来,OSGi技术随着Java一起飞速发展,它已经成为一种被广泛认可的软件架构技术和方法,许多世界著名的IT企业都加入到OSGi的阵营之中,OSGi...
《深入理解OSGi:Equinox原理、应用与最佳实践》共14章,分4个部分。第一部分(第1 章):走近OSGi,主要介绍了什么是OSGi以及为什么要使用OSGi。第二部分(第2~4章):OSGi规范与原理,对最新的OSGi R5.0中的核心...
《深入理解OSGi:Equinox原理、应用与最佳实践》,作者周志明 / 谢小明,本书是原创Java技术图书领域继《深入理解Java虚拟机》后的又一实力之作,也是全球首本基于最新OSGi R5.0规范的著作。
《Eclipse RCP与Spring OSGi:技术详解与最佳实践》由资源的Eclipse专家亲自执笔,并得到了Eclipse官方技术社区的强烈推荐,权威性毋庸置疑!内容全面,系统讲解了利用Eclipse RCP和Spring OSGi开发大规模Java应用的...
You'll quickly learn how to use OSGi, through concise code examples and a set of best practices derived from the authors' experiences with real-world projects. Through the course of this book, you'...
全面解读OSGi规范,深刻揭示OSGi原理,详细讲解OSGi服务,系统地介绍Equinox框架的用法,并通过源代码分析其工作机制,包含大量可操作性极强的解决方案和最佳实践。
java web OSGi (Open Services Gateway Initiative) provides a programming... Java programmers who are familiar with SSH can experience the benefits of OSGi. Keywords: OSGi; Integration Framework; Java Web;
Getting Started with OSGi 教你如何开发OSGi服务.
NULL 博文链接:https://springsfeng.iteye.com/blog/1259203