Package org.jboss.fresh.shell

Examples of org.jboss.fresh.shell.ShellOutputStream


        }

        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 + ".");
            }

        } finally {
            if (ctx != null) {
                try {
                    ctx.close();
                } catch (Exception e) {
                }
            }
            // should not close shells. if(shell  != null) { try { shell.close();   } catch (Exception e) { } }
            if (rshell != null) {
                try {
                    rshell.remove();
                } catch (Exception e) {
                }
            }
            if (osw != null) {
                try {
                    osw.close();
                } catch (Exception e) {
                }
            }
            if (sos != null) {
                try {
                    sos.close();
                } catch (Exception e) {
                }
            }
        }
    }
View Full Code Here

TOP

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

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.