# Equinox OSGi Konsole um eigene Commands erweitern.



## Thomas Darimont (7. September 2007)

Hallo,

hier mal ein kleines Beispiel wie man die Equinox OSGi Konsole über einen eigenen CommandProvider um eigene Commands erweitern kann.

Dazu bauen wir uns einfach ein neues OSGi Bundle mit folgendem Activator:

```
package de.tutorials.eclipse.osgi.commands;

import java.util.Date;

import javax.script.ScriptEngineManager;
import javax.script.ScriptException;

import org.eclipse.core.runtime.Plugin;
import org.eclipse.osgi.framework.console.CommandInterpreter;
import org.eclipse.osgi.framework.console.CommandProvider;
import org.osgi.framework.BundleContext;

/**
 * The activator class controls the plug-in life cycle
 */
public class Activator extends Plugin {

    public class UtilCommandProvider implements CommandProvider {
        public String getHelp() {
            StringBuilder help = new StringBuilder();
             help.append("\ttime - tells the current time\n");
             help.append("\teval - evaluates the given expression\n");
             return help.toString();
        }

        public void _time(CommandInterpreter commandInterpreter) {
            System.out.println("Current date: " + new Date());
        }
        
        public void _eval(CommandInterpreter commandInterpreter){
            String expression = commandInterpreter.nextArgument();
            
            if(null != expression){
                try {
                    Object result = new ScriptEngineManager().getEngineByName("JavaScript").eval(expression);
                    System.out.println(result);
                } catch (ScriptException e) {
                    e.printStackTrace();
                }    
            }
        }
    }

    // The plug-in ID
    public static final String PLUGIN_ID = "de.tutorials.eclipse.osgi.commands";

    // The shared instance
    private static Activator plugin;

    /**
     * The constructor
     */
    public Activator() {
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.core.runtime.Plugins#start(org.osgi.framework.BundleContext)
     */
    public void start(BundleContext context) throws Exception {
        super.start(context);
        plugin = this;

	context.registerService(
			CommandProvider.class.getName(),
			new UtilCommandProvider(), null);
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.core.runtime.Plugin#stop(org.osgi.framework.BundleContext)
     */
    public void stop(BundleContext context) throws Exception {
        plugin = null;
        super.stop(context);
    }

    /**
     * Returns the shared instance
     * 
     * @return the shared instance
     */
    public static Activator getDefault() {
        return plugin;
    }

}
```

Dann starten wir das ganze in einer entsprechenden Konfiguration.
Geben wir nun in der Konsole Help ein sehen wir folgendes:

```
osgi> help
    time - tells the current time
    eval - evaluates the given expression

---Application Admin Commands---
    activeApps - lists all running application IDs
    apps - lists all installed application IDs
    lockApp <application id> - locks the specified application ID
    schedApp <application id> <time fil
...
```

Unsere Befehle können wir dann so ausprobieren:

```
osgi> time
Current date: Fri Sep 07 16:37:16 CEST 2007

osgi> eval 1+2*3
7.0

osgi> eval (1+2)*3
9.0

osgi> eval Math.sqrt(2)
1.4142135623730951
```

Gruß Tom


----------

