论坛首页 Java企业应用论坛

偶也研究OSGi了之二

浏览 7869 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2006-12-28  
SOA

继“偶也研究OSGi了之一”之后不到12小时,偶又开始发博了,大家检查一下内容质量如何~

这次,在上次的示例基础上,展示一下MINI OSGi的一些基本特性。还是刚才的代码(有少许变化):

PS:为了简化代码,省略了所有的注释,代码结构还算不错,一般可以看懂~

java 代码
  1. public class TFramework extends TestCase {   
  2.   
  3.   private Framework framework;   
  4.   
  5.   public void setUp() {   
  6.     framework = new Framework();   
  7.     framework.addFrameworkListener(new IFrameworkListener() {   
  8.       public void frameworkEvent(IFrameworkEvent event) {   
  9.         if (event.getType() != IFrameworkEvent.TYPE.DEBUG && event.getMessage() != null) {   
  10.           System.out.println(event.getMessage());   
  11.         }   
  12.         if (event.getThrowable() != null) {   
  13.           event.getThrowable().printStackTrace();   
  14.         }   
  15.       }   
  16.     });   
  17.     framework.addBundleListener(new ISynchronousBundleListener(){   
  18.       public void bundleChanged(IBundleEvent event) {   
  19.         IBundle bundle = event.getBundle();   
  20.         System.out.println("Bundle(SymbolicName:" + bundle.getSymbolicName() + ") state is changed to " + event.getType().name());   
  21.       }   
  22.          
  23.     });   
  24.   }   
  25.   
  26.   public void tearDown() {   
  27.     framework = null;   
  28.   }   
  29.   
  30.   public void testBundle() throws Exception {   
  31.     framework.addClassPath("C:\\osgi\\lib");   
  32.     framework.addClassPath("C:\\osgi\\lib\\commons-logging.jar");   
  33.     IBundle[] bundle = framework.installBundles("C:\\osgi");   
  34.     bundle[0].start();   
  35.   }   
  36. }  

看到了吧,这里使用Bundle对象了~看看Bundle所对应的Activator类吧:

java 代码
  1. public class Activator implements IBundleActivator{   
  2.   
  3.   public void start(IBundleContext context) throws Exception {   
  4.     System.out.println("Test.start(" + context + ")");   
  5.   }   
  6.   
  7.   public void stop(IBundleContext context) throws Exception {   
  8.     System.out.println("Test.stop(" + context + ")");   
  9.   }   
  10. }  

"C:\osgi"目录下有一个叫 test.jar 的bundle包,MANIFEST.MF文件内容为:

Bundle-SymbolicName: com.yipsilon.osgi.test
Bundle-Version: 1.0.0.20061212
Bundle-NativeCode: swt-gdip-win32-3235.dll,swt-awt-win32-3235.dll,swt-wgl-win32-3235.dll,swt-win32-3235.dll
Bundle-Activator: com.yipsilon.osgi.test.Activator
Export-Package: com.yipsilon.osgi.test
Import-Package: org.apache.commons.logging
Bundle-ClassPath: swt.3.2.1.v3235.jar

其中 test.jar 的文件结构为:

test.jar
│  swt-awt-win32-3235.dll
│  swt-gdip-win32-3235.dll
│  swt-wgl-win32-3235.dll
│  swt-win32-3235.dll
│  swt.3.2.1.v3235.jar

├─com
│  └─yipsilon
│      └─osgi
│          ├─test
│          │      Activator.java
│          │      Test.java
│          │
│          └─test1
│                  ApplicationWindow.java
│                  Hello.java

└─META-INF
        MANIFEST.MF

使用JUnit3运行TFramework后,后台打印出:

01. Bundle(SymbolicName:com.yipsilon.osgi.test) state is changed to INSTALLED
02. Bundle(SymbolicName:com.yipsilon.osgi.test) state is changed to RESOLVED
03. Bundle(SymbolicName:com.yipsilon.osgi.test) state is changed to STARTING
04. Test.start(com.yipsilon.osgi.BundleContext@14a9972)
05. Bundle(SymbolicName:com.yipsilon.osgi.test) state is changed to STARTED
06. Bundle(SymbolicName:com.yipsilon.osgi.test) state is changed to STOPPING
07. Test.stop(com.yipsilon.osgi.BundleContext@14a9972)
08. Bundle(SymbolicName:com.yipsilon.osgi.test) state is changed to STOPPED
09. Bundle(SymbolicName:com.yipsilon.osgi.test) state is changed to UNINSTALLED

目前,MINI OSGI已经实现了import-package和require-bundle的resolve检查(也就是输出第2行的执行结果)。

我使用了addShutdownHook来在系统退出时释放所有没有卸载的bundle资源,以防止内存泄漏(第6-9行就是自动释放的输出)。

目前Module Layer部分还没有完全实现MANIFEST属性条件的判断,我觉得那个东西太复杂,大多数情况也用不上(至少以我现在的项目来说),所以先搁置一下。其他重要的功能就剩下update()方法了。HOHO~~

 

   发表时间:2006-12-28  
MINI OSGI??

支持一下!

期待有更深的讨论..
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics