KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > tools > upgrade > certconversion > ProcessAdaptor


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

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 JavaDoc str, OutputStream outputStream){
36         String JavaDoc[] args = str.split(" ");
37         return executeProcess(args, outputStream);
38     }
39     
40     public static int executeProcess(String JavaDoc[] str, OutputStream outputStream){
41         int exitVal = -1;
42         try{
43             Process JavaDoc 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 JavaDoc ie){
54             _logger.log(Level.WARNING,sm.getString("enterprise.tools.upgrade.certconversion.process_execution_error"),ie);
55         }catch(SecurityException JavaDoc 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 JavaDoc str, Writer writer){
64         String JavaDoc[] args = str.split(" ");
65         int exitVal = executeProcess(args, writer);
66         return exitVal;
67     }
68     
69     public static int executeProcess(String JavaDoc[] str, Writer writer){
70         int exitVal = -1;
71         try{
72             // Runtime rt = Runtime.getRuntime();
73
Process JavaDoc 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 JavaDoc ie){
84             _logger.log(Level.WARNING,sm.getString("enterprise.tools.upgrade.certconversion.process_execution_error"),ie);
85         }catch(SecurityException JavaDoc 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 JavaDoc {
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 JavaDoc line=null;
116             while((line = bufferedReader.readLine()) != null) {
117                 if (writer == null) {
118                     outpStream.write(line.getBytes());
119                     outpStream.write(new String JavaDoc("\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