org.apache.felix.gogo.runtime_0.8.0.v201108120515

16:41:04.642 INFO  jd.cli.Main - Decompiling org.apache.felix.gogo.runtime_0.8.0.v201108120515.jar
package org.apache.felix.gogo.api;

import org.apache.felix.service.command.CommandSession;

public abstract interface CommandSessionListener
{
  public abstract void beforeExecute(CommandSession paramCommandSession, CharSequence paramCharSequence);
  
  public abstract void afterExecute(CommandSession paramCommandSession, CharSequence paramCharSequence, Exception paramException);
  
  public abstract void afterExecute(CommandSession paramCommandSession, CharSequence paramCharSequence, Object paramObject);
}

/* Location:
 * Qualified Name:     org.apache.felix.gogo.api.CommandSessionListener
 * Java Class Version: 5 (49.0)
 * JD-Core Version:    0.7.1
 */
package org.apache.felix.gogo.runtime;

class Closure$1 {}

/* Location:
 * Qualified Name:     org.apache.felix.gogo.runtime.Closure.1
 * Java Class Version: 5 (49.0)
 * JD-Core Version:    0.7.1
 */
package org.apache.felix.gogo.runtime;

import java.util.AbstractList;
import java.util.List;

class Closure$ArgList
  extends AbstractList<Object>
{
  private List<Object> list;
  
  public Closure$ArgList(List<Object> arg1)
  {
    list = args;
  }
  
  public String toString()
  {
    StringBuilder buf = new StringBuilder();
    for (Object o : list)
    {
      if (buf.length() > 0) {
        buf.append(' ');
      }
      buf.append(o);
    }
    return buf.toString();
  }
  
  public Object get(int index)
  {
    return index < list.size() ? list.get(index) : null;
  }
  
  public Object remove(int index)
  {
    return list.remove(index);
  }
  
  public int size()
  {
    return list.size();
  }
}

/* Location:
 * Qualified Name:     org.apache.felix.gogo.runtime.Closure.ArgList
 * Java Class Version: 5 (49.0)
 * JD-Core Version:    0.7.1
 */
package org.apache.felix.gogo.runtime;

import java.io.EOFException;
import java.io.PrintStream;
import java.util.AbstractList;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import org.apache.felix.service.command.CommandSession;
import org.apache.felix.service.command.Function;

public class Closure
  implements Function, Evaluate
{
  public static final String LOCATION = ".location";
  private static final String DEFAULT_LOCK = ".defaultLock";
  private static final long serialVersionUID = 1L;
  private static final ThreadLocal<String> location = new ThreadLocal();
  private final CommandSessionImpl session;
  private final Closure parent;
  private final CharSequence source;
  private final List<List<List<Token>>> program;
  private final Object script;
  private Token errTok;
  private Token errTok2;
  private List<Object> parms = null;
  private List<Object> parmv = null;
  
  public Closure(CommandSessionImpl session, Closure parent, CharSequence source)
    throws Exception
  {
    this.session = session;
    this.parent = parent;
    this.source = source;
    script = session.get("0");
    try
    {
      program = new Parser(source).program();
    }
    catch (Exception e)
    {
      throw setLocation(e);
    }
  }
  
  public CommandSessionImpl session()
  {
    return session;
  }
  
  private Exception setLocation(Exception e)
  {
    if (session.get(".defaultLock") == null)
    {
      String loc = (String)location.get();
      if (null == loc)
      {
        loc = script + ":";
        if ((e instanceof SyntaxError))
        {
          SyntaxError se = (SyntaxError)e;
          loc = loc + se.line() + "." + se.column();
        }
        else if (null != errTok)
        {
          loc = loc + errTok.line + "." + errTok.column;
        }
        location.set(loc);
      }
      else if ((null != script) && (!loc.contains(":")))
      {
        location.set(script + ":" + loc);
      }
      session.put(".location", location.get());
    }
    if ((e instanceof EOFError))
    {
      EOFException eofe = new EOFException(e.getMessage());
      eofe.initCause(e);
      return eofe;
    }
    return e;
  }
  
  public Object execute(CommandSession x, List<Object> values)
    throws Exception
  {
    try
    {
      location.remove();
      session.variables.remove(".location");
      return execute(values);
    }
    catch (Exception e)
    {
      throw setLocation(e);
    }
  }
  
  private Object execute(List<Object> values)
    throws Exception
  {
    if (null != values)
    {
      parmv = values;
      parms = new ArgList(parmv);
    }
    else if (null != parent)
    {
      parms = parent.parms;
      parmv = parent.parmv;
    }
    else
    {
      Object args = session.get("args");
      if ((null != args) && ((args instanceof List)))
      {
        parmv = ((List)args);
        parms = new ArgList(parmv);
      }
    }
    Pipe last = null;
    Object[] mark = Pipe.mark();
    for (List<List<Token>> pipeline : program)
    {
      ArrayList<Pipe> pipes = new ArrayList();
      for (List<Token> statement : pipeline)
      {
        Pipe current = new Pipe(this, statement);
        if (pipes.isEmpty())
        {
          if (out == null)
          {
            current.setIn(session.in);
            current.setOut(session.out);
            current.setErr(session.err);
          }
        }
        else
        {
          Pipe previous = (Pipe)pipes.get(pipes.size() - 1);
          previous.connect(current);
        }
        pipes.add(current);
      }
      if (pipes.size() == 1)
      {
        ((Pipe)pipes.get(0)).run();
      }
      else if (pipes.size() > 1)
      {
        for (Pipe pipe : pipes) {
          pipe.start();
        }
        try
        {
          for (Pipe pipe : pipes) {
            pipe.join();
          }
        }
        catch (InterruptedException e)
        {
          for (Pipe pipe : pipes) {
            pipe.interrupt();
          }
          throw e;
        }
      }
      last = (Pipe)pipes.remove(pipes.size() - 1);
      for (Pipe pipe : pipes) {
        if (exception != null)
        {
          Object oloc = session.get(".location");
          String loc = String.valueOf(oloc).contains(":") ? oloc + ": " : "pipe: ";
          
          session.err.println(loc + exception);
          session.put("pipe-exception", exception);
        }
      }
      if (exception != null)
      {
        Pipe.reset(mark);
        throw exception;
      }
    }
    Pipe.reset(mark);
    
    return last == null ? null : result;
  }
  
  private Object eval(Object v)
  {
    String s = v.toString();
    if ("null".equals(s)) {
      v = null;
    } else if ("false".equals(s)) {
      v = Boolean.valueOf(false);
    } else if ("true".equals(s)) {
      v = Boolean.valueOf(true);
    } else {
      v = s;
    }
    return v;
  }
  
  public Object eval(Token t)
    throws Exception
  {
    Object v = null;
    switch (type)
    {
    case WORD: 
      v = Tokenizer.expand(t, this);
      if (t == v) {
        v = eval(v);
      }
      break;
    case CLOSURE: 
      v = new Closure(session, this, t);
      break;
    case EXECUTION: 
      v = new Closure(session, this, t).execute(session, parms);
      break;
    case ARRAY: 
      v = array(t);
      break;
    case ASSIGN: 
      v = type;
      break;
    default: 
      throw new SyntaxError(line, column, "unexpected token: " + type);
    }
    return v;
  }
  
  public Object executeStatement(List<Token> statement)
    throws Exception
  {
    Object echo = session.get("echo");
    String xtrace = null;
    if ((echo != null) && (!"false".equals(echo.toString())))
    {
      StringBuilder buf = new StringBuilder("+");
      for (Token token : statement)
      {
        buf.append(' ');
        buf.append(token.source());
      }
      xtrace = buf.toString();
      session.err.println(xtrace);
    }
    List<Object> values = new ArrayList();
    errTok = ((Token)statement.get(0));
    if ((statement.size() > 3) && (Tokenizer.Type.ASSIGN.equals(get1type))) {
      errTok2 = ((Token)statement.get(2));
    }
    for (Token t : statement)
    {
      Object v = eval(t);
      if ((Tokenizer.Type.EXECUTION == type) && (statement.size() == 1)) {
        return v;
      }
      if ((parms == v) && (parms != null)) {
        values.addAll(parms);
      } else {
        values.add(v);
      }
    }
    Object cmd = values.remove(0);
    if (cmd == null)
    {
      if (values.isEmpty()) {
        return null;
      }
      throw new RuntimeException("Command name evaluates to null: " + errTok);
    }
    if (((cmd instanceof CharSequence)) && (values.size() > 0) && (Tokenizer.Type.ASSIGN.equals(values.get(0))))
    {
      values.remove(0);
      String scmd = cmd.toString();
      if (values.size() == 0) {
        return session.variables.remove(scmd);
      }
      Object value;
      Object value;
      if (values.size() == 1)
      {
        value = values.get(0);
      }
      else
      {
        cmd = values.remove(0);
        if (null == cmd) {
          throw new RuntimeException("Command name evaluates to null: " + errTok2);
        }
        trace2(xtrace, cmd, values);
        
        value = bareword((Token)statement.get(2)) ? executeCmd(cmd.toString(), values) : executeMethod(cmd, values);
      }
      return assignment(scmd, value);
    }
    trace2(xtrace, cmd, values);
    
    return bareword((Token)statement.get(0)) ? executeCmd(cmd.toString(), values) : executeMethod(cmd, values);
  }
  
  private void trace2(String trace1, Object cmd, List<Object> values)
  {
    if ("verbose".equals(session.get("echo")))
    {
      StringBuilder buf = new StringBuilder("+ " + cmd);
      for (Object value : values)
      {
        buf.append(' ');
        buf.append(value);
      }
      String trace2 = buf.toString();
      if (!trace2.equals(trace1)) {
        session.err.println("+" + trace2);
      }
    }
  }
  
  private boolean bareword(Token t)
    throws Exception
  {
    return (type == Tokenizer.Type.WORD) && (t == Tokenizer.expand(t, this)) && ((eval(t) instanceof String));
  }
  
  private Object executeCmd(String scmd, List<Object> values)
    throws Exception
  {
    String scopedFunction = scmd;
    Object x = get(scmd);
    if (!(x instanceof Function))
    {
      if (scmd.indexOf(':') < 0) {
        scopedFunction = "*:" + scmd;
      }
      x = get(scopedFunction);
      if ((x == null) || (!(x instanceof Function)))
      {
        if (session.get(".defaultLock") == null)
        {
          x = get("default");
          if (x == null) {
            x = get("*:default");
          }
          if ((x instanceof Function)) {
            try
            {
              session.put(".defaultLock", Boolean.valueOf(true));
              values.add(0, scmd);
              return ((Function)x).execute(session, values);
            }
            finally
            {
              session.variables.remove(".defaultLock");
            }
          }
        }
        throw new CommandNotFoundException(scmd);
      }
    }
    return ((Function)x).execute(session, values);
  }
  
  private Object executeMethod(Object cmd, List<Object> values)
    throws Exception
  {
    if (values.isEmpty()) {
      return cmd;
    }
    boolean dot = (values.size() > 1) && (".".equals(String.valueOf(values.get(0))));
    if (dot)
    {
      Object target = cmd;
      ArrayList<Object> args = new ArrayList();
      values.remove(0);
      for (Object arg : values) {
        if (".".equals(arg))
        {
          target = Reflective.method(session, target, args.remove(0).toString(), args);
          
          args.clear();
        }
        else
        {
          args.add(arg);
        }
      }
      if (args.size() == 0) {
        return target;
      }
      return Reflective.method(session, target, args.remove(0).toString(), args);
    }
    if ((cmd.getClass().isArray()) && (values.size() == 1))
    {
      Object[] cmdv = (Object[])cmd;
      String index = values.get(0).toString();
      return "length".equals(index) ? Integer.valueOf(cmdv.length) : cmdv[Integer.parseInt(index)];
    }
    return Reflective.method(session, cmd, values.remove(0).toString(), values);
  }
  
  private Object assignment(String name, Object value)
  {
    session.variables.put(name, value);
    return value;
  }
  
  private Object array(Token array)
    throws Exception
  {
    List<Token> list = new ArrayList();
    Map<Token, Token> map = new LinkedHashMap();
    new Parser(array).array(list, map);
    if (map.isEmpty())
    {
      List<Object> olist = new ArrayList();
      for (Token t : list)
      {
        Object oval = eval(t);
        if (oval.getClass().isArray()) {
          for (Object o : (Object[])oval) {
            olist.add(o);
          }
        } else {
          olist.add(oval);
        }
      }
      return olist;
    }
    Map<Object, Object> omap = new LinkedHashMap();
    for (Map.Entry<Token, Token> e : map.entrySet())
    {
      Token key = (Token)e.getKey();
      Object k = eval(key);
      if (!(k instanceof String)) {
        throw new SyntaxError(line, column, "map key null or not String: " + key);
      }
      omap.put(k, eval((Token)e.getValue()));
    }
    return omap;
  }
  
  public Object get(String name)
  {
    if (parms != null)
    {
      if ("args".equals(name)) {
        return parms;
      }
      if ("argv".equals(name)) {
        return parmv;
      }
      if ("it".equals(name)) {
        return parms.get(0);
      }
      if ((name.length() == 1) && (Character.isDigit(name.charAt(0))))
      {
        int i = name.charAt(0) - '0';
        if (i > 0) {
          return parms.get(i - 1);
        }
      }
    }
    return session.get(name);
  }
  
  public Object put(String key, Object value)
  {
    return session.variables.put(key, value);
  }
  
  public String toString()
  {
    return source.toString().trim().replaceAll("\n+", "\n").replaceAll("([^\\\\{(\\[])\n", "\\1;").replaceAll("[ \\\\\t\n]+", " ");
  }
  
  class ArgList
    extends AbstractList<Object>
  {
    private List<Object> list;
    
    public ArgList()
    {
      list = args;
    }
    
    public String toString()
    {
      StringBuilder buf = new StringBuilder();
      for (Object o : list)
      {
        if (buf.length() > 0) {
          buf.append(' ');
        }
        buf.append(o);
      }
      return buf.toString();
    }
    
    public Object get(int index)
    {
      return index < list.size() ? list.get(index) : null;
    }
    
    public Object remove(int index)
    {
      return list.remove(index);
    }
    
    public int size()
    {
      return list.size();
    }
  }
}

/* Location:
 * Qualified Name:     org.apache.felix.gogo.runtime.Closure
 * Java Class Version: 5 (49.0)
 * JD-Core Version:    0.7.1
 */
package org.apache.felix.gogo.runtime;

public class CommandNotFoundException
  extends IllegalArgumentException
{
  private final String command;
  
  public CommandNotFoundException(String command)
  {
    super("Command not found: " + command);
    this.command = command;
  }
  
  public String getCommand()
  {
    return command;
  }
}

/* Location:
 * Qualified Name:     org.apache.felix.gogo.runtime.CommandNotFoundException
 * Java Class Version: 5 (49.0)
 * JD-Core Version:    0.7.1
 */
package org.apache.felix.gogo.runtime;

import java.io.InputStream;
import java.io.PrintStream;
import java.lang.reflect.Method;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.TreeSet;
import java.util.WeakHashMap;
import java.util.concurrent.CopyOnWriteArraySet;
import org.apache.felix.gogo.api.CommandSessionListener;
import org.apache.felix.service.command.CommandProcessor;
import org.apache.felix.service.command.CommandSession;
import org.apache.felix.service.command.Converter;
import org.apache.felix.service.command.Function;
import org.apache.felix.service.threadio.ThreadIO;

public class CommandProcessorImpl
  implements CommandProcessor
{
  protected final Set<Converter> converters = new HashSet();
  protected final Set<CommandSessionListener> listeners = new CopyOnWriteArraySet();
  protected final Map<String, Object> commands = new LinkedHashMap();
  protected final Map<String, Object> constants = new HashMap();
  protected final ThreadIO threadIO;
  protected final WeakHashMap<CommandSession, Object> sessions = new WeakHashMap();
  
  public CommandProcessorImpl(ThreadIO tio)
  {
    threadIO = tio;
  }
  
  public CommandSession createSession(InputStream in, PrintStream out, PrintStream err)
  {
    CommandSessionImpl session = new CommandSessionImpl(this, in, out, err);
    sessions.put(session, null);
    return session;
  }
  
  public void stop()
  {
    for (CommandSession session : sessions.keySet()) {
      session.close();
    }
  }
  
  public void addConverter(Converter c)
  {
    converters.add(c);
  }
  
  public void removeConverter(Converter c)
  {
    converters.remove(c);
  }
  
  public void addListener(CommandSessionListener l)
  {
    listeners.add(l);
  }
  
  public void removeListener(CommandSessionListener l)
  {
    listeners.remove(l);
  }
  
  public Set<String> getCommands()
  {
    return commands.keySet();
  }
  
  Function getCommand(String name, Object path)
  {
    int colon = name.indexOf(':');
    if (colon < 0) {
      return null;
    }
    name = name.toLowerCase();
    Object cmd = commands.get(name);
    String cfunction = name.substring(colon);
    boolean anyScope = (colon == 1) && (name.charAt(0) == '*');
    if ((null == cmd) && (anyScope))
    {
      String scopePath = null == path ? "*" : path.toString();
      for (String scope : scopePath.split(":"))
      {
        if (scope.equals("*")) {
          for (Map.Entry<String, Object> entry : commands.entrySet()) {
            if (((String)entry.getKey()).endsWith(cfunction))
            {
              cmd = entry.getValue();
              break;
            }
          }
        } else {
          cmd = commands.get(scope + cfunction);
        }
        if (cmd != null) {
          break;
        }
      }
    }
    if ((null == cmd) || ((cmd instanceof Function))) {
      return (Function)cmd;
    }
    return new CommandProxy(cmd, cfunction.substring(1));
  }
  
  public void addCommand(String scope, Object target)
  {
    Class<?> tc = (target instanceof Class) ? (Class)target : target.getClass();
    
    addCommand(scope, target, tc);
  }
  
  public void addCommand(String scope, Object target, Class<?> functions)
  {
    if (target == null) {
      return;
    }
    String[] names = getFunctions(functions);
    for (String function : names) {
      addCommand(scope, target, function);
    }
  }
  
  public Object addConstant(String name, Object target)
  {
    return constants.put(name, target);
  }
  
  public Object removeConstant(String name)
  {
    return constants.remove(name);
  }
  
  public void addCommand(String scope, Object target, String function)
  {
    commands.put((scope + ":" + function).toLowerCase(), target);
  }
  
  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();
      }
    }
  }
  
  private String[] getFunctions(Class<?> target)
  {
    Set<String> list = new TreeSet();
    Method[] methods = target.getMethods();
    for (Method m : methods) {
      if (!m.getDeclaringClass().equals(Object.class))
      {
        list.add(m.getName());
        if (m.getName().startsWith("get"))
        {
          String s = m.getName().substring(3);
          if (s.length() > 0) {
            list.add(s.substring(0, 1).toLowerCase() + s.substring(1));
          }
        }
      }
    }
    String[] functions = (String[])list.toArray(new String[list.size()]);
    return functions;
  }
  
  protected void put(String name, Object target)
  {
    commands.put(name, target);
  }
  
  public Object convert(Class<?> desiredType, Object in)
  {
    for (Converter c : converters) {
      try
      {
        Object converted = c.convert(desiredType, in);
        if (converted != null) {
          return converted;
        }
      }
      catch (Exception e)
      {
        e.printStackTrace();
      }
    }
    return null;
  }
  
  public Object eval(CommandSession session, Object[] argv)
    throws Exception
  {
    StringBuilder buf = new StringBuilder();
    for (Object arg : argv)
    {
      if (buf.length() > 0) {
        buf.append(' ');
      }
      buf.append(arg);
    }
    return session.execute(buf);
  }
  
  void beforeExecute(CommandSession session, CharSequence commandline)
  {
    for (CommandSessionListener l : listeners) {
      try
      {
        l.beforeExecute(session, commandline);
      }
      catch (Throwable t) {}
    }
  }
  
  void afterExecute(CommandSession session, CharSequence commandline, Exception exception)
  {
    for (CommandSessionListener l : listeners) {
      try
      {
        l.afterExecute(session, commandline, exception);
      }
      catch (Throwable t) {}
    }
  }
  
  void afterExecute(CommandSession session, CharSequence commandline, Object result)
  {
    for (CommandSessionListener l : listeners) {
      try
      {
        l.afterExecute(session, commandline, result);
      }
      catch (Throwable t) {}
    }
  }
}

/* Location:
 * Qualified Name:     org.apache.felix.gogo.runtime.CommandProcessorImpl
 * Java Class Version: 5 (49.0)
 * JD-Core Version:    0.7.1
 */
package org.apache.felix.gogo.runtime;

import java.util.List;
import org.apache.felix.service.command.CommandSession;
import org.apache.felix.service.command.Function;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;

public class CommandProxy
  implements Function
{
  private BundleContext context;
  private ServiceReference reference;
  private String function;
  private Object target;
  
  public CommandProxy(BundleContext context, ServiceReference reference, String function)
  {
    this.context = context;
    this.reference = reference;
    this.function = function;
  }
  
  public CommandProxy(Object target, String function)
  {
    this.function = function;
    this.target = target;
  }
  
  public Object getTarget()
  {
    return context != null ? context.getService(reference) : target;
  }
  
  public void ungetTarget()
  {
    if (context != null) {
      try
      {
        context.ungetService(reference);
      }
      catch (IllegalStateException e) {}
    }
  }
  
  public Object execute(CommandSession session, List<Object> arguments)
    throws Exception
  {
    Object tgt = getTarget();
    try
    {
      Object localObject1;
      if ((tgt instanceof Function)) {
        return ((Function)tgt).execute(session, arguments);
      }
      return Reflective.method(session, tgt, function, arguments);
    }
    finally
    {
      ungetTarget();
    }
  }
}

/* Location:
 * Qualified Name:     org.apache.felix.gogo.runtime.CommandProxy
 * Java Class Version: 5 (49.0)
 * JD-Core Version:    0.7.1
 */
package org.apache.felix.gogo.runtime;

import java.io.InputStream;
import java.io.PrintStream;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.Arrays;
import java.util.Collection;
import java.util.Dictionary;
import java.util.Enumeration;
import java.util.Formatter;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import org.apache.felix.service.command.CommandSession;
import org.apache.felix.service.command.Converter;
import org.apache.felix.service.command.Function;
import org.apache.felix.service.threadio.ThreadIO;

public class CommandSessionImpl
  implements CommandSession, Converter
{
  public static final String SESSION_CLOSED = "session is closed";
  public static final String VARIABLES = ".variables";
  public static final String COMMANDS = ".commands";
  private static final String COLUMN = "%-20s %s\n";
  protected InputStream in;
  protected PrintStream out;
  PrintStream err;
  private final CommandProcessorImpl processor;
  protected final Map<String, Object> variables = new HashMap();
  private boolean closed;
  
  protected CommandSessionImpl(CommandProcessorImpl shell, InputStream in, PrintStream out, PrintStream err)
  {
    processor = shell;
    this.in = in;
    this.out = out;
    this.err = err;
  }
  
  ThreadIO threadIO()
  {
    return processor.threadIO;
  }
  
  public void close()
  {
    closed = true;
  }
  
  public Object execute(CharSequence commandline)
    throws Exception
  {
    assert (processor != null);
    assert (processor.threadIO != null);
    if (closed) {
      throw new IllegalStateException("session is closed");
    }
    processor.beforeExecute(this, commandline);
    try
    {
      Closure impl = new Closure(this, null, commandline);
      Object result = impl.execute(this, null);
      processor.afterExecute(this, commandline, result);
      return result;
    }
    catch (Exception e)
    {
      processor.afterExecute(this, commandline, e);
      throw e;
    }
  }
  
  public InputStream getKeyboard()
  {
    return in;
  }
  
  public Object get(String name)
  {
    if ((name == null) || (".variables".equals(name))) {
      return variables.keySet();
    }
    if (".commands".equals(name)) {
      return processor.getCommands();
    }
    if (processor.constants.containsKey(name)) {
      return processor.constants.get(name);
    }
    if (variables.containsKey("#" + name))
    {
      Object f = variables.get("#" + name);
      if ((f instanceof Function)) {
        try
        {
          f = ((Function)f).execute(this, null);
        }
        catch (Exception e) {}
      }
      return f;
    }
    if (variables.containsKey(name)) {
      return variables.get(name);
    }
    return processor.getCommand(name, variables.get("SCOPE"));
  }
  
  public void put(String name, Object value)
  {
    variables.put(name, value);
  }
  
  public PrintStream getConsole()
  {
    return out;
  }
  
  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 : processor.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.TYPE) {
          return Arrays.toString((boolean[])target);
        }
        if (target.getClass().getComponentType() == Byte.TYPE) {
          return Arrays.toString((byte[])target);
        }
        if (target.getClass().getComponentType() == Short.TYPE) {
          return Arrays.toString((short[])target);
        }
        if (target.getClass().getComponentType() == Integer.TYPE) {
          return Arrays.toString((int[])target);
        }
        if (target.getClass().getComponentType() == Long.TYPE) {
          return Arrays.toString((long[])target);
        }
        if (target.getClass().getComponentType() == Float.TYPE) {
          return Arrays.toString((float[])target);
        }
        if (target.getClass().getComponentType() == Double.TYPE) {
          return Arrays.toString((double[])target);
        }
        if (target.getClass().getComponentType() == Character.TYPE) {
          return Arrays.toString((char[])target);
        }
      }
      target = Arrays.asList((Object[])target);
    }
    if ((target instanceof Collection))
    {
      if (level == 0)
      {
        StringBuilder sb = new StringBuilder();
        Collection<?> c = (Collection)target;
        for (Object o : c)
        {
          sb.append(format(o, level + 1, this));
          sb.append("\n");
        }
        return sb;
      }
      if (level == 1)
      {
        StringBuilder sb = new StringBuilder();
        Collection<?> c = (Collection)target;
        sb.append("[");
        for (Object o : c)
        {
          if (sb.length() > 1) {
            sb.append(", ");
          }
          sb.append(format(o, level + 1, this));
        }
        sb.append("]");
        return sb;
      }
    }
    if ((target instanceof Dictionary))
    {
      Map<Object, Object> result = new HashMap();
      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 == 0)
      {
        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;
      }
      if (level == 1)
      {
        StringBuilder sb = new StringBuilder();
        Map<?, ?> c = (Map)target;
        sb.append("[");
        for (Map.Entry<?, ?> entry : c.entrySet())
        {
          if (sb.length() > 1) {
            sb.append(", ");
          }
          sb.append(format(entry, level + 1, this));
        }
        sb.append("]");
        return sb;
      }
    }
    if (level == 0) {
      return inspect(target);
    }
    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("%-20s %s\n", new Object[] { name, format(value, 1, this) });
        }
      }
      catch (IllegalAccessException e) {}catch (Exception e)
      {
        e.printStackTrace();
      }
    }
    if (found) {
      return (StringBuilder)f.out();
    }
    return b.toString();
  }
  
  public Object convert(Class<?> desiredType, Object in)
  {
    return processor.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;
    }
  }
}

/* Location:
 * Qualified Name:     org.apache.felix.gogo.runtime.CommandSessionImpl
 * Java Class Version: 5 (49.0)
 * JD-Core Version:    0.7.1
 */
package org.apache.felix.gogo.runtime;

public class EOFError
  extends SyntaxError
{
  private static final long serialVersionUID = 1L;
  
  public EOFError(int line, int column, String message)
  {
    super(line, column, message);
  }
}

/* Location:
 * Qualified Name:     org.apache.felix.gogo.runtime.EOFError
 * Java Class Version: 5 (49.0)
 * JD-Core Version:    0.7.1
 */
package org.apache.felix.gogo.runtime;

public abstract interface Evaluate
{
  public abstract Object eval(Token paramToken)
    throws Exception;
  
  public abstract Object get(String paramString);
  
  public abstract Object put(String paramString, Object paramObject);
}

/* Location:
 * Qualified Name:     org.apache.felix.gogo.runtime.Evaluate
 * Java Class Version: 5 (49.0)
 * JD-Core Version:    0.7.1
 */
package org.apache.felix.gogo.runtime;

class Parser$1 {}

/* Location:
 * Qualified Name:     org.apache.felix.gogo.runtime.Parser.1
 * Java Class Version: 5 (49.0)
 * JD-Core Version:    0.7.1
 */
package org.apache.felix.gogo.runtime;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class Parser
{
  private final Tokenizer tz;
  
  public Parser(CharSequence program)
  {
    tz = new Tokenizer(program);
  }
  
  public List<List<List<Token>>> program()
  {
    List<List<List<Token>>> program = new ArrayList();
    while (tz.next() != Tokenizer.Type.EOT)
    {
      program.add(pipeline());
      switch (tz.type())
      {
      case SEMICOLON: 
      case NEWLINE: 
        break;
      }
    }
    if (tz.next() != Tokenizer.Type.EOT) {
      throw new RuntimeException("Program has trailing text: " + tz.value());
    }
    return program;
  }
  
  private List<List<Token>> pipeline()
  {
    List<List<Token>> pipeline = new ArrayList();
    do
    {
      pipeline.add(command());
      switch (tz.type())
      {
      }
    } while (tz.next() != Tokenizer.Type.EOT);
    Token t = tz.token();
    throw new EOFError(line, column, "unexpected EOT after pipe '|'");
    
    return pipeline;
  }
  
  private List<Token> command()
  {
    List<Token> command = new ArrayList();
    for (;;)
    {
      Token t = tz.token();
      switch (type)
      {
      case WORD: 
      case CLOSURE: 
      case EXECUTION: 
      case ARRAY: 
      case ASSIGN: 
        break;
      default: 
        throw new SyntaxError(line, column, "unexpected token: " + type);
      }
      command.add(t);
      switch (tz.next())
      {
      case SEMICOLON: 
      case NEWLINE: 
      case PIPE: 
      case EOT: 
        return command;
      }
    }
  }
  
  public void array(List<Token> list, Map<Token, Token> map)
    throws Exception
  {
    Token lt = null;
    boolean isMap = false;
    while (tz.next() != Tokenizer.Type.EOT) {
      if (isMap)
      {
        Token key = lt;
        lt = null;
        if (null == key)
        {
          key = tz.token();
          if (tz.next() != Tokenizer.Type.ASSIGN)
          {
            Token t = tz.token();
            throw new SyntaxError(line, column, "map expected '=', found: " + t);
          }
          tz.next();
        }
        Token k = list.isEmpty() ? key : (Token)list.remove(0);
        Token v = tz.token();
        map.put(k, v);
      }
      else
      {
        switch (tz.type())
        {
        case WORD: 
        case CLOSURE: 
        case EXECUTION: 
        case ARRAY: 
          lt = tz.token();
          list.add(lt);
          break;
        case ASSIGN: 
          if (list.size() == 1) {
            isMap = true;
          }
          break;
        default: 
          lt = tz.token();
          throw new SyntaxError(line, column, "unexpected token in list: " + lt);
        }
      }
    }
  }
}

/* Location:
 * Qualified Name:     org.apache.felix.gogo.runtime.Parser
 * Java Class Version: 5 (49.0)
 * JD-Core Version:    0.7.1
 */
package org.apache.felix.gogo.runtime;

import java.io.IOException;
import java.io.InputStream;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.io.PrintStream;
import java.lang.reflect.Method;
import java.util.List;
import org.apache.felix.service.threadio.ThreadIO;

public class Pipe
  extends Thread
{
  static final ThreadLocal<InputStream> tIn = new ThreadLocal();
  static final ThreadLocal<PrintStream> tOut = new ThreadLocal();
  static final ThreadLocal<PrintStream> tErr = new ThreadLocal();
  InputStream in;
  PrintStream out;
  PrintStream err;
  PipedOutputStream pout;
  Closure closure;
  Exception exception;
  Object result;
  List<Token> statement;
  
  public static Object[] mark()
  {
    Object[] mark = { tIn.get(), tOut.get(), tErr.get() };
    return mark;
  }
  
  public static void reset(Object[] mark)
  {
    tIn.set((InputStream)mark[0]);
    tOut.set((PrintStream)mark[1]);
    tErr.set((PrintStream)mark[2]);
  }
  
  public Pipe(Closure closure, List<Token> statement)
  {
    super("pipe-" + statement);
    this.closure = closure;
    this.statement = statement;
    
    in = ((InputStream)tIn.get());
    out = ((PrintStream)tOut.get());
    err = ((PrintStream)tErr.get());
  }
  
  public String toString()
  {
    return "pipe<" + statement + "> out=" + out;
  }
  
  public void setIn(InputStream in)
  {
    this.in = in;
  }
  
  public void setOut(PrintStream out)
  {
    this.out = out;
  }
  
  public void setErr(PrintStream err)
  {
    this.err = err;
  }
  
  public Pipe connect(Pipe next)
    throws IOException
  {
    next.setOut(out);
    next.setErr(err);
    pout = new PipedOutputStream();
    next.setIn(new PipedInputStream(pout));
    out = new PrintStream(pout);
    return next;
  }
  
  public void run()
  {
    tIn.set(in);
    tOut.set(out);
    tErr.set(err);
    closure.session().threadIO().setStreams(in, out, err);
    try
    {
      result = closure.executeStatement(statement);
      if ((result != null) && (pout != null)) {
        if (!Boolean.FALSE.equals(closure.session().get(".FormatPipe"))) {
          out.println(closure.session().format(result, 0));
        }
      }
    }
    catch (Exception e)
    {
      exception = e;
    }
    finally
    {
      out.flush();
      closure.session().threadIO().close();
      try
      {
        if (pout != null) {
          pout.close();
        }
        if ((in instanceof PipedInputStream))
        {
          in.close();
          
          Method m = in.getClass().getDeclaredMethod("receivedLast", (Class[])null);
          
          m.setAccessible(true);
          m.invoke(in, (Object[])null);
        }
      }
      catch (Exception e)
      {
        e.printStackTrace();
      }
    }
  }
}

/* Location:
 * Qualified Name:     org.apache.felix.gogo.runtime.Pipe
 * Java Class Version: 5 (49.0)
 * JD-Core Version:    0.7.1
 */
package org.apache.felix.gogo.runtime;

import java.io.PrintStream;
import java.lang.annotation.Annotation;
import java.lang.reflect.Array;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import org.apache.felix.service.command.CommandSession;
import org.apache.felix.service.command.Parameter;

public final class Reflective
{
  public static final Object NO_MATCH = new Object();
  public static final String MAIN = "_main";
  public static final Set<String> KEYWORDS = new HashSet(Arrays.asList(new String[] { "abstract", "continue", "for", "new", "switch", "assert", "default", "goto", "package", "synchronized", "boolean", "do", "if", "private", "this", "break", "double", "implements", "protected", "throw", "byte", "else", "import", "public", "throws", "case", "enum", "instanceof", "return", "transient", "catch", "extends", "int", "short", "try", "char", "final", "interface", "static", "void", "class", "finally", "long", "strictfp", "volatile", "const", "float", "native", "super", "while" }));
  
  public static Object method(CommandSession session, Object target, String name, List<Object> args)
    throws IllegalArgumentException, IllegalAccessException, InvocationTargetException, Exception
  {
    Method[] methods = target.getClass().getMethods();
    name = name.toLowerCase();
    
    String get = "get" + name;
    String is = "is" + name;
    String set = "set" + name;
    if (KEYWORDS.contains(name)) {
      name = "_" + name;
    }
    if ((target instanceof Class))
    {
      Method[] staticMethods = ((Class)target).getMethods();
      for (Method m : staticMethods)
      {
        String mname = m.getName().toLowerCase();
        if ((mnam
1 2 3

Further reading...

For more information on Java 1.5 Tiger, you may find Java 1.5 Tiger, A developer's Notebook by D. Flanagan and B. McLaughlin from O'Reilly of interest.

New!JAR listings


Copyright 2006-2017. Infinite Loop Ltd