1 23 24 package com.sun.enterprise.tools.upgrade.certconversion; 25 26 import java.io.*; 27 import java.util.logging.*; 28 29 import com.sun.enterprise.tools.upgrade.logging.*; 30 import com.sun.enterprise.util.i18n.StringManager; 31 32 public class ProcessAdaptor{ 33 private static Logger _logger = LogService.getLogger(LogService.UPGRADE_LOGGER); 34 private static StringManager sm = StringManager.getManager(LogService.UPGRADE_CERTCONVERSION_LOGGER); 35 public static int executeProcess(String str, OutputStream outputStream){ 36 String [] args = str.split(" "); 37 return executeProcess(args, outputStream); 38 } 39 40 public static int executeProcess(String [] str, OutputStream outputStream){ 41 int exitVal = -1; 42 try{ 43 Process p = Runtime.getRuntime().exec(str); 44 PrintStream errorOut = new PrintStream(p.getErrorStream(), outputStream); 45 PrintStream resultOut = new PrintStream(p.getInputStream(), outputStream); 46 errorOut.start(); 47 resultOut.start(); 48 errorOut.join(); 49 resultOut.join(); 50 exitVal = p.waitFor(); 51 p.destroy(); 52 return exitVal; 53 }catch(InterruptedException ie){ 54 _logger.log(Level.WARNING,sm.getString("enterprise.tools.upgrade.certconversion.process_execution_error"),ie); 55 }catch(SecurityException se){ 56 _logger.log(Level.WARNING,sm.getString("enterprise.tools.upgrade.certconversion.process_execution_error"),se); 57 }catch(IOException ioe){ 58 _logger.log(Level.WARNING,sm.getString("enterprise.tools.upgrade.certconversion.process_execution_error"),ioe); 59 } 60 return exitVal; 61 } 62 63 public static int executeProcess(String str, Writer writer){ 64 String [] args = str.split(" "); 65 int exitVal = executeProcess(args, writer); 66 return exitVal; 67 } 68 69 public static int executeProcess(String [] str, Writer writer){ 70 int exitVal = -1; 71 try{ 72 Process p = Runtime.getRuntime().exec(str); 74 PrintStream errorOut = new PrintStream(p.getErrorStream(), writer); 75 PrintStream resultOut = new PrintStream(p.getInputStream(), writer); 76 errorOut.start(); 77 resultOut.start(); 78 errorOut.join(); 79 resultOut.join(); 80 exitVal = p.waitFor(); 81 p.destroy(); 82 return exitVal; 83 }catch(InterruptedException ie){ 84 _logger.log(Level.WARNING,sm.getString("enterprise.tools.upgrade.certconversion.process_execution_error"),ie); 85 }catch(SecurityException se){ 86 _logger.log(Level.WARNING,sm.getString("enterprise.tools.upgrade.certconversion.process_execution_error"),se); 87 }catch(IOException ioe){ 88 _logger.log(Level.WARNING,sm.getString("enterprise.tools.upgrade.certconversion.process_execution_error"),ioe); 89 } 90 return exitVal; 91 } 92 } 93 94 class PrintStream extends Thread { 95 InputStream inpStream = null; 96 OutputStream outpStream = null; 97 Writer writer = null; 98 private static Logger _logger = LogService.getLogger(LogService.UPGRADE_LOGGER); 99 private static StringManager sm = StringManager.getManager(LogService.UPGRADE_LOGGER); 100 101 PrintStream(InputStream in, OutputStream out) { 102 this.inpStream = in; 103 this.outpStream = out; 104 } 105 106 PrintStream(InputStream in, Writer writer) { 107 this.inpStream = in; 108 this.writer = writer; 109 } 110 111 public void run() { 112 try { 113 InputStreamReader inpStreamReader = new InputStreamReader(inpStream); 114 BufferedReader bufferedReader = new BufferedReader(inpStreamReader); 115 String line=null; 116 while((line = bufferedReader.readLine()) != null) { 117 if (writer == null) { 118 outpStream.write(line.getBytes()); 119 outpStream.write(new String ("\n").getBytes()); 120 outpStream.flush(); 121 } else { 122 writer.write(line+"\n"); 123 writer.flush(); 124 } 125 } 126 } catch(IOException ioe) { 127 _logger.log(Level.WARNING,sm.getString("enterprise.tools.upgrade.certconversion.process_execution_error"),ioe); 128 } 129 } 130 } 131 | Popular Tags |