`
zyl
  • 浏览: 484265 次
社区版块
存档分类
最新评论

osgi 入门 三 关于类装载机制

    博客分类:
  • osgi
阅读更多

    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: * ,这是比较极端的设置,一般不会推荐使用,主要破坏了相关的封装性。
完整的类查询如下图:
 

关于类装载机制基本就是这些,对于初学者来说,这还是会有些困惑的。

  • 大小: 15.1 KB
  • 大小: 11.6 KB
  • 大小: 27.2 KB
1
0
分享到:
评论
1 楼 fanfan159357 2014-07-21  
nice,必须赞一个

相关推荐

Global site tag (gtag.js) - Google Analytics