WEB开发网
开发学院软件开发Java 类加载器特技:OSGi代码生成 阅读

类加载器特技:OSGi代码生成

 2010-03-01 00:00:00 来源:WEB开发网   
核心提示: .../*Asktheapptoresolvethetargetclass*/Bundleapp=...Classtarget=app.loadClass("com.acme.devices.SinisterEngine");/*Getthedefiningclassload

... 
/* Ask the app to resolve the target class */ 
Bundle app = ... 
Class target = app.loadClass("com.acme.devices.SinisterEngine"); 
 
/* Get the defining classloader of the target */ 
ClassLoader targetSpace = target.getClassLoader(); 
 
/* Get the bridge for the class space of the target */ 
BridgeClassLoaderCache cache = ... 
ClassLoader bridge = cache.resolveBridge(targetSpace); 

桥缓存看起来会是这样:

public class BridgeClassLoaderCache { 
 private final ClassLoader primary; 
 private final Map<ClassLoader, WeakReference<ClassLoader>> cache; 
 
 public BridgeClassLoaderCache(ClassLoader primary) { 
  this.primary = primary; 
  this.cache = new WeakHashMap<ClassLoader, WeakReference<ClassLoader>>(); 
 } 
 
 public synchronized ClassLoader resolveBridge(ClassLoader secondary) { 
  ClassLoader bridge = null; 
 
  WeakReference<ClassLoader> ref = cache.get(secondary); 
  if (ref != null) { 
   bridge = ref.get(); 
  } 
 
  if (bridge == null) { 
   bridge = new BridgeClassLoader(primary, secondary); 
   cache.put(secondary, new WeakReference<ClassLoader>(bridge)); 
  } 
 
  return bridge; 
 } 
}

上一页  1 2 3 4 5 6 7 8 9 10  下一页

Tags:加载 特技 OSGi

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接