Package org.jboss.fresh.shell

Examples of org.jboss.fresh.shell.Shell


      out.close();
      log.debug("CtxDelegateExe done.");
      return;
    }

    Shell shell = getShell();

    PrintWriter err = new PrintWriter(new BufferWriter(getStdOut()));

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

      String tmp = params[i];

      // take parameter 1
      // look up the name through InitialContext
      RegistryContext ctx = new RegistryContext();
      Object obj = null;
      try {
        obj = (Context) ctx.lookup(tmp);
      } catch (Exception ex) {
        if (canThrowEx()) {
          throw new Exception("No object found in JNDI for name: " + tmp);
        } else {
          err.println("No object found in JNDI for name: " + tmp);
          log.debug("CtxDelegateExe done.");
          return;
        }
      }

      // See if what you got is instance of Context

      if (!(obj instanceof Context)) {
        if (canThrowEx()) {
          throw new Exception("Object found in JNDI for name: " + tmp + " is not org.jboss.fresh.ctx.Context type.");
        } else {
          err.println("Object found in JNDI for name: " + tmp + " is not org.jboss.fresh.ctx.Context type.");
          log.debug("CtxDelegateExe done.");
          return;
        }
      }

      Context ctx0 = (Context) obj;
      // now shell.getContext()
      if(log.isDebugEnabled()) {
        log.debug("shell: " + shell + ", ctx: "  + shell.getContext());
      }
      shell.getContext().registerDelegate(ctx0);
      shell.getContext().put("AppContext", ctx0);

      Context gctx = null;
      try {
        tmp = "java:/FRESH/GlobalContext";
        gctx = (Context) ctx.lookup(tmp);
      } catch (Exception ex) {
        if (canThrowEx()) {
          throw new Exception("No object found in JNDI for name: " + tmp);
        } else {
          err.println("No object found in JNDI for name: " + tmp);
          log.debug("CtxDelegateExe done.");
          return;
        }
      }

      shell.getContext().put("GlobalContext", gctx);

      // on context call registerDelegate()
      // done

    }
View Full Code Here


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

    Shell sh = getShell();
    VFS vfs = sh.getVFS();
    OutBuffer out = getStdOut();
    FileName fname;
    if (params == null || params.length == 0) {
      fname = new FileName(sh.getEnvProperty("PWD"));
    } else {
      fname = new FileName(sh.getEnvProperty("PWD")).absolutize(params[0]);
    }

    if (vfs.exists(null, fname, false)) {
      out.put("\n Directory of " + fname + "\n\n", 10000L);
      Collection col = vfs.list(null, fname, false);
View Full Code Here

        if (file == null || file.trim().length() == 0) {
            throw new Exception("File name not defined. Please define VFS file name.");
        }

        Context ctx = null;
        Shell shell = null;
        RemoteShell rshell = null;
        ShellOutputStream sos = null;
        OutputStreamWriter osw = null;
        try {
            ctx = new RegistryContext();

            try {
                SystemShell sysshell = (SystemShell) ctx.lookup("java:/FRESH/SystemShell");
                log.log(TRACE, "Retrieved SystemShell from RegistryContext: 'SystemShell'.");
                shell = sysshell.startSession(null, false);
            } catch (javax.naming.NameNotFoundException nnfe) {
                throw nnfe;

            }
            log.debug("Using shell: " + shell);

            String path = "";
            String[] parts = file.split("/");
            for (int i = 0; i < parts.length - 1; i++) {
                path = path + "/" + parts[i];
                String cmd = "mkdir " + path;
                log.debug(cmd);
                if (shell != null) {
                    shell.executeAsObject(cmd);
                } else {
                    rshell.executeAsObject(cmd);
                }
            }

            log.info("Creating file " + file);
            String cmd;

            boolean create = true;
            if (classpathFile != null && classpathFile.length() > 0) {
                url = this.getClass().getResource(classpathFile);
                if (url != null) {
                    log.debug("Located resource using class.getResource(): " + url.toExternalForm());
                    content = null;
                } else {
                    url = Thread.currentThread().getContextClassLoader().getResource(classpathFile);
                    if (url != null) {
                        log.debug("Located using context class loader: " + url.toExternalForm());
                        content = null;
                    } else {
                        log.warn("ClassPath file defined to '" + classpathFile + "' but not found using getResource() mechanism.");
                        create = false;
                    }
                }
            }

            // content over matter
            if (content != null && content.length() > 0) {

                java.io.StringReader sr = null;
                java.io.BufferedReader br = null;

                try {
                    sr = new java.io.StringReader(content);
                    br = new java.io.BufferedReader(sr);

                    // let's style it a bit
                    if (br.readLine().trim().length() == 0) {
                        log.debug("Whitespace removal activated.");

                        StringBuffer result = new StringBuffer(content.length());
                        String whitespace = null;
                        boolean first = true;
                        String line = br.readLine();
                        while (line != null) {
                            if (first) {
                                int i = 0;
                                while (i < line.length() && Character.isWhitespace(line.charAt(i))) {
                                }

                                if (i == line.length() - 1) {
                                    whitespace = "";
                                } else {
                                    whitespace = line.substring(0, i);
                                    log.log(TRACE, "Removing " + whitespace.length() + " whitespace characters from lines.");
                                }
                                first = false;
                            }
                            if (line.startsWith(whitespace)) {
                                line = line.substring(whitespace.length());
                            }
                            result.append(line).append("\r\n");

                            line = br.readLine();
                        }

                        content = result.toString();
                    }

                } finally {
                    if (br != null) {
                        try {
                            br.close();
                        } catch (Exception e) {
                        }
                    }
                    if (sr != null) {
                        try {
                            sr.close();
                        } catch (Exception e) {
                        }
                    }
                }

                ProcessInfo pinf = null;
                cmd = "cat > " + file;
                log.debug(cmd);
                if (shell != null) {
                    pinf = shell.execute(cmd);
                    sos = new ShellOutputStream(shell, pinf.procid);
                } else {
                    pinf = rshell.execute(cmd);
                    sos = new ShellOutputStream(rshell, pinf.procid);
                }
                osw = new OutputStreamWriter(sos, "UTF-8");
                osw.write(content);
                log.log(NOTICE, "Created file " + file + ", processed " + content.length() + " characters.");
            } else if (sourceFile != null && sourceFile.length() > 0) {
                java.io.File f = new java.io.File(sourceFile);
                if (!f.exists()) {
                    log.error("File " + f.getAbsolutePath() + " does not exist! Will not create VFS file: " + file);
                    return;
                }
                if (!f.isFile()) {
                    log.error("Path " + f.getAbsolutePath() + " is not a file! Will not create VFS file: " + file);
                    return;
                }
                cmd = "cat > " + file + " &< " + f.getAbsolutePath();
                log.debug(cmd);
                if (shell != null) {
                    shell.execute(cmd);
                } else {
                    rshell.execute(cmd);
                }
                log.log(NOTICE, "Created file " + file + ", wrote " + f.length() + " bytes.");
            } else if (url != null) {
                InputStream is = null;
                try {
                    java.net.URLConnection uc = url.openConnection();
                    uc.setConnectTimeout(30 * 1000);
                    uc.setReadTimeout(60 * 1000);
                    is = uc.getInputStream();

                    ProcessInfo pinf = null;
                    cmd = "cat > " + file;
                    log.debug(cmd);
                    if (shell != null) {
                        pinf = shell.execute(cmd);
                        sos = new ShellOutputStream(shell, pinf.procid);
                    } else {
                        pinf = rshell.execute(cmd);
                        sos = new ShellOutputStream(rshell, pinf.procid);
                    }

                    long size = IOUtils.copy(is, sos);

                    log.log(NOTICE, "Created file " + file + ", processed " + size + " bytes.");
                } finally {
                    if (is != null) {
                        try {
                            is.close();
                        } catch (Exception e) {
                        }
                    }
                }
            } else if (create) {
                cmd = "touch " + file;
                log.debug(cmd);
                if (shell != null) {
                    shell.execute(cmd);
                } else {
                    rshell.execute(cmd);
                }
                log.log(NOTICE, "Created empty file " + file + ".");
            }
View Full Code Here

      out.put(" cp: No directory specified.\n", 10000L);
      log.debug("done");
      return;
    }

    Shell sh = getShell();
    VFS vfs = sh.getVFS();
    String to = params[0];
    FileName fname = new FileName(to);
    if (fname.isRelative())
      fname = new FileName(sh.getEnvProperty("PWD")).absolutize(fname);

//    System.out.println("****");
//    System.out.println("****  fname: " + fname);
//    System.out.println("****");
//    if(vfs.exists(null, fname, false)) {
//      sh._setPWD(fname.toString());
//    }


    FileInfo info = vfs.getFileInfo(null, fname, false);
    if (info == null || !info.isDirectory()) {
      if (canThrowEx()) {
        throw new RuntimeException("Can't find the path specified: " + fname + "\n\n");
      } else {
        out.put("Can't find the path specified: " + fname + "\n\n", 10000L);
      }
    } else {
      sh._setPWD(fname.toString());
    }

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

      return;
    }

    BufferObjectWriter oout = new BufferObjectWriter(getStdOut());

    Shell sh = getShell();
    FileName fname = new FileName(params[0]);
    if (!fname.isAbsolute()) {
      fname = new FileName(sh.getEnvProperty("PWD")).absolutize(fname);
    }
    log.debug("fname: " + fname);

    FileInfo inf = sh.getVFS().getFileInfo(sh.getUserCtx(), fname, true);

    log.debug("info: " + inf);
    for (int i = 1; i < params.length - 1; i++) {

      String val = params[i];
      log.debug("val: " + val);
//      if(val.equals("createDate")) {
//        inf.setCreateDate(params[++i]);
//      } else if(val.equals("lastModified")) {
//        inf.setLastModified(params[++i]);
//      } else if(val.equals("isComplete")) {
      if (val.equals("isComplete")) {
        inf.setComplete(params[++i].startsWith("t") || params[i].startsWith("T") || params[i].startsWith("1"));
      } else if (val.equals("mime")) {
        inf.setMime(params[++i]);
      } else if (val.equals("linkto")) {
        FileName fn = new FileName(params[++i]);
        if (fn.isRelative()) {
          fn = new FileName(sh.getEnvProperty("PWD")).absolutize(fn);
        }

        if (!sh.getVFS().exists(sh.getUserCtx(), fn, true)) {
          if (canThrowEx()) {
            throw new RuntimeException("The specfied linkto file does not exist:" + fn);
          } else {
            getStdOut().put(" The specified linkto file does not exist: " + fn, 10000L);
          }
          log.debug("done");
          return;
        }

        inf.setTarget(fn);
      } else {
        usage(out);
        return;
      }
    }

    sh.getVFS().updateFile(sh.getUserCtx(), inf);

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

TOP

Related Classes of org.jboss.fresh.shell.Shell

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.