`
classtwo5367
  • 浏览: 37462 次
  • 性别: 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.
分享到:
评论

相关推荐

    php入门留言板 php+access PHP语言基础

    【PHP】php入门留言板 php+access PHP语言基础 【实例简介】php入门留言板 php access php入门留言板 让你轻松学会php 基本语言结构.php连 access数据库的语法以及功能.php access 【核心代码】 文件清单 ├── admin.php ├── detail.php ├── images │ ├── arrow2.gif │ ├── arrow.gif │ ├── bg.gif │ ├── bottom-bg.gif │ ├── column.gif │ ├── dished_x.gif │ ├── favicon.ico │ ├── layout-bodybg.gif │ ├── layout-footer.gif │ ├── layout-top.gif │ ├── li-right.gif │ └── Thumbs.db ├── inc │ ├── config.php │ ├── conn.php │ └── data.mdb ├── index.php ├── sty

    关于C语言的学习代码和C语言的刷题代码.zip

    C语言诞生于美国的贝尔实验室,由丹尼斯·里奇(Dennis MacAlistair Ritchie)以肯尼斯·蓝·汤普森(Kenneth Lane Thompson)设计的B语言为基础发展而来,在它的主体设计完成后,汤普森和里奇用它完全重写了UNIX,且随着UNIX的发展,c语言也得到了不断的完善。为了利于C语言的全面推广,许多专家学者和硬件厂商联合组成了C语言标准委员会,并在之后的1989年,诞生了第一个完备的C标准,简称“C89”,也就是“ANSI C”,截至2020年,最新的C语言标准为2018年6月发布的“C18”。 [5] C语言之所以命名为C,是因为C语言源自Ken Thompson发明的B语言,而B语言则源自BCPL语言。 1967年,剑桥大学的Martin Richards对CPL语言进行了简化,于是产生了BCPL(Basic Combined Programming Language)语言。

    安卓图片上传和文件上传带jsp服务端源码.zip

    android 源码学习. 资料部分来源于合法的互联网渠道收集和整理,供大家学习参考与交流。本人不对所涉及的版权问题或内容负法律责任。如有侵权,请通知本人删除。感谢CSDN官方提供大家交流的平台

    物资管理系统项目源码.rar

    物资管理系统项目源码.rar是一个综合性的软件开发包,旨在为高校学生提供一个完整的框架和参考实现,以便他们能够进行毕业设计或课程设计。这个压缩包包含了多个关键组件,如用户认证、库存管理、订单处理、报表生成等模块,每个模块都配备了详细的文档和代码实例,确保学生可以快速理解并开始构建自己的物资管理系统。该系统采用了现代的软件架构理念,比如MVC模式,使得前后端分离,便于维护和升级。同时,它支持多种数据库系统,如MySQL、SQLite等,提供了数据持久化的灵活性。在安全性方面,系统实现了基于角色的访问控制,保障了操作的权限划分。此外,它还考虑了用户体验,界面友好,操作直观。对于即将步入职场的软件工程专业的学生而言,通过分析和扩展这个源码包中的项目,不仅可以锻炼他们的编程实践能力,还能帮助他们理解企业级应用的开发流程和标准。无论是作为学习资源还是实践平台,物资管理系统项目源码.rar都是一个宝贵的资料,有助于学生将理论知识转化为实际操作技能,为他们日后的职业发展奠定坚实的基础。问问助手:解决方案编制助手重新回答||

    可二次开发程序员表白代码.rar

    “可二次开发程序员表白代码.rar”是一个专为计算机专业的学生和编程爱好者设计的源码文件包,它旨在帮助用户通过编写和定制专属的表白程序来表达他们的情感。这个文件包不仅适合作为毕业设计或课程设计项目,而且也是一个绝佳的实践工具,用于提升编程技能和理解软件开发的全过程。该源码文件包含有多个模块,每个模块都经过精心设计,易于理解和修改,以适应不同的个性化需求。用户可以在这些模板的基础上进行二次开发,添加自己的创意元素,如特定的文本信息、背景音乐、动画效果等,使得表白程序更具个性和情感色彩。此外,源码文件包还附带详细的文档说明和注释,为初学者提供了丰富的学习资源。通过阅读和实践这些材料,学生能够深入理解编程语言的本质,提高解决实际问题的能力,并学会如何将理论知识应用到实际项目中。总之,“可二次开发程序员表白代码.rar”不仅是一个富有创意和技术挑战的项目,它还鼓励用户发挥想象力,用技术的方式传达爱意,是一份融合了情感与科技、教育与娱乐的独特礼物。问问助手:资深编程大师重新回答||

    数字同轴全息显微术中孪晶图像去除的神经网络方法matlab代码.zip

    1.版本:matlab2014/2019a/2021a 2.附赠案例数据可直接运行matlab程序。 3.代码特点:参数化编程、参数可方便更改、代码编程思路清晰、注释明细。 4.适用对象:计算机,电子信息工程、数学等专业的大学生课程设计、期末大作业和毕业设计。

    返回键退出程序的两种方式.zip

    android 源码学习. 资料部分来源于合法的互联网渠道收集和整理,供大家学习参考与交流。本人不对所涉及的版权问题或内容负法律责任。如有侵权,请通知本人删除。感谢CSDN官方提供大家交流的平台

    浮动窗口.zip

    android 源码学习. 资料部分来源于合法的互联网渠道收集和整理,供大家学习参考与交流。本人不对所涉及的版权问题或内容负法律责任。如有侵权,请通知本人删除。感谢CSDN官方提供大家交流的平台

    二维码扫描案例.zip

    android 源码学习. 资料部分来源于合法的互联网渠道收集和整理,供大家学习参考与交流。本人不对所涉及的版权问题或内容负法律责任。如有侵权,请通知本人删除。感谢CSDN官方提供大家交流的平台

    基于matlab的CDMA

    写出所有n=5的m序列发生器的特征多项式,画出电路结构,仿真生成m序列并运算,画出其自相关和互相关曲线。

    基于单片机的智能家居环境监控系统的设计,单片机能够检测家居环境并显示,根据数据智能预警

    当今家居生活中面临各种环境与健康安全问题,如空气湿度过低,容易让人患上呼吸系统的疾病;CO、甲醛等有害气体危害人体健康;天燃气泄漏引起的爆炸事故频发等。人们对高品质生活环境的追求越来越强烈,所以居住环境的各种参数得到了大家的广泛重视。随着智能化与信息化的快速发展,我们可以利用现代科技对家居环境进行监测及调整,使我们的居住体验更加美好。 本设计完成一个可以监测温湿度、有害气体以及非法入侵的智能家居监控系统,包括主控模块、传感器模块、显示模块、报警驱动模块等。 系统的控制核心是STC89C52单片机,通过DHT11传感器来监测室内温湿度,烟雾传感器MQ-2监测有害气体烟雾浓度,HC-SR501传感器用来监测人体信号,按键电路可以设置监测数据上下限阈值及人体红外监测布防状态,当超过阈值时,蜂鸣器和LED灯声光报警,同时通过继电器驱动相应电器,实时对家居环境进行调控。此外,通过LCD1602液晶屏显示实时温湿度、烟雾浓度等信息供人们实时了解家庭环境状况,从而保证家庭生活环境的安全与舒适。

    网易推出的AI智能翻译平台,支持音频、文档、图片、字幕等.txt

    网易推出的AI智能翻译平台,支持音频、文档、图片、字幕等.txt

    NC65 UAP65 主子单据开发 带审批流 详细笔记

    NC65 UAP65 主子单据开发 带审批流 详细笔记,共同学习,共同进步。

    机械臂的碰撞检测研究.pdf

    机械臂的碰撞检测研究.pdf

    c语言-c语言编程基础之leetcode题解第23题合并K个升序链表.zip

    c语言 c语言_c语言编程基础之leetcode题解第23题合并K个升序链表

    Swift代码转换指南(Swift Swift Code Convension Guide .)

    【Swift】说明:Swift Swift代码转换指南。 (Swift Swift Code Convension Guide .) 文件列表: CODE_OF_CONDUCT.md LICENSE 【Swift】说明:Swift Swift代码转换指南。

    不确定性移动机械臂的轨迹跟踪控制研究.doc

    不确定性移动机械臂的轨迹跟踪控制研究.doc

    玖祺企业官网_v2.1.3.zip

    玖祺企业官网_v2.1.3

    s7200课题一_图文.ppt

    s7200课题一_图文.ppt

    仿网易新闻listview加header图片滚动,上拉下拉刷新.zip

    android 源码学习. 资料部分来源于合法的互联网渠道收集和整理,供大家学习参考与交流。本人不对所涉及的版权问题或内容负法律责任。如有侵权,请通知本人删除。感谢CSDN官方提供大家交流的平台

Global site tag (gtag.js) - Google Analytics