Source code: jtt/util/Executable.java
1 ///////////////////////////////////////////////////////////////////////////////
2 // Filename: $RCSfile: Executable.java,v $
3 // Purpose: Calls corina to create 3D structures.
4 // Language: Java
5 // Compiler: JDK 1.4
6 // Authors: Joerg K. Wegner
7 // Version: $Revision: 1.2 $
8 // $Date: 2003/10/13 08:17:00 $
9 // $Author: wegner $
10 //
11 // Copyright (c) Dept. Computer Architecture, University of Tuebingen, Germany
12 ///////////////////////////////////////////////////////////////////////////////
13 package jtt.util;
14
15 import wsi.ra.tool.*;
16
17 /*==========================================================================*
18 * IMPORTS
19 *========================================================================== */
20 import java.io.*;
21
22 import java.util.*;
23
24 //import cformat.*;
25 import org.apache.log4j.*;
26 import org.apache.tools.ant.Project;
27 import org.apache.tools.ant.taskdefs.Execute;
28 import org.apache.tools.ant.taskdefs.Jikes;
29
30
31 /*==========================================================================*
32 * CLASS DECLARATION
33 *========================================================================== */
34
35 /**
36 * Calls corina to create 3D structures.
37 *
38 * @author wegnerj
39 */
40 public class Executable
41 {
42 //~ Static fields/initializers /////////////////////////////////////////////
43
44 /*-------------------------------------------------------------------------*
45 * private static member variables
46 *------------------------------------------------------------------------- */
47
48 /**
49 * Obtain a suitable logger.
50 */
51 private static Category logger = Category.getInstance("jtt.util.Executable");
52
53 //~ Constructors ///////////////////////////////////////////////////////////
54
55 /*-------------------------------------------------------------------------*
56 * private member variables
57 *------------------------------------------------------------------------- */
58 /*-------------------------------------------------------------------------*
59 * constructor
60 *------------------------------------------------------------------------- */
61
62 /**
63 * Constructor for the Corina object
64 */
65 public Executable()
66 {
67 }
68
69 //~ Methods ////////////////////////////////////////////////////////////////
70
71 /*-------------------------------------------------------------------------*
72 * public static methods
73 *------------------------------------------------------------------------- */
74 public static boolean execute(String dir, String exe, String[] arguments)
75 {
76 System.out.println("EXE: " + dir);
77 System.out.println("EXE: " + exe);
78
79 for (int i = 0; i < arguments.length; i++)
80 {
81 System.out.println("EXE: " + arguments[i]);
82 }
83
84 Execute exec = new Execute();
85 exec.setAntRun(new Project());
86 exec.setWorkingDirectory(new File(dir));
87
88 String[] args = new String[arguments.length + 1];
89 System.arraycopy(arguments, 0, args, 1, arguments.length);
90 args[0] = exe;
91 exec.setCommandline(args);
92
93 try
94 {
95 exec.execute();
96 }
97 catch (IOException e)
98 {
99 e.printStackTrace();
100 }
101
102 Jikes t;
103
104 return true;
105 }
106
107 /**
108 * Description of the Method
109 *
110 * @param mol Description of the Parameter
111 * @return Description of the Return Value
112 */
113 public static boolean execute(String[] arguments, boolean wait)
114 {
115 for (int i = 0; i < arguments.length; i++)
116 {
117 System.out.println(arguments[i]);
118 }
119
120 //String args[]=new String[arguments.length+1];
121 //System.arraycopy(arguments,0,args,1,arguments.length);
122 //arguments[0]="jade";
123 String[] args = new String[arguments.length];
124 System.arraycopy(arguments, 0, args, 0, arguments.length);
125
126 // execute
127 Process process;
128 StringBuffer buffer = new StringBuffer(1000);
129 StringBuffer errors = new StringBuffer(1000);
130
131 try
132 {
133 Runtime runtime = Runtime.getRuntime();
134 process = runtime.exec(args);
135
136 // set input pipe
137 BufferedReader in = new BufferedReader(new InputStreamReader(
138 process.getInputStream()));
139
140 // set output pipe
141 BufferedWriter out = new BufferedWriter(new OutputStreamWriter(
142 process.getOutputStream()));
143 out.close();
144
145 BufferedReader error = new BufferedReader(new InputStreamReader(
146 process.getErrorStream()));
147
148 // wait for extern process termination
149 if (wait)
150 {
151 process.waitFor();
152 }
153
154 // get input pipe data
155 String nextLine = null;
156
157 System.out.println("get in");
158
159 while ((nextLine = in.readLine()) != null)
160 {
161 buffer.append(nextLine + "\n");
162
163 //System.out.println("IN: "+nextLine);
164 }
165
166 while ((nextLine = error.readLine()) != null)
167 {
168 errors.append(nextLine + "\n");
169
170 //System.out.println("ERROR: "+nextLine);
171 }
172 }
173 catch (Exception e)
174 {
175 logger.error("Could not start executable: " + args[0]);
176 e.printStackTrace();
177
178 return false;
179 }
180
181 ByteArrayInputStream sReader = new ByteArrayInputStream(buffer.toString()
182 .getBytes());
183
184 return true;
185 }
186
187 /**
188 * The main program for the TestSmarts class
189 *
190 * @param args The command line arguments
191 */
192 public static void main(String[] args)
193 {
194 //Executable.execute(args, true);
195 Executable.execute(args[0], args[1], new String[]{args[2]});
196 System.exit(0);
197 }
198 }
199 ///////////////////////////////////////////////////////////////////////////////
200 // END OF FILE.
201 ///////////////////////////////////////////////////////////////////////////////