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

Getting started with OSGi: Your first bundle

阅读更多
Over the next week or two, EclipseZone will be running a series of short posts on OSGi. Taken together they should form a smooth path into mastering the art of OSGi programming, but each post will introduce just one new technique and it should be possible to work through in under ten minutes. Also, we want to show how simple OSGi development can be, so we will not be using Eclipse for development - just a text editor and the basic command line tools will do. So, welcome to the "Getting started with OSGi" series.

Actually this first post will be a little longer than the others, because we need to set up a very basic working environment. Before getting started, we need an OSGi framework to run on. There are three open source implementations to choose from: Apache Felix , Knopflerfish , and Equinox . The code we're going to write will be identical no matter which one you choose, but the instructions for running it will be a little different. Since this is EclipseZone we will use Equinox, the runtime that Eclipse itself is built on. You can pull a copy of it right out of your existing Eclipse installation: just find the file org.eclipse.osgi_3.2.1.R32x_v20060919.jar and copy it to an empty directory (NB the version string might be a little different depending on what version of Eclipse you have). If you don't have a copy of Eclipse anywhere, then you can download just that Jar file from http://download.eclipse.org/eclipse/equinox/ .

To keep the commands short, let's rename the Jar file to equinox.jar . Now bring up a Command Prompt in our development directory and run the command

> java -jar equinox.jar -console



In a few seconds, the osgi> prompt should appear. Congratulations, you are now running OSGi!

The osgi> prompt gives us access to commands in Equinox to control the framework. If you like, type help to see a list of commands, and have a play with them. Done that? Now type ss . This is the most frequently used command; it stands for "short status" and it shows us the list of bundles that are installed, and what their current status is. (A "bundle" is a module in OSGi terminology. Or if you are an Eclipse developer, you may know them as plug-ins; bundles and plug-ins are basically the same things.)

Equinox should print out the following:

Framework is launched.

id      State       Bundle
0       ACTIVE      system.bundle_3.2.1.R32x_v20060919



This tells us that there is one bundle installed and active, and it is the System Bundle. This is a special bundle in OSGi that is always present, and it represents the framework itself.

Now, we're going to write our own bundle. In the same directory as before, create a file called HelloActivator.java and copy the following code into it:
import org.osgi.framework.*;
 
public class HelloActivator implements BundleActivator {
  public void start(BundleContext context) {
    System.out.println("Hello EclipseZone Readers!");
  }
 
  public void stop(BundleContext context) {
    System.out.println("Goodbye EclipseZone Readers!");
  }
}




A bundle also needs a manifest file that declares various metadata about the bundle, e.g. its name, version, etc. So create a file called HelloWorld.mf and copy the following text into it. Make very sure that this file ends with a blank line, otherwise the jar command line tool will truncate the file.
Manifest-Version: 1.0
Bundle-Name: HelloWorld
Bundle-Activator: HelloActivator
Bundle-SymbolicName: HelloWorld
Bundle-Version: 1.0.0
Import-Package: org.osgi.framework
 




Now open a new Command Prompt (because we want to leave OSGi running) and build the Jar with the following commands:


> javac -classpath equinox.jar HelloActivator.java

> jar -cfm HelloWorld.jar HelloWorld.mf HelloActivator.class



Going back into the OSGi console, type install file:HelloWorld.jar . The reply should be "Bundle id is 1" . Type ss again and you will see the following:

Framework is launched.

id      State       Bundle
0       ACTIVE      system.bundle_3.2.1.R32x_v20060919
1       INSTALLED   HelloWorld_1.0.0



Our HelloWorld bundle is installed... but it's not yet active. We'll look into what these states mean in a later post, but for now we just need to start the bundle by typing start 1 . The "1" is the ID of the bundle from the first column. When you do this you should see the message "Hello EclipseZone Readers!". Now type stop 1 and you will see "Goodbye EclipseZone Readers!". Repeat this until you get bored. Don't forget to do ss occasionally to see the state of the bundle changing.

What's happening here? Our code implements the BundleActivator interface, allowing the framework to notify us of important lifecycle events. When the bundle is started, the framework calls the start method, and when the bundle is stopped, the framework calls the stop method. The other thing going on here is the line in the manifest file "Bundle-Activator: HelloActivator" , which tells the framework which class in our bundle is the activator. Normally the name we give is a fully-qualified class name, but we were lazy and used the default package.

And that concludes our first installment. See you next time.
分享到:
评论

相关推荐

    Getting Started with OSGi_ Part1

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

    Getting Started with OSGi_ Part2

    实际上,这是与OSGi API交互的唯一方式,当bundle启动时,OSGi框架通过bundle的`BundleActivator`为每个bundle颁发一张“票”。 在本教程的这一部分,我们将研究`BundleContext`以及我们可以用它做什么。我们还将...

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

    OSGi框架的核心是其模块化系统,它将程序分割成一系列的“Bundle”,每个Bundle都是一个独立的模块,拥有自己的类加载器。这样做的好处是可以在运行时动态地添加、移除或者替换Bundle,而不需要停掉整个应用程序。...

    Getting Started with OSGi_ Part3

    OSGi框架在启动时会解析所有Bundle的依赖关系,并根据这些依赖关系决定哪些Bundle可以激活。如果一个Bundle依赖的其他Bundle尚未启动或不存在,那么这个Bundle将被标记为“等待”状态,直到其依赖得到满足。 五、...

    OSGi Technology 教程

    首先,"Getting Started with OSGi 1 Your First Bundle.pdf"是入门篇,讲解了如何创建你的第一个OSGi捆绑包(Bundle)。捆绑包是OSGi的基本模块单位,包含了可执行的Java代码和其他资源。开发者需要了解如何定义...

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

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

    OSGi with CAR-Bundle

    3. **Bundle作为组件**:每个Bundle可以被视为独立的组件,它们之间通过OSGi服务注册表进行交互,实现动态的依赖管理和服务发现。 4. **使用脚本管理组件**:引入脚本语言,如JavaScript或Groovy,可以更灵活地管理...

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

    2. **生命周期管理**:Bundle有启动、暂停、停止和更新四个状态,便于控制和管理服务的生命周期。 3. **类加载器隔离**:每个bundle有自己的类加载器,实现了类的隔离,避免了命名冲突。 4. **动态性**: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 ...

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

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

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

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

    tomcat嵌入OSGI容器

    3. **配置管理**:OSGI的配置管理允许动态配置Bundle,这需要在Tomcat的配置文件中进行相应的设置,以便在启动或运行时更新Bundle的配置。 4. **Web应用部署**:传统的WAR文件可以在OSGI环境中部署为Bundle。这通常...

    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_Service

    - **生命周期管理**:Bundle有启动、暂停、停止和更新等生命周期状态,可以通过OSGi框架动态地控制这些状态,实现热部署。 - **依赖管理**:OSGi框架负责管理Bundle间的依赖关系,确保正确版本的依赖被加载,并在...

    OSGI bundle

    **OSGI Bundle详解** OSGI(Open Service Gateway Initiative)是一种Java模块化系统,它定义了一种标准的方式来组织和管理Java应用程序的组件。OSGI的核心概念是bundle,它类似于Java的jar文件,但具有更强大的...

    基于Eclipse的Equinox框架开发OSGi Bundle应用

    【标题】基于Eclipse的Equinox框架开发OSGi Bundle应用 在Java世界中,OSGi(Open Services Gateway Initiative)是一种模块化系统,它允许开发者创建可独立更新和依赖管理的模块,即Bundle。Eclipse的Equinox是...

Global site tag (gtag.js) - Google Analytics