`
classtwo5367
  • 浏览: 39048 次
  • 性别: 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`以及我们可以用它做什么。我们还将...

    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,可以更灵活地管理...

    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应用程序以模块化的方式...

    OSGi原理与最佳实践pdf下载(完整版)

    OSGI原理与最佳实践的完整版,共12章 第1 章OSGi 简介 第2 章OSGi 框架简介 第3 章基于Spring-DM 实现Petstore 第4 章基于Apache CXF 实现分布式...第5 章构建OSGI Bundle Repositor'y 第6 章OSGi 规范解读 ……

    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是...

    Geting started with Equinox and OSGi

    2. **创建第一个Bundle**:在OSGi中,模块被称为Bundle。本教程将指导你如何创建并运行一个Bundle。首先,在Eclipse中创建一个新的插件项目作为Bundle。选择“File” > “New” > “Project…”;在弹出的对话框中,...

    OSGI bundle change listener

    **OSGI Bundle Change Listener** OSGi(Open Service Gateway Initiative)是一种Java模块化系统,它允许在运行时动态地发现、加载、卸载和更新服务。OSGi的核心是其框架,它管理一组称为bundle的模块。每个bundle...

    spring-boot-with-embeded-osgi:带有嵌入式OSGI框架的Spring Boot项目

    带有嵌入式OSGI的Spring Boot 这是一个嵌入了Felix OSGI框架的示例Spring Boot项目。 其他项目是API(接口和模型类)及其实现。 Spring Boot应用程序将这些程序包作为OSGI框架的额外程序包公开(以便能够使用公开的...

    osgi介绍osgi介绍

    3. **生命周期**:bundle有安装、解析、启动、暂停、停止和卸载等状态,通过OSGi框架进行管理。 4. **类加载器**:每个bundle有自己的类加载器,负责加载bundle内的类,避免类冲突。 5. **Repository**:存储bundle...

Global site tag (gtag.js) - Google Analytics