ERLEDIGT
NEIN
NEIN
ANTWORTEN
0
0
ZUGRIFFE
1644
1644
EMPFEHLEN
-
07.09.07 16:38 #1
- Registriert seit
- Jun 2002
- Ort
- Saarbrücken (Saarland)
- Beiträge
- 9.886
- Blog-Einträge
- 29
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:
Code java:1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
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:
Code :1 2 3 4 5 6 7 8 9 10
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:
Code :1 2 3 4 5 6 7 8 9 10 11
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ß TomJava rocks!
How to become a good Java Programmer?
Does IT in Java and .Net
The only valid measurement of code quality: WTFs / minute
Blog
Xing
Twitter
Ähnliche Themen
-
Kleines Beispiel zum EventAdmin Service unter OSGi / Equinox
Von Thomas Darimont im Forum JavaAntworten: 6Letzter Beitrag: 03.08.09, 22:26 -
Beispiel für Springframework / Hibernate / Equinox / OSGi / Eclipse / ExtensionPoints
Von Thomas Darimont im Forum JavaAntworten: 5Letzter Beitrag: 06.11.08, 19:21 -
Synth und OSGi (Equinox) Frage
Von daywalkertp im Forum JavaAntworten: 1Letzter Beitrag: 21.04.08, 16:15 -
Videotutorials zu Eclipse Equinox OSGi Runtime
Von Thomas Darimont im Forum Java Technology NewsAntworten: 0Letzter Beitrag: 15.01.08, 17:14 -
Beispiel für die Verwendung von OSGi Services auf Basis von Equinox
Von Thomas Darimont im Forum JavaAntworten: 0Letzter Beitrag: 17.03.07, 15:00






Zitieren
Login





