Felix(1)
本来是想多看点再写的,不过不写会忘的。。先吧结构写下来,以后添肉。
Felix是apache的OSGI开源实现,同时也是Netbeans6.9引入的OSGI实现,我之前试了一下,把原有的RCP编译成OSGI Bundle,编译成功,运行却失败了,没深究,不过这个功能确实眼前一亮。废话不说了。
org.apache.felix.main.Main是Felix的入口,OSGI规范中要求实现FrameWorkFactory来创建OSGI的FrameWork,而这个FrameWork就是OSGI生命周期中比较重要的一个类,比如bundle的install,uninstall等都是通过Framework来实现的。那么feilx是怎么创建FrameworkFactory的呢?规范中有几种建议方式,如SPI,直接创建对象等。因为SPI是JDK6新加入的API,Felix可能出于对向前兼容的考虑,没有直接采用,而是模仿SPI的方式:
URL url = Main.class.getClassLoader().getResource( "META-INF/services/org.osgi.framework.launch.FrameworkFactory"); if (url != null) { BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream())); try { for (String s = br.readLine(); s != null; s = br.readLine()) { s = s.trim(); // Try to load first non-empty, non-commented line. if ((s.length() > 0) && (s.charAt(0) != '#')) { return (FrameworkFactory) Class.forName(s).newInstance(); } } } finally { if (br != null) br.close(); } } throw new Exception("Could not find framework factory.");
org.apache.felix.framework.FrameworkFactory
public class FrameworkFactory implements org.osgi.framework.launch.FrameworkFactory{ public Framework newFramework(Map configuration) { return new Felix(configuration); }}