1 /*
   2  * Copyright (c) 2013, 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.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 package sun.hotspot.tools.ctw;
  25 
  26 import java.net.MalformedURLException;
  27 import java.net.URL;
  28 import java.net.URLClassLoader;
  29 import java.util.Set;
  30 import java.util.EnumSet;
  31 import java.util.HashSet;
  32 import java.util.concurrent.Executor;
  33 
  34 import java.io.*;
  35 import java.nio.file.*;
  36 import java.nio.file.attribute.*;
  37 
  38 /**
  39  * * Handler for dirs containing classes to compile.
  40  * @author igor.ignatyev@oracle.com
  41  */
  42 public class ClassPathDirEntry extends PathHandler {
  43 
  44     private final int rootLength = root.toString().length();
  45 
  46     public ClassPathDirEntry(Path root, Executor executor) {
  47         super(root, executor);
  48         try {
  49             URL url = root.toUri().toURL();
  50             setLoader(new URLClassLoader(new URL[]{url}));
  51         } catch (MalformedURLException e) {
  52             e.printStackTrace();
  53         }
  54     }
  55 
  56     @Override
  57     public void process() {
  58         System.out.println("# dir: " + root);
  59         if (!Files.exists(root)) {
  60             return;
  61         }
  62         try {
  63             Files.walkFileTree(root, EnumSet.of(FileVisitOption.FOLLOW_LINKS),
  64                     Integer.MAX_VALUE, new CompileFileVisitor());
  65         } catch (IOException ioe) {
  66             ioe.printStackTrace();
  67         }
  68     }
  69 
  70     private void compileFile(Path file) {
  71         if (Utils.isClassFile(file.toString())) {
  72             compileWholeClass(pathToClassName(file));
  73         }
  74     }
  75 
  76     private String pathToClassName(Path file) {
  77         String fileString;
  78         if (root == file) {
  79             fileString = file.normalize().toString();
  80         } else {
  81             fileString = file.normalize().toString().substring(rootLength + 1);
  82         }
  83         return Utils.fileNameToClassName(fileString);
  84     }
  85 
  86     private class CompileFileVisitor extends SimpleFileVisitor<Path> {
  87 
  88         private final Set<Path> ready = new HashSet<>();
  89 
  90         @Override
  91         public FileVisitResult preVisitDirectory(Path dir,
  92                 BasicFileAttributes attrs) throws IOException {
  93             if (ready.contains(dir)) {
  94                 return FileVisitResult.SKIP_SUBTREE;
  95             }
  96             ready.add(dir);
  97             return super.preVisitDirectory(dir, attrs);
  98         }
  99 
 100         @Override
 101         public FileVisitResult visitFile(Path file,
 102                 BasicFileAttributes attrs) throws IOException {
 103             if (!ready.contains(file)) {
 104                 compileFile(file);
 105             }
 106             return isFinished() ? FileVisitResult.TERMINATE
 107                     : FileVisitResult.CONTINUE;
 108         }
 109 
 110         @Override
 111         public FileVisitResult visitFileFailed(Path file,
 112                 IOException exc) throws IOException {
 113             return FileVisitResult.CONTINUE;
 114         }
 115     }
 116 }
 117