Package org.jboss.fresh.io

Examples of org.jboss.fresh.io.PrintWriter2


  /* (non-Javadoc)
   * @see org.jboss.fresh.shell.AbstractExecutable#process(java.lang.String, java.lang.String[])
   */
  protected void process(String exepath, String[] args) throws Exception {

    PrintWriter2 pout = new PrintWriter2(new BufferedWriter(new BufferWriter(getStdOut())));

    String[] invalids = getInvalidSwitches(args, "cpxuot", new String[]{"--ex", "--help"});

    boolean clientInfo = isSwitchActive(args, "c", null);
    boolean project = isSwitchActive(args, "p", null);
    boolean global = isSwitchActive(args, "x", null);
    boolean user = isSwitchActive(args, "u", null);
    boolean objs = isSwitchActive(args, "o", null);
    boolean time = isSwitchActive(args, "t", null);
   
    if ((invalids.length > 0)) {

      StringBuffer sb = new StringBuffer("Unknown or invalid switch(es):");
      for (int i = 0; i < invalids.length; i++) {
        sb.append(" " + invalids[i]);
      }

      error(sb.toString());
      return;
    }

    if (helpRequested()) {

      StringBuffer sb = new StringBuffer();
      sb.append("Usage: history [-cpxuo] [--help]\r\n");
      sb.append("    -p : display history for current project\r\n");
      sb.append("    -x : display global history (all projects)\r\n");
      sb.append("    -c : display all client info\r\n");
      sb.append("    -u : display client user\r\n");
      sb.append("    -o : output HistoryItem objects\r\n");
      sb.append("    -t : output timestamps\r\n");
      sb.append("    --help : this help\r\n");
      error(sb.toString());
      return;
    }

    History h = null;
    Context context = getShell().getContext();
    if(project) {
      context = (Context) context.get("AppContext");
    } else if(global) {
      context = (Context) context.get("GlobalContext");
    }

    if(context == null) {
      if(project)
        error("AppContext not available");
      else if(global)
        error("GlobalContext not available");
      else
        error("Context not available");
      return;
    }

    h = (History) context.get("History");

    if(h == null) return;

    BufferObjectWriter oout = null;
    SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");

    String val = null;

    try {
      Iterator it = h.list().iterator();
      while(it.hasNext()) {
        HistoryItem hi = (HistoryItem) it.next();

        if(objs) {
          if(oout == null)
            oout = new BufferObjectWriter(getStdOut());
          oout.writeObject(pout);
        } else {
          StringBuffer sb = new StringBuffer();
          if(time) {
            sb.append(sdf.format(new Date(hi.getTime())));
            sb.append(" ");
          }

          if(global) {
            val = hi.getProject();
            if(val == null)
              val = "<undefined>";
            sb.append(val);
            sb.append(" ");
          }

          if(user) {
            val = hi.getUser();
            if(val == null)
              val = "<undefined>";
            sb.append(val);
            sb.append(" ");         
          }

          sb.append(hi.getCmd());
          sb.append(" ");

          if(clientInfo) {
            String vuser = hi.getUser();
            String vhost = hi.getHost();
            String vapp = hi.getApp();
            if(vuser != null || vhost != null || vapp == null) {

              if(vuser == null) vuser = "<unknown>";
              if(vhost == null) vhost = "<unknown>";
              if(vapp == null) vapp = "<unknown>";

              sb.append("     (").append(vuser).append("@")
                .append(vhost).append(" [")
                .append(vapp).append("])");
            }
          }

          pout.println(sb);
        }
      }
    } finally {
      pout.close();
    }
  }
View Full Code Here


  public void process(String exename, String[] params) throws Exception {
    log.debug("entered");

    if (helpRequested()) {
      PrintWriter2 out = new PrintWriter2(new BufferWriter(getStdOut()));
      out.println("Usage: alias [--help] [-ex] <alias> <command_line>");
            out.println("    command_line parameter can be several parameters which will be concatenated");
            out.println("                 and seperated by spaces");
            out.println("    --list    : list existing aliases");
            out.println("    --listfull : list existing aliases and their values");
            out.println("    --help    : this help");
      out.close();
      log.debug("done");
      return;
    }

        String alias = null;
        if(params.length > 0) {

            alias = params[0];
            boolean full = "--listfull".equals(alias);

            if("--list".equals(alias) || full) {
                Map aliases = ((ShellImpl)shell).getRuntime().listAliases();
                PrintWriter2 out = new PrintWriter2(new BufferWriter(getStdOut()));
                Iterator it = aliases.entrySet().iterator();
                while(it.hasNext()) {
                    Map.Entry ent = (Map.Entry) it.next();
                    out.println(ent.getKey() + ( full ?  "\t\t" + ent.getValue() : ""));
                }
                out.flush();
                return;
            }
        }

View Full Code Here

    }
  }

  public void process(String exename, String[] params) throws Exception {

    PrintWriter2 out = new PrintWriter2(new BufferWriter(getStdOut()));

    String word = new String();

    String[] invalids = getInvalidSwitches(args, "cEJPisvVhwx", new String[]{"count", "regexp", "ignore-case", "no-messages", "invert-match", "version", "help", "words", "lines"});
    useRegExp = isSwitchActive(args, new String[]{"E", "J", "P", "regexp"});
    ignoreCase = isSwitchActive(args, "i", "ignore-case");
    onlyWords = isSwitchActive(args, "w", "words");
    onlyLines = isSwitchActive(args, "x", "lines");
    supressMessages = isSwitchActive(args, "s", "no-messages");
    invertMatch = isSwitchActive(args, "v", "invert-match");
    printVersion = isSwitchActive(args, "V", "version");
    onlyCount = isSwitchActive(args, "c", "count");

    if (isSwitchActive(args, "V", "version")) {
      out.println("grep (CP2 grep), version: " + VERSION);
      out.println("");
      return;
    }

    if ((invalids.length > 0) || helpRequested() || (params.length == 0)) {

      if (canThrowEx()) {
        if (invalids.length > 0) {
          StringBuffer sb = new StringBuffer("Unknown or invalid switch(es):");
          for (int i = 0; i < invalids.length; i++) {
            sb.append(" " + invalids[i]);
          }
          throw new RuntimeException(sb.toString());
        }
      } else {
        if (invalids.length > 0) {
          out.println("Unknown or invalid switch:");
          for (int i = 0; i < invalids.length; i++) {
            out.println("\t" + invalids[i]);
          }
          out.println();
        }

        printHelp(out);
      }
      return;
    }

    int i = 0;
    while (i < params.length) {
      if (!params[i].startsWith("-")) {
        break;
      }
      i++;
    }

    word = params[i];

    if (!useRegExp && ignoreCase) {
      word = word.toUpperCase();
    }

    BufferObjectReader oin = new BufferObjectReader(getStdIn());
    AutoConvertInputStream acis = new AutoConvertInputStream(oin);
    InputStreamReader isr = new InputStreamReader(acis);
    java.io.BufferedReader br = new java.io.BufferedReader(isr);

    String line = br.readLine();

    long count = 0;

    while (line != null) {
      if (matches(line, word) && !invertMatch) {
        if (!onlyCount) {
          out.println(line);
        } else {
          count++;
        }
      } else if (invertMatch) {
        if (!onlyCount) {
          out.println(line);
        } else {
          count++;
        }
      }
      line = br.readLine();
    }
    if (onlyCount) {
      out.println(count);
    }

  }
View Full Code Here

   */

  public void process(String exepath, String[] params) throws Exception {
    log.debug("entered");

    PrintWriter2 out = new PrintWriter2(new BufferWriter(getStdOut()));

    boolean lb = false;
    boolean ll = false;
    boolean lr = false;
    boolean all = false;
    boolean help = false;

    String name = null;


    // see what to do:
    for (int i = 0; i < params.length; i++) {
      String val = params[i];

      if (val.equals("--lb")) {
        lb = true;
      } else if (val.equals("--ll")) {
        ll = true;
      } else if (val.equals("--lr")) {
        lr = true;
      } else if (val.equals("--name")) {
        if(i<params.length-1) {
          name = params[++i];
        } else {
          error("Parameter --name should be followed by registry name pointing to EventCentral instance");
          return;
        }
      } else if (val.equals("-a") || val.equals("--all")) {
        all = true;
      } else if (val.equals("-h") || val.equals("--help")) {
        help = true;
      }
    }

    if (params.length == 0 || help) {
      out.println("Usage: ecquery [-a] ( --lb | --ll | --lr ) [--name <lookup_name>] ");
      out.println("    --lb: list broadcasters");
      out.println("    --ll: list listeners");
      out.println("    --lr: list routers");
      out.println("    -a, --all:  include hidden");
      out.println("       --name:  get the object for the specified name");
      out.println("    -h, --help : this help");
      out.close();
      log.debug("done");
      return;
    }


    EventCentral ec = null;

    if (name != null) {
      RegistryContext ctx = new RegistryContext();
      try {
         ec = (EventCentral) ctx.lookup(name);
      } catch(ClassCastException ex) {
        error("Object bound in registry under " + name + " is not a valid EventCentral object: " + ctx.lookup(name));
        return;
      } catch(NamingException ex) {
        log.error("Failed to retrieve EventCentral: ", ex);
        error("Failed to find EventCentral with registry name: " + name);
        return;
      }
    } else {
      Context ctx = getShell().getContext();
      ec = (EventCentral) ctx.get("EventCentral");
    }

    if(ec == null) {
      error("Failed to locate EventCentral (make sure you specify EventCentral lookup name, or make sure instance is available in shell context under 'EventCentral')");
      return;
    }


    if(ll) {

      Map listeners = ec.getListeners(all);

      Iterator it = listeners.entrySet().iterator();
      while(it.hasNext()) {
        Map.Entry ent = (Map.Entry) it.next();

        EventCentral.ListenerData ldat = (EventCentral.ListenerData) ent.getValue();
        out.println(ent.getKey() + ": " + ldat.getFilter() + " - " + ldat.getListener());
      }
    } else if(lb) {
      Map broadcasters = ec.getBroadcasters(all);

      Iterator it = broadcasters.entrySet().iterator();
      while(it.hasNext()) {
        Map.Entry ent = (Map.Entry) it.next();
        out.println(ent.getKey() + ": " + ent.getValue());
      }
    }

    log.debug("done");
  }
View Full Code Here

  public void process(String exename, String[] params) throws Exception {
    log.debug("entered");

    if (helpRequested()) {
      PrintWriter2 out = new PrintWriter2(new BufferWriter(getStdOut()));
      out.print("Usage: touch [--help] [-c] [files]\n");
      out.print("    files : files to be 'touched'\n");
      out.print("       -c : don't create inexisting files\n");
      out.print("   --help : this help\n\n");
      out.close();
      log.debug("done");
      return;
    }

    PrintWriter2 out = new PrintWriter2(new BufferWriter(getStdOut()));

    VFS vfs = shell.getVFS();
    FileName pwd = new FileName(shell.getEnvProperty("PWD"));
    boolean create = true;

    List paths = new LinkedList();

    for (int i = 0; i < params.length; i++) {
      String param = params[i];

      if (param.equals(CREATE))
        create = false;
      else {
        FileName path = new FileName(param);
        if (path.isRelative())
          path = pwd.absolutize(path);

//        path = vfs.resolve(shell.getUserCtx(), path, true);
        paths.add(path);
      }
    }


    // List result = new LinkedList();

    // if no paths given, read from stdin
    if (!paths.isEmpty()) {
      Iterator it = paths.iterator();
      while (it.hasNext()) {
        FileName path = (FileName) it.next();

        if (vfs.exists(shell.getUserCtx(), path, true)) {
          FileInfo info = vfs.getFileInfo(shell.getUserCtx(), path, true);
          info.setLastModified(new Date());
          vfs.updateFile(shell.getUserCtx(), info);
          out.println(path + ": Modified date set to now.");
        } else if (create) {
          FileInfo info = new FileInfo(path, FileInfo.TYPE_FILE);
          info.setMime("undefined");
          vfs.createFile(shell.getUserCtx(), info);
          out.println(path + ": File created.");
        } else {
          out.println(path + ": File does not exist.");
        }
      }

    } else {
      out.println("Usage: touch [-c] [FILES]");
      out.println("       -c = don't create inexisting files");
    }


    out.close();

    log.debug("done");
  }
View Full Code Here

   * Displays properties of all processes and sessions.
   */
  public void process(String exepath, String[] params) throws Exception {
    log.debug("entered");

    pout = new PrintWriter2(new BufferedWriter(new BufferWriter(getStdOut())));

    String[] invalids = getInvalidSwitches(args, "axflitT", new String[]{"--ex", "--help", "--order", "--out", "--full-thread-dump"});

    alles = isSwitchActive(args, "a", null);
    boolean xall = isSwitchActive(args, "x", null);
View Full Code Here

    //  cat [PATHS]
    public void process(String exename, String[] params) throws Exception {
        getLog().debug("entered");

        if (helpRequested()) {
            PrintWriter2 out = new PrintWriter2(new BufferWriter(getStdOut()));
            out.println("Usage: " + getCmd() + " [-ex] [-notx] <service name> <method sig>");
            out.println("      --help : this help");
            out.close();
            getLog().debug("done");
            return;
        }

        // zaklju�imo transakcijo �e je za�eta
        // get mbean name
        // get method
        PrintWriter out = new PrintWriter(new BufferWriter(getStdOut()));

        if (params.length < 2) {
            if (canThrowEx()) {
                throw new Exception("Wrong number of parameters. Usage: " + getCmd() + " [-notx] <service name> <method sig>");
            } else {
                printUsage(out);
                return;
            }
        }

        String mbeanName = params[0];
        String method = params[1];

        boolean notx = false;
        boolean wasActive = false;

        if (params[0].equals("-notx")) {
            notx = true;
            mbeanName = params[1];

            if (params.length < 3) {
                if (canThrowEx()) {
                    throw new Exception("Wrong number of parameters. Usage: " + getCmd() + " [-notx] <srevice name> <method sig>");
                } else {
                    printUsage(out);
                    return;
                }
            }

            method = params[2];
        }


        String methodName;
        String[] sig = null;
        Object[] vals = null;

        int pos = method.indexOf("(");

        if (pos >= 0) {
            methodName = method.substring(0, pos);
            int epos = method.lastIndexOf(")");
            String prs;
            if (epos != -1) {
                prs = method.substring(pos + 1, epos);
            } else {
                prs = method.substring(pos + 1);
            }

            LinkedList l = new LinkedList();
            StringTokenizer st = new StringTokenizer(prs, ",");
            while (st.hasMoreTokens()) {
                l.add(st.nextToken());
            }

            sig = new String[l.size()];
            Iterator it = l.iterator();
            for (int i = 0; i < sig.length && it.hasNext(); i++) {
                sig[i] = (String) it.next();
            }

            vals = new Object[sig.length];
        } else {
            methodName = method;
        }

        BufferObjectWriter oout = new BufferObjectWriter(getStdOut());
        BufferObjectReader oin = new BufferObjectReader(getStdIn());

        if (vals != null) {
            for (int i = 0; i < vals.length; i++) {

                if (!oin.isFinished())
                    vals[i] = oin.readObject();
                else if (canThrowEx()) {
                    throw new Exception("Not enough input objects on stdin.");
                } else {
                    out.println("Not enough input objects on stdin.");
                    return;
                }
            }
        }
View Full Code Here

  private static final String ADD = "-a";

  public void process(String exename, String[] params) throws Exception {
    log.debug("entered");

    PrintWriter2 out = new PrintWriter2(new BufferWriter(getStdOut()));

    if (helpRequested()) {
      out.println("Usage: setattr [-ex] [--help] [-a] FILE_NAME  ATTR_NAME ATTR_VALUE  [ATTR_NAME ATTR_VALUE]");
      out.println("    ATTR_NAME : name of the attribute to be set.");
      out.println("    ATTR_VALUE : new value of the specified attribute.");
      out.println("    -a : add the attribute instead of replacing an existing one.");
      out.println("    --help : this help.");
      out.close();
      log.debug("done");
      return;
    }

    VFS vfs = shell.getVFS();
    FileName pwd = new FileName(shell.getEnvProperty("PWD"));
    FileName path = null;
    boolean add = false;
    HashMap attrs = new HashMap();

    for (int i = 0; i < params.length; i++) {
      String param = params[i];

      if ((i == 0) && param.equals(ADD))
        add = true;

      else if (add && (i == 1) || (i == 0)) {
        FileName filepath = new FileName(param);
        if (filepath.isRelative())
          filepath = pwd.absolutize(filepath);

//        filepath = vfs.resolve(shell.getUserCtx(), filepath, true);
        if (vfs.exists(shell.getUserCtx(), filepath, true)) {
          path = filepath;
        } else {
          if (canThrowEx()) {
            throw new RuntimeException(filepath + ": Path does not exist!");
          } else {
            out.println(filepath + ": Path does not exist!");
          }
        }

      } else {
        String name = param;
        String value = null;
        if (params.length > ++i)
          value = params[i];

        attrs.put(name, value);
      }
    }

    if (path == null) {
      out.println("Usage: setattr [-ex] [-a] FILE NAME VALUE {NAME VALUE}");
      out.println("       -a = add attributes insted of set");

    } else {
      FileInfo info = vfs.getFileInfo(shell.getUserCtx(), path, true);
      if (add) {
        HashMap oldAttrs = info.getAttributes();
        oldAttrs.putAll(attrs);
      } else
        info.setAttributes(attrs);

      vfs.updateFile(shell.getUserCtx(), info);
    }

    out.close();

    log.debug("done");
  }
View Full Code Here

       
    // if no name
    if (key == null) {
      // display all props
      // name=value
            PrintWriter2 pout = new PrintWriter2(new BufferedOutputStream(new BufferOutputStream(getStdOut())));
      Properties p = null;
      if(procid == null) {
        p = getProcess().getEnv().getEnvProperties();
      } else {
        SystemShellImpl ss = (SystemShellImpl) ((ShellImpl) getShell()).getSystemShell();
        Process proc = ss.findProcess(procid);
        if (proc == null) {
          Map m = ss.getShellMap();
          ShellImpl shel = (ShellImpl) m.get(procid);
          if(shel == null) {
            error("No process / shell found for id: " + procid);
            return;
          }
          p = shell.getEnv().getEnvProperties();
        } else {
          if(proc.getEnv() != null) {
            error("Process has no environment - it means it exited but wasn't yet evicted");
            return;
          }
          p = proc.getEnv().getEnvProperties();
        }
       
      }
      HashMap map = new HashMap();
      Enumeration enu = p.propertyNames();
      while (enu.hasMoreElements()) {
        String nm = (String) enu.nextElement();
        map.put(nm, p.getProperty(nm));
      }

      TreeMap sm = new TreeMap(map);
      Iterator it = sm.entrySet().iterator();
      while (it.hasNext()) {
        Map.Entry ent = (Map.Entry) it.next();
        pout.println(ent.getKey() + "=" + ent.getValue());
      }

      pout.close();
      log.debug("done");
      return;
    }

        if(procid == null) {
View Full Code Here

   */

  public void process(String exepath, String[] params) throws Exception {
    log.debug("entered");
    PrintWriter2 out = new PrintWriter2(new BufferWriter(getStdOut()));

    if (helpRequested() || (params.length < 3)) {
      usage(out);
      out.close();
      log.debug("done");
      return;
    }

    BufferObjectWriter oout = new BufferObjectWriter(getStdOut());
View Full Code Here

TOP

Related Classes of org.jboss.fresh.io.PrintWriter2

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.