當(dāng)前位置:
首頁 >
使用NetBeans6开发OSGi应用(3)——整合Knopflerfish![88250原创]
發(fā)布時間:2025/7/14
40
豆豆
生活随笔
收集整理的這篇文章主要介紹了
使用NetBeans6开发OSGi应用(3)——整合Knopflerfish![88250原创]
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
轉(zhuǎn)載請保留作者信息:
作者:88250
Blog:http:/blog.csdn.net/DL88250
MSN & Gmail & QQ:DL88250@gmail.com
摘要
上一次,我們編寫了兩個Bundles,一個是服務(wù)提供商,一個是使用服務(wù)的客戶 ,運(yùn)行得還不錯 :-)這一次,我們先簡要分析一下KF(Knopflerfish)框架的設(shè)計,學(xué)習(xí)應(yīng)用程序框架的設(shè)計。最后,結(jié)合上一次文章結(jié)尾時提到了關(guān)于控制KF框架、讓OSGi服務(wù)于我們的應(yīng)用的問題,今天就圍繞這些內(nèi)容展開。
關(guān)于Knopflerfish框架的設(shè)計
在開始,我們將看一下KF框架的設(shè)計。Main
在閱讀了KF框架的一些代碼后,從KF框架的主程序入手(org.knopflerfish.framework.Main)我們可以看出,KF除了實(shí)現(xiàn)OSGi規(guī)范,實(shí)現(xiàn)了自己的framework(org.knopflerfish.framework.Framework)外,主要是圍繞它的框架啟動類:org.knopflerfish.framework.Main做了一系列的鋪墊:比如所實(shí)現(xiàn)的OSGi規(guī)范的版本(我的版本是OSGi 4.0.6版本),存儲Bundle的文件位置,操作系統(tǒng)版本,等等。這個Main類,主要做的是命令行參數(shù)(啟動參數(shù))的處理,因?yàn)镵F框架啟動的時候可以用xargs文件(把所有參數(shù)寫成一個文件),所以處理上比較繁瑣。在Main下面的handleArgs方法可以看出,所有的Bundles的基本操作(start、stop、install、etc.)都是調(diào)用org.knopflerfish.framework.Framework這個類實(shí)現(xiàn)的,這個類就是KF框架的基本實(shí)現(xiàn),已經(jīng)把功能封裝地相當(dāng)好用了!
那我們應(yīng)該直接使用Framework類嗎?
我們應(yīng)該直接使用KF實(shí)現(xiàn)的Framework類,但是整個框架的啟動鋪墊是很繁瑣的。從Main還有其相關(guān)的Utils的代碼量就可以看出。在整個框架啟動之前,要做一系列的鋪墊。這里,結(jié)合我自己的項(xiàng)目,由于時間比較緊了,再重寫這些可能時間不允許,所以我選擇的是改寫Main,把那個Main類作為自己應(yīng)用框架的一個對KF的基本封裝。具體做法就是委托Framework類的一些方法,暴露這些方法在Main外部。
總之,要基于Knopflerfish,完成自己的一個應(yīng)用框架還是比較簡單的 :-)
讓我們簡單實(shí)踐一下!
準(zhǔn)備
同上一次 :-)開工:
1. 創(chuàng)建工程
打開NB6,創(chuàng)建一個普通Java應(yīng)用工程——MyOSGiFramework:注意那個version文件,這個文件是指KF實(shí)現(xiàn)的OSGi的版本,在OSGi框架實(shí)現(xiàn)里,有一個Version類,用于版本的管理。要在我們的src目下建立一個version文件。
2.改寫Knopflerfish的Main類
添加對Bundle的基本操作到Main類里,達(dá)到封裝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嗎?為了它啟動在我們的框架里,編寫一個啟動參數(shù)文件secondosgi.xargs:
-istart?/home/daniel/Work/Sources/Java/SecondOSGi/dist/SecondOSGi.jar
然后,修改啟動參數(shù):
測試輸出:
總結(jié)
這一次,我們簡單地分析了KF框架的啟動,還有一些設(shè)計。并修改了KF框架的啟動類,作為了我們自己的一個底層應(yīng)用框架封裝。下一次,我們將結(jié)合具體的一個項(xiàng)目(把OSGi作為整個項(xiàng)目的實(shí)現(xiàn)基礎(chǔ),支持插件的辭典——StoneAgeDict)實(shí)踐OSGi!轉(zhuǎn)載于:https://www.cnblogs.com/lanzhi/archive/2008/02/15/6470594.html
總結(jié)
以上是生活随笔為你收集整理的使用NetBeans6开发OSGi应用(3)——整合Knopflerfish![88250原创]的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Ubuntu 下Ape转Mp3[8825
- 下一篇: 忙点杂事