‏הצגת רשומות עם תוויות osgi. הצג את כל הרשומות
‏הצגת רשומות עם תוויות osgi. הצג את כל הרשומות

יום רביעי, 5 ביוני 2013

OSGI service registration

Declare the operator service:

  1: public class OperatorData {
  2:    String mName; 
  3: 
  4:     public String getmName() {
  5:         return mName;
  6:     }
  7: 
  8:     public void setmName(String mName) {
  9:         this.mName = mName;
 10:     }
 11: }

  1: public interface IOperatorDataService {   
  2:     OperatorData GetOperatorDataByID (int pOperatorID);
  3: }
  4: 

The interface implementer class


  1: public class OperatorDataService implements IOperatorDataService{
  2: 
  3:     public OperatorData GetOperatorDataByID(int pOperatorID) {
  4:         OperatorData theOperatorData = new OperatorData();
  5:         theOperatorData.setmName("Zvika");
  6:         return theOperatorData;    
  7:     }
  8: }

Register the service in the activator start command
UnRegister the service in the activator stop command


  1: package com.mycompany.mybundle;
  2: 
  3: import org.osgi.framework.BundleActivator;
  4: import org.osgi.framework.BundleContext;
  5: import org.osgi.framework.ServiceRegistration;
  6: 
  7: public class Activator implements BundleActivator {
  8: 
  9:     private ServiceRegistration registration;
 10: 
 11:     public void start(BundleContext context) throws Exception {
 12:         System.out.println("Start operator services!" );
 13:         registration = context.registerService(IOperatorDataService.class.getName(),
 14:             new OperatorDataService(), null);
 15:     }
 16: 
 17:     public void stop(BundleContext context) throws Exception {
 18:         System.out.println("Stop operator services!" );
 19:         registration.unregister();
 20:     }
 21: }

uninstall the last version
felix:uninstall 5


Install the new version and start it
g! felix:install file:bundle/myBundle-1.0-SNAPSHOT.jar
Bundle ID: 7
g! start 7


Inspect the bundle interfaces


g! inspect cap service 7
com.mycompany.myBundle [7] provides:
------------------------------------
service; com.mycompany.mybundle.IOperatorDataService with properties:
   service.id = 17
g!

יום שלישי, 14 במאי 2013

Simple felix osgi bundle

The Osgi felix framework can be download from here. Extract the zip file to your local storage and execute the following command in order to start the container :
java -jar bin/felix.jar

Capture6 
Then create a simple Osgi bundle (I used net beans )
Capture7 

In the default created Activator I add a prints command :

package com.mycompany.mybundle;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
public class Activator implements BundleActivator {
public void start(BundleContext context) throws Exception {
System.out.println("Hello World!" );
}
public void stop(BundleContext context) throws Exception {
System.out.println("Goodbye World!" );
}
}
I Compiled the program and copy the created jar into the felix bundle directory :\felix-framework-4.2.1\bundle

In order to  start the bungle follow the following steps :
Import an install the bundle :
felix:install file:bundle/myBundle-1.0-SNAPSHOT.jar
Start the bundle: start 5
Stop the bundle: stop 5
List all the bundles: felix lb
List all the bundles when my bundle is active : start 5 and then felix lb


Capture8 a good starting point to using felix  can be found here