1 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 ; 22 import java.io.IOException ; 23 import java.lang.reflect.Field ; 24 import java.util.ArrayList ; 25 import java.util.Iterator ; 26 import java.util.List ; 27 import java.util.jar.Attributes ; 28 import java.util.jar.JarFile ; 29 import java.util.jar.Manifest ; 30 31 36 public final class CargoJava extends Java { 37 38 private static final boolean DEBUG = false; 39 40 public static final Link LINK = new Link(); 42 43 private Java java; 44 private Path classpath; 45 private String className; 46 private List args; 47 private boolean dirSet = false; 48 49 public CargoJava(Java java) { 50 this.java = java; 51 this.args = new ArrayList (); 52 } 53 54 private void wrapProcess() { 55 Args linkArgs; 56 try { 57 linkArgs = Link.take(); 58 } catch (InterruptedException e) { 59 throw new RuntimeException (e); 60 } 61 62 File dir = linkArgs.instancePath; 63 64 String logFile = new File (dir.getParent(), dir.getName() + ".log").getAbsolutePath(); 65 66 if (!dirSet) { 67 setDir(dir); 68 } 69 70 java.setOutput(new File (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 e) { 78 throw new RuntimeException ("Unable to load: " + TestConfigObject.class.getName()); 79 } 80 java.setClassname(CargoLinkedChildProcess.class.getName()); 81 Environment.Variable envVar = new Environment.Variable(); 83 envVar.setKey("JAVA_HOME"); 84 envVar.setValue(System.getProperty("java.home")); 85 java.addEnv(envVar); 86 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 try { 102 Field f = j.getClass().getDeclaredField("cmdl"); 103 f.setAccessible(true); 104 return (CommandlineJava) f.get(j); 105 } catch (Exception e) { 106 throw new RuntimeException (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 iter = this.args.iterator(); 117 while (iter.hasNext()) { 118 String [] 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 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 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 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 arg0, int arg1) { 197 this.java.log(arg0, arg1); 198 } 199 200 public void log(String 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 arg0) { 213 this.java.setArgs(arg0); 214 } 215 216 public void setClassname(String 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 arg0) { 230 this.java.setDescription(arg0); 231 } 232 233 public void setDir(File 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 arg0) throws BuildException { 247 try { 248 String absPath = arg0.getCanonicalFile().getParentFile().getParent(); 249 JarFile jar = new JarFile (arg0); 250 Manifest manifest = jar.getManifest(); 251 Attributes attrib = manifest.getMainAttributes(); 252 253 String classPathAttrib = attrib.getValue("Class-Path"); 254 String 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 263 setClassname(attrib.getValue("Main-Class")); 264 265 } catch (IOException ioe) { 266 throw new BuildException("problem reading manifest"); 267 } 268 } 269 270 285 public void setJvm(String arg0) { 286 this.java.setJvm(arg0); 287 } 288 289 public void setJvmargs(String arg0) { 290 this.java.setJvmargs(arg0); 291 } 292 293 public void setJVMVersion(String 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 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 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 arg0) { 326 this.java.setTaskName(arg0); 327 } 328 329 public void setTimeout(Long arg0) { 330 this.java.setTimeout(arg0); 331 } 332 333 public String toString() { 334 return this.java.toString(); 335 } 336 337 public static class Args { 338 final File instancePath; 339 final int port; 340 341 public Args(int port, File 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 lock = new Object (); 350 351 public static Args take() throws InterruptedException { 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 { 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 |