`
xiangxingchina
  • 浏览: 510925 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

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

分享到:
评论

相关推荐

    osgi介绍osgi介绍

    osgi介绍osgi介绍osgi介绍osgi介绍osgi介绍osgi介绍osgi介绍osgi介绍osgi介绍osgi介绍

    OSGI原理与最佳实践

    资源名称:OSGI原理与最佳实践内容简介:国内第一本OSGi图书OSGi国内推广者林昊多年经验的结晶涵盖OSGi从入门到深入的知识体系引领OSGi国内研究和普及本书基于作者多年使用OSGi的经验而编写,涵盖了OSGi从入门到深入...

    利用R-OSGi实现分布式OSGi应用

    利用R-OSGi实现分布式OSGi应用 本文通过介绍传统 OSGi 应用程序及 R-OSGi 的实现方式入手,阐述了 R-OSGi 对于 OSGi 规范的实现方式。然后通过一个简单的功能实现由浅入深地讲述传统 OSGi 和 R-OSGi 上的两种不同...

    OSGI进阶.pdf

    讲OSGI应用的讲OSGI应用的讲OSGI应用的讲OSGI应用的讲OSGI应用的

    OSGI合集 OSGi原理与最佳实践

    网上收集的OSGI资料. 包括: OSGi原理与最佳实践(精选版).pdf OSGI实战和源码.rar osgi进阶.pdf Introduce.OSGi.ppt OSGi.in.action.ppt r4.cmpn.pdf r4.core.pdf r4.enterprise.pdf

    OSGI 入门资料PDF

    OSGI 入门资料PDF,包括OSGI实战、OSGI进阶、OSGI 入门和整合Spring、OSGI原理与最佳实践

    OSGI 实例eclipse插件开发

    OSGI 实例 eclipse 插件开发

    《OSGi实战》完整中文版

    《 OSGi实战》是学习OSGi的全面指导,利用与架构和开发人员相关的示例清楚地讲解OSGi概念,同时探讨了很多实践场景和技术,阐述了开发人员有多需要OSGi,怎么将OSGi嵌入其他容器中,将遗留系统移入OSGi的最佳实践,...

    OSGi入门教程(OSGi Introduce)

    OSGi的入门教程,帮助初学者快速了解OSGi的定义、用途及组成。

    OSGI原理最佳实践(包含源代码)

    引领OSGi国内研究和普及 本书基于作者多年使用OSGi的经验而编写,涵盖了OSGi从入门到深入的知识体系,从OSGi的简介开题,介绍OSGi的作用及基本概念;其后进入OSGi实战,结合实例讲解如何基于OSGi框架编写模块化、...

    OSGI进阶实战教程

    OSGi学习不错的材料 OSGi学习不错的材料 OSGi学习不错的材料 OSGi学习不错的材料

    osgi,林昊写的osgi实战和进阶

    osgi,林昊写的osgi实战和进阶,学习osgi的好东西,入门的首选。

    OSGI进阶--OSGi开发指南

    OSGi开发文档和实践指南,描述了OSGI的开发流程

    OSGi入门资料-初探OSGi 的全文

    OSGi的入门资料,网上找的,初探OSGi 的全文

    OSGI组件编程(osgi.component.programming)

    介绍OSGI组件编程,详细介绍用eclipse和Equinox开发OSGI程序

    osgi资料

    osgi资料

    OSGI.R4.cmpn英文开发文档

    适合开始研究和深入研究JAVA OSGI框架的应用开发者

    OSGi原理与最佳实践

    本书基于作者多年使用OSGi的经验而编写,涵盖了OSGi从入门到深入的知识体系,从OSGi的简介开题,介绍OSGi的作用及基本概念;其后进入OSGi实战,结合实例讲解如何基于OSGi框架编写模块化、动态化的各种Java应用;最后...

    OSGi.NET框架 Documentation

    本文档主要针对OSGi.NET模块化框架使用进行了描述 OSGi.NET框架是一个参照了OSGi规范的模块化管理框架。框架为应用程序(组件(bundle))提供了一个标准环境。整个框架可以划分为一些层次: 1.运行环境 2.模块...

    OSGi规范(r4)中文版

    OSGi 中文版 中文 OSGi中文OSGi 中文版 中文 OSGi中文OSGi 中文版 中文 OSGi中文OSGi 中文版 中文 OSGi中文OSGi 中文版 中文 OSGi中文OSGi 中文版 中文 OSGi中文OSGi 中文版 中文 OSGi中文OSGi 中文版 中文 OSGi中文

Global site tag (gtag.js) - Google Analytics