原创作者: yipsilon
阅读:8436次
评论:1条
更新时间:2011-06-01
偶也开始研究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");
- // ----------------------------------------------
- // 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
- // ----------------------------------------------
- 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)); // Returns true
- System.out.println("explicitURL: " + (explicitURL != null)); // Returns true
- System.out.println("externalURL: " + (externalURL != null)); // Returns true
- 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)); // Returns false
- System.out.println("explicitClass: " + (explicitClass != null)); // Returns true
- System.out.println("externalClass: " + (externalClass != null)); // Returns true
- }
- }
看完了知道这东西该怎么用了吧... 嘿嘿!!
不过要真正实现Webapp功能,还需要加一些东西,这些稍后我会开发出来~~进度的快慢全凭偶的空闲时间的多少... faint
PS:怎么附件加不进去呐??才1.5MB而已....
1 楼 luckychen 2009-05-12 10:18