偶也研究OSGi

偶也开始研究OSGi了,这东西真不错。不过官方的OSGi标准包太大了,前两天偶根据规范实现了其Module Layer部分并根据大家提到的一些不足,增加了自定义ClassPath和NativeCode的功能,这下应该可以用在基于Spring的 Webapp上了~

不多说了,看看测试代码吧:

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.getMessage() !=  null ) {      
  10.           System.out.println(event.getMessage());      
  11.         }      
  12.          if  (event.getThrowable() !=  null ) {      
  13.           event.getThrowable().printStackTrace();      
  14.         }      
  15.       }      
  16.     });      
  17.   }      
  18.      
  19.    public   void  tearDown() {      
  20.     framework =  null ;      
  21.     System.out.println();      
  22.   }      
  23.      
  24.    public   void  testBundle()  throws  Exception {      
  25.      
  26.     framework.addClassPath( "C:\\osgi\\ws\\commons-logging.jar" );      
  27.     framework.addLibraryPath( "C:\\osgi\\os" );      
  28.      
  29.      // ----------------------------------------------   
  30.      // Bundle-SymbolicName: com.yipsilon.osgi.test   
  31.      // Bundle-Version: 1.0.0.20061212   
  32.      // Bundle-NativeCode: swt-gdip-win32-3235.dll,swt-awt-win32-3235.dll,swt-wgl-win32-3235.dll,swt-win32-3235.dll   
  33.      // Bundle-Activator: com.yipsilon.osgi.test.Activator   
  34.      // Export-Package: com.yipsilon.osgi.test   
  35.      // Import-Package: org.apache.commons.logging   
  36.      // Bundle-ClassPath: swt.3.2.1.v3235.jar   
  37.      // ----------------------------------------------   
  38.   
  39.     framework.installBundle( "C:\\osgi\\test.jar" );      
  40.      
  41.     ClassLoader cl = framework.getClassLoader();      
  42.      
  43.     URL explicitURL = cl.getResource( "com/yipsilon/osgi/test/Test.class" );      
  44.     URL implicitURL = cl.getResource( "com/yipsilon/osgi/test1/Hello.class" );      
  45.     URL externalURL = cl.getResource( "org/apache/commons/logging/LogFactory.class" );      
  46.      
  47.     System.out.println( "implicitURL: "  + (implicitURL !=  null ));   // Returns true      
  48.     System.out.println( "explicitURL: "  + (explicitURL !=  null ));  // Returns true      
  49.     System.out.println( "externalURL: "  + (externalURL !=  null ));  // Returns true      
  50.      
  51.     Class explicitClass = cl.loadClass( "com.yipsilon.osgi.test.Test" );      
  52.     Class implicitClass = cl.loadClass( "com.yipsilon.osgi.test1.Hello" );      
  53.     Class externalClass = cl.loadClass( "org.apache.commons.logging.LogFactory" );      
  54.      
  55.     System.out.println( "implicitClass: "  + (implicitClass !=  null ));  // Returns false      
  56.     System.out.println( "explicitClass: "  + (explicitClass !=  null ));  // Returns true      
  57.     System.out.println( "externalClass: "  + (externalClass !=  null ));  // Returns true      
  58.   }      
  59. }  

看完了知道这东西该怎么用了吧... 嘿嘿!!

不过要真正实现Webapp功能,还需要加一些东西,这些稍后我会开发出来~~进度的快慢全凭偶的空闲时间的多少... faint

PS:怎么附件加不进去呐??才1.5MB而已....

 

 

 

继“偶也研究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~~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值