使用NetBeans6开发OSGi应用(3)——整合Knopflerfish![88250原创]
生活随笔
收集整理的這篇文章主要介紹了
使用NetBeans6开发OSGi应用(3)——整合Knopflerfish![88250原创]
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
轉載請保留作者信息:
作者:88250
Blog:http:/blog.csdn.net/DL88250
MSN & Gmail & QQ:DL88250@gmail.com
摘要
上一次,我們編寫了兩個Bundles,一個是服務提供商,一個是使用服務的客戶 ,運行得還不錯 :-)這一次,我們先簡要分析一下KF(Knopflerfish)框架的設計,學習應用程序框架的設計。最后,結合上一次文章結尾時提到了關于控制KF框架、讓OSGi服務于我們的應用的問題,今天就圍繞這些內容展開。
關于Knopflerfish框架的設計
在開始,我們將看一下KF框架的設計。Main
在閱讀了KF框架的一些代碼后,從KF框架的主程序入手(org.knopflerfish.framework.Main)我們可以看出,KF除了實現OSGi規范,實現了自己的framework(org.knopflerfish.framework.Framework)外,主要是圍繞它的框架啟動類:org.knopflerfish.framework.Main做了一系列的鋪墊:比如所實現的OSGi規范的版本(我的版本是OSGi 4.0.6版本),存儲Bundle的文件位置,操作系統版本,等等。這個Main類,主要做的是命令行參數(啟動參數)的處理,因為KF框架啟動的時候可以用xargs文件(把所有參數寫成一個文件),所以處理上比較繁瑣。在Main下面的handleArgs方法可以看出,所有的Bundles的基本操作(start、stop、install、etc.)都是調用org.knopflerfish.framework.Framework這個類實現的,這個類就是KF框架的基本實現,已經把功能封裝地相當好用了!
那我們應該直接使用Framework類嗎?
我們應該直接使用KF實現的Framework類,但是整個框架的啟動鋪墊是很繁瑣的。從Main還有其相關的Utils的代碼量就可以看出。在整個框架啟動之前,要做一系列的鋪墊。這里,結合我自己的項目,由于時間比較緊了,再重寫這些可能時間不允許,所以我選擇的是改寫Main,把那個Main類作為自己應用框架的一個對KF的基本封裝。具體做法就是委托Framework類的一些方法,暴露這些方法在Main外部。
總之,要基于Knopflerfish,完成自己的一個應用框架還是比較簡單的 :-)
讓我們簡單實踐一下!
準備
同上一次 :-)開工:
1. 創建工程
打開NB6,創建一個普通Java應用工程——MyOSGiFramework:注意那個version文件,這個文件是指KF實現的OSGi的版本,在OSGi框架實現里,有一個Version類,用于版本的管理。要在我們的src目下建立一個version文件。
2.改寫Knopflerfish的Main類
添加對Bundle的基本操作到Main類里,達到封裝KF框架的目的。下面是添加的一些示例方法:/**
?????*?Get?the?bundle?context?used?by?the?system?bundle.
?????*?@return?system?<code>BundleContext</code>
?????*/
????public?static?BundleContext?getBundleContext()?{
????????return?framework.getSystemBundleContext();
????}
????/**
?????*?Start?a?bundle.
?????*?@param?id?Id?of?bundle?to?start
?????*/
????public?static?void?startBundle(long?id)?{
????????try?{
????????????framework.startBundle(id);
????????}?catch?(BundleException?ex)?{
????????????Logger.getLogger(Main.class.getName()).log(Level.SEVERE,?null,?ex);
????????}
????}
????/**
?????*?Stop?a?bundle.
?????*?@param?id?Id?of?bundle?to?stop
?????*/
????public?static?void?stopBundle(long?id)?{
????????try?{
????????????framework.stopBundle(id);
????????}?catch?(BundleException?ex)?{
????????????Logger.getLogger(Main.class.getName()).log(Level.SEVERE,?null,?ex);
????????}
????}
這里,做的工作就是“純”委托。
編寫我們對Main的測試類:
/*
?*?@(#)MainTest.java
?*?
?*?This?program?is?free?software;?you?can?redistribute?it?and/or?modify
?*?it?under?the?terms?of?the?GNU?General?Public?License?as?published?by
?*?the?Free?Software?Foundation;?either?version?3?of?the?License,?or
?*?(at?your?option)?any?later?version.
?*?
?*?This?program?is?distributed?in?the?hope?that?it?will?be?useful,
?*?but?WITHOUT?ANY?WARRANTY;?without?even?the?implied?warranty?of
?*?MERCHANTABILITY?or?FITNESS?FOR?A?PARTICULAR?PURPOSE.??See?the
?*?GNU?Library?General?Public?License?for?more?details.
?*?
?*?You?should?have?received?a?copy?of?the?GNU?General?Public?License
?*?along?with?this?program;?if?not,?write?to?the?Free?Software
?*?Foundation,?Inc.,?59?Temple?Place?-?Suite?330,?Boston,?MA?02111-1307,?USA.
?*/
package?myosgiframework;
import?org.knopflerfish.framework.Main;
import?org.osgi.framework.Bundle;
import?org.osgi.framework.BundleContext;
/**
?*?A?test?case?of?<code>org.knopflerfish.framework.Main</code>.
?*?<p>
?*?Base?on?knopflerfish?framework,?modify?some?source?in?<code>Main</code>,
?*?annotate?these?modifications?with?<code>@since</code>?mark,?
?*?mark?as?<tt>@since?4.0.6</tt>.
?*?</p>
?*?@author?88250
?*?@version?1.0.0.0,?Feb?15,?2008
?*/
public?class?MainTest?{
????/**
?????*?Program?entry?point.
?????*?@param?args?should?be?<code>null</code>
?????*/
????public?static?void?main(String[]?args)?{
????????Main.main(args);
????????Main.startBundle(1);
????????displayBundlesStatus();
????????Main.stopBundle(1);
????????displayBundlesStatus();
????????Main.shutdown(0);
????}
????private?static?void?displayBundlesStatus()?{
????????BundleContext?bc?=?Main.getBundleContext();
????????Bundle[]?b?=?bc.getBundles();
????????for?(int?i?=?0;?i?<?b.length;
????????????????i++)?{
????????????Bundle?bundle?=?b[i];
????????????System.out.println("ID:?#"?+?bundle.getBundleId()?+
???????????????????????????????"????????Location:?"?+?bundle.getLocation()?+
???????????????????????????????"????????Status:?"?+?bundle.getState());
????????}
????}
}
3. 測試
還記得上一次我們的那個Bundle嗎?為了它啟動在我們的框架里,編寫一個啟動參數文件secondosgi.xargs:
-istart?/home/daniel/Work/Sources/Java/SecondOSGi/dist/SecondOSGi.jar
然后,修改啟動參數:
測試輸出:
總結
這一次,我們簡單地分析了KF框架的啟動,還有一些設計。并修改了KF框架的啟動類,作為了我們自己的一個底層應用框架封裝。下一次,我們將結合具體的一個項目(把OSGi作為整個項目的實現基礎,支持插件的辭典——StoneAgeDict)實踐OSGi!轉載于:https://www.cnblogs.com/lanzhi/archive/2008/02/15/6470594.html
總結
以上是生活随笔為你收集整理的使用NetBeans6开发OSGi应用(3)——整合Knopflerfish![88250原创]的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Ubuntu 下Ape转Mp3[8825
- 下一篇: 忙点杂事