首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 其他教程 > 操作系统 >

从外部起动OSGi,并和OSGi进行交互

2014-05-24 
从外部启动OSGi,并和OSGi进行交互本范例主要演示如下技术点:????? 1、从OSGi外部启动OSGi容器????? 2、在OSG

从外部启动OSGi,并和OSGi进行交互

本范例主要演示如下技术点:

????? 1、从OSGi外部启动OSGi容器

????? 2、在OSGi容器内访问外部的资源文件

????? 3、在OSGi容器内访问外部的类方法(通过反射机制)

?

关键点:

????? 开发一个单独的Bundle,提供一个供外部访问的OSGi服务,外部程序在启动OSGi容器时通过反射的方式将外部的ClassLoader设置到该Bundle中,供其他Bundle访问外部资源用。

?

一、开发单独Bundle

???? 1、接口及实现类

?

???? 2、Activator类

?

二、外部程序启动OSGi容器

public class Test {public void run(){try{//要加载的bundlesString osgiBundles = "org.eclipse.osgi_3.3.0.v20070530.jar@1:start,com.cjm.osgi.common.ClassLoaderBundle_1.0.0.jar@2:start";//## 启动Equinox的配置 ##//重要:缺省情况下,Equinox启动后马上shutdown,通过该参数配置Equinox启动后不关闭FrameworkProperties.setProperty("osgi.noShutdown", "true");//重要:让Equinox不检查eclipse.product和eclipse.application配置FrameworkProperties.setProperty("eclipse.ignoreApp", "true");FrameworkProperties.setProperty("osgi.bundles.defaultStartLevel", "4");FrameworkProperties.setProperty("osgi.bundles", osgiBundles);//bundles所在路径String bundlePath = "./lib";FrameworkProperties.setProperty("osgi.syspath", bundlePath);//启动OSGi容器EclipseStarter.run(new String[]{"-configuration", "configuration", "-console"}, null);//取得BundleContextBundleContext bundleContext = EclipseStarter.getSystemBundleContext();if(bundleContext!=null){for(int i=0;i<bundleContext.getBundles().length;i++){Bundle bundle = bundleContext.getBundles()[i];//通过反射将OSGi外部的ClassLoader对象设置到ClassLoaderBundle Bundle中//这样,就可以在OSGi容器内部加载OSGi外部的资源了if(bundle.getSymbolicName().equalsIgnoreCase("com.cjm.osgi.common.ClassLoaderBundle")){Class clazz = bundle.loadClass(ClassLoaderServiceImpl.class.getName());Object obj = clazz.newInstance();Method method = clazz.getMethod("setClassLoader", ClassLoader.class);method.invoke(obj, this.getClass().getClassLoader());}}}else{System.out.println("bundleContext is null");}while(true){Thread.sleep(10000);}}catch(Exception ex){ex.printStackTrace();}finally{try{EclipseStarter.shutdown();}catch(Exception e){e.printStackTrace();}}}public static void main(String[] args) {Test t = new Test();t.run();}}

?

Equinox的启动参数:
http://help.eclipse.org/helios/index.jsp?topic=/org.eclipse.platform.doc.isv/reference/misc/runtime-options.html

??

1 楼 neusoft_jerry 2011-12-19   哥们你演示的   3、在OSGi容器内访问外部的类方法(通过反射机制)
代码运行过嘛?我这边怎么在class.newinstance的时候就不返回了呢? 2 楼 neusoft_jerry 2011-12-20   neusoft_jerry 写道哥们你演示的   3、在OSGi容器内访问外部的类方法(通过反射机制)
代码运行过嘛?我这边怎么在class.newinstance的时候就不返回了呢?

lz,好使了,因为我newInstance()的时候,该类运行所必须link的某些类没link,所以会出现能load但不能newInstance()的问题。
严重感谢楼主啊!
还是知识共享好!楼主加油啊!
也希望以后常交流!

热点排行