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などの既に定義されているものは再定義はできないようです。

以下サンプルです。
package ph.jpn.calm.osgi.sample;

import java.net.URL;
import java.rmi.server.ServerCloneException;
import java.util.Dictionary;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.logging.Logger;

import javax.naming.Context;

import org.osgi.framework.Bundle;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.Constants;
import org.osgi.framework.ServiceReference;
import org.eclipse.osgi.framework.console.CommandInterpreter;
import org.eclipse.osgi.framework.console.CommandProvider;
import org.eclipse.osgi.framework.util.Headers;

public class Activator implements BundleActivator ,CommandProvider {

 private BundleContext context;
 
 /*
  * (non-Javadoc)
  * @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext)
  */
 public void start(BundleContext context) throws Exception {
  
  System.out.println("Hello World!!");
  this.context=context;
  Hashtable properties = new Hashtable();
  context.registerService(CommandProvider.class.getName(), this, properties);
  
 }
 
 /*
  * (non-Javadoc)
  * @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
  */
 public void stop(BundleContext context) throws Exception {
  System.out.println("Goodbye World!!");
 }

 @Override
 public String getHelp() {
  StringBuffer buffer = new StringBuffer();
  buffer.append("\tuname - returns framework information\n");
  return buffer.toString();
 }
 
 public void _uname(CommandInterpreter ci) throws Exception {
  String vendor = context.getProperty(Constants.FRAMEWORK_VENDOR);
  String version = context.getProperty(Constants.FRAMEWORK_VERSION);
  String osName = context.getProperty(Constants.FRAMEWORK_OS_NAME);
  String osVersion = context.getProperty(Constants.FRAMEWORK_OS_VERSION);
  System.out.println("\n " + vendor + " " 
    + version + " (" + osName + " " 
    + osVersion + ")");
  
  
 }


 public void _whatami(CommandInterpreter ci) throws Exception {
  try {
   long id = Long.parseLong(ci.nextArgument());
   Bundle bundle = context.getBundle(id);
   //
   if(bundle==null){
    System.out.println("No ID");
   }
   URL url = bundle.getEntry("plugin.xml");
   if(url != null) {
    System.out.println("\n I'm" + bundle.getSymbolicName() + ") a plug-in");
   } else {
    System.out.println("\n I'm " + bundle.getSymbolicName() + ") not a plug-in");
   }
  } catch (NumberFormatException nfe) {
   System.out.println("\n Error processing command");
  }
 }
 

}