KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > test > server > appserver > cargo > CargoJava


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tc.test.server.appserver.cargo;
5
6 import org.apache.tools.ant.BuildException;
7 import org.apache.tools.ant.Location;
8 import org.apache.tools.ant.Project;
9 import org.apache.tools.ant.RuntimeConfigurable;
10 import org.apache.tools.ant.Target;
11 import org.apache.tools.ant.taskdefs.Java;
12 import org.apache.tools.ant.types.CommandlineJava;
13 import org.apache.tools.ant.types.Environment;
14 import org.apache.tools.ant.types.Path;
15 import org.apache.tools.ant.types.Reference;
16 import org.apache.tools.ant.types.Commandline.Argument;
17 import org.apache.tools.ant.types.Environment.Variable;
18
19 import com.tc.test.TestConfigObject;
20
21 import java.io.File JavaDoc;
22 import java.io.IOException JavaDoc;
23 import java.lang.reflect.Field JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.jar.Attributes JavaDoc;
28 import java.util.jar.JarFile JavaDoc;
29 import java.util.jar.Manifest JavaDoc;
30
31 /**
32  * This class is used in the process of patching CARGO to allow it's child processes to know whether the parent process
33  * is still alive and kill themselves off if need be. It is a decorator for the ANT Java task which calls
34  * {@link CargoLinkedChildProcess} which in turn spawns the desired appserver instance.
35  */

36 public final class CargoJava extends Java {
37
38   private static final boolean DEBUG = false;
39
40   // this static thing is TERRIBLE, but trying to get tigher integration with Cargo is worse
41
public static final Link LINK = new Link();
42
43   private Java java;
44   private Path classpath;
45   private String JavaDoc className;
46   private List JavaDoc args;
47   private boolean dirSet = false;
48
49   public CargoJava(Java java) {
50     this.java = java;
51     this.args = new ArrayList JavaDoc();
52   }
53
54   private void wrapProcess() {
55     Args linkArgs;
56     try {
57       linkArgs = Link.take();
58     } catch (InterruptedException JavaDoc e) {
59       throw new RuntimeException JavaDoc(e);
60     }
61
62     File JavaDoc dir = linkArgs.instancePath;
63
64     String JavaDoc logFile = new File JavaDoc(dir.getParent(), dir.getName() + ".log").getAbsolutePath();
65
66     if (!dirSet) {
67       setDir(dir);
68     }
69
70     java.setOutput(new File JavaDoc(logFile));
71     java.setFailonerror(true);
72
73     assignWrappedArgs(linkArgs);
74     try {
75       TestConfigObject config = TestConfigObject.getInstance();
76       classpath.setPath(classpath.toString() + File.pathSeparatorChar + config.linkedChildProcessClasspath());
77     } catch (IOException JavaDoc e) {
78       throw new RuntimeException JavaDoc("Unable to load: " + TestConfigObject.class.getName());
79     }
80     java.setClassname(CargoLinkedChildProcess.class.getName());
81     // java.setMaxmemory("128m");
82
Environment.Variable envVar = new Environment.Variable();
83     envVar.setKey("JAVA_HOME");
84     envVar.setValue(System.getProperty("java.home"));
85     java.addEnv(envVar);
86     // Argument argument = java.createJvmarg();
87
// argument.setValue("-verbose:gc");
88

89     if (DEBUG) {
90       CommandlineJava cmdLineJava = getCommandLine(this.java);
91       System.err.println(cmdLineJava.describeCommand());
92     }
93
94     java.createJvmarg().setValue("-DNODE=" + dir.getName());
95
96     java.execute();
97   }
98
99   private static CommandlineJava getCommandLine(Java j) {
100     // more utter gross-ness
101
try {
102       Field JavaDoc f = j.getClass().getDeclaredField("cmdl");
103       f.setAccessible(true);
104       return (CommandlineJava) f.get(j);
105     } catch (Exception JavaDoc e) {
106       throw new RuntimeException JavaDoc(e);
107     }
108   }
109
110   private void assignWrappedArgs(Args linkArgs) {
111     java.clearArgs();
112     java.createArg().setValue(this.className);
113     java.createArg().setValue(Integer.toString(linkArgs.port));
114     java.createArg().setValue(linkArgs.instancePath.getAbsolutePath());
115
116     Iterator JavaDoc iter = this.args.iterator();
117     while (iter.hasNext()) {
118       String JavaDoc[] parts = ((Argument) iter.next()).getParts();
119       for (int i = 0; i < parts.length; ++i)
120         java.createArg().setValue(parts[i]);
121     }
122   }
123
124   public void addEnv(Variable arg0) {
125     this.java.addEnv(arg0);
126   }
127
128   public void addSysproperty(Variable arg0) {
129     this.java.addSysproperty(arg0);
130   }
131
132   public void clearArgs() {
133     this.java.clearArgs();
134   }
135
136   public Argument createArg() {
137     Argument out = this.java.createArg();
138     this.args.add(out);
139     return out;
140   }
141
142   public Path createClasspath() {
143     Path path = this.java.createClasspath();
144     this.classpath = path;
145     return path;
146   }
147
148   public Argument createJvmarg() {
149     return this.java.createJvmarg();
150   }
151
152   public boolean equals(Object JavaDoc obj) {
153     return this.java.equals(obj);
154   }
155
156   public void execute() throws BuildException {
157     wrapProcess();
158   }
159
160   public int executeJava() throws BuildException {
161     return this.java.executeJava();
162   }
163
164   public String JavaDoc getDescription() {
165     return this.java.getDescription();
166   }
167
168   public Location getLocation() {
169     return this.java.getLocation();
170   }
171
172   public Target getOwningTarget() {
173     return this.java.getOwningTarget();
174   }
175
176   public Project getProject() {
177     return this.java.getProject();
178   }
179
180   public RuntimeConfigurable getRuntimeConfigurableWrapper() {
181     return this.java.getRuntimeConfigurableWrapper();
182   }
183
184   public String JavaDoc getTaskName() {
185     return this.java.getTaskName();
186   }
187
188   public int hashCode() {
189     return this.java.hashCode();
190   }
191
192   public void init() throws BuildException {
193     this.java.init();
194   }
195
196   public void log(String JavaDoc arg0, int arg1) {
197     this.java.log(arg0, arg1);
198   }
199
200   public void log(String JavaDoc arg0) {
201     this.java.log(arg0);
202   }
203
204   public void maybeConfigure() throws BuildException {
205     this.java.maybeConfigure();
206   }
207
208   public void setAppend(boolean arg0) {
209     this.java.setAppend(arg0);
210   }
211
212   public void setArgs(String JavaDoc arg0) {
213     this.java.setArgs(arg0);
214   }
215
216   public void setClassname(String JavaDoc arg0) throws BuildException {
217     this.className = arg0;
218     this.java.setClassname(arg0);
219   }
220
221   public void setClasspath(Path arg0) {
222     this.java.setClasspath(arg0);
223   }
224
225   public void setClasspathRef(Reference arg0) {
226     this.java.setClasspathRef(arg0);
227   }
228
229   public void setDescription(String JavaDoc arg0) {
230     this.java.setDescription(arg0);
231   }
232
233   public void setDir(File JavaDoc arg0) {
234     this.java.setDir(arg0);
235     dirSet = true;
236   }
237
238   public void setFailonerror(boolean arg0) {
239     this.java.setFailonerror(arg0);
240   }
241
242   public void setFork(boolean arg0) {
243     this.java.setFork(arg0);
244   }
245
246   public void setJar(File JavaDoc arg0) throws BuildException {
247     try {
248       String JavaDoc absPath = arg0.getCanonicalFile().getParentFile().getParent();
249       JarFile JavaDoc jar = new JarFile JavaDoc(arg0);
250       Manifest JavaDoc manifest = jar.getManifest();
251       Attributes JavaDoc attrib = manifest.getMainAttributes();
252
253       String JavaDoc classPathAttrib = attrib.getValue("Class-Path");
254       String JavaDoc absClassPath = classPathAttrib.replaceAll("^\\.\\.", absPath).replaceAll("\\s\\.\\.",
255                                                                                       File.pathSeparatorChar + absPath);
256       this.classpath.setPath(classpath.toString() + File.pathSeparatorChar + absClassPath);
257       this.classpath.setPath(classpath.toString() + File.pathSeparator + arg0);
258
259       // TODO: make sysprops
260
// this.classpath.setPath(classpath.toString() + createExtraManifestClassPath("Endorsed-Dirs", attrib, absPath));
261
// this.classpath.setPath(classpath.toString() + createExtraManifestClassPath("Extension-Dirs", attrib, absPath));
262

263       setClassname(attrib.getValue("Main-Class"));
264
265     } catch (IOException JavaDoc ioe) {
266       throw new BuildException("problem reading manifest");
267     }
268   }
269
270   // private String createExtraManifestClassPath(String attributeName, Attributes attrib, String absPath) {
271
// String extraDirAttrib = attrib.getValue(attributeName);
272
// File absExtraDir = new File(absPath + File.separator + extraDirAttrib);
273
// String[] extraJars = absExtraDir.list(new FilenameFilter() {
274
// public boolean accept(File dir, String name) {
275
// return (name.endsWith(".jar")) ? true : false;
276
// }
277
// });
278
// String extraClassPath = "";
279
// for (int i = 0; i < extraJars.length; i++) {
280
// extraClassPath += File.pathSeparatorChar + absExtraDir.toString() + File.separator + extraJars[i];
281
// }
282
// return extraClassPath;
283
// }
284

285   public void setJvm(String JavaDoc arg0) {
286     this.java.setJvm(arg0);
287   }
288
289   public void setJvmargs(String JavaDoc arg0) {
290     this.java.setJvmargs(arg0);
291   }
292
293   public void setJVMVersion(String JavaDoc arg0) {
294     this.java.setJVMVersion(arg0);
295   }
296
297   public void setLocation(Location arg0) {
298     this.java.setLocation(arg0);
299   }
300
301   public void setMaxmemory(String JavaDoc arg0) {
302     this.java.setMaxmemory(arg0);
303   }
304
305   public void setNewenvironment(boolean arg0) {
306     this.java.setNewenvironment(arg0);
307   }
308
309   public void setOutput(File JavaDoc arg0) {
310     this.java.setOutput(arg0);
311   }
312
313   public void setOwningTarget(Target arg0) {
314     this.java.setOwningTarget(arg0);
315   }
316
317   public void setProject(Project arg0) {
318     this.java.setProject(arg0);
319   }
320
321   public void setRuntimeConfigurableWrapper(RuntimeConfigurable arg0) {
322     this.java.setRuntimeConfigurableWrapper(arg0);
323   }
324
325   public void setTaskName(String JavaDoc arg0) {
326     this.java.setTaskName(arg0);
327   }
328
329   public void setTimeout(Long JavaDoc arg0) {
330     this.java.setTimeout(arg0);
331   }
332
333   public String JavaDoc toString() {
334     return this.java.toString();
335   }
336
337   public static class Args {
338     final File JavaDoc instancePath;
339     final int port;
340
341     public Args(int port, File JavaDoc instancePath) {
342       this.port = port;
343       this.instancePath = instancePath;
344     }
345   }
346
347   public static class Link {
348     private static Args args = null;
349     private static final Object JavaDoc lock = new Object JavaDoc();
350
351     public static Args take() throws InterruptedException JavaDoc {
352       synchronized (lock) {
353         while (args == null) {
354           lock.wait();
355         }
356         Args rv = args;
357         args = null;
358         lock.notifyAll();
359         return rv;
360       }
361     }
362
363     public static void put(Args putArgs) throws InterruptedException JavaDoc {
364       synchronized (lock) {
365         while (args != null) {
366           lock.wait();
367         }
368
369         args = putArgs;
370         lock.notifyAll();
371       }
372     }
373   }
374
375 }
376
Popular Tags