|
From: <ri...@us...> - 2007-03-03 21:28:47
|
Revision: 107
http://techne-dev.svn.sourceforge.net/techne-dev/?rev=107&view=rev
Author: rickles
Date: 2007-03-03 13:28:47 -0800 (Sat, 03 Mar 2007)
Log Message:
-----------
Added show components and show bundles commands
Modified Paths:
--------------
sandbox/rickles/org.digivitality.techne.shell/src/org/digivitality/techne/shell/Shell.java
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-03-03 20:29:20 UTC (rev 106)
+++ sandbox/rickles/org.digivitality.techne.shell/src/org/digivitality/techne/shell/Shell.java 2007-03-03 21:28:47 UTC (rev 107)
@@ -3,37 +3,39 @@
*/
package org.digivitality.techne.shell;
-import java.io.BufferedReader;
-import java.io.IOException;
-import java.io.InputStreamReader;
-import java.io.PrintStream;
-import java.lang.reflect.Method;
+import java.io.*;
+import java.util.ArrayList;
+import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
-import java.util.Stack;
-import java.util.Vector;
+import java.util.Map;
+import java.util.Set;
+import java.util.TreeSet;
+import java.lang.reflect.Method;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
-import org.apache.felix.framework.Felix;
-import org.apache.felix.shell.ShellService;
-import org.digivitality.techne.core.ContainerFactory;
-import org.digivitality.techne.core.util.ContainerState;
-import org.digivitality.techne.core.util.bundle;
-import org.digivitality.techne.core.util.containertype;
-import org.digivitality.techne.launch.felix.FelixLauncher;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.FrameworkEvent;
import org.osgi.framework.FrameworkListener;
import org.osgi.framework.ServiceReference;
+import org.apache.felix.shell.ShellService;
+import org.apache.felix.framework.*;
+import org.digivitality.techne.component.ComponentManager;
+import org.digivitality.techne.component.Fragment;
+import org.digivitality.techne.core.ContainerFactory;
+import org.digivitality.techne.core.util.bundle;
+import org.digivitality.techne.core.util.containertype;
+import org.digivitality.techne.launch.felix.*;
+
/**
- * This is intended to demonstrate the concept of a container shell. To set the
- * framework, type 'set framework to Felix' on the command prompt.
+ * This is intended to demonstrate the concept of a container shell.
+ * To set the framework, type 'set framework to Felix' on the command prompt.
*
* @author Rick Liton
- *
+ *
*/
public class Shell implements FrameworkListener {
@@ -41,96 +43,90 @@
private String profile = "Peter";
private static String DEFAULT_CONTAINER_TYPE = "basic";
protected static final Log logger = LogFactory.getLog(Shell.class);
- private boolean stopping = false;
- private ContainerFactory containerFactory;
- private Thread techneThread;
- private Thread frameworkThread;
- protected FelixLauncher launcher;
- protected FelixFrameworkThread felix;
- private static Method executeCommand;
- private BundleContext context;
- protected Shell container;
- private ContainerState containerState;
+ private boolean stopping = false;
+ private ContainerFactory containerFactory;
+ private Thread techneThread;
+ private Thread frameworkThread;
+ protected FelixLauncher launcher;
+ protected FelixFrameworkThread felix;
+ private static Method executeCommand;
+ private BundleContext context;
+ protected Shell container;
+ private List containerInstances;
+ private List bundleList;
+ private String containerType = "basic";
+ private ComponentManager cm = new ComponentManager();
+
+ public Shell() {
- public Shell() {
-
- }
-
+ }
+
public void init() {
- container = this;
- System.out
- .println("\n===============================================================================\n");
- System.out
- .println(" TTTTTTTTTTT EEEEEEEEE CCCCCCCCCC HHHHH HHHHH NNNNN NNNNN EEEEEEEEE");
- System.out
- .println(" TTTTTTTTTTT EEEEEEEEE CCCCCCCCCC HHHHH HHHHH NNNNNN NNNNN EEEEEEEEE");
- System.out
- .println(" TTTTT EEEEE CCCCC HHHHH HHHHH NNNNNNNNNNNN EEEEE");
- System.out
- .println(" TTTTT EEEEEEEE CCCCC HHHHHHHHHHHH NNNNN NNNNNN EEEEEEEE");
- System.out
- .println(" TTTTT EEEEEEEE CCCCC HHHHHHHHHHHH NNNNN NNNNN EEEEEEEE");
- System.out
- .println(" TTTTT EEEEE CCCCC HHHHH HHHHH NNNNN NNNNN EEEEE");
- System.out
- .println(" TTTTT EEEEEEEEE CCCCCCCCCC HHHHH HHHHH NNNNN NNNNN EEEEEEEEE");
- System.out
- .println(" TTTTT EEEEEEEEE CCCCCCCCCC HHHHH HHHHH NNNNN NNNNN EEEEEEEEE\n");
- System.out
- .println(" WELCOME TO THE TECHNE SHELL!");
- System.out
- .println("===============================================================================\n\n");
- System.out.println("Your default profile is " + profile + ".");
- System.out
- .println("Please select a framework by executing the `set framework to' command");
- System.out.println("or specify it in your techne.properties file.\n\n");
- execute();
+ container = this;
+ System.out.println("\n===============================================================================\n");
+ System.out.println(" TTTTTTTTTTT EEEEEEEEE CCCCCCCCCC HHHHH HHHHH NNNNN NNNNN EEEEEEEEE");
+ System.out.println(" TTTTTTTTTTT EEEEEEEEE CCCCCCCCCC HHHHH HHHHH NNNNNN NNNNN EEEEEEEEE");
+ System.out.println(" TTTTT EEEEE CCCCC HHHHH HHHHH NNNNNNNNNNNN EEEEE");
+ System.out.println(" TTTTT EEEEEEEE CCCCC HHHHHHHHHHHH NNNNN NNNNNN EEEEEEEE");
+ System.out.println(" TTTTT EEEEEEEE CCCCC HHHHHHHHHHHH NNNNN NNNNN EEEEEEEE");
+ System.out.println(" TTTTT EEEEE CCCCC HHHHH HHHHH NNNNN NNNNN EEEEE");
+ System.out.println(" TTTTT EEEEEEEEE CCCCCCCCCC HHHHH HHHHH NNNNN NNNNN EEEEEEEEE");
+ System.out.println(" TTTTT EEEEEEEEE CCCCCCCCCC HHHHH HHHHH NNNNN NNNNN EEEEEEEEE\n");
+ System.out.println(" WELCOME TO THE TECHNE SHELL!");
+ System.out.println("===============================================================================\n\n");
+ System.out.println("Your default profile is " + profile + ".");
+ System.out.println("Please select a framework by executing the `set framework to' command");
+ System.out.println("or specify it in your techne.properties file.\n\n");
+ execute();
}
-
+
public void execute() {
final Runnable shell = new Runnable() {
+
+ public void run() {
+ String line = null;
+ BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
- public void run() {
- String line = null;
- BufferedReader in = new BufferedReader(new InputStreamReader(
- System.in));
+ while (!stopping)
+ {
+ System.out.print("techne> ");
- while (!stopping) {
- System.out.print("techne> ");
+ try {
+ line = in.readLine();
+ } catch (IOException ex) {
+ System.err.println("Could not read input, please try again.");
+ continue;
+ }
- try {
- line = in.readLine();
- } catch (IOException ex) {
- System.err
- .println("Could not read input, please try again.");
- continue;
- }
+ synchronized (Shell.this)
+ {
+ if (line == null)
+ {
+ continue;
+ }
- synchronized (Shell.this) {
- if (line == null) {
- continue;
- }
+ line = line.trim();
- line = line.trim();
+ if (line.length() == 0)
+ {
+ continue;
+ }
- if (line.length() == 0) {
- continue;
- }
-
- try {
- runCommand(line, System.out, System.err);
- } catch (Exception ex) {
- System.err.println("ERROR: " + ex);
- ex.printStackTrace();
- }
- }
- }
+ try {
+ runCommand(line, System.out, System.err);
+ } catch (Exception ex) {
+ System.err.println("ERROR: " + ex);
+ ex.printStackTrace();
+ }
+ }
+ }
}
-
+
/*
* 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");
@@ -139,27 +135,71 @@
frameworkThread = new Thread(felix);
frameworkThread.setPriority(1);
frameworkThread.start();
+
+ try {
+
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
}
- } else if (line.startsWith("set container type to")) {
- switchContainerTypeCommand(line, out, err);
- } else if (line.equals("show container type")) {
- System.out.println("container type is "
- + containerState.getType());
- } else if (line.equals("inspect container")) {
- System.out.println(containerState.toString());
- } else if (line.equals("exit") || line.equals("quit")) {
- stopping = true;
- try {
- Runtime.getRuntime().halt(0);
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- System.exit(0);
+ } else if (line.equals("set container type to web")) {
+ List list = containerFactory.getContainerTypes();
+ for (int i = 0; i < list.size(); i++) {
+ containertype ct = (containertype)list.get(i);
+ Map props = ct.getProperties();
+ String type = (String)props.get("value");
+ if (type.equals("web")) {
+ bundleList = ct.getBundles();
+ for (int j = 0; j < bundleList.size(); j++) {
+ bundle b = (bundle)bundleList.get(j);
+ Map p = b.getProperties();
+ execute("start " + p.get("url"), System.out, System.out);
+ }
+ }
}
+ containerType = "web";
+ } else if (line.equals("set container type to basic") || line.equals("set container type to default")) {
+ if (containerType.equals("web")) {
+ int id = 4;
+ for (int j = 0; j < bundleList.size(); j++) {
+ execute("uninstall " + id, System.out, System.out);
+ id++;
+ }
+ }
+ containerType = "basic";
+ } else if (line.equals("show container type")) {
+ System.out.println("Container type is " + containerType);
+ } else if (line.equals("show bundles")) {
+ execute("ps", System.out, System.out);
+ } else if (line.equals("show components")) {
+ Object[] objs = cm.getComponents().toArray();
+ List fragments = new ArrayList();
+ List features = new ArrayList();
+ System.out.println("START LEVEL 1");
+ System.out.println(" ID State Type Name");
+ for (int i = 0; i < objs.length; i++) {
+ ArrayList list = (ArrayList)objs[i];
+ Object[] components = list.toArray();
+ for (int j = 0; j < components.length; j++) {
+ if (components[j] instanceof org.digivitality.techne.component.Fragment)
+ fragments.add(components[j]);
+ else if (components[j] instanceof org.digivitality.techne.component.Feature)
+ features.add(components[j]);
+ }
+ displayComponents(fragments, features, null);
+ }
+ } else if (line.equals("exit") || line.equals("quit") ) {
+ stopping = true;
+ 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);
+ //out.print("Unsupported command. Please try another command.\n");
+ execute(line, System.out, System.out);
}
}
};
@@ -167,179 +207,107 @@
techneThread.setPriority(5);
techneThread.start();
}
-
- private void switchContainerTypeCommand(String line, PrintStream out,
- PrintStream err) {
- String wantType = line.substring(line.lastIndexOf(' ')).trim()
- .toLowerCase();
- List newBundles = new Vector();
- Stack removeBundles = new Stack();
-
- // create "plan" (which to remove and which bundles to install
- if (wantType.equals(containerState.getType())) {
- out.println("container is already in type " + wantType);
- return;
- } else if (wantType.equalsIgnoreCase("basic")
- || wantType.equalsIgnoreCase("default")) {
- // just select "all" to be removed
- removeBundles = containerState.getInstalledBundles();
- } else {
- List list = containerFactory.getContainerTypes();
- removeBundles = containerState.getInstalledBundles();
- for (int i = 0; i < list.size(); i++) {
- containertype ct = (containertype) list.get(i);
- String type = (String) ct.getProperties().get("value");
- if (type.equals(wantType)) {
- // found wanted type
- for (Iterator itBundle = ct.getBundles().iterator(); itBundle
- .hasNext();) {
- bundle b = (bundle) itBundle.next();
- // check if already installed and running..
- // hook it
- String newBundleURL = (String) b.getProperties().get(
- "url");
- if (removeBundles.contains(newBundleURL)) {
- removeBundles.remove(newBundleURL);
- } else {
- newBundles.add(newBundleURL);
- }
- }
- }
- }
- }
- // execute "plan"
- // step 1: uninstall old
- while (removeBundles.size() > 0) {
- String bundleURL = (String) removeBundles.pop();
- containerState.remove(getBundleId(bundleURL), bundleURL);
- execute("uninstall " + getBundleId(bundleURL), out, err);
- }
- // step 2: install new
- for (Iterator itNew = newBundles.iterator(); itNew.hasNext();) {
- String bundleURL = (String) itNew.next();
- execute("start " + bundleURL, out, err);
- containerState.addInstalled(getBundleId(bundleURL), bundleURL);
- }
- // reset container type
- containerState.setType(wantType);
- }
-
- private long getBundleId(String newBundleURL) {
- long id = -1;
- try {
- Bundle[] bundles = (context.getBundles());
- for (int i = 0; i < bundles.length; i++) {
- Bundle bundle = bundles[i];
- if (bundle.getLocation().equals(newBundleURL)) {
- return bundle.getBundleId();
- }
- }
- } catch (Exception e) {
- // this may fail because of the dynamic nature
- }
- return id;
- }
-
+
protected void execute(String line, PrintStream in, PrintStream out) {
+ ShellService shell = null;
try {
- 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);
- }
+ 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);
+ }
} catch (NullPointerException npe) {
- logger.warn("Could not invoke execute method: ", npe);
- System.out
- .println("Cannot execute framework command. Please select a framework.");
+ logger.warn("Could not invoke execute method: ", npe);
+ System.out.println("Cannot execute framework command. Please select a framework.");
} catch (Exception e) {
e.printStackTrace();
}
}
-
+
public static BundleContext getBundleContext(Bundle bundle) {
try {
- Class clazz = bundle.getClass();
- 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 clazz = bundle.getClass();
+ 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 {
- launcher = new FelixLauncher();
- launcher.launch();
- initializeContainer();
- System.out.print("techne> ");
+ launcher = new FelixLauncher();
+ launcher.launch();
+ setContainer();
+ System.out.print("techne> ");
} catch (Exception e) {
- e.printStackTrace();
+ e.printStackTrace();
}
}
/*
- * public Bundle[] getBundles() { return launcher.getBundles(); }
- *
- * public Object getService(Bundle b, ServiceReference ref) { return
- * launcher.getService(b, ref); }
- */
+ public Bundle[] getBundles() {
+ return launcher.getBundles();
+ }
+
+ public Object getService(Bundle b, ServiceReference ref) {
+ return launcher.getService(b, ref);
+ }
+ */
}
-
- protected void initializeContainer() {
+
+ protected void setContainer() {
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);
-
- loadContainer();
-
+ 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);
+ containerFactory = ContainerFactory.getInstance();
+ containerFactory.init();
+ containerInstances = containerFactory.getContainerInstances();
+ System.out.println("Available container instances: " + containerInstances.size());
+ System.out.println("Container type: " + DEFAULT_CONTAINER_TYPE);
} catch (Exception e) {
e.printStackTrace();
}
}
-
- private void loadContainer() {
- containerFactory = ContainerFactory.getInstance();
-
- System.out.println("Available container instances: "
- + containerFactory.getContainerInstances().size());
- // load default bundles
- containerState = new ContainerState(DEFAULT_CONTAINER_TYPE);
- // load
-
- System.out.println("Container type: " + containerState.getType());
+
+ private void displayComponents(List fragments, List features, List featureSets) {
+ // display fragments, features, feature sets in this order
+ if (fragments != null) {
+ Iterator iter = fragments.iterator();
+ while (iter.hasNext()) {
+ Fragment f = (Fragment)iter.next();
+ String n = f.getName();
+ String s = f.getState();
+ String v = f.getVersion();
+ String id = f.getId();
+ System.out.println("[ " + id + "] " + "[" + s + "] [Fragment ] " + n + " (" + v + ")");
+ }
+ }
}
protected Shell getContainer() {
- System.out.println("Container: " + container);
+ System.out.println("Container: " + container);
return container;
}
-
+
/*
* need to create a Techne event dispatcher later
*/
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|