From: <ri...@us...> - 2007-02-12 00:30:11
|
Revision: 42 http://techne-dev.svn.sourceforge.net/techne-dev/?rev=42&view=rev Author: rickles Date: 2007-02-11 16:30:11 -0800 (Sun, 11 Feb 2007) Log Message: ----------- Intercept all felix shell commands. Modified Paths: -------------- sandbox/rickles/org.digivitality.techne.shell/.classpath sandbox/rickles/org.digivitality.techne.shell/src/org/digivitality/techne/shell/Shell.java Modified: sandbox/rickles/org.digivitality.techne.shell/.classpath =================================================================== --- sandbox/rickles/org.digivitality.techne.shell/.classpath 2007-02-12 00:27:16 UTC (rev 41) +++ sandbox/rickles/org.digivitality.techne.shell/.classpath 2007-02-12 00:30:11 UTC (rev 42) @@ -4,6 +4,8 @@ <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/> <classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/> <classpathentry kind="lib" path="/org.digivitality.techne.core/lib/commons-logging-1.0.4.jar"/> - <classpathentry kind="lib" path="C:/Workspace/eclipse-3.2/techne/sandbox/tonit/techne-env/bin/techne.jar"/> + <classpathentry kind="lib" path="/org.digivitality.techne.core/lib/felix.jar"/> + <classpathentry kind="lib" path="/org.digivitality.techne.core/bundle/org.apache.felix.shell-0.8.0-SNAPSHOT.jar"/> + <classpathentry kind="lib" path="/org.digivitality.techne.core/classes"/> <classpathentry kind="output" path="classes"/> </classpath> Modified: sandbox/rickles/org.digivitality.techne.shell/src/org/digivitality/techne/shell/Shell.java =================================================================== --- sandbox/rickles/org.digivitality.techne.shell/src/org/digivitality/techne/shell/Shell.java 2007-02-12 00:27:16 UTC (rev 41) +++ sandbox/rickles/org.digivitality.techne.shell/src/org/digivitality/techne/shell/Shell.java 2007-02-12 00:30:11 UTC (rev 42) @@ -5,11 +5,20 @@ import java.io.*; import java.text.*; +import java.lang.reflect.Field; +import java.lang.reflect.Method; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.osgi.framework.Bundle; +import org.osgi.framework.BundleContext; +import org.osgi.framework.ServiceReference; +//import org.ungoverned.osgi.service.shell.ShellService; +import org.apache.felix.shell.ShellService; +import org.apache.felix.shell.impl.*; +import org.apache.felix.framework.*; -import techne.launch.*; +import org.digivitality.techne.launch.felix.*; /** * This is intended to demonstrate the concept of a container shell. @@ -26,6 +35,11 @@ private boolean stopping = false; Thread techneThread; Thread frameworkThread; + FelixLauncher launcher; + FelixFrameworkThread felix; + //static ShellService shellService; + static Method executeCommand; + BundleContext context; public void init() { System.out.println("\n===============================================================================\n"); @@ -47,6 +61,7 @@ public void execute() { final Runnable shell = new Runnable() { + public void run() { String line = null; BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); @@ -90,20 +105,34 @@ * Will use Command pattern later on. */ public void runCommand(String line, PrintStream out, PrintStream err) { - + if (line.equals("set framework to Felix")) { if (FRAMEWORK_SET) { out.print("Framework has already been set to Felix\n"); } else { - frameworkThread = new Thread(new FelixFrameworkThread()); + try { + File file = new File("mytempfile"); + System.out.println("Where am i: " + file.getCanonicalPath()); + } catch (Exception e) { + e.printStackTrace(); + } + felix = new FelixFrameworkThread(); + frameworkThread = new Thread(felix); frameworkThread.setPriority(1); frameworkThread.start(); } } else if (line.equals("exit") || line.equals("quit") ) { stopping = true; - System.exit(0); - } else { - out.print("Unsupported command. Please try another command.\n"); + try { + Runtime.getRuntime().halt(0); + } catch (Exception e) { + e.printStackTrace(); + } finally { + System.exit(0); + } + } else { // assume that it is a framework command + //out.print("Unsupported command. Please try another command.\n"); + execute(line, System.out, System.out); } } }; @@ -112,14 +141,96 @@ techneThread.start(); } - private class FelixFrameworkThread implements Runnable { + protected void execute(String line, PrintStream in, PrintStream out) { + //Bundle[] bundles = felix.getBundles(); + ShellService shell = null; + //System.out.println("Bundles found: " + bundles.length); + + /* + for (int i = 0; i < bundles.length; i++) { + System.out.println("Got this bundle: " + bundles[i].getLocation()); + if (bundles[i].getSymbolicName().startsWith("org.digivitality.techne.bundle")) { + try { + Class clazz = bundles[i].getClass(); + System.out.println("clazz: " + clazz.getName()); + Method m = clazz.getSuperclass().getDeclaredMethod("getShellService", new Class[0]); + m.setAccessible(true); + shell = (ShellService)m.invoke(bundles[i], new Object[0]); + } catch (Exception e) { + e.printStackTrace(); + } + } + } + */ + try { + Method m = Felix.class.getDeclaredMethod("getBundle", new Class[] { long.class }); + m.setAccessible(true); + Bundle systemBundle = (Bundle) m.invoke(launcher.getFelix(), new Object[] { new Long(0) }); + System.out.println("systemBundle superclass: " + systemBundle.getClass().getSuperclass().getName()); + Method getContext = systemBundle.getClass().getSuperclass().getDeclaredMethod("getContext", null); + getContext.setAccessible(true); + context = (BundleContext) getContext.invoke(systemBundle, null); + System.out.println("BundleContext: " + context); + + Class clazz = Class.forName("org.apache.felix.shell.ShellService"); + Object[] args = {line, in, out}; + + ServiceReference ref = context.getServiceReference(ShellService.class.getName()); + if (ref!=null) { + Object o = context.getService(ref); + System.out.println("o: " + o.getClass().getName() + " cl: " + o.getClass().getClassLoader()); + Class[] parameterTypes={String.class,PrintStream.class,PrintStream.class}; + executeCommand = o.getClass().getDeclaredMethod("executeCommand", parameterTypes); + executeCommand.setAccessible(true); + executeCommand.invoke(o, args); + //Thread.currentThread().setContextClassLoader(clazz.getClassLoader()); + //shell = ShellService.class.cast(o); + } + } catch (Exception e) { + e.printStackTrace(); + } + } + + public static BundleContext getBundleContext(Bundle bundle) { + try { + Class clazz = bundle.getClass(); + /* + System.out.println("clazz is " + clazz.getName()); Field field = clazz.getDeclaredField("bundleContext"); + field.setAccessible(true); + return (BundleContext) field.get(bundle); + */ + Method m = clazz.getDeclaredMethod("getContext", new Class[0]); + m.setAccessible(true); + return (BundleContext) m.invoke(bundle, new Object[0]); + } catch (Exception e) { + logger.error("getBundleContext failed", e); + e.printStackTrace(); + return null; + } + } + + class FelixFrameworkThread implements Runnable { + + Bundle[] bundles; + public void run() { try { - TechneLauncher.main(null); + launcher = new FelixLauncher(); + launcher.launch(); System.out.print("techne> "); } catch (Exception e) { e.printStackTrace(); } } + + /* + public Bundle[] getBundles() { + return launcher.getBundles(); + } + + public Object getService(Bundle b, ServiceReference ref) { + return launcher.getService(b, ref); + } + */ } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |