`
classtwo5367
  • 浏览: 38579 次
  • 性别: Icon_minigender_1
  • 来自: Cork
最近访客 更多访客>>
社区版块
存档分类
最新评论

Getting started with OSGi: Interacting with the Framework

阅读更多
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

    在标题“Getting Started with OSGi Part1”中,指明了这是一个关于OSGi入门的系列文章中的第一部分。描述部分虽然为“NULL”,但可以从给定的内容中提取出文章的重点信息。标签“源码工具”可能意味着在文章的系列...

    深入理解OSGi:Equinox原理、应用与最佳实践.pdf

    OSGi(Open Service Gateway Initiative)是一个定义了Java应用程序如何组织和模块化以及如何动态发现、启动、停止、更新这些模块化组件的规范。Equinox是OSGi规范的一个实现,它是由Eclipse基金会开发的。本文将...

    Getting Started with OSGi_ Part2

    OSGi(Open Services Gateway initiative)是一种Java框架,它定义了服务加载和模块化应用的标准方式。OSGi技术广泛应用于企业级应用开发中,尤其是在Eclipse插件开发和Java EE应用服务器中。OSGi规范定义了如何在...

    Getting Started with OSGi_ Part3

    《OSGi初识系列教程——第三部分:模块间的依赖关系》 在OSGi(Open Service Gateway Initiative)框架中,理解并管理模块间的依赖关系是至关重要的。本篇教程将深入探讨这一主题,帮助开发者们更好地掌握OSGi环境...

    OSGi Technology 教程

    "Getting Started with OSGi 2 Interacting with the Framework.pdf"接着介绍了如何与OSGi框架交互,包括启动、停止、更新和安装捆绑包。此外,还会讨论如何使用OSGi框架API来管理和控制服务生命周期。 "Getting ...

    深入理解OSGi:Equinox原理、应用与最佳实践源代码+equinox-SDK-3.8源代码

    本资源包括两部分:《深入理解OSGi:Equinox原理、应用与最佳实践》的源代码和equinox-SDK-3.8的源代码。 深入理解OSGi这本书提供了对OSGi,特别是Equinox实现的全面洞察。书中可能涵盖以下几个知识点: 1. **OSGi...

    深入理解OSGi:Equinox原理、应用与最佳实践

    ### 深入理解OSGi:Equinox原理、应用与最佳实践 #### OSGi概述 OSGi(Open Service Gateway Initiative)是一种模块化系统和服务组件模型,它为Java平台提供了一种动态部署、管理和更新应用程序和服务的方法。...

    《深入理解OSGi:Equinox原理、应用与最佳实践》

    《深入理解OSGi:Equinox原理、应用与最佳实践》自从1999年OSGi联盟成立以来,OSGi技术随着Java一起飞速发展,它已经成为一种被广泛认可的软件架构技术和方法,许多世界著名的IT企业都加入到OSGi的阵营之中,OSGi...

    深入理解OSGi:Equinox原理、应用与最佳实践.zip

    在《深入理解OSGi:Equinox原理、应用与最佳实践》这本书中,作者深入探讨了OSGi的核心概念、Equinox的工作原理以及如何在实际项目中应用OSGi。这本书的源码可能是为了辅助读者理解和实践书中所讲解的内容。 **OSGi...

    深入理解OSGi:Equinox原理、应用与最佳实践,书本源代码

    在深入理解OSGi:Equinox原理、应用与最佳实践中,我们可以学习到以下几个关键知识点: 1. **模块化编程**:OSGi的核心是模块化,它将应用程序划分为独立的单元,称为服务或bundle。每个bundle都有自己的类路径,...

    《深入理解OSGi:Equinox原理、应用与最佳实践》迷你书

    本书《深入理解OSGi:Equinox原理、应用与最佳实践》深入剖析了OSGi技术的原理和应用,着重介绍了基于OSGi R5.0规范的内容,并结合了Equinox框架的实践经验,旨在帮助读者更好地理解和应用OSGi技术。 本书共分为四...

    《深入理解OSGi:Equinox原理、应用与最佳实践》附赠光盘

    《深入理解OSGi:Equinox原理、应用与最佳实践》这本书是关于OSGi技术的一部权威著作,其附赠光盘包含丰富的学习资源,旨在帮助读者深入掌握OSGi的精髓,特别是Equinox实现的细节。OSGi(Open Services Gateway ...

    SpringDM笔记28-Spring And OSGi:Layers of Integration

    标题中的"SpringDM笔记28-Spring And OSGi:Layers of Integration"表明这是一篇关于Spring框架与OSGi(Open Service Gateway Initiative)集成的详细笔记。OSGi是一种模块化系统,它允许Java应用程序以模块化的方式...

    Eclipse RCP与Spring OSGi技术详解与最佳实践

    《Eclipse RCP与Spring OSGi:技术详解与最佳实践》由资源的Eclipse专家亲自执笔,并得到了Eclipse官方技术社区的强烈推荐,权威性毋庸置疑!内容全面,系统讲解了利用Eclipse RCP和Spring OSGi开发大规模Java应用的...

    深入理解OSGi:Equinox原理、应用与最佳实践.mobi

    全面解读OSGi规范,深刻揭示OSGi原理,详细讲解OSGi服务,系统地介绍Equinox框架的用法,并通过源代码分析其工作机制,包含大量可操作性极强的解决方案和最佳实践。

    org.osgi.framework.BundleException-glassfish

    标题中的"org.osgi.framework.BundleException-glassfish"指的是在使用Glassfish应用服务器时遇到的OSGi框架相关的异常。OSGi(Open Services Gateway Initiative)是一种Java模块化系统,它允许动态地管理和部署...

Global site tag (gtag.js) - Google Analytics