modules/jdk.packager/src/main/java/com/oracle/tools/packager/IOUtils.java

Print this page




 202                 consumer.print(lineRead + '\n');
 203             } else if (verbose) {
 204                Log.info(lineRead);
 205             } else {
 206                Log.debug(lineRead);
 207             }
 208         }
 209         try {
 210             int ret = p.waitFor();
 211             if (ret != 0 && !(testForPresenseOnly && ret != 127)) {
 212                 throw new IOException("Exec failed with code " + ret +
 213                         " command [" + Arrays.toString(pb.command().toArray(new String[0])) +
 214                         " in " + (pb.directory() != null ?
 215                            pb.directory().getAbsolutePath() : "unspecified directory"));
 216             }
 217         } catch (InterruptedException ex) {
 218         }
 219     }
 220 
 221     @SuppressWarnings("unchecked")
 222     public static int execute(Object ... args) throws IOException, InterruptedException {
 223         final ArrayList<String> argsList = new ArrayList<>();
 224         for (Object a : args) {
 225             if (a instanceof List) {
 226                 argsList.addAll((List)a);
 227             } else if (a instanceof String) {
 228                 argsList.add((String)a);
 229             }
 230         }
 231         final Process p = Runtime.getRuntime().exec(argsList.toArray(new String[argsList.size()]));























 232         final BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
 233         Thread t = new Thread(() -> {
 234             try {
 235                 String line;
 236                 while ((line = in.readLine()) != null) {
 237                     Log.info(line);
 238                 }
 239             } catch (IOException ioe) {
 240                 com.oracle.tools.packager.Log.verbose(ioe);
 241             }
 242         });
 243         t.setDaemon(true);
 244         t.start();
 245         final BufferedReader err = new BufferedReader(new InputStreamReader(p.getErrorStream()));
 246         t = new Thread(() -> {










 247             try {
 248                 String line;
 249                 while ((line = err.readLine()) != null) {
 250                     Log.error(line);
 251                 }
 252             } catch (IOException ioe) {
 253                 Log.verbose(ioe);
 254             }
 255         });
 256         t.setDaemon(true);
 257         t.start();
 258         return p.waitFor();








 259     }
 260 
 261     //no good test if we are running pre-JRE7
 262     //use heuristic approach
 263     // "false positive" is better than wrong answer
 264     public static boolean isNotSymbolicLink(File file) {
 265         //no symlinks on windows
 266         if (Platform.getPlatform() == Platform.WINDOWS) {
 267             return true;
 268         }
 269         try {
 270             if (file == null || file.getParent() == null) {
 271                 return false;
 272             }
 273             File file_canonical = new File(
 274                     file.getParentFile().getCanonicalFile(), file.getName());
 275             if (file_canonical.getCanonicalFile().equals(
 276                        file_canonical.getAbsoluteFile())) {
 277                 return true;
 278             }


 202                 consumer.print(lineRead + '\n');
 203             } else if (verbose) {
 204                Log.info(lineRead);
 205             } else {
 206                Log.debug(lineRead);
 207             }
 208         }
 209         try {
 210             int ret = p.waitFor();
 211             if (ret != 0 && !(testForPresenseOnly && ret != 127)) {
 212                 throw new IOException("Exec failed with code " + ret +
 213                         " command [" + Arrays.toString(pb.command().toArray(new String[0])) +
 214                         " in " + (pb.directory() != null ?
 215                            pb.directory().getAbsolutePath() : "unspecified directory"));
 216             }
 217         } catch (InterruptedException ex) {
 218         }
 219     }
 220 
 221     @SuppressWarnings("unchecked")
 222     private static Process startProcess(Object... args) throws IOException {
 223         final ArrayList<String> argsList = new ArrayList<>();
 224         for (Object a : args) {
 225             if (a instanceof List) {
 226                 argsList.addAll((List)a);
 227             } else if (a instanceof String) {
 228                 argsList.add((String)a);
 229             }
 230         }
 231 
 232         return Runtime.getRuntime().exec(argsList.toArray(new String[argsList.size()]));
 233     }
 234 
 235     private static void logErrorStream(Process p) {
 236         final BufferedReader err = new BufferedReader(new InputStreamReader(p.getErrorStream()));
 237         Thread t = new Thread(() -> {
 238             try {
 239                 String line;
 240                 while ((line = err.readLine()) != null) {
 241                     Log.error(line);
 242                 }
 243             } catch (IOException ioe) {
 244                 Log.verbose(ioe);
 245             }
 246         });
 247         t.setDaemon(true);
 248         t.start();
 249     }
 250 
 251     @SuppressWarnings("unchecked")
 252     public static int execute(Object ... args) throws IOException, InterruptedException {
 253         final Process p = startProcess(args);
 254         
 255         final BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
 256         Thread t = new Thread(() -> {
 257             try {
 258                 String line;
 259                 while ((line = in.readLine()) != null) {
 260                     Log.info(line);
 261                 }
 262             } catch (IOException ioe) {
 263                 com.oracle.tools.packager.Log.verbose(ioe);
 264             }
 265         });
 266         t.setDaemon(true);
 267         t.start();
 268 
 269         logErrorStream(p);
 270         return p.waitFor();
 271     }
 272 
 273     public static int getProcessOutput(List<String> result, Object... args) 
 274             throws IOException, InterruptedException {
 275         final Process p = startProcess(args);
 276 
 277         List<String> list = new ArrayList<>();
 278         final BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
 279         Thread t = new Thread(() -> {
 280             try {
 281                 String line;
 282                 while ((line = in.readLine()) != null) {
 283                     list.add(line);
 284                 }
 285             } catch (IOException ioe) {
 286                 com.oracle.tools.packager.Log.verbose(ioe);
 287             }
 288         });
 289         t.setDaemon(true);
 290         t.start();
 291 
 292         logErrorStream(p);
 293 
 294         int ret = p.waitFor();
 295 
 296         result.clear();
 297         result.addAll(list);
 298 
 299         return ret;
 300     }
 301 
 302     //no good test if we are running pre-JRE7
 303     //use heuristic approach
 304     // "false positive" is better than wrong answer
 305     public static boolean isNotSymbolicLink(File file) {
 306         //no symlinks on windows
 307         if (Platform.getPlatform() == Platform.WINDOWS) {
 308             return true;
 309         }
 310         try {
 311             if (file == null || file.getParent() == null) {
 312                 return false;
 313             }
 314             File file_canonical = new File(
 315                     file.getParentFile().getCanonicalFile(), file.getName());
 316             if (file_canonical.getCanonicalFile().equals(
 317                        file_canonical.getAbsoluteFile())) {
 318                 return true;
 319             }