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.nio.file.Files;
  27 import java.nio.file.Path;
  28 import java.nio.file.Paths;
  29 import java.io.File;
  30 
  31 import java.util.Objects;
  32 import java.util.regex.Pattern;
  33 import java.util.regex.Matcher;
  34 import java.util.concurrent.Executor;
  35 
  36 /**
  37  * Abstract handler for path.
  38  * <p/>
  39  * Concrete subclasses should implement method {@link #process()}.
  40  *
  41  * @author igor.ignatyev@oracle.com
  42  */
  43 public abstract class PathHandler {
  44     private static final Pattern JAR_IN_DIR_PATTERN
  45             = Pattern.compile("^(.*[/\\\\])?\\*$");
  46     protected final Path root;
  47     protected final Executor executor;
  48     private ClassLoader loader;
  49 
  50     /**
  51      * @param root     root path to process
  52      * @param executor executor used for process task invocation
  53      * @throws NullPointerException if {@code root} or {@code executor} is
  54      *                              {@code null}
  55      */
  56     protected PathHandler(Path root, Executor executor) {
  57         Objects.requireNonNull(root);
  58         Objects.requireNonNull(executor);
  59         this.root = root.normalize();
  60         this.executor = executor;
  61         this.loader = ClassLoader.getSystemClassLoader();
  62     }
  63 
  64    /**
  65      * Factory method. Construct concrete handler in depends from {@code path}.
  66      *
  67      * @param path     the path to process
  68      * @param executor executor used for compile task invocation
  69      * @throws NullPointerException if {@code path} or {@code executor} is
  70      *                              {@code null}
  71      */
  72     public static PathHandler create(String path, Executor executor) {
  73         Objects.requireNonNull(path);
  74         Objects.requireNonNull(executor);
  75         Matcher matcher = JAR_IN_DIR_PATTERN.matcher(path);
  76         if (matcher.matches()) {
  77             path = matcher.group(1);
  78             path = path.isEmpty() ? "." : path;
  79             return new ClassPathJarInDirEntry(Paths.get(path), executor);
  80         } else {
  81             path = path.isEmpty() ? "." : path;
  82             Path p = Paths.get(path);
  83             if (isJarFile(p)) {
  84                 return new ClassPathJarEntry(p, executor);
  85             } else if (isListFile(p)) {
  86                 return new ClassesListInFile(p, executor);
  87             } else {
  88                 return new ClassPathDirEntry(p, executor);
  89             }
  90         }
  91     }
  92 
  93     private static boolean isJarFile(Path path) {
  94         if (Files.isRegularFile(path)) {
  95             String name = path.toString();
  96             return Utils.endsWithIgnoreCase(name, ".zip")
  97                     || Utils.endsWithIgnoreCase(name, ".jar");
  98         }
  99         return false;
 100     }
 101 
 102     private static boolean isListFile(Path path) {
 103         if (Files.isRegularFile(path)) {
 104             String name = path.toString();
 105             return Utils.endsWithIgnoreCase(name, ".lst");
 106         }
 107         return false;
 108     }
 109 
 110     /**
 111      * Processes all classes in specified path.
 112      */
 113     public abstract void process();
 114 
 115    /**
 116      * Sets class loader, that will be used to define class at
 117      * {@link #processClass(String)}.
 118      *
 119      * @param loader class loader
 120      * @throws NullPointerException if {@code loader} is {@code null}
 121      */
 122     protected final void setLoader(ClassLoader loader) {
 123         Objects.requireNonNull(loader);
 124         this.loader = loader;
 125     }
 126 
 127     /**
 128      * Processes specificed class.
 129      * @param name fully qualified name of class to process
 130      */
 131     protected final void processClass(String name) {
 132         try {
 133             Class aClass = Class.forName(name, true, loader);
 134             Compiler.compileClass(aClass, executor);
 135         } catch (ClassNotFoundException | LinkageError e) {
 136             System.out.printf("Class %s loading failed : %s%n", name,
 137                 e.getMessage());
 138         }
 139     }
 140 
 141     /**
 142      * @return {@code true} if processing should be stopped
 143      */
 144     public static boolean isFinished() {
 145         return Compiler.isLimitReached();
 146     }
 147 
 148 }
 149