---
title: "OSGi Note"
author: "louisling"
date: "2009-01-09T01:11:21Z"
tags: ["C", "C++", "Eclipse", "Java", "OSGI"]
slug: "osgi-note"
draft: false
lastmod: "2009-01-09T01:11:21Z"
id: 313140
url: "https://louisling.iteye.com/blog/313140"
---

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

```java
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
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
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
@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

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

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

public class Activator implements BundleActivator {
    ServiceReference ref;

    public void start(BundleContext context) throws Exception {			
        ref = context.getServiceReference(Calculator.class.getName());
        
        Calculator d = (Calculator) context.getService(ref);		
        System.out.println(d.plus(2, 3));
    }
    
    public void stop(BundleContext context) throws Exception {		
        context.ungetService(ref);
    }
}
```