יום רביעי, 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!

אין תגובות:

הוסף רשומת תגובה