閑人野居
          好好學習,天天向上
          posts - 57,  comments - 137,  trackbacks - 0
            osgi一個最大的特色就是使用不同的ClassLoader,讓多個bundle共享一個虛擬機,而每一個bundle使用單獨的ClassLoader。
          如下圖所示:
           
          對于資源和類的裝載主要包括以下三種途徑:
          Boot classpath:啟動類環境,包括java.*包和相應的實現包。
          Framework classpath:osgi框架擁有一個獨立的ClassLoader,用于裝載框架接口類,以及相應的實現類。
          Bundle Space:每個bundle包括了與bundle相關的jar文件,以及相關的資源。

          對于一個類的查詢,主要通過以下途徑進行查詢:
          1.從Parent ClassLoader中裝載
          2.從Import Package中查詢類路徑。
          3.從Required bundles中查詢類
          4.自身bundle,相關ClassPath
          5.相關的插件片段Fragment。
          如下圖
           
          這么說起來,還是比較的抽象,還是從一個實例來具體的分析。
          假設有如下Bundle,
          相關的描述文件如下:
          Bundle:org.zframework.core
          Manifest
          -Version: 1.0
          Bundle
          -Name: core
          Bundle
          -SymbolicName: org.zframework.core;singleton:=true
          Bundle
          -ClassPath: .
          Import
          -Package: net.sf.ehcache;version="1.3.0",
           net.sf.ehcache.config;version
          ="1.3.0",
           net.sf.ehcache.event;version
          ="1.3.0",
           org.osgi.framework;version
          ="1.4.0",
           org.osgi.service.log;version
          ="1.3.0",
           
          Require
          -Bundle: org.eclipse.equinox.common;visibility:=reexport,
           
          Export
          -Package: org.zframework.core;version="1.0.0",
           


          現在創建了如下的類:
          package org.zframework.core;
          import java.net.URL;
          import java.util.Collection;
          import net.sf.ehcache.Cache;
          import net.sf.ehcache.CacheManager;
          import net.sf.ehcache.Element;
          import org.osgi.framework.BundleContext;
          public class EhCacheManager extends AbstractCacheManager implements
              BundleContextAware {
              
          private String cacheFile;
              
          private BundleContext context;
              
          private CacheManager manager;
              
          public Cache getCache(String cacheName) {
              Cache cache 
          = manager.getCache(cacheName);
              
          if (cache == null) {
                    
              }
              
          return cache;
              }
              
          }

          假設有一個Bundle org.zframework.model需要使用此類,描述如下:
          Manifest-Version: 1.0
          Bundle
          -Name: model
          Bundle
          -SymbolicName: org.zframework.model;singleton:=true
          Bundle
          -ClassPath: .
          Import
          -Package: javax.persistence;version="1.0.0",
            javax.persistence.spi;version
          ="1.0.0",
            org.osgi.framework;version
          ="1.4.0",
            
           Require
          -Bundle: org.zframework.core

          類Model
          package org.zframework.model;
          import java.net.URL;
          import java.util.Collection;
          import org.zframework.core.EhCacheManager ;
          public class Model{
              
          private EhCacheManager manager;
              
          public Object getCache(String key) {
              Object o 
          = manager.getCache(key);
              
          return o;
              }
              
          }

          下面簡單說明一下裝載過程:
          在裝載Model類時,
          1.對于java.*類的裝載,由相應的Boot ClassLoader 裝載
          2.裝載EhCacheManager,
              1.在Import-Package中查詢是否存在匹配中的org.zframework.core包聲明,如果有,則委派給org.zframework.core包聲明的Bundle進行裝載,本例沒有,則繼續。
              2.在Require-Bundle中查詢所有的Export聲明包,判斷是否存在org.zframework.core匹配。本例中,應該存在。如果不存在,則執行步驟2.3
              3.查詢本bundle的類路徑,是否有此類存在。不存在,繼續2.4步驟
              4.查詢所有的fragments bundle(特殊的bundle) 類路徑。不存在,繼續2.5步驟     
              5.判斷是否使用DynamicImport-Package: * 聲明,如果是則查詢所有的bundle中聲明的Export包。如果還是不存在,則拋出ClassNotFound例外。
          關于DynamicImport-Package: * ,這是比較極端的設置,一般不會推薦使用,主要破壞了相關的封裝性。
          完整的類查詢如下圖:
           
          關于類裝載機制基本就是這些,對于初學者來說,這還是會有些困惑的。







          posted on 2009-02-17 16:49 布衣郎 閱讀(3881) 評論(0)  編輯  收藏 所屬分類: eclipse

          <2009年2月>
          25262728293031
          1234567
          891011121314
          15161718192021
          22232425262728
          1234567

          常用鏈接

          留言簿(12)

          隨筆分類(59)

          隨筆檔案(57)

          blog

          java

          uml

          搜索

          •  

          積分與排名

          • 積分 - 357330
          • 排名 - 155

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 苏尼特右旗| 吴忠市| 静安区| 江川县| 苏州市| 墨竹工卡县| 吕梁市| 江城| 英山县| 扶风县| 桃江县| 宜阳县| 南昌县| 辛集市| 黑水县| 广水市| 孟村| 湄潭县| 永济市| 永宁县| 吴桥县| 若尔盖县| 大城县| 依兰县| 昌乐县| 铁力市| 寿光市| 石首市| 巴塘县| 青龙| 台中市| 岢岚县| 肇东市| 富锦市| 辽中县| 体育| 新宾| 龙川县| 昆山市| 隆化县| 伊吾县|