Package org.jboss.fresh.io

Examples of org.jboss.fresh.io.PrintWriter2


   */

  public void process(String exepath, String[] params) throws Exception {
    log.debug("entered");
   
    PrintWriter2 out = new PrintWriter2(new BufferWriter(getStdOut()));
    if (helpRequested() || (params.length != 2 && params.length != 0)) {
      out = new PrintWriter2(new BufferWriter(getStdOut()));
      out.println("Usage: mount [--help] [<path> <vfs>]");
      out.println("    path : mount point");
      out.println("    vfs : pointer to vfs bound in registry");
      out.println("    --help : this help");
      out.println();
      out.println("    Run without parameters it shows a list of mounts");
      out.close();
      log.debug("done");
      return;
    }

    RootVFS rootfs = null;

    RegistryContext ctx = new RegistryContext();
    try {
      rootfs = (RootVFS) ctx.lookup("java:/CP2/VFS");
    } catch(ClassCastException ex) {
      error("VFS bound at java:/CP2/VFS is not RootVFS");
      return;
    } catch(NameNotFoundException ex) {
      error("Root VFS not bound (should be at: java:/CP2/VFS)");
      return;     
    }

    if(params.length == 0) {
      Iterator it = rootfs.listMounts().entrySet().iterator();
      while(it.hasNext()) {
        Map.Entry ent = (Map.Entry) it.next();       
        out.println(ent.getKey() + "\t\t" + ent.getValue());
      }
   
      out.flush();
      return;
    }
   

    String path = params[0];
View Full Code Here


    if (helpRequested()) {
      usage();
      return;
    }

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

    boolean alpha = false;
    int srv = 1;
    char action = QUERY;
    boolean info = false;

    String pattern = null;
    String mbname = null;

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

      if (tmp.equals("-a")) {
        alpha = true;
      } else if (tmp.equals("-s")) {
        try {
          srv = Integer.parseInt(params[i + 1]);
               i++; // move past server ordinal
        } catch (Exception ex) {
          pout.println("-s should be followed by an ordinal number");
          return;
        }
      } else if (tmp.equals("-l")) {
        action = LIST;
      } else if (tmp.equals("-i")) {
        info = true;
      } else {
        pattern = tmp;
        break; // this conditions order :-)
      }
    }


    if (action == LIST) {
      ArrayList ls = MBeanServerFactory.findMBeanServer(null);
      pout.println(ls.size() + " server" + (ls.size() == 1 ? "" : "s") + " available:");
      Iterator it = ls.iterator();
      for (int i = 1; it.hasNext(); i++) {
        MBeanServer mbsrv = (MBeanServer) it.next();
        pout.println(i + ")  has " + mbsrv.getMBeanCount() + " mbeans registered.");
      }
      return;
    }

    if (pattern == null) {
      pout.println("No query specified.");
      usage();
      return;
    }

    MBeanServer mbsrv = (MBeanServer) MBeanServerFactory.findMBeanServer(null).get(srv - 1);

/*
    int idx = pattern.indexOf(",");
    if(idx==-1) {
      mbname = pattern;
      pattern = null;
    } else {
      mbname = pattern.substring(0, idx);
      if(mbname.trim().length()==0) mbname = null;

      pattern = pattern.substring(idx+1);
      if(pattern.trim().length()==0) pattern = null;
*/
    Set res = mbsrv.queryNames(new ObjectName(pattern), null);
    Iterator it = res.iterator();

    while (it.hasNext()) {
      ObjectName nm = (ObjectName) it.next();
      pout.println(nm);
      if(info) {
        //Iterator it2 = mbsrv.queryMBeans(nm, null).iterator();
        //while(it2.hasNext()) {
          //ObjectInstance inst = (ObjectInstance) it2.next();
          //pout.println();
        //}
       
         MBeanInfo mbinf = mbsrv.getMBeanInfo(nm);
         pout.println("  Description: " + mbinf.getDescription());
         pout.println("  Class: " + mbinf.getClassName());
         pout.println("  Attributes:");
         MBeanAttributeInfo [] ainf = mbinf.getAttributes();
         for(int i=0; i<ainf.length; i++) {
           pout.println("    " + ainf[i].getType() + "   " + ainf[i].getName() + "   (" + (ainf[i].getDescription() == null ? "no description"  : ainf[i].getDescription()) + ")");
         }
        
         pout.println("  Operations:");
         MBeanOperationInfo [] oinf = mbinf.getOperations();
         for(int i=0; i<oinf.length; i++) {
           MBeanParameterInfo [] pinf = oinf[i].getSignature();
           StringBuffer sb = new StringBuffer();
           for(int j=0; j<pinf.length; j++) {
             if(j > 0)
               sb.append(", ");
             sb.append(pinf[j].getType() + " " + pinf[j].getName());
           }
           pout.println("    " + oinf[i].getReturnType() + "   " + oinf[i].getName() + "   (" + sb + ")");
         }


         //mbsrv.add
         pout.println("  Notifications:");
         MBeanNotificationInfo [] ninf = mbinf.getNotifications();
         for(int i=0; i<ninf.length; i++) {
           pout.println("    " + ninf[i].getName() + " :: " + Arrays.asList(ninf[i].getNotifTypes()));
         }        
      }
    }
  }
View Full Code Here

    }
  }


  void usage() throws Exception {
    PrintWriter2 pout = new PrintWriter2(new BufferWriter(getStdOut()));
    pout.println("Usage:  mbquery [-a] <query pattern>");
    pout.println();
    pout.println("          -a : list results in alphabetical order.");
    pout.println("          -s : server expressed as ordinal");
    pout.println("          -l : list available servers");
    pout.println("         --help : this help");
    pout.println();
    pout.println("  Example:  mbquery -a *:*");
    pout.close();
    log.debug("done");
  }
 
View Full Code Here

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


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

        boolean g = false;
        boolean l = false;
        boolean b = false;
        boolean u = false;
        boolean help = false;
        boolean tex = false;
        String spiObjectFactory = null;
        ArrayList spiParams = null;

        String name = null;
        String factory = null;
        String host = null;
        Hashtable env = null;

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

            if (val.equals("-g") || val.equals("--get")) {
                g = true;
            } else if (val.equals("-l") || val.equals("--list")) {
                l = true;
            } else if (val.equals("-b") || val.equals("--bind")) {
                b = true;
            } else if (val.equals("-u") || val.equals("--unbind")) {
                u = true;
            } else if (val.equals("-h") || val.equals("--help")) {
                help = true;
            } else if (val.equals("--ex")) {
                tex = true;
            } else if (val.equals("--factory")) {
                try {
                    factory = params[++i];
                } catch (Exception ex) {
                    error("--factory parameter must be followed by jndi-factory specification");
                    return;
                }
            } else if (val.equals("--bind-ref") || val.equals("-r")) {
                try {
                    spiObjectFactory = params[++i];
                } catch (Exception ex) {
                    error("--bind-ref parameter must be followed by spi-object-factory specification");
                    return;
                }

                // copy remaining parameters in paramList
                if("JNDI2Registry".equals(spiObjectFactory)) {
                    spiParams = new ArrayList(2);

                    try {
                        name = params[++i];
                        spiParams.add(name);
                        spiParams.add(params[++i]);
                    } catch (Exception ex) {
                        error(spiObjectFactory + " requires 2 parameters: <bind-name> <referenced-name>");
                        return;
                    }
                }
            } else if (val.equals("--host")) {
                try {
                    host = params[++i];
                } catch (Exception ex) {
                    error("--host parameter must be followed by inital-context-url specification");
                    return;
                }
            } else {
                name = val;
            }
        }

        if (params.length == 0 || help) {
            out.println("Usage: jndi [-glbh] <lookup name>");
            out.println("    lookup name : lookup name i.e. java:UserTransaction");
            out.println("    -g, --get      : get the object for the specified name");
            out.println("    -l, --list     : list the contents of the context");
            out.println("    -b, --bind     : bind an object passed on stdin under the specified name");
            out.println("    -r, --bind-ref <spi-object-factory> <object-factory-specific-params> ...   : create and bind a reference");
            out.println("    --factory <jndi factory class>: use the specified jndi factory class");
            out.println("    --host <jndi host> : connect to the specified host");
            out.println("    -h, --help : this help");
            out.println();
            out.println("  Example:");
            out.println("     jndi --bind-ref JNDI2Registry java:/CP2/bof_zrk/Persistence java:/CP2/bof_zrk/Persistence");
            out.flush();
            log.debug("done");
            return;
        }

        if (factory != null || host != null) {
            env = new Hashtable();
            if (factory != null) env.put(InitialContext.INITIAL_CONTEXT_FACTORY, factory);
            if (host != null) env.put(InitialContext.PROVIDER_URL, host);
        }

        Context ctx = null;
        if (env == null)
            ctx = new InitialContext();
        else
            ctx = new InitialContext(env);


        if (g) {
            BufferObjectWriter oout = new BufferObjectWriter(getStdOut());
            Object o = null;

            try {
                o = ctx.lookup(name);
            } catch (NamingException ex) {
                if (tex)
                    throw new RuntimeException(ex);
                else
                    out.print("Name not bound: " + name);
                return;
            }

            oout.writeObject(o);
            oout.flush();
            return;
        }

        if (l) {
            BufferObjectWriter oout = new BufferObjectWriter(getStdOut());
            Object o = ctx.lookup(name);
            if (!(o instanceof Context)) {
                if (tex)
                    throw new RuntimeException("Object is not instance of Context. Can't list it. : " + name);
                else
                    out.print("Object is not instance of Context. Can't list it. : " + name);
                return;
            }

            NamingEnumeration it = ctx.listBindings(name);
            while (it.hasMore()) {
                Binding bing = (Binding) it.next();
                out.println(bing.toString());
            }
            out.flush();
            return;
        }

        if (u) {
            ctx.unbind(name);
            return;
        }

        if (b || spiObjectFactory != null) {

            Object o = null;
            String factoryCName = null;

            if(spiObjectFactory == null || "StaticStore".equals(spiObjectFactory)) {
                BufferObjectReader oin = new BufferObjectReader(getStdIn());
                if(!oin.isFinished()) {
                    o = oin.readObject();
                }
            } else if("JNDI2Registry".equals(spiObjectFactory)) {
                o = new RegistryContext().lookup((String) spiParams.get(1));
                factoryCName = org.jboss.fresh.registry.JNDI2RegistryResolverFactory.class.getName();
            } else if("StaticStore".equals(spiObjectFactory)) {
                factoryCName = org.jboss.fresh.naming.StaticObjectStore.class.getName();
            }


            Name n = ctx.getNameParser("").parse(name);
            Context ct = ctx;
            while (n.size() > 1) {
                String ctxName = n.get(0);
                log.debug("CtxName: "+ctxName);
                try {
                    ct = (Context) ct.lookup(ctxName);
                    if (ct==null){//to workaround resin bug!
                        log.debug("Lookup retured null, will create new subcontext");
                        ct = ct.createSubcontext(ctxName);
                    }
                } catch (NameNotFoundException e) {
                    log.warn("Could not lookup for ctx ",e);
                    ct = ct.createSubcontext(ctxName);
                    log.debug("ctx after create subcontext: "+ct);
                }
                n = n.getSuffix(1);
            }

            if(spiObjectFactory != null) {
                StringRefAddr addr = new StringRefAddr("nns", name);
                Reference ref = new Reference(o.getClass().getName(), addr, factoryCName, null);
                o = ref;
            }

            ct.bind(n.get(0), o);
            out.flush();
            return;
        }


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

  }
*/


  private void usage() {
    PrintWriter2 out = new PrintWriter2(new BufferWriter(getStdOut()));
    out.println("Usage: eventnet [-h <host_label>] [-a <application>] -i <agentid>");
    out.println("    -h,  --host : host label");
    out.println("    -a,  --app  : application (this parameter is optional)");
    out.println("    -i,  --id   : agent id");
    out.println("    --help : this help");
    out.println();
    out.println("  Agent id is required every time. It uniquely identifies remote agent which");
    out.println("  represents connection with remote EventCentral.");
    out.println("  Host label is required the first time remote agent establishes a connection.");
    out.println("  To continue an already established eventnet connection you only specify id.");
    out.println();
    out.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: RM [--help] [-ex] FILES");
      out.println("    FILES : files to be removed.");
      out.println("    --help : this help");
      out.close();
      log.debug("done");
      return;
    }

    PrintWriter2 out = new PrintWriter2(new BufferWriter(getStdOut()));
    VFS vfs = shell.getVFS();
    FileName pwd = new FileName(shell.getEnvProperty("PWD"));

    List paths = new LinkedList();

    for (int i = 0; i < params.length; i++) {

      String param = params[i];

      FileName path = new FileName(param);
      if (path.isRelative())
        path = pwd.absolutize(path);

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

      log.debug("FREAKING PATH = " + path);
      if (vfs.exists(shell.getUserCtx(), path, true)) {
        vfs.remove(shell.getUserCtx(), path, true);
        out.println(param + ": Path and all children removed!");
      } else {
        if (canThrowEx()) {
          throw new RuntimeException(param + ": Path does not exist! Cannot delete inexisting path!");
        } else {
          out.println(param + ": Path does not exist! Cannot delete inexisting path!");
        }
      }
    }


    out.close();

    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.println("Usage: info [--help] [-ex] [-d] files");
      out.println("    -d = direct (if links are not resolved)");
      out.println("    -ex = if the executable is allowed to throw an exception or just print an error message.");
      out.println("    --help : this help");
      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 direct = false;
    LinkedList paths = new LinkedList();

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

      if (!direct && param.equals(DIRECT))
        direct = true;

      else {
        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)) {
          paths.add(filepath);
        } else {
          if (canThrowEx()) {
            throw new NoSuchFileException("Path does not exist: " + filepath);
          } else {
            out.println("Path does not exist: " + filepath);
          }
        }
      }
    }

    if (paths.isEmpty()) {
      if (canThrowEx()) {
        throw new Exception("No path specified.");
      } else {
        out.println("Usage: info [-d] FILES");
        out.println("       -d = direct (links are not resolved)");
      }
    } else {
      Iterator it = paths.iterator();
      while (it.hasNext()) {
        FileName file = (FileName) it.next();

        FileInfo info = vfs.getFileInfo(shell.getUserCtx(), file, direct);
        log.debug("Got info: " + info);

        out.println(info);
      }
    }

    out.close();

    log.debug("done");

  }
View Full Code Here

  private static transient org.apache.log4j.Logger log = org.apache.log4j.Logger.getLogger(DiagExe.class);

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

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


    if (helpRequested()) {
      pout.println("Usage: diag [--help] [-ex] [-gc]");
      pout.println("    -gc : run garbage collection before getting diagnostics.");
      pout.println(" --help : this help");
      pout.close();
      log.debug("done");
      return;
    }

    Runtime run = Runtime.getRuntime();

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


      if (tmp.equals("-gc")) {
        run.gc();
      }
    }

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    Date startTime = null;

    try {
      startTime = sdf.parse(getShell().getEnvProperty("SystemShell.startTime"));
    } catch (Exception ex) {
      log.error(ex.getMessage(),ex);
    }


    Calendar c = null;

    if (startTime != null) {
      long upTime = System.currentTimeMillis() - startTime.getTime();
      c = Calendar.getInstance();
      c.setTime(new Date(upTime));
    }


    //c.get(Calendar.DAY_OF_YEAR);
    //c.get(Calendar.HOUR_OF_DAY);
    //c.get(Calendar.MINUTE);
    //c.get(Calendar.SECOND);


    long fmem = run.freeMemory();
    long tmem = run.totalMemory();

    pout.println("Memory used: " + (tmem - fmem));
    pout.println("Memory free: " + (fmem));
    pout.println("Memory total: " + (tmem));
    pout.println("System start time: " + getShell().getEnvProperty("SystemShell.startTime"));
    pout.println("System up time: " + (c.get(Calendar.DAY_OF_YEAR) - 1) + "d " + (c.get(Calendar.HOUR_OF_DAY) - 1) + "h " + c.get(Calendar.MINUTE) + "m " + c.get(Calendar.SECOND) + "s");


    Map m = ((SystemShellImpl) ((ShellImpl) getShell()).getSystemShell()).getShellMap();
    pout.println("Session count: " + new HashSet(m.entrySet()).size());

    m = ((SystemShellImpl) ((ShellImpl) getShell()).getSystemShell()).getProcMap();
    pout.println("Process count: " + new HashSet(m.entrySet()).size());

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

  private int total = 0;

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

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

    if (helpRequested()) {
      printHelp(pout);
      return;
    }

    if(params.length == 0) {
      error("Wrong number of parameters. Use --help to get help.");
      return;
    }


    URL url = null;
    String vhost = null;
    String user = null;
    String pass = null;
    boolean displayHeaders = false;

    LinkedHashMap headers = new LinkedHashMap();

    for (int i = 0; i < params.length; i++) {
      if("--vhost".equals(params[i])) {
        if(i<params.length+2) {
          vhost = params[++i];
        } else {
          error("parameter vhost specified but no value");
          return;
        }
      } else if("--user".equals(params[i])) {
        if(i<params.length+2) {
          user = params[++i];
        } else {
          error("parameter user specified but no value");
          return;
        }
      } else if("--pass".equals(params[i])) {
        if(i<params.length+2) {
          pass = params[++i];
        } else {
          error("parameter pass specified but no value");
          return;
        }
      } else if("--header".equals(params[i])) {
        if(i<params.length+3) {
          List l = (List) headers.get(params[++i]);
          if(l==null) {
            l=new LinkedList();
            headers.put(params[i], l);
          }
          l.add(params[++i]);
        } else {
          error("parameter header specified but no key or value");
          return;
        }

      } else if("--headers".equals(params[i])) {

        displayHeaders = true;

      } else if(url!=null) {
        error("Wrong number of parameters. Use --help to get help.");
        return;
      } else {
        url = new URL(params[i]);
      }
    }


    PrintWriter2 pw = null;
    InputStream in = null;
    Socket cli = null;
    try {
      int port = url.getPort();
      if(port == -1) port = deducePort(url);

      cli = new Socket(url.getHost(), port);
      pw = new PrintWriter2(new OutputStreamWriter(cli.getOutputStream()));
      pw.println("GET " + url.getFile() + " HTTP/1.0");
      if(headers!=null) {
        Iterator it = headers.entrySet().iterator();
        while(it.hasNext()) {
          Map.Entry ent = (Map.Entry) it.next();
          String name = (String) ent.getKey();
          Iterator it2 = ((List)ent.getValue()).iterator();
          while(it2.hasNext()) {
            //log.debug(" " + name + ": " + vals.nextElement());
            String v = (String)it2.next();
            pw.println(name + ": " + v);
          }
        }
      }

      if(vhost==null) {
        vhost = url.getHost();
      }

      pw.println("Host: " + vhost);

      if (user != null) {
        if (pass == null) pass = "";
        String authentication = "Basic " + Base64.encodeObject((user + ":" + pass).getBytes());
        pw.println("Authorization: " + authentication);
      }


      pw.println();
      pw.flush();


      BufferedOutputStream bos = new BufferedOutputStream(new BufferOutputStream(getStdOut()));

      try {
        boolean freeOn = false;

        in = cli.getInputStream();
        byte [] buff = new byte [160];
        int rc = in.read(buff);
        while(rc != -1) {
          if(!displayHeaders && !freeOn) {
            log.debug("no headers && still have to cut headers");
            int pos = findEOHead(buff, rc);
            log.debug("find end-of-head produced: " + pos);
            if(pos!=-1) {
              log.debug("found EOH at " + pos);
              freeOn = true;
              if(pos<rc)
                bos.write(buff, pos, rc-pos);
            //} else {
              //bos.write(buff, 0, rc);
            }
          } else {
            bos.write(buff, 0, rc);
          }

          rc = in.read(buff);
        }
      } finally {
        bos.close();
      }

    } catch(Throwable th) {
      log.error(th.getMessage(), th);
      error("error occured while retrieving url content: " + th.getMessage());
    } finally {
      try { if(pw!=null) pw.close(); } catch(Exception e) {}
      try { if(in!=null) in.close(); } catch(Exception e) {}
      try { if(cli!=null) cli.close(); } catch(Exception e) {}
    }

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

   
  public void throwException(String msg) throws Exception {
    if (canThrowEx()) {
      throw new Exception(msg);
    } else {
      PrintWriter2 out = new PrintWriter2(new BufferWriter(getStdOut()));
      out.println(msg);
      out.close();
      System.out.println("[CropBinaryExe] : done.");
      return;
    }
  }
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.