|
From: <to...@us...> - 2007-03-03 20:02:20
|
Revision: 103
http://techne-dev.svn.sourceforge.net/techne-dev/?rev=103&view=rev
Author: tonit
Date: 2007-03-03 12:02:20 -0800 (Sat, 03 Mar 2007)
Log Message:
-----------
implemented dynamic provisioning
Modified Paths:
--------------
sandbox/rickles/org.digivitality.techne.shell/src/org/digivitality/techne/shell/Shell.java
Property Changed:
----------------
sandbox/rickles/org.digivitality.techne.shell/
Property changes on: sandbox/rickles/org.digivitality.techne.shell
___________________________________________________________________
Name: svn:ignore
+ classes
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 16:58:08 UTC (rev 102)
+++ sandbox/rickles/org.digivitality.techne.shell/src/org/digivitality/techne/shell/Shell.java 2007-03-03 20:02:20 UTC (rev 103)
@@ -3,32 +3,37 @@
*/
package org.digivitality.techne.shell;
-import java.io.*;
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.io.PrintStream;
+import java.lang.reflect.Method;
+import java.util.Iterator;
import java.util.List;
-import java.util.Map;
-import java.lang.reflect.Method;
+import java.util.Stack;
+import java.util.Vector;
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.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 {
@@ -36,89 +41,96 @@
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 List containerInstances;
- private List bundleList;
- private String containerType;
-
- public Shell() {
+ 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;
- }
-
+ 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));
- while (!stopping)
- {
- System.out.print("techne> ");
+ public void run() {
+ String line = null;
+ BufferedReader in = new BufferedReader(new InputStreamReader(
+ System.in));
- try {
- line = in.readLine();
- } catch (IOException ex) {
- System.err.println("Could not read input, please try again.");
- continue;
- }
+ while (!stopping) {
+ System.out.print("techne> ");
- synchronized (Shell.this)
- {
- if (line == null)
- {
- continue;
- }
+ try {
+ line = in.readLine();
+ } catch (IOException ex) {
+ System.err
+ .println("Could not read input, please try again.");
+ continue;
+ }
- line = line.trim();
+ synchronized (Shell.this) {
+ if (line == null) {
+ continue;
+ }
- if (line.length() == 0)
- {
- continue;
- }
+ line = line.trim();
- try {
- runCommand(line, System.out, System.err);
- } catch (Exception ex) {
- System.err.println("ERROR: " + ex);
- ex.printStackTrace();
- }
- }
- }
+ if (line.length() == 0) {
+ continue;
+ }
+
+ 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");
@@ -127,52 +139,27 @@
frameworkThread = new Thread(felix);
frameworkThread.setPriority(1);
frameworkThread.start();
-
- try {
-
- } catch (Exception e) {
- e.printStackTrace();
- }
}
- } 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);
- }
- }
+ } 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);
}
- 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("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);
}
}
};
@@ -180,92 +167,179 @@
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();
- setContainer();
- System.out.print("techne> ");
+ launcher = new FelixLauncher();
+ launcher.launch();
+ initializeContainer();
+ 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 setContainer() {
+
+ protected void initializeContainer() {
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);
- containerFactory = ContainerFactory.getInstance();
- containerFactory.init();
- containerInstances = containerFactory.getContainerInstances();
- System.out.println("Available container instances: " + containerInstances.size());
- System.out.println("Container type: " + DEFAULT_CONTAINER_TYPE);
+ 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();
+
} 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());
+ }
+
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.
|