Chasing an mobile web vision

          闖蕩在移動互聯網的世界中

          OSGi介紹(三)OSGi service platform的體系結構

          先讓我們來看看OSGi service platform的體系結構。另外要說明的是,我在后面的文章中,將采用framework來代替OSGi service platfrom,這樣比較簡便。
          下面這張圖來自OSGi Alliance的主頁(http://www.osgi.org/
           
          OSGi Service Platform Architecture

          層次很分明吧。放到我們假想的案例中,OS&Hardware可以對應為PDA的硬件和操作系統,您可以想象它是Intel xscacle + Microsoft window mobile,或者是Arm + embedded Linux
          而Execution Environment當然是我們上次提到的CVM + CDC + FP + PP,有這個jvm的配置運行framework就綽綽有余了。而再往上,就是我們要重點學習和分析的OSGi framework了。而Modules, Life Cycle, Service Registry, Services和Security是從不同的5個角度來劃分framework所具備的功能,后面我將會從除了Security外的四個方面分別結合我們的假設場景來分析。而體系結構的最上層是符合OSGi framework接口標準的應用程序,也就是OSGi世界中有名的“bundle”。

          下面來看看OSGi規范是如何定義一個bundle的。在r4規范的第27頁中大致這樣描述到:Framework定義了模塊(modularization)的單元,叫作bundle。Bundle實際就是一個具有jar(Java ARchive)格式的文件,其中包含了java的class文件和其他資源文件(比如圖標,配置文件等等)。Bundle可以在自己的manifest文件中說明自己能夠提供哪些java包,其他bundle如果在自己的manifest文件中指定了它需要這個包,那他們之間就可能產生java包的依賴關系,這樣多個bundle之間就可以共享java包。值得注意的是,bundle是能夠在framework中部署的唯一格式。下面給出原文的描述:
          A bundle is a JAR file that:
          ? Contains the resources necessary to provide some functionality. These resources may be class files for the Java programming language, as well as other data such as HTML files, help files, icons, and so on. A bundle JAR file can also embed additional JAR files that are available as resources and classes. This is however not recursive.
          ? Contains a manifest file describing the contents of the JAR file and providing information about the bundle. This file uses headers to specify information that the Framework needs to install correctly and activate a bundle. For example, it states dependencies on other resources, such as Java packages, that must be available to the bundle before it can run.
          ? Can contain optional documentation in the OSGI-OPT directory of the JAR file or one of its sub-directories. Any information in this directory is optional. For example, the OSGI-OPT directory is useful to store the source code of a bundle. Management systems may remove this information to save storage space in the OSGi Service Platform.

          framework的modules這一方面功能將主要負責bundle的安裝部署,更新和卸載,以及bundle在設備的物理存儲(如果有的話)。在這個層次,每個bundle都是獨立的,它的安裝,升級和卸載完全不依賴任何其他bundle,這點framework提供了強大的隔離性。Life Cycle專門負責對bundle的解析(比如關聯兩個有相互依賴關系的bundle),啟動(相當于運行應用程序)和停止(相當于停止應用程序)。這個層次中,bundle間的邏輯關系被創建起來,這些關系能否成功的創建,將會直接影響bundle的成功解析和啟動。Service Registry可以認為是一個數據庫,bundle啟動后,可以向這個數據庫注冊它動態提供的服務。只要bundle不被停止,且bundle不主動撤銷注冊的服務,這個服務將一直保存在這個數據庫中供其它bundle來查詢和使用。而Services就是由bundle運行時提供的具體服務對象,這些服務對象的存在,使得framework具有極其動態的特征,并為framework運行時提供靈活強大的功能。
          另外,根據OSGi Alliance的說法,OSGi的運行平臺包括了j2me(kvm + cldc + midp, cvm + cdc+fp), j2se, j2ee。不過,我個人還是覺得目前的midp規范還太弱,OSGi要想運行在上面,很多功能實現起來都比較不爽。

          好,有了對framework結構層次的皮毛認識,下面我們就開始著手改造那個“扶貧助手”的程序,使其變成OSGi的bundle(s),然后從上面提到的4個方面來分析framework的機制。
          這里,我先給出“扶貧助手”的java application模擬程序代碼:

          package aa.bb.cc;

          public class FamilyInfo {
           
          private String familyName; //家庭名稱
           private int population; //人口數量
           private int incomePerYear; //家庭年收入

            …..
          //省略了getter和setter方法

          //獲得家庭人均年收入
           public int getIncomePerMember(){
            
          return (int)(this.incomePerYear/this.population);
           }


           
          public String toString() {
            
            
          return "Family: " + this.familyName + ", population: " + this.population + ", income: " + this.incomePerYear;
           }

           
          //按家庭年收入又低到高排序
           public static void sortByIncomePerYear(FamilyInfo[] families){
            FamilyInfo temp 
          = null;
            
          for(int i = 0; i < families.length -1; i ++){
             
          for(int j = i + 1; j < families.length; j ++){
              
              
          if(families[i].getIncomePerYear() > families[j].getIncomePerYear()){
               temp 
          = families[i];
               families[i] 
          = families[j];
               families[j] 
          = temp;
              }

             }

            }

            
           }


           
          //按家庭人均年收入由低到高排序
           public static void sortByIncomePerMember(FamilyInfo[] families){
            FamilyInfo temp 
          = null;
            
          for(int i = 0; i < families.length -1; i ++){
             
          for(int j = i + 1; j < families.length; j ++){
              
              
          if(families[i].getIncomePerMember() > families[j].getIncomePerMember()){
               temp 
          = families[i];
               families[i] 
          = families[j];
               families[j] 
          = temp;
              }

             }

            }

            
           }


           
          public static void main(String[] args){
            FamilyInfo[] families 
          = new FamilyInfo[3];
            families[
          0= new FamilyInfo();
            families[
          0].setFamilyName("Zhang");
            families[
          0].setPopulation(3);
            families[
          0].setIncomePerYear(1200);
            families[
          1= new FamilyInfo();
            families[
          1].setFamilyName("Li");
            families[
          1].setPopulation(6);
            families[
          1].setIncomePerYear(1800);
            families[
          2= new FamilyInfo();
            families[
          2].setFamilyName("Liu");
            families[
          2].setPopulation(4);
            families[
          2].setIncomePerYear(1500);
            FamilyInfo.sortByIncomePerYear(families);
            
          for(int i = 0; i < families.length; i ++){
             System.out.println(families[i].toString());
            }

            FamilyInfo.sortByIncomePerMember(families);
            
          for(int i = 0; i < families.length; i ++){
             System.out.println(families[i].toString());
            }

            
           }

          }


           

          posted on 2006-02-14 15:42 勤勞的蜜蜂 閱讀(5177) 評論(3)  編輯  收藏

          評論

          # re: OSGi介紹(三)OSGi service platform的體系結構 2007-06-21 17:25 hata

          這篇主要講了OSGI的體系結構,我覺得比較清晰,后面的POJO可能是后面用到的吧,收藏那張圖和那段講體系的話,HOHO!繼續!  回復  更多評論   

          # re: OSGi介紹(三)OSGi service platform的體系結構[未登錄] 2009-01-06 23:14 過客

          不錯,喝彩一把  回復  更多評論   

          # re: OSGi介紹(三)OSGi service platform的體系結構 2009-04-29 14:21 guest

          對于OSGI體系結構,說的不錯
          請繼續啊  回復  更多評論   


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 西华县| 格尔木市| 永善县| 应用必备| 樟树市| 莱西市| 绥化市| 佛山市| 吴川市| 农安县| 宁陕县| 商水县| 胶南市| 丰原市| 禹城市| 赞皇县| 美姑县| 民县| 民乐县| 鹤壁市| 马尔康县| 营口市| 柘城县| 扶风县| 大宁县| 望奎县| 南昌县| 改则县| 玛纳斯县| 迁西县| 稷山县| 邵东县| 湘乡市| 泗水县| 娄底市| 莲花县| 静乐县| 丹棱县| 莱芜市| 贵南县| 青川县|