felix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From gno...@apache.org
Subject svn commit: r787270 - in /felix/trunk/gogo: src/aQute/shell/console/ src/aQute/shell/osgi/ src/aQute/shell/runtime/ src/aQute/threadio/ test/test/aQute/shell/runtime/
Date Mon, 22 Jun 2009 15:21:32 GMT
Author: gnodet
Date: Mon Jun 22 15:21:30 2009
New Revision: 787270

URL: http://svn.apache.org/viewvc?rev=787270&view=rev
Log:
FELIX-946: Apply Derek Baum patch on gogo

Modified:
    felix/trunk/gogo/src/aQute/shell/console/Console.java
    felix/trunk/gogo/src/aQute/shell/osgi/OSGiCommands.java
    felix/trunk/gogo/src/aQute/shell/osgi/OSGiShell.java
    felix/trunk/gogo/src/aQute/shell/runtime/Closure.java
    felix/trunk/gogo/src/aQute/shell/runtime/CommandSessionImpl.java
    felix/trunk/gogo/src/aQute/shell/runtime/CommandShellImpl.java
    felix/trunk/gogo/src/aQute/shell/runtime/Parser.java
    felix/trunk/gogo/src/aQute/shell/runtime/Pipe.java
    felix/trunk/gogo/src/aQute/shell/runtime/Reflective.java
    felix/trunk/gogo/src/aQute/threadio/ThreadIOImpl.java
    felix/trunk/gogo/test/test/aQute/shell/runtime/TestParser.java

Modified: felix/trunk/gogo/src/aQute/shell/console/Console.java
URL: http://svn.apache.org/viewvc/felix/trunk/gogo/src/aQute/shell/console/Console.java?rev=787270&r1=787269&r2=787270&view=diff
==============================================================================
--- felix/trunk/gogo/src/aQute/shell/console/Console.java (original)
+++ felix/trunk/gogo/src/aQute/shell/console/Console.java Mon Jun 22 15:21:30 2009
@@ -40,7 +40,7 @@
 		try {
 			while (!quit) {
 				try {
-					CharSequence line = getLine(session.getKeybord());
+					CharSequence line = getLine(session.getKeyboard());
 					if (line != null) {
 						history.add(line);
 						if (history.size() > 40)

Modified: felix/trunk/gogo/src/aQute/shell/osgi/OSGiCommands.java
URL: http://svn.apache.org/viewvc/felix/trunk/gogo/src/aQute/shell/osgi/OSGiCommands.java?rev=787270&r1=787269&r2=787270&view=diff
==============================================================================
--- felix/trunk/gogo/src/aQute/shell/osgi/OSGiCommands.java (original)
+++ felix/trunk/gogo/src/aQute/shell/osgi/OSGiCommands.java Mon Jun 22 15:21:30 2009
@@ -16,6 +16,8 @@
  * specific language governing permissions and limitations
  * under the License.
  */
+// DWB1: osgi:each too verbose (formats reults to System.out)
+// DWB2: ClassNotFoundException should be caught in convert() method
 package aQute.shell.osgi;
 
 import java.io.*;
@@ -152,8 +154,10 @@
 		args.add(null);
 		for (Object x : list) {
 			args.set(0, x);
-			Object result = closure.execute(session, args);
-			System.out.println(session.format(result,Converter.INSPECT));
+			//Object result = closure.execute(session, args);
+			// System.out.println(session.format(result,Converter.INSPECT));
+			// derek: this is way too noisy
+			closure.execute(session, args);
 		}
 	}
 
@@ -204,8 +208,14 @@
 			return convertBundle(in);
         else if (desiredType == ServiceReference.class)
             return convertServiceReference(in);
-        else if (desiredType == Class.class)
-            return Class.forName(in.toString());
+        else if (desiredType == Class.class) {
+            // derek.baum@paremus.com - added try/catch
+            try {
+                return Class.forName(in.toString());
+            } catch (ClassNotFoundException e) {
+                return null;
+            }
+        }
         else if (desiredType.isAssignableFrom(String.class) && in instanceof InputStream)
             return read(((InputStream) in));
 

Modified: felix/trunk/gogo/src/aQute/shell/osgi/OSGiShell.java
URL: http://svn.apache.org/viewvc/felix/trunk/gogo/src/aQute/shell/osgi/OSGiShell.java?rev=787270&r1=787269&r2=787270&view=diff
==============================================================================
--- felix/trunk/gogo/src/aQute/shell/osgi/OSGiShell.java (original)
+++ felix/trunk/gogo/src/aQute/shell/osgi/OSGiShell.java Mon Jun 22 15:21:30 2009
@@ -16,14 +16,14 @@
  * specific language governing permissions and limitations
  * under the License.
  */
+// DWB3: dynamically load optional framework components to reduce dependencies
+// DWB4: get() with trailing colon causes org.osgi.framework.InvalidSyntaxException
 package aQute.shell.osgi;
 
 import org.osgi.framework.*;
 import org.osgi.service.command.*;
 import org.osgi.service.component.*;
 import org.osgi.service.packageadmin.*;
-import org.osgi.service.permissionadmin.*;
-import org.osgi.service.startlevel.*;
 import org.osgi.service.threadio.*;
 
 import aQute.shell.runtime.*;
@@ -45,14 +45,28 @@
         addCommand("osgi", commands);
         setConverter(commands);
         if (bundle.getState() == Bundle.ACTIVE) {
-            addCommand("osgi", commands.service(StartLevel.class.getName(),
-                    null), StartLevel.class);
             addCommand("osgi", commands.service(PackageAdmin.class.getName(),
                     null), PackageAdmin.class);
-            addCommand("osgi", commands.service(
-                    PermissionAdmin.class.getName(), null),
-                    PermissionAdmin.class);
             addCommand("osgi", commands.getContext(), BundleContext.class);
+            
+            try {
+                // derek - dynamically load StartLevel to avoid import dependency
+                String sl = "org.osgi.service.startlevel.StartLevel";
+                Class<?> slClass = bundle.loadClass(sl);
+                addCommand("osgi", commands.service(sl, null), slClass);
+            } catch (ClassNotFoundException e) {
+            }
+            
+            try {
+                // derek - dynamically load PermissionAdmin to avoid import dependency
+                String pa = "org.osgi.service.permissionadmin.PermissionAdmin";
+                Class<?> paClass = bundle.loadClass(pa);
+                addCommand("osgi", commands.service(pa, null), paClass);
+            } catch (ClassNotFoundException e) {
+            }
+        }
+        else {
+            System.err.println("eek! bundle not active: " + bundle);
         }
     }
 
@@ -74,6 +88,10 @@
 
                 String service = name.substring(0, n);
                 String function = name.substring(n + 1);
+                
+                // derek - fix org.osgi.framework.InvalidSyntaxException
+                if (service.length() == 0 || function.length() == 0)
+                    return null;
 
                 String filter = String.format(
                         "(&(osgi.command.scope=%s)(osgi.command.function=%s))",

Modified: felix/trunk/gogo/src/aQute/shell/runtime/Closure.java
URL: http://svn.apache.org/viewvc/felix/trunk/gogo/src/aQute/shell/runtime/Closure.java?rev=787270&r1=787269&r2=787270&view=diff
==============================================================================
--- felix/trunk/gogo/src/aQute/shell/runtime/Closure.java (original)
+++ felix/trunk/gogo/src/aQute/shell/runtime/Closure.java Mon Jun 22 15:21:30 2009
@@ -16,6 +16,9 @@
  * specific language governing permissions and limitations
  * under the License.
  */
+// DWB5: session.err is not redirected when creating pipeline
+// DWB6: add 'set -x' trace feature if echo is set
+// DWB7: removing variable via 'execute("name=") throws OutOfBoundsException
 package aQute.shell.runtime;
 
 import java.util.*;
@@ -48,6 +51,7 @@
             if (pipes.isEmpty()) {
                 current.setIn(session.in);
                 current.setOut(session.out);
+                current.setErr(session.err);    // XXX: derek.baum@paremus.com
             } else {
                 Pipe previous = pipes.get(pipes.size() - 1);
                 previous.connect(current);
@@ -81,9 +85,21 @@
     Object executeStatement(List<CharSequence> statement) throws Exception {
         Object result;
         List<Object> values = new ArrayList<Object>();
-        Object cmd = eval(statement.remove(0));
-        for (CharSequence token : statement)
+        CharSequence statement0 = statement.remove(0);
+        
+        // derek: FEATURE: add set -x facility if echo is set
+        StringBuilder buf = new StringBuilder("+ ");
+        buf.append(statement0);
+        
+        Object cmd = eval(statement0);
+        for (CharSequence token : statement) {
+            buf.append(' ');
+            buf.append(token);
             values.add(eval(token));
+        }
+        
+        if (Boolean.TRUE.equals(session.get("echo")))
+            System.err.println(buf);
 
         result = execute(cmd, values);
         return result;
@@ -108,7 +124,8 @@
             String scmd = cmd.toString();
 
             if (values.size() > 0 && "=".equals(values.get(0))) {
-                if (values.size() == 0)
+                //if (values.size() == 0)
+                if (values.size() == 1)            // derek: BUGFIX
                     return session.variables.remove(scmd);
                 else {
                     Object value = execute(values.get(1), values.subList(2,

Modified: felix/trunk/gogo/src/aQute/shell/runtime/CommandSessionImpl.java
URL: http://svn.apache.org/viewvc/felix/trunk/gogo/src/aQute/shell/runtime/CommandSessionImpl.java?rev=787270&r1=787269&r2=787270&view=diff
==============================================================================
--- felix/trunk/gogo/src/aQute/shell/runtime/CommandSessionImpl.java (original)
+++ felix/trunk/gogo/src/aQute/shell/runtime/CommandSessionImpl.java Mon Jun 22 15:21:30 2009
@@ -16,6 +16,9 @@
  * specific language governing permissions and limitations
  * under the License.
  */
+// DWB8: throw IllegatlStateException if session used after closed (as per rfc132)
+// DWB9: there is no API to list all variables: https://www.osgi.org/bugzilla/show_bug.cgi?id=49
+// DWB10: add SCOPE support: https://www.osgi.org/bugzilla/show_bug.cgi?id=51
 package aQute.shell.runtime;
 
 import java.io.*;
@@ -25,191 +28,209 @@
 import org.osgi.service.command.*;
 
 public class CommandSessionImpl implements CommandSession, Converter {
-	String						COLUMN			= "%-20s %s\n";
-	InputStream in;
-	PrintStream out;
-	PrintStream err;
-	CommandShellImpl service;
-	Map<Object, Object> variables = new HashMap<Object, Object>();
-
-	CommandSessionImpl(CommandShellImpl service, InputStream in,
-			PrintStream out, PrintStream err) {
-		this.service = service;
-		this.in = in;
-		this.out = out;
-		this.err = err;
-	}
-
-	public void close() {
-	}
-
-	public Object execute(CharSequence commandline) throws Exception {
-		assert service != null;
-		assert service.threadIO != null;
-		
-		Closure impl = new Closure(this, null, commandline);
-		Object result = impl.execute(this, null);
-		return result;
-	}
-
-	public InputStream getKeybord() {
-		return in;
-	}
-
-	public Object get(String name) {
-		if (variables != null && variables.containsKey(name))
-			return variables.get(name);
-
-		return service.get(name);
-	}
-
-	public void put(String name, Object value) {
-		variables.put(name, value);
-	}
-
-	public PrintStream getConsole() {
-		return out;
-	}
-	
-	
-	@SuppressWarnings("unchecked")
-	public
-	CharSequence format(Object target, int level, Converter escape ) throws Exception {
-		if (target == null)
-			return "null";
-
-		if (target instanceof CharSequence )
-			return (CharSequence) target;
-		
-		for (Converter c : service.converters) {
-			CharSequence s = c.format(target, level, this);
-			if (s != null)
-				return s;
-		}
-
-		if (target.getClass().isArray()) {
-			if ( target.getClass().getComponentType().isPrimitive()) {
-				if ( target.getClass().getComponentType() == boolean.class ) 
-					return Arrays.toString((boolean[]) target);
-				else if ( target.getClass().getComponentType() == byte.class ) 
-					return Arrays.toString((byte[]) target);
-				else if ( target.getClass().getComponentType() == short.class ) 
-					return Arrays.toString((short[]) target);
-				else if ( target.getClass().getComponentType() == int.class ) 
-					return Arrays.toString((int[]) target);
-				else if ( target.getClass().getComponentType() == long.class ) 
-					return Arrays.toString((long[]) target);
-				else if ( target.getClass().getComponentType() == float.class ) 
-					return Arrays.toString((float[]) target);
-				else if ( target.getClass().getComponentType() == double.class ) 
-					return Arrays.toString((double[]) target);
-				else if ( target.getClass().getComponentType() == char.class ) 
-					return Arrays.toString((char[]) target);
-			}
-			target = Arrays.asList((Object[]) target);
-		}
-		if (target instanceof Collection) {
-			if (level == Converter.INSPECT) {
-				StringBuilder sb = new StringBuilder();
-				Collection<?> c = (Collection<?>) target;
-				for (Object o : c) {
-					sb.append(format(o, level + 1, this));
-					sb.append("\n");
-				}
-				return sb;
-			} else if (level == Converter.LINE) {
-				StringBuilder sb = new StringBuilder();
-				String del = "[";
-				Collection<?> c = (Collection<?>) target;
-				for (Object o : c) {
-					sb.append(del);
-					sb.append(format(o, level + 1, this));
-					del = ", ";
-				}
-				sb.append("]");
-				return sb;
-			}
-		}
-		if ( target instanceof Dictionary ) {
-			Map<Object,Object> result = new HashMap<Object,Object>();
-			for ( Enumeration e = ((Dictionary)target).keys(); e.hasMoreElements(); ) {
-				Object key = e.nextElement();
-				result.put(key, ((Dictionary)target).get(key));
-			}
-			target = result;
-		}
-		if (target instanceof Map) {
-			if (level == Converter.INSPECT) {
-				StringBuilder sb = new StringBuilder();
-				Map<?,?> c = (Map<?,?>) target;
-				for (Map.Entry<?,?> entry : c.entrySet()) {
-					CharSequence key = format(entry.getKey(), level + 1, this);
-					sb.append(key);
-					for ( int i=key.length(); i<20; i++ )
-						sb.append(' ');
-					sb.append(format(entry.getValue(), level + 1, this));
-					sb.append("\n");
-				}
-				return sb;
-			} else if (level == Converter.LINE) {
-				StringBuilder sb = new StringBuilder();
-				String del = "[";
-				Map<?,?> c = (Map<?,?>) target;
-				for (Map.Entry<?,?> entry : c.entrySet()) {
-					sb.append(del);
-					sb.append(format(entry, level + 1,this));
-					del = ", ";
-				}
-				sb.append("]");
-				return sb;
-			}
-		}
-		if (level == Converter.INSPECT)
-			return inspect(target);
-		else
-			return target.toString();
-	}
-
-	CharSequence inspect(Object b) {
-		boolean found = false;
-		Formatter f = new Formatter();
-		Method methods[] = b.getClass().getMethods();
-		for (Method m : methods) {
-			try {
-				String name = m.getName();
-				if (m.getName().startsWith("get")
-						&& !m.getName().equals("getClass")
-						&& m.getParameterTypes().length == 0
-						&& Modifier.isPublic(m.getModifiers())) {
-					found = true;
-					name = name.substring(3);
-					m.setAccessible(true);
-					Object value = m.invoke(b, (Object[]) null);
-					f.format(COLUMN, name, format(value, Converter.LINE, this));
-				}
-			} catch (IllegalAccessException e) {
-				// Ignore
-			} catch (Exception e) {
-				e.printStackTrace();
-			}
-		}
-		if (found)
-			return (StringBuilder) f.out();
-		else
-			return b.toString();
-	}
-
-
-	public Object convert(Class<?> desiredType, Object in)  {
-		return service.convert(desiredType, in);
-	}
-
-	public CharSequence format(Object result, int inspect) {
-	    try {
-		return format(result,inspect,this);
-	    } catch(Exception e ) {
-	        return "<can not format " + result + ":" + e;
-	    }
-	}
+    String COLUMN = "%-20s %s\n";
+    InputStream in;
+    PrintStream out;
+    PrintStream err;
+    CommandShellImpl service;
+    Map<Object, Object> variables = new HashMap<Object, Object>();
+    private boolean closed;    // derek
+
+    CommandSessionImpl(CommandShellImpl service, InputStream in, PrintStream out, PrintStream
err) {
+        this.service = service;
+        this.in = in;
+        this.out = out;
+        this.err = err;
+    }
+
+    public void close() {
+        this.closed = true;    // derek
+    }
+
+    public Object execute(CharSequence commandline) throws Exception {
+        assert service != null;
+        assert service.threadIO != null;
+        
+        if (closed)
+            throw new IllegalStateException("session is closed");    // derek
+
+        Closure impl = new Closure(this, null, commandline);
+        Object result = impl.execute(this, null);
+        return result;
+    }
+
+    public InputStream getKeyboard() {
+        return in;
+    }
+
+    public Object get(String name) {
+        // XXX: derek.baum@paremus.com
+        // there is no API to list all variables, so overload name == null
+        if (name == null)
+            return variables.keySet();
+
+        if (variables != null && variables.containsKey(name))
+            return variables.get(name);
+
+        // XXX: derek: add SCOPE support
+        if (name.startsWith("*:")) {
+            String path = variables.containsKey("SCOPE") ? variables.get("SCOPE").toString()
+                    : "osgi:*";
+            String func = name.substring(2);
+            for (String scope : path.split(":")) {
+                Object result = service.get(scope + ":" + func);
+                if (result != null)
+                    return result;
+            }
+            return null;
+        }
+        return service.get(name);
+    }
+
+    public void put(String name, Object value) {
+        variables.put(name, value);
+    }
+
+    public PrintStream getConsole() {
+        return out;
+    }
+
+    @SuppressWarnings("unchecked")
+    public CharSequence format(Object target, int level, Converter escape) throws Exception
{
+        if (target == null)
+            return "null";
+
+        if (target instanceof CharSequence)
+            return (CharSequence) target;
+
+        for (Converter c : service.converters) {
+            CharSequence s = c.format(target, level, this);
+            if (s != null)
+                return s;
+        }
+
+        if (target.getClass().isArray()) {
+            if (target.getClass().getComponentType().isPrimitive()) {
+                if (target.getClass().getComponentType() == boolean.class)
+                    return Arrays.toString((boolean[]) target);
+                else if (target.getClass().getComponentType() == byte.class)
+                    return Arrays.toString((byte[]) target);
+                else if (target.getClass().getComponentType() == short.class)
+                    return Arrays.toString((short[]) target);
+                else if (target.getClass().getComponentType() == int.class)
+                    return Arrays.toString((int[]) target);
+                else if (target.getClass().getComponentType() == long.class)
+                    return Arrays.toString((long[]) target);
+                else if (target.getClass().getComponentType() == float.class)
+                    return Arrays.toString((float[]) target);
+                else if (target.getClass().getComponentType() == double.class)
+                    return Arrays.toString((double[]) target);
+                else if (target.getClass().getComponentType() == char.class)
+                    return Arrays.toString((char[]) target);
+            }
+            target = Arrays.asList((Object[]) target);
+        }
+        if (target instanceof Collection) {
+            if (level == Converter.INSPECT) {
+                StringBuilder sb = new StringBuilder();
+                Collection<?> c = (Collection<?>) target;
+                for (Object o : c) {
+                    sb.append(format(o, level + 1, this));
+                    sb.append("\n");
+                }
+                return sb;
+            }
+            else if (level == Converter.LINE) {
+                StringBuilder sb = new StringBuilder();
+                String del = "[";
+                Collection<?> c = (Collection<?>) target;
+                for (Object o : c) {
+                    sb.append(del);
+                    sb.append(format(o, level + 1, this));
+                    del = ", ";
+                }
+                sb.append("]");
+                return sb;
+            }
+        }
+        if (target instanceof Dictionary) {
+            Map<Object, Object> result = new HashMap<Object, Object>();
+            for (Enumeration e = ((Dictionary) target).keys(); e.hasMoreElements();) {
+                Object key = e.nextElement();
+                result.put(key, ((Dictionary) target).get(key));
+            }
+            target = result;
+        }
+        if (target instanceof Map) {
+            if (level == Converter.INSPECT) {
+                StringBuilder sb = new StringBuilder();
+                Map<?, ?> c = (Map<?, ?>) target;
+                for (Map.Entry<?, ?> entry : c.entrySet()) {
+                    CharSequence key = format(entry.getKey(), level + 1, this);
+                    sb.append(key);
+                    for (int i = key.length(); i < 20; i++)
+                        sb.append(' ');
+                    sb.append(format(entry.getValue(), level + 1, this));
+                    sb.append("\n");
+                }
+                return sb;
+            }
+            else if (level == Converter.LINE) {
+                StringBuilder sb = new StringBuilder();
+                String del = "[";
+                Map<?, ?> c = (Map<?, ?>) target;
+                for (Map.Entry<?, ?> entry : c.entrySet()) {
+                    sb.append(del);
+                    sb.append(format(entry, level + 1, this));
+                    del = ", ";
+                }
+                sb.append("]");
+                return sb;
+            }
+        }
+        if (level == Converter.INSPECT)
+            return inspect(target);
+        else
+            return target.toString();
+    }
+
+    CharSequence inspect(Object b) {
+        boolean found = false;
+        Formatter f = new Formatter();
+        Method methods[] = b.getClass().getMethods();
+        for (Method m : methods) {
+            try {
+                String name = m.getName();
+                if (m.getName().startsWith("get") && !m.getName().equals("getClass")
+                        && m.getParameterTypes().length == 0 && Modifier.isPublic(m.getModifiers()))
{
+                    found = true;
+                    name = name.substring(3);
+                    m.setAccessible(true);
+                    Object value = m.invoke(b, (Object[]) null);
+                    f.format(COLUMN, name, format(value, Converter.LINE, this));
+                }
+            } catch (IllegalAccessException e) {
+                // Ignore
+            } catch (Exception e) {
+                e.printStackTrace();
+            }
+        }
+        if (found)
+            return (StringBuilder) f.out();
+        else
+            return b.toString();
+    }
+
+    public Object convert(Class<?> desiredType, Object in) {
+        return service.convert(desiredType, in);
+    }
+
+    public CharSequence format(Object result, int inspect) {
+        try {
+            return format(result, inspect, this);
+        } catch (Exception e) {
+            return "<can not format " + result + ":" + e;
+        }
+    }
 
 }

Modified: felix/trunk/gogo/src/aQute/shell/runtime/CommandShellImpl.java
URL: http://svn.apache.org/viewvc/felix/trunk/gogo/src/aQute/shell/runtime/CommandShellImpl.java?rev=787270&r1=787269&r2=787270&view=diff
==============================================================================
--- felix/trunk/gogo/src/aQute/shell/runtime/CommandShellImpl.java (original)
+++ felix/trunk/gogo/src/aQute/shell/runtime/CommandShellImpl.java Mon Jun 22 15:21:30 2009
@@ -16,7 +16,9 @@
  * specific language governing permissions and limitations
  * under the License.
  */
-
+// DWB11: add removeCommand: https://www.osgi.org/bugzilla/show_bug.cgi?id=49
+// DWB12: there is no API to list commands: https://www.osgi.org/bugzilla/show_bug.cgi?id=49
+// DWB13: addCommand() fails to add static methods (if target is Class)
 package aQute.shell.runtime;
 
 import java.io.*;
@@ -34,8 +36,9 @@
 
     public CommandShellImpl() {
         addCommand("shell", this, "addCommand" );
+        addCommand("shell", this, "removeCommand" );    // derek
     }
-
+    
     public CommandSession createSession(InputStream in, PrintStream out,
             PrintStream err) {
 
@@ -61,7 +64,7 @@
             return null;
 
         String function = name.substring(n);
-
+        
         Object cmd = null;
 
         if (commands.containsKey(name)) {
@@ -76,7 +79,15 @@
                     }
                 }
             }
+            
+            // XXX: derek.baum@paremus.com
+            // there is no API to list commands
+            // so override otherwise illegal name ":"
+            if (cmd == null && name.equals(":")) {
+                return Collections.unmodifiableSet(commands.keySet());
+            }
         }
+        
         if (cmd == null)
             return null;
 
@@ -87,7 +98,9 @@
     }
 
     public void addCommand(String scope, Object target) {
-        addCommand(scope,target,target.getClass());
+        // derek - fix target class
+        Class<?> tc = (target instanceof Class) ? (Class<?>) target : target.getClass();
+        addCommand(scope, target, tc);
     }
 
     public void addCommand(String scope, Object target, Class<?> functions) {
@@ -103,12 +116,27 @@
     public void addCommand(String scope, Object target, String function) {
         commands.put((scope + ":" + function).toLowerCase(), target);
     }
+    
+    // derek.baum@paremus.com: need removeCommand, so stopped bundles can clean up.
+    public void removeCommand(String scope, String function) {
+        String func = (scope + ":" + function).toLowerCase();
+        commands.remove(func);
+    }
+    
+    public void removeCommand(Object target) {
+        for (Iterator<Object> i = commands.values().iterator(); i.hasNext();) {
+            if (i.next() == target)
+                i.remove();
+        }
+    }
 
-    public String[] getFunctions(Class<?> target) {
+    private String[] getFunctions(Class<?> target) {
         String[] functions;
         Set<String> list = new TreeSet<String>();
         Method methods[] = target.getMethods();
         for (Method m : methods) {
+            if (m.getDeclaringClass().equals(Object.class))    // derek
+                continue;
             list.add(m.getName());
             if (m.getName().startsWith("get")) {
                 String s = m.getName().substring(3);
@@ -116,6 +144,7 @@
                     list.add(s.substring(0, 1).toLowerCase() + s.substring(1));
             }
         }
+        
         functions = list.toArray(new String[list.size()]);
         return functions;
     }

Modified: felix/trunk/gogo/src/aQute/shell/runtime/Parser.java
URL: http://svn.apache.org/viewvc/felix/trunk/gogo/src/aQute/shell/runtime/Parser.java?rev=787270&r1=787269&r2=787270&view=diff
==============================================================================
--- felix/trunk/gogo/src/aQute/shell/runtime/Parser.java (original)
+++ felix/trunk/gogo/src/aQute/shell/runtime/Parser.java Mon Jun 22 15:21:30 2009
@@ -16,6 +16,8 @@
  * specific language governing permissions and limitations
  * under the License.
  */
+// DWB14: parser loops if // comment at start of program
+// DWB15: allow program to have trailing ';'
 package aQute.shell.runtime;
 
 import java.util.*;
@@ -31,11 +33,16 @@
 	}
 
 	void ws() {
-		while (!eof() && Character.isWhitespace(peek())) {
-			current++;
+	        // derek: BUGFIX: loop if comment  at beginning of input
+		//while (!eof() && Character.isWhitespace(peek())) {
+		while (!eof() && (Character.isWhitespace(peek()) || current == 0)) {
+        		if (current != 0 || Character.isWhitespace(peek()))
+        			current++;
 			if (peek() == '/' && current < text.length()-2 && text.charAt(current
+ 1) == '/') {
 				comment();
 			}
+			if (current == 0)
+			    break;
 		}
 	}
 
@@ -57,7 +64,11 @@
 
 		if (c == '\\') {
 			escaped = true;
-			c = text.charAt(++current);
+			++current;
+			if (eof())
+        			throw new RuntimeException("Eof found after \\"); // derek
+			    
+			c = text.charAt(current);
 
 			switch (c) {
 			case 't':
@@ -117,14 +128,17 @@
 		statements.add(statement());
 		while (peek() == ';') {
 			current++;
-			statements.add(statement());
+			// derek: BUGFIX: allow trailing ;
+			ws();
+			if (!eof())
+        			statements.add(statement());
 		}
 		return statements;
 	}
 
 	public List<CharSequence> statement() {
 		List<CharSequence> statement = new ArrayList<CharSequence>();
-		statement.add(value());
+        	statement.add(value());
 		while (!eof()) {
 			ws();
 			if (peek() == '|' || peek() == ';')
@@ -192,6 +206,7 @@
 					break;
 			}
 		}
+		
 		return text.subSequence(start, current);
 	}
 

Modified: felix/trunk/gogo/src/aQute/shell/runtime/Pipe.java
URL: http://svn.apache.org/viewvc/felix/trunk/gogo/src/aQute/shell/runtime/Pipe.java?rev=787270&r1=787269&r2=787270&view=diff
==============================================================================
--- felix/trunk/gogo/src/aQute/shell/runtime/Pipe.java (original)
+++ felix/trunk/gogo/src/aQute/shell/runtime/Pipe.java Mon Jun 22 15:21:30 2009
@@ -16,7 +16,7 @@
  * specific language governing permissions and limitations
  * under the License.
  */
-
+// DWB16: redirect System.err when creating pipe
 package aQute.shell.runtime;
 
 import java.io.*;
@@ -27,6 +27,7 @@
 public class Pipe extends Thread {
 	InputStream in;
 	PrintStream out;
+        PrintStream err;    // derek
 	PipedOutputStream pout;
 	Closure closure;
 	Exception exception;
@@ -47,6 +48,10 @@
 		this.out = out;
 	}
 
+	public void setErr(PrintStream err) {
+		this.err = err;
+	}
+
 	public Pipe connect(Pipe next) throws IOException {
 		next.setOut(out);
 		pout = new PipedOutputStream();
@@ -57,7 +62,8 @@
 	}
 
 	public void run() {
-		closure.session.service.threadIO.setStreams(in, out, System.err);
+		//closure.session.service.threadIO.setStreams(in, out, System.err);
+		closure.session.service.threadIO.setStreams(in, out, err);    // derek
 		try {
 			for (List<CharSequence> statement : statements) {
 				result = closure.executeStatement(statement);

Modified: felix/trunk/gogo/src/aQute/shell/runtime/Reflective.java
URL: http://svn.apache.org/viewvc/felix/trunk/gogo/src/aQute/shell/runtime/Reflective.java?rev=787270&r1=787269&r2=787270&view=diff
==============================================================================
--- felix/trunk/gogo/src/aQute/shell/runtime/Reflective.java (original)
+++ felix/trunk/gogo/src/aQute/shell/runtime/Reflective.java Mon Jun 22 15:21:30 2009
@@ -1,5 +1,5 @@
 /*
- * Licensed to the Apache Software Foundation (ASF) under one
+* Licensed to the Apache Software Foundation (ASF) under one
  * or more contributor license agreements.  See the NOTICE file
  * distributed with this work for additional information
  * regarding copyright ownership.  The ASF licenses this file
@@ -16,6 +16,10 @@
  * specific language governing permissions and limitations
  * under the License.
  */
+// DWB16: coerce() doesn't support static methods
+// DWB17: coerce() doesn't support static void main(String[]) in rfc132
+// DWB18: coerce() doesn't extract cause from InvocationTargetException
+// DWB19: coerce() won't add empty array to satisfy Object[] argument
 package aQute.shell.runtime;
 
 import java.lang.reflect.*;
@@ -39,7 +43,10 @@
     public Object method(CommandSession session, Object target, String name,
             List<Object> args) throws IllegalArgumentException,
             IllegalAccessException, InvocationTargetException, Exception {
-        Method[] methods = target.getClass().getMethods();
+        // derek - support static methods
+        //Method[] methods = target.getClass().getMethods();
+        Class<?> tc = (target instanceof Class) ? (Class<?>)target : target.getClass();
+        Method[] methods = tc.getMethods();
         name = name.toLowerCase();
 
         String get = "get" + name;
@@ -52,26 +59,36 @@
         Method bestMethod = null;
         Object[] bestArgs = null;
         int match = -1;
+        ArrayList<Class<?>[]> possibleTypes = new ArrayList<Class<?>[]>();
   // derek
+
         for (Method m : methods) {
             String mname = m.getName().toLowerCase();
             if (mname.equals(name) || mname.equals(get) || mname.equals(set)
-                    || mname.equals(is)) {
+                    || mname.equals(is) || mname.equals("_main")) {    // derek - added _main
                 Class<?>[] types = m.getParameterTypes();
+                ArrayList<Object> xargs = new ArrayList<Object>(args); // derek
- BUGFIX don't modify args
 
                 // Check if the command takes a session
                 if (types.length > 0
                         && CommandSession.class.isAssignableFrom(types[0])) {
-                    args.add(0, session);
+                    xargs.add(0, session);
                 }
 
                 Object[] parms = new Object[types.length];
                 // if (types.length >= args.size() ) {
-                int local = coerce(session, target, types, parms, args);
-                if (local == types.length || local > match) {
-                    bestMethod = m;
-                    bestArgs = parms;
-                    match = local;
+                int local = coerce(session, target, types, parms, xargs);
+                if ((local >= xargs.size()) && (local >= types.length)) { 
     // derek - stop no-args
+                    boolean exact = (local == xargs.size() && local == types.length);
+                    if (exact || local > match) {
+                        bestMethod = m;
+                        bestArgs = parms;
+                        match = local;
+                    }
+                    if (exact)
+                        break;
                 }
+                else
+                    possibleTypes.add(types);    // derek
                 // }
                 // if (match == -1 && types.length == 1
                 // && types[0] == Object[].class) {
@@ -84,10 +101,36 @@
 
         if (bestMethod != null) {
             bestMethod.setAccessible(true);
-            return bestMethod.invoke(target, bestArgs);
-        } else
-            throw new IllegalArgumentException("Cannot find command:" + name
-                    + " with args:" + args);
+            // derek: BUGFIX catch InvocationTargetException
+            // return bestMethod.invoke(target, bestArgs);
+            try {
+                return bestMethod.invoke(target, bestArgs);
+            } catch (InvocationTargetException e) {
+                Throwable cause = e.getCause();
+                if (cause instanceof Exception)
+                    throw (Exception)cause;
+                throw e;
+            }
+        } else {
+            //throw new IllegalArgumentException("Cannot find command:" + name + " with args:"
+ args);
+            // { derek
+            ArrayList<String> list = new ArrayList<String>();
+            for (Class<?>[] types : possibleTypes) {
+                StringBuilder buf = new StringBuilder();
+                buf.append('(');
+                for (Class<?> type : types) {
+                    if (buf.length() > 1)
+                        buf.append(", ");
+                    buf.append(type.getSimpleName());
+                }
+                buf.append(')');
+                list.add(buf.toString());
+            }
+
+            throw new IllegalArgumentException(
+                    String.format("Cannot coerce %s%s to any of %s", name,  args, list));
+            // } derek
+        }
     }
 
     /**
@@ -104,6 +147,7 @@
      * @return
      * @throws Exception
      */
+    @SuppressWarnings("unchecked")
     private int coerce(CommandSession session, Object target, Class<?> types[],
             Object out[], List<Object> in) throws Exception {
         int i = 0;
@@ -111,11 +155,29 @@
             out[i] = null;
             try {
                 // Try to convert one argument
-                out[i] = coerce(session, target, types[i], in.get(i));
+                // derek: add empty array as extra argument
+                //out[i] = coerce(session, target, types[i], in.get(i));
+                if (i == in.size())
+                    out[i] = NO_MATCH;
+                else
+                    out[i] = coerce(session, target, types[i], in.get(i));
+                
                 if (out[i] == NO_MATCH) {
                     // Failed
                     // No match, check for varargs
                     if (types[i].isArray() && i == types.length - 1) {
+                        
+                        // derek - expand final array arg
+                        if (i < in.size()) {
+                            Object arg = in.get(i);
+                            if (arg instanceof List) {
+                                List<Object> args = (List<Object>)arg;
+                                in = new ArrayList<Object>(in);
+                                in.remove(i);
+                                in.addAll(args);
+                            }
+                        }
+                    
                         // Try to parse the remaining arguments in an array
                         Class<?> component = types[i].getComponentType();
                         Object components = Array.newInstance(component, in
@@ -132,14 +194,18 @@
                         }
                         out[n] = components;
                         // Is last element, so we will quite hereafter
-                        return n;
+                        // return n;
+                        if (i == in.size())
+                            ++i;
+                        return i;    // derek - return number of args converted
                     }
                     return -1;
                 }
                 i++;
             } catch (Exception e) {
-                // e.printStackTrace();
-                System.err.println(e);
+                System.err.println("Reflective:" + e);
+                e.printStackTrace();
+                
                 // should get rid of those exceptions, but requires
                 // reg ex matching to see if it throws an exception.
                 // dont know what is better
@@ -175,23 +241,28 @@
                 return NO_MATCH;
             }
         }
-        if (type == boolean.class)
-            return new Boolean(string);
-        else if (type == byte.class)
-            return new Byte(string);
-        else if (type == char.class) {
-            if (string.length() == 1)
-                return string.charAt(0);
-        } else if (type == short.class)
-            return new Short(string);
-        else if (type == int.class)
-            return new Integer(string);
-        else if (type == float.class)
-            return new Float(string);
-        else if (type == double.class)
-            return new Double(string);
-        else if (type == long.class)
-            return new Long(string);
+        
+        try {
+            if (type == boolean.class)
+                return new Boolean(string);
+            else if (type == byte.class)
+                return new Byte(string);
+            else if (type == char.class) {
+                if (string.length() == 1)
+                    return string.charAt(0);
+            } else if (type == short.class)
+                return new Short(string);
+            else if (type == int.class)
+                return new Integer(string);
+            else if (type == float.class)
+                return new Float(string);
+            else if (type == double.class)
+                return new Double(string);
+            else if (type == long.class)
+                return new Long(string);
+        }
+        catch (NumberFormatException e) {
+        }
 
         return NO_MATCH;
     }

Modified: felix/trunk/gogo/src/aQute/threadio/ThreadIOImpl.java
URL: http://svn.apache.org/viewvc/felix/trunk/gogo/src/aQute/threadio/ThreadIOImpl.java?rev=787270&r1=787269&r2=787270&view=diff
==============================================================================
--- felix/trunk/gogo/src/aQute/threadio/ThreadIOImpl.java (original)
+++ felix/trunk/gogo/src/aQute/threadio/ThreadIOImpl.java Mon Jun 22 15:21:30 2009
@@ -16,14 +16,17 @@
  * specific language governing permissions and limitations
  * under the License.
  */
+// DWB20: ThreadIO should check and reset IO if something (e.g. jetty) overrides
 package aQute.threadio;
 
 import java.io.*;
+import java.util.logging.Logger;
 
 import org.osgi.service.component.*;
 import org.osgi.service.threadio.*;
 
 public class ThreadIOImpl implements ThreadIO {
+    static private final Logger log = Logger.getLogger(ThreadIOImpl.class.getName());
 	ThreadPrintStream err = new ThreadPrintStream(System.err);
 	ThreadPrintStream out = new ThreadPrintStream(System.out);
 	ThreadInputStream in = new ThreadInputStream(System.in);
@@ -51,7 +54,25 @@
         System.setErr(err);
 	}
 	
+	private void checkIO() {    // derek
+	    if (System.in != in) {
+	        log.fine("ThreadIO: eek! who's set System.in=" + System.in);
+		System.setIn(in);
+	    }
+	    
+	    if (System.out != out) {
+	        log.fine("ThreadIO: eek! who's set System.out=" + System.out);
+		System.setOut(out);
+	    }
+	    
+	    if (System.err != err) {
+	        log.fine("ThreadIO: eek! who's set System.err=" + System.err);
+		System.setErr(err);
+	    }
+	}
+	
 	public void close() {
+	    checkIO(); // derek
 	    Marker top = this.current.get();
 	    if ( top == null )
 	        throw new IllegalStateException("No thread io active");
@@ -71,6 +92,7 @@
         assert in != null;
         assert out != null;
         assert err != null;
+        checkIO(); // derek
         Marker marker = new Marker(this,in,out,err, current.get());
 	    this.current.set(marker);
 	    marker.activate();

Modified: felix/trunk/gogo/test/test/aQute/shell/runtime/TestParser.java
URL: http://svn.apache.org/viewvc/felix/trunk/gogo/test/test/aQute/shell/runtime/TestParser.java?rev=787270&r1=787269&r2=787270&view=diff
==============================================================================
--- felix/trunk/gogo/test/test/aQute/shell/runtime/TestParser.java (original)
+++ felix/trunk/gogo/test/test/aQute/shell/runtime/TestParser.java Mon Jun 22 15:21:30 2009
@@ -194,18 +194,18 @@
 
     public void ls() {
         beentheredonethat++;
-        System.out.println("Yes!");
+        System.out.println("ls(): Yes!");
     }
 
     public int ls(int onoff) {
         beentheredonethat += onoff;
-        System.out.println("ls " + onoff);
+        System.out.println("ls(int) " + onoff);
         return onoff;
     }
 
     public void ls(Object args[]) {
         beentheredonethat = args.length;
-        System.out.print("ls [");
+        System.out.print("ls(Object[]) [");
         for (Object i : args)
             System.out.print(i + " ");
         System.out.println("]");



Mime
View raw message