OSGi Note

文章介绍了如何搭建最简化的osgi环境,这个在osgi实战中也有提到。

转自:http://louisling.javaeye.com/blog/313140

 

Create OSGi environment outside Eclipse.
------------------------------------------------------------------------------
1) Create folder and files:

Java代码 复制代码

  1. C:/equinox  
  2. │  run.bat  
  3. │  
  4. ├─configuration  
  5. │      config.ini  
  6. │  
  7. └─plugins  
  8.         org.eclipse.osgi.services_3.1.200.v20070605.jar  
  9.         org.eclipse.osgi_3.3.0.v20070530.jar  
  10.         OsgiHello_1.0.jar  
  11.         OsgiHelloTest_1.0.jar 
C:/equinox
│  run.bat
│
├─configuration
│      config.ini
│
└─plugins
        org.eclipse.osgi.services_3.1.200.v20070605.jar
        org.eclipse.osgi_3.3.0.v20070530.jar
        OsgiHello_1.0.jar
        OsgiHelloTest_1.0.jar

2) config.ini

Java代码 复制代码

  1. osgi.noShutdown=true
  2. osgi.bundles=org.eclipse.osgi.services_3.1.200.v20071203.jar@start
  3. osgi.bundles.defaultStartLevel=4
  4. osgi.configuration.cascaded=false
  5. eclipse.ignoreApp=true
osgi.noShutdown=true
osgi.bundles=org.eclipse.osgi.services_3.1.200.v20071203.jar@start
osgi.bundles.defaultStartLevel=4
osgi.configuration.cascaded=false
eclipse.ignoreApp=true

//If want to start other bundles when start OSGi by run.bat, then use the following:

Java代码 复制代码

  1. gi.bundles=org.eclipse.osgi.services_3.1.200.v20071203.jar@start,OsgiHello_1.0.jar@start,OsgiHelloTest_1.0.jar@start
gi.bundles=org.eclipse.osgi.services_3.1.200.v20071203.jar@start,OsgiHello_1.0.jar@start,OsgiHelloTest_1.0.jar@start

3) run.bat

Java代码 复制代码

  1. @echo off  
  2. java -jar plugins/org.eclipse.osgi_3.4.2.R34x_v20080826-1230.jar -configuration configuration -console 
@echo off
java -jar plugins/org.eclipse.osgi_3.4.2.R34x_v20080826-1230.jar -configuration configuration -console

4) start OSGi bundle by run.bat
5) Install bundle

Java代码 复制代码

  1. install file:/C:/Equinox/plugins/OsgiHello_1.0.jar  
  2. install file:/C:/Equinox/plugins/OsgiHelloTest_1.0.jar  
  3. //Or:
  4. osgi>install file:plugins/OsgiHello_1.0.jar  
  5. osgi>install file:plugins/OsgiHelloTest_1.0.jar 
install file:/C:/Equinox/plugins/OsgiHello_1.0.jar
install file:/C:/Equinox/plugins/OsgiHelloTest_1.0.jar
//Or:
osgi>install file:plugins/OsgiHello_1.0.jar
osgi>install file:plugins/OsgiHelloTest_1.0.jar

6) If not specify -configuration configuration in run.bat, would generate configuration in folder of plugins
7) The bundle "org.eclipse.osgi.services" is optional.
   If don't include it, don't need specify it in config.ini/osgi.bundles
New an OSGi bundle in Eclipse
------------------------------------------------------------------------------
1) New a OSGi bundle
   New a Plug-in Project
   Select "Target Platform": an OSGi framework: Equinox/standard
   Create a plug-in using one of the templates: Hello OSGi Bundle
2) Export service in BundleActivator
1. Just export interface package, don't need implementation package in META-INF/MANIFEST.MF/Runtime/Exported Packages
2. Register and unregister bundle in BundleActivator:

Java代码 复制代码

  1. public class Activator implements BundleActivator {  
  2.     ServiceRegistration reg;  
  3.     BundleActivator.start() {  
  4.         Calculator c = new CalculatorImpl();  
  5.         reg = context.registerService(Calculator.class.getName(), c, null);      
  6.     }  
  7. public void stop(BundleContext context) throws Exception {  
  8.         reg.unregister();  
  9.     }  
public class Activator implements BundleActivator {
    ServiceRegistration reg;

    BundleActivator.start() {
        Calculator c = new CalculatorImpl();
        reg = context.registerService(Calculator.class.getName(), c, null);    
    }

    public void stop(BundleContext context) throws Exception {
        reg.unregister();
    }
}

3. Use it in other bundle:

Java代码 复制代码

  1. public class Activator implements BundleActivator {  
  2.     ServiceReference ref;  
  3. public void start(BundleContext context) throws Exception {           
  4.         ref = context.getServiceReference(Calculator.class.getName());  
  5.         Calculator d = (Calculator) context.getService(ref);          
  6.         System.out.println(d.plus(2, 3));  
  7.     }  
  8. public void stop(BundleContext context) throws Exception {        
  9.         context.ungetService(ref);  
  10.     }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值