KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > ProcessStarter4ASP


1 import com.ms.mtx.*;
2 import com.ms.asp.*;
3 import com.ms.com.*;
4 import java.io.*;
5 //import org.apache.log4j.*;
6
import java.net.URL JavaDoc;
7
8 /**
9  * Hilfsklasse zum Starten eines (Java-)Prozesses (z.B. Stager) aus einer ASP-Seite heraus
10  * @version $revision
11  * @author $author
12  */

13 public class ProcessStarter4ASP
14 {
15       /**Das integrierte ASP-Response-Objekt*/
16     private IResponse iResponseObject;
17     /**Log4J-Kategorie
18     //private static Category CAT = Category.getInstance(ProcessStarter4ASP.class);*/

19
20     /**initialisiert die Log4J-Umgebung
21     static
22     {
23       URL url = ProcessStarter4ASP.class.getResource("ProcessStarter4ASP_log4j.properties");
24         PropertyConfigurator.configure(url);
25     }*/

26     
27     /**
28      * Konstruiert neuen ProcessStarter4ASP
29      */

30     public ProcessStarter4ASP()
31     {
32         // Get the Object Context
33
IGetContextProperties iConProps = (IGetContextProperties)MTx.GetObjectContext();
34         // Get the Response object
35
Variant vResponse = iConProps.GetProperty("Response");
36         iResponseObject = (IResponse) vResponse.getDispatch();
37         //CAT.debug("process starter created");
38
}
39         
40     /**
41      * schreibt eine Zeichenkette in die ASP-Ausgabe
42      * @param s auszugebende Zeichenkette
43      */

44     public void write(String JavaDoc s)
45     {
46         Variant vMsg = new Variant(s + "\n");
47         iResponseObject.Write(vMsg);
48     }
49     
50     /**
51      * startet einen neuen Prozess und leitet dessen Ausgabe in die ASP-Ausgabe um
52      * @param cmd das Aufrufkommando für den Prozess
53      */

54     public void startProcess(String JavaDoc cmd)
55     {
56         Runtime JavaDoc rt = Runtime.getRuntime();
57         Process JavaDoc proc = null;
58         try
59         {
60             // generating start command is now done in SiteReceiver.asp
61
/*
62             String pathSeparator = System.getProperty("path.separator");
63             String javaClasspath = System.getProperty("java.class.path");
64             String cmd = javaCmd + " " + javaArgs + " -classpath " + javaClasspath + pathSeparator
65                         + addClassPath + " " + classname + " " + arguments;
66             cmd = javaCmd + " -version";
67             cmd = "cmd.exe /c java -version";
68             //starting process
69             */

70             //CAT.debug(cmd);
71
proc = rt.exec(cmd);
72             InputStream in = proc.getInputStream();
73             BufferedReader reader = new BufferedReader(new InputStreamReader(in));
74             String JavaDoc line;
75             while ((line = reader.readLine()) != null)
76             {
77                 write(line);
78                 try
79                 {
80                     Thread.sleep(100);
81                 }
82                 catch (InterruptedException JavaDoc e)
83                 {
84                     write(e.toString());
85                 }
86             }
87             InputStream err = proc.getErrorStream();
88             BufferedReader errreader = new BufferedReader(new InputStreamReader(err));
89             while ((line = errreader.readLine()) != null)
90             {
91                 write(line);
92                 try
93                 {
94                     Thread.sleep(100);
95                 }
96                 catch (InterruptedException JavaDoc e)
97                 {
98                     write(e.toString());
99                 }
100             }
101         }
102         catch (Exception JavaDoc e)
103         {
104             write(e.toString());
105                         //CAT.error("Error while executing process",e);
106
}
107         try
108         {
109             proc.waitFor();
110         }
111         catch (InterruptedException JavaDoc e)
112         {
113             write(e.toString());
114         }
115         if ( proc != null )
116         {
117             proc.destroy();
118         }
119     }
120 }
Popular Tags