偶也开始研究OSGi了,这东西真不错。不过官方的OSGi标准包太大了,前两天偶根据规范实现了其Module Layer部分并根据大家提到的一些不足,增加了自定义ClassPath和NativeCode的功能,这下应该可以用在基于Spring的 Webapp上了~
不多说了,看看测试代码吧:
java 代码
- public class TFramework extends TestCase {
-
-
private Framework framework;
-
-
public void setUp() {
-
framework = new Framework();
-
framework.addFrameworkListener( new IFrameworkListener() {
-
public void frameworkEvent(IFrameworkEvent event) {
-
if (event.getMessage() != null ) {
-
System.out.println(event.getMessage());
-
}
-
if (event.getThrowable() != null ) {
-
event.getThrowable().printStackTrace();
-
}
-
}
-
});
-
}
-
-
public void tearDown() {
-
framework = null ;
-
System.out.println();
-
}
-
-
public void testBundle() throws Exception {
-
-
framework.addClassPath( "C:\\osgi\\ws\\commons-logging.jar" );
-
framework.addLibraryPath( "C:\\osgi\\os" );
-
-
-
-
-
-
-
-
-
-
-
-
framework.installBundle( "C:\\osgi\\test.jar" );
-
-
ClassLoader cl = framework.getClassLoader();
-
-
URL explicitURL = cl.getResource( "com/yipsilon/osgi/test/Test.class" );
-
URL implicitURL = cl.getResource( "com/yipsilon/osgi/test1/Hello.class" );
-
URL externalURL = cl.getResource( "org/apache/commons/logging/LogFactory.class" );
-
-
System.out.println( "implicitURL: " + (implicitURL != null ));
-
System.out.println( "explicitURL: " + (explicitURL != null ));
-
System.out.println( "externalURL: " + (externalURL != null ));
-
-
Class explicitClass = cl.loadClass( "com.yipsilon.osgi.test.Test" );
-
Class implicitClass = cl.loadClass( "com.yipsilon.osgi.test1.Hello" );
-
Class externalClass = cl.loadClass( "org.apache.commons.logging.LogFactory" );
-
-
System.out.println( "implicitClass: " + (implicitClass != null ));
-
System.out.println( "explicitClass: " + (explicitClass != null ));
-
System.out.println( "externalClass: " + (externalClass != null ));
-
}
-
}
看完了知道这东西该怎么用了吧... 嘿嘿!!
不过要真正实现Webapp功能,还需要加一些东西,这些稍后我会开发出来~~进度的快慢全凭偶的空闲时间的多少... faint
PS:怎么附件加不进去呐??才1.5MB而已....
继“偶也研究OSGi了之一 ”之后不到12小时,偶又开始发博了,大家检查一下内容质量如何~
这次,在上次的示例基础上,展示一下MINI OSGi的一些基本特性。还是刚才的代码(有少许变化):
PS:为了简化代码,省略了所有的注释,代码结构还算不错,一般可以看懂~
java 代码
- public class TFramework extends TestCase {
-
-
private Framework framework;
-
-
public void setUp() {
-
framework = new Framework();
-
framework.addFrameworkListener( new IFrameworkListener() {
-
public void frameworkEvent(IFrameworkEvent event) {
-
if (event.getType() != IFrameworkEvent.TYPE.DEBUG && event.getMessage() != null ) {
-
System.out.println(event.getMessage());
-
}
-
if (event.getThrowable() != null ) {
-
event.getThrowable().printStackTrace();
-
}
-
}
-
});
-
framework.addBundleListener( new ISynchronousBundleListener(){
-
public void bundleChanged(IBundleEvent event) {
-
IBundle bundle = event.getBundle();
-
System.out.println( "Bundle(SymbolicName:" + bundle.getSymbolicName() + ") state is changed to " + event.getType().name());
-
}
-
-
});
-
}
-
-
public void tearDown() {
-
framework = null ;
-
}
-
-
public void testBundle() throws Exception {
-
framework.addClassPath( "C:\\osgi\\lib" );
-
framework.addClassPath( "C:\\osgi\\lib\\commons-logging.jar" );
-
IBundle[] bundle = framework.installBundles( "C:\\osgi" );
-
bundle[ 0 ].start();
-
}
-
}
看到了吧,这里使用Bundle对象了~看看Bundle所对应的Activator类吧:
java 代码
- public class Activator implements IBundleActivator{
-
-
public void start(IBundleContext context) throws Exception {
-
System.out.println( "Test.start(" + context + ")" );
-
}
-
-
public void stop(IBundleContext context) throws Exception {
-
System.out.println( "Test.stop(" + context + ")" );
-
}
-
}
"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从入门到深入...
利用R-OSGi实现分布式OSGi应用 本文通过介绍传统 OSGi 应用程序及 R-OSGi 的实现方式入手,阐述了 R-OSGi 对于 OSGi 规范的实现方式。然后通过一个简单的功能实现由浅入深地讲述传统 OSGi 和 R-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实战、OSGI进阶、OSGI 入门和整合Spring、OSGI原理与最佳实践
OSGI 实例 eclipse 插件开发
《 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组件编程,详细介绍用eclipse和Equinox开发OSGI程序
osgi资料
适合开始研究和深入研究JAVA OSGI框架的应用开发者
本书基于作者多年使用OSGi的经验而编写,涵盖了OSGi从入门到深入的知识体系,从OSGi的简介开题,介绍OSGi的作用及基本概念;其后进入OSGi实战,结合实例讲解如何基于OSGi框架编写模块化、动态化的各种Java应用;最后...
本文档主要针对OSGi.NET模块化框架使用进行了描述 OSGi.NET框架是一个参照了OSGi规范的模块化管理框架。框架为应用程序(组件(bundle))提供了一个标准环境。整个框架可以划分为一些层次: 1.运行环境 2.模块...
OSGi 中文版 中文 OSGi中文OSGi 中文版 中文 OSGi中文OSGi 中文版 中文 OSGi中文OSGi 中文版 中文 OSGi中文OSGi 中文版 中文 OSGi中文OSGi 中文版 中文 OSGi中文OSGi 中文版 中文 OSGi中文OSGi 中文版 中文 OSGi中文