< prev index next >

buildSrc/src/main/java/workaround/GradleJUnitWorker.java

Print this page


   1 /*
   2  * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


  36 import java.lang.reflect.Constructor;
  37 import java.net.URL;
  38 import java.net.URLClassLoader;
  39 import java.util.ArrayList;
  40 import java.util.Collections;
  41 import java.util.Enumeration;
  42 import java.util.HashSet;
  43 import java.util.Properties;
  44 
  45 /*
  46  * This wrapper is designed to workaround a class loader issue
  47  * when working with JDK9 and Gradle in the JFX builds
  48  * NOTE: this worker assumes the @argfile support found in JDK9
  49  *
  50  * Due to the nature of the command line and where the this class is
  51  * specified, certain command line properties may either be :
  52  *  an argument - because it came after this file
  53  *  a property - because it was before this file and was processed as a property
  54  * Because of this, everything is checked as both.
  55  *





  56  */
  57 public class GradleJUnitWorker {
  58 
  59     public static boolean debug = false;
  60 
  61     private static final String ENCODERCLASS
  62             = "jarjar.org.gradle.process.internal.child.EncodedStream$EncodedInput";
  63 
  64     private static URL fileToURL(File file) throws IOException {
  65         return file.getCanonicalFile().toURI().toURL();
  66     }
  67 
  68     // all of the "standard" System Properties that we will not forward
  69     // to the worker.
  70     private final static String[] defSysProps = {
  71         "awt.toolkit",
  72         "file.encoding.pkg",
  73         "file.encoding",
  74         "file.separator",
  75         "ftp.nonProxyHosts",


 119         "sun.os.patch.level",
 120         "user.country",
 121         "user.dir",
 122         "user.home",
 123         "user.language",
 124         "user.name",
 125         "user.timezone",
 126         // windows
 127         "user.script",
 128         "user.variant",
 129         "sun.desktop",
 130         // Jake
 131         "java.vm.compressedOopsMode",
 132         "jdk.boot.class.path.append",};
 133 
 134     static HashSet<String> ignoreSysProps  =  new HashSet(defSysProps.length + 10);
 135 
 136     public static void main(String args[]) {
 137 
 138         try {
 139             ArrayList<String> cmd = new ArrayList<>(30);
 140             String gradleWorkerJar = null;
 141             String patchesDir = null;
 142             String exportsFile = null;
 143             String classpathFile = null;
 144             String libraryPath = null;
 145             String java9path = null;
 146 
 147             final String exportsFileProperty = "worker.exports.file";
 148             final String workerDebugProperty = "worker.debug";
 149             final String patchesDirProperty = "worker.xpatch.dir";
 150             final String classpathFileProperty = "worker.classpath.file";
 151             final String libraryPathProperty = "worker.library.path";
 152             final String java9Property = "worker.java9";
 153 
 154             Collections.addAll(ignoreSysProps, defSysProps);
 155             ignoreSysProps.add(exportsFileProperty);
 156             ignoreSysProps.add(workerDebugProperty);
 157             ignoreSysProps.add(patchesDirProperty);
 158             ignoreSysProps.add(classpathFileProperty);
 159             ignoreSysProps.add(libraryPathProperty);
 160             ignoreSysProps.add(java9Property);
 161 
 162             debug = Boolean.parseBoolean(System.getProperty(workerDebugProperty, "false"));
 163 
 164             ArrayList<String> newArgs = new ArrayList<>(50);
 165             for (int i = 0; i < args.length; i++) {
 166                 if (args[i].startsWith("-cp")) {
 167                     gradleWorkerJar = args[i + 1];
 168                     i++; // skip the path argument
 169                     if (debug) System.err.println("XWORKER gradleWorkerJar="+exportsFile);
 170                 } else if (args[i].contains(exportsFileProperty)) {
 171                     int equals = args[i].indexOf("=");
 172                     exportsFile = args[i].substring(equals+1);
 173                     if (debug) System.err.println("XWORKER "+exportsFileProperty+"="+exportsFile);
 174                 } else if (args[i].contains(patchesDirProperty)) {
 175                     int equals = args[i].indexOf("=");
 176                     patchesDir = args[i].substring(equals+1);
 177                     if (debug) System.err.println("XWORKER "+patchesDirProperty+"="+patchesDir);
 178                 } else if (args[i].contains(java9Property)) {
 179                     int equals = args[i].indexOf("=");
 180                     java9path = args[i].substring(equals+1);
 181                     if (debug) System.err.println("XWORKER "+java9Property+"="+java9path);
 182                 } else if (args[i].contains(classpathFileProperty)) {
 183                     int equals = args[i].indexOf("=");
 184                     classpathFile = args[i].substring(equals+1);
 185                     if (debug) System.err.println("XWORKER "+classpathFileProperty+"="+classpathFile);
 186                 } else if (args[i].contains(libraryPathProperty)) {
 187                     int equals = args[i].indexOf("=");
 188                     libraryPath = args[i].substring(equals+1);
 189                     if (debug) System.err.println("XWORKER "+libraryPathProperty+"="+libraryPath);
 190                 } else {
 191                     if (debug) System.err.println("XWORKER forwarding cmd "+args[i]);
 192                     newArgs.add(args[i]);
 193                 }
 194             }
 195 
 196             // Debug routine to capture a worker stream for replaying
 197             if (false) {
 198                 File dumper = new File("datadump.txt");
 199                 FileOutputStream out
 200                         = new FileOutputStream(dumper);
 201 


 322                 }
 323                 br.write("\"");
 324                 br.newLine();
 325                 br.close();
 326             } catch (IOException e) {
 327                 throw new RuntimeException("Could not write @classpath.txt");
 328             }
 329 
 330             String jdk_home_env = System.getenv("JDK9_HOME");
 331             if (debug) System.err.println("XWORKER JDK9_HOME (env) is set to " + jdk_home_env);
 332 
 333             if (jdk_home_env == null) {
 334                 jdk_home_env = System.getenv("JDK_HOME");
 335                 if (debug) System.err.println("XWORKER JDK_HOME (env) is set to " + jdk_home_env);
 336             }
 337 
 338             String jdk_home = System.getProperty("JDK_HOME");
 339             if (debug) System.err.println("XWORKER JDK_HOME is set to " + jdk_home);
 340 
 341             if (java9path == null) {
 342                 java9path = System.getProperty(java9Property);
 343             }
 344 
 345             String java_cmd = "java";
 346             if (java9path != null) {
 347                 // good we have it - probably the safest way on windows
 348                 java_cmd = java9path;
 349                 if (debug) System.err.println("XWORKER JAVA9 is set to " + java_cmd);
 350             } else if (jdk_home_env != null) {
 351                 java9path = jdk_home_env
 352                         + File.separatorChar + "bin"
 353                         + File.separatorChar + "java";
 354             } else if (jdk_home != null) {
 355                 java_cmd = jdk_home
 356                         + File.separatorChar + "bin"
 357                         + File.separatorChar + "java";
 358             }
 359 
 360             if (debug) {
 361                 System.err.println("XWORKER using java  " + java_cmd);
 362             }
 363 
 364             cmd.add(java_cmd);
 365 


 366             if (patchesDir == null) {
 367                 patchesDir = System.getProperty(patchesDirProperty);
 368             }
 369 
 370             if (patchesDir != null) {
 371                 cmd.add("-Xpatch:" + patchesDir);

 372             }
 373 
 374             if (exportsFile == null) {
 375                 exportsFile = System.getProperty(exportsFileProperty);
 376             }
 377 
 378             if (exportsFile != null) {
 379                 cmd.add("@" + exportsFile);

 380             }
 381 
 382             cmd.add("@" + classPathArgFile.getAbsolutePath());



 383 
 384             if (libraryPath == null) {
 385                 libraryPath = System.getProperty(libraryPathProperty);
 386             }
 387 
 388             if (libraryPath != null) {
 389                 cmd.add("-Djava.library.path=" + libraryPath);





 390             }
 391 
 392             //forward any old system properties, other than the stock ones
 393             Properties p = System.getProperties();
 394             Enumeration keys = p.keys();
 395             while (keys.hasMoreElements()) {
 396                 String key = (String) keys.nextElement();
 397                 if (!ignoreSysProps.contains(key)) {
 398                     String value = (String) p.get(key);
 399                     String newprop = "-D"+key+"="+value;
 400                     cmd.add(newprop);
 401                     if (debug) System.out.println("XWORKER adding "+newprop);
 402 
 403                 }
 404             }
 405 
 406             newArgs.forEach(s-> {
 407                 cmd.add(s);
 408             });
 409 


   1 /*
   2  * Copyright (c) 2015, 2016 Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


  36 import java.lang.reflect.Constructor;
  37 import java.net.URL;
  38 import java.net.URLClassLoader;
  39 import java.util.ArrayList;
  40 import java.util.Collections;
  41 import java.util.Enumeration;
  42 import java.util.HashSet;
  43 import java.util.Properties;
  44 
  45 /*
  46  * This wrapper is designed to workaround a class loader issue
  47  * when working with JDK9 and Gradle in the JFX builds
  48  * NOTE: this worker assumes the @argfile support found in JDK9
  49  *
  50  * Due to the nature of the command line and where the this class is
  51  * specified, certain command line properties may either be :
  52  *  an argument - because it came after this file
  53  *  a property - because it was before this file and was processed as a property
  54  * Because of this, everything is checked as both.
  55  *
  56  * The worker specific properties are found below as worker.*. These
  57  * properties also need to be forwarded as system properties just in case
  58  * we have something like MainLauncherTest that needs some of these to
  59  * construct a new java command.
  60  *
  61  */
  62 public class GradleJUnitWorker {
  63 
  64     public static boolean debug = false;
  65 
  66     private static final String ENCODERCLASS
  67             = "jarjar.org.gradle.process.internal.child.EncodedStream$EncodedInput";
  68 
  69     private static URL fileToURL(File file) throws IOException {
  70         return file.getCanonicalFile().toURI().toURL();
  71     }
  72 
  73     // all of the "standard" System Properties that we will not forward
  74     // to the worker.
  75     private final static String[] defSysProps = {
  76         "awt.toolkit",
  77         "file.encoding.pkg",
  78         "file.encoding",
  79         "file.separator",
  80         "ftp.nonProxyHosts",


 124         "sun.os.patch.level",
 125         "user.country",
 126         "user.dir",
 127         "user.home",
 128         "user.language",
 129         "user.name",
 130         "user.timezone",
 131         // windows
 132         "user.script",
 133         "user.variant",
 134         "sun.desktop",
 135         // Jake
 136         "java.vm.compressedOopsMode",
 137         "jdk.boot.class.path.append",};
 138 
 139     static HashSet<String> ignoreSysProps  =  new HashSet(defSysProps.length + 10);
 140 
 141     public static void main(String args[]) {
 142 
 143         try {
 144             final ArrayList<String> cmd = new ArrayList<>(30);
 145             String gradleWorkerJar = null;
 146             String patchesDir = null;
 147             String exportsFile = null;
 148             String classpathFile = null;
 149             String libraryPath = null;
 150             String java9path = null;
 151 
 152             final String exportsFileProperty = "worker.exports.file";
 153             final String workerDebugProperty = "worker.debug";
 154             final String patchesDirProperty = "worker.xpatch.dir";
 155             final String classpathFileProperty = "worker.classpath.file";
 156             final String libraryPathProperty = "worker.library.path";
 157             final String javaCmdProperty = "worker.java.cmd";
 158 
 159             Collections.addAll(ignoreSysProps, defSysProps);
 160             ignoreSysProps.add(exportsFileProperty);
 161             ignoreSysProps.add(workerDebugProperty);
 162             ignoreSysProps.add(patchesDirProperty);
 163             ignoreSysProps.add(classpathFileProperty);
 164             ignoreSysProps.add(libraryPathProperty);
 165             ignoreSysProps.add(javaCmdProperty);
 166 
 167             debug = Boolean.parseBoolean(System.getProperty(workerDebugProperty, "false"));
 168 
 169             ArrayList<String> newArgs = new ArrayList<>(50);
 170             for (int i = 0; i < args.length; i++) {
 171                 if (args[i].startsWith("-cp")) {
 172                     gradleWorkerJar = args[i + 1];
 173                     i++; // skip the path argument
 174                     if (debug) System.err.println("XWORKER gradleWorkerJar="+exportsFile);
 175                 } else if (args[i].contains(exportsFileProperty)) {
 176                     int equals = args[i].indexOf("=");
 177                     exportsFile = args[i].substring(equals+1);
 178                     if (debug) System.err.println("XWORKER "+exportsFileProperty+"="+exportsFile);
 179                 } else if (args[i].contains(patchesDirProperty)) {
 180                     int equals = args[i].indexOf("=");
 181                     patchesDir = args[i].substring(equals+1);
 182                     if (debug) System.err.println("XWORKER "+patchesDirProperty+"="+patchesDir);
 183                 } else if (args[i].contains(javaCmdProperty)) {
 184                     int equals = args[i].indexOf("=");
 185                     java9path = args[i].substring(equals+1);
 186                     if (debug) System.err.println("XWORKER "+javaCmdProperty+"="+java9path);
 187                 } else if (args[i].contains(classpathFileProperty)) {
 188                     int equals = args[i].indexOf("=");
 189                     classpathFile = args[i].substring(equals+1);
 190                     if (debug) System.err.println("XWORKER "+classpathFileProperty+"="+classpathFile);
 191                 } else if (args[i].contains(libraryPathProperty)) {
 192                     int equals = args[i].indexOf("=");
 193                     libraryPath = args[i].substring(equals+1);
 194                     if (debug) System.err.println("XWORKER "+libraryPathProperty+"="+libraryPath);
 195                 } else {
 196                     if (debug) System.err.println("XWORKER forwarding cmd "+args[i]);
 197                     newArgs.add(args[i]);
 198                 }
 199             }
 200 
 201             // Debug routine to capture a worker stream for replaying
 202             if (false) {
 203                 File dumper = new File("datadump.txt");
 204                 FileOutputStream out
 205                         = new FileOutputStream(dumper);
 206 


 327                 }
 328                 br.write("\"");
 329                 br.newLine();
 330                 br.close();
 331             } catch (IOException e) {
 332                 throw new RuntimeException("Could not write @classpath.txt");
 333             }
 334 
 335             String jdk_home_env = System.getenv("JDK9_HOME");
 336             if (debug) System.err.println("XWORKER JDK9_HOME (env) is set to " + jdk_home_env);
 337 
 338             if (jdk_home_env == null) {
 339                 jdk_home_env = System.getenv("JDK_HOME");
 340                 if (debug) System.err.println("XWORKER JDK_HOME (env) is set to " + jdk_home_env);
 341             }
 342 
 343             String jdk_home = System.getProperty("JDK_HOME");
 344             if (debug) System.err.println("XWORKER JDK_HOME is set to " + jdk_home);
 345 
 346             if (java9path == null) {
 347                 java9path = System.getProperty(javaCmdProperty);
 348             }
 349 
 350             String java_cmd = "java";
 351             if (java9path != null) {
 352                 // good we have it - probably the safest way on windows
 353                 java_cmd = java9path;
 354                 if (debug) System.err.println("XWORKER JAVA9 is set to " + java_cmd);
 355             } else if (jdk_home_env != null) {
 356                 java9path = jdk_home_env
 357                         + File.separatorChar + "bin"
 358                         + File.separatorChar + "java";
 359             } else if (jdk_home != null) {
 360                 java_cmd = jdk_home
 361                         + File.separatorChar + "bin"
 362                         + File.separatorChar + "java";
 363             }
 364 
 365             if (debug) {
 366                 System.err.println("XWORKER using java  " + java_cmd);
 367             }
 368 
 369             cmd.add(java_cmd);
 370 
 371             cmd.add("-D"+javaCmdProperty+"="+java_cmd);
 372 
 373             if (patchesDir == null) {
 374                 patchesDir = System.getProperty(patchesDirProperty);
 375             }
 376 
 377             if (patchesDir != null) {
 378                 cmd.add("-Xpatch:" + patchesDir);
 379                 cmd.add("-D"+patchesDirProperty+"="+patchesDir);
 380             }
 381 
 382             if (exportsFile == null) {
 383                 exportsFile = System.getProperty(exportsFileProperty);
 384             }
 385 
 386             if (exportsFile != null) {
 387                 cmd.add("@" + exportsFile);
 388                 cmd.add("-D"+exportsFileProperty+"="+exportsFile);
 389             }
 390 
 391             final String cleanpath =
 392                 classPathArgFile.getAbsolutePath().replaceAll("\\\\", "/");
 393             cmd.add("@" + cleanpath);
 394             cmd.add("-D"+classpathFileProperty+"="+cleanpath);
 395 
 396             if (libraryPath == null) {
 397                 libraryPath = System.getProperty(libraryPathProperty);
 398             }
 399 
 400             if (libraryPath != null) {
 401                 cmd.add("-Djava.library.path=" + libraryPath);
 402                 cmd.add("-D"+libraryPathProperty+"="+libraryPath);
 403             }
 404 
 405             if (debug) {
 406                 cmd.add("-D"+workerDebugProperty+"="+debug);
 407             }
 408 
 409             //forward any old system properties, other than the stock ones
 410             Properties p = System.getProperties();
 411             Enumeration keys = p.keys();
 412             while (keys.hasMoreElements()) {
 413                 String key = (String) keys.nextElement();
 414                 if (!ignoreSysProps.contains(key)) {
 415                     String value = (String) p.get(key);
 416                     String newprop = "-D"+key+"="+value;
 417                     cmd.add(newprop);
 418                     if (debug) System.out.println("XWORKER adding "+newprop);
 419 
 420                 }
 421             }
 422 
 423             newArgs.forEach(s-> {
 424                 cmd.add(s);
 425             });
 426 


< prev index next >