2011年6月5日日曜日

OSGiのコマンドを作成する

前々からOSGiの勉強はせねばと思いながら、何も着手せずでしたが、
ちょろちょろと勉強し始めました。

今回はOSGiコマンドの作成方法を調査しました。
OSGiコマンドは、OSGiコンソールで実行できるコマンドです。
> headers 1

今回はuname,whatamiコマンドを作成します。

1.ActivatorにCommandProviderを継承する
2.public void _XXXX(CommandInterpreter ci) throws Exception を定義する。
※XXXXはコマンド名になります。

これだけで完了です。
では、実際動作するか確認します。
Run As→ OSGi Framework でosgi consoleを立ち上げる
osgi consoleでuname,whatamiをタイプする。

これで、実装したコマンドが実行されます。

OSGiコマンドは基本的に、先勝ちの方式のようです。
なので、headersなどの既に定義されているものは再定義はできないようです。

以下サンプルです。
  1. package ph.jpn.calm.osgi.sample;  
  2.   
  3. import java.net.URL;  
  4. import java.rmi.server.ServerCloneException;  
  5. import java.util.Dictionary;  
  6. import java.util.Enumeration;  
  7. import java.util.Hashtable;  
  8. import java.util.logging.Logger;  
  9.   
  10. import javax.naming.Context;  
  11.   
  12. import org.osgi.framework.Bundle;  
  13. import org.osgi.framework.BundleActivator;  
  14. import org.osgi.framework.BundleContext;  
  15. import org.osgi.framework.Constants;  
  16. import org.osgi.framework.ServiceReference;  
  17. import org.eclipse.osgi.framework.console.CommandInterpreter;  
  18. import org.eclipse.osgi.framework.console.CommandProvider;  
  19. import org.eclipse.osgi.framework.util.Headers;  
  20.   
  21. public class Activator implements BundleActivator ,CommandProvider {  
  22.   
  23.  private BundleContext context;  
  24.    
  25.  /* 
  26.   * (non-Javadoc) 
  27.   * @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext) 
  28.   */  
  29.  public void start(BundleContext context) throws Exception {  
  30.     
  31.   System.out.println("Hello World!!");  
  32.   this.context=context;  
  33.   Hashtable properties = new Hashtable();  
  34.   context.registerService(CommandProvider.class.getName(), this, properties);  
  35.     
  36.  }  
  37.    
  38.  /* 
  39.   * (non-Javadoc) 
  40.   * @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext) 
  41.   */  
  42.  public void stop(BundleContext context) throws Exception {  
  43.   System.out.println("Goodbye World!!");  
  44.  }  
  45.   
  46.  @Override  
  47.  public String getHelp() {  
  48.   StringBuffer buffer = new StringBuffer();  
  49.   buffer.append("\tuname - returns framework information\n");  
  50.   return buffer.toString();  
  51.  }  
  52.    
  53.  public void _uname(CommandInterpreter ci) throws Exception {  
  54.   String vendor = context.getProperty(Constants.FRAMEWORK_VENDOR);  
  55.   String version = context.getProperty(Constants.FRAMEWORK_VERSION);  
  56.   String osName = context.getProperty(Constants.FRAMEWORK_OS_NAME);  
  57.   String osVersion = context.getProperty(Constants.FRAMEWORK_OS_VERSION);  
  58.   System.out.println("\n " + vendor + " "   
  59.     + version + " (" + osName + " "   
  60.     + osVersion + ")");  
  61.     
  62.     
  63.  }  
  64.   
  65.   
  66.  public void _whatami(CommandInterpreter ci) throws Exception {  
  67.   try {  
  68.    long id = Long.parseLong(ci.nextArgument());  
  69.    Bundle bundle = context.getBundle(id);  
  70.    //  
  71.    if(bundle==null){  
  72.     System.out.println("No ID");  
  73.    }  
  74.    URL url = bundle.getEntry("plugin.xml");  
  75.    if(url != null) {  
  76.     System.out.println("\n I'm" + bundle.getSymbolicName() + ") a plug-in");  
  77.    } else {  
  78.     System.out.println("\n I'm " + bundle.getSymbolicName() + ") not a plug-in");  
  79.    }  
  80.   } catch (NumberFormatException nfe) {  
  81.    System.out.println("\n Error processing command");  
  82.   }  
  83.  }  
  84.    
  85.   
  86. }