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

本文介绍如何在OSGi容器外部启动容器,并实现在容器内访问外部资源和调用外部类的方法。通过创建一个特殊的Bundle来传递外部ClassLoader,使得其他Bundle能够加载并使用外部资源。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

      1、从OSGi外部启动OSGi容器

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

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

 

关键点:

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

 

一、开发单独Bundle

     1、接口及实现类

public interface ClassLoaderService {
	public ClassLoader getClassLoader();
}

 

public class ClassLoaderServiceImpl implements com.cjm.osgi.common.service.ClassLoaderService {
	private ClassLoader classLoader;
	
	public ClassLoader getClassLoader() {
		return classLoader;
	}

	public void setClassLoader(ClassLoader classLoader) {
		this.classLoader = classLoader;
		
		//## 以下代码仅供参考,在实际的应用中不应该出现在此方法中 ##
		try{
			//读取OSGi容器外的资源文件
			BufferedInputStream stream = new BufferedInputStream(classLoader.getResourceAsStream("readme.txt"));
			byte[] b = new byte[1024];
			int i;
			while((i=stream.read(b))!=-1){
				System.out.println(new String(b, 0, i));
			}
			
			//通过反射调用OSGi容器外的类方法
			Class clazz = classLoader.loadClass("OuterService");
			Object obj = clazz.newInstance();
			Method method = clazz.getMethod("sayHello", String.class);
			Object result = method.invoke(obj, "cjm");
			System.out.println("sayHello result: " + result);
			
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}
}

 

     2、Activator类

public class ClassLoaderBundleActivator implements BundleActivator {
	private ServiceRegistration serviceRegistration;
	
	public void start(BundleContext context) throws Exception {
		//注册服务
		serviceRegistration = context.registerService(ClassLoaderService.class.getName(), new ClassLoaderServiceImpl(), null);
		System.out.println(">>>>> start " + context.getBundle().getSymbolicName());
	}

	public void stop(BundleContext context) throws Exception {
		//注销服务
		serviceRegistration.unregister();
	}
}

 

二、外部程序启动OSGi容器

public class Test {
	public void run(){
		try{
			//要加载的bundles
			String 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);
			
			//取得BundleContext
			BundleContext 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

  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值