1 /*
   2  * Copyright (c) 2009, 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
  23  * questions.
  24  */
  25 
  26 package com.sun.tools.javac.file;
  27 
  28 import java.io.IOException;
  29 import java.io.InputStream;
  30 import java.lang.ref.SoftReference;
  31 import java.lang.reflect.Constructor;
  32 import java.lang.reflect.Method;
  33 import java.net.URL;
  34 import java.net.URLClassLoader;
  35 import java.nio.ByteBuffer;
  36 import java.nio.CharBuffer;
  37 import java.nio.charset.Charset;
  38 import java.nio.charset.CharsetDecoder;
  39 import java.nio.charset.CoderResult;
  40 import java.nio.charset.CodingErrorAction;
  41 import java.nio.charset.IllegalCharsetNameException;
  42 import java.nio.charset.UnsupportedCharsetException;
  43 import java.nio.file.Path;
  44 import java.util.Collection;
  45 import java.util.HashMap;
  46 import java.util.Iterator;
  47 import java.util.Map;
  48 import java.util.Objects;
  49 import java.util.Set;
  50 
  51 import javax.tools.JavaFileManager;
  52 import javax.tools.JavaFileObject;
  53 import javax.tools.JavaFileObject.Kind;
  54 
  55 import com.sun.tools.javac.main.Option;
  56 import com.sun.tools.javac.main.OptionHelper;
  57 import com.sun.tools.javac.main.OptionHelper.GrumpyHelper;
  58 import com.sun.tools.javac.resources.CompilerProperties.Errors;
  59 import com.sun.tools.javac.util.Abort;
  60 import com.sun.tools.javac.util.Context;
  61 import com.sun.tools.javac.util.DefinedBy;
  62 import com.sun.tools.javac.util.DefinedBy.Api;
  63 import com.sun.tools.javac.util.Log;
  64 import com.sun.tools.javac.util.Options;
  65 
  66 /**
  67  * Utility methods for building a filemanager.
  68  * There are no references here to file-system specific objects such as
  69  * java.io.File or java.nio.file.Path.
  70  */
  71 public abstract class BaseFileManager implements JavaFileManager {
  72     protected BaseFileManager(Charset charset) {
  73         this.charset = charset;
  74         byteBufferCache = new ByteBufferCache();
  75         locations = createLocations();
  76     }
  77 
  78     /**
  79      * Set the context for JavacPathFileManager.
  80      * @param context the context containing items to be associated with the file manager
  81      */
  82     public void setContext(Context context) {
  83         log = Log.instance(context);
  84         options = Options.instance(context);
  85         classLoaderClass = options.get("procloader");
  86 
  87         // Avoid initializing Lint
  88         boolean warn = options.isLintSet("path");
  89         locations.update(log, warn, FSInfo.instance(context));
  90 
  91         // Setting this option is an indication that close() should defer actually closing
  92         // the file manager until after a specified period of inactivity.
  93         // This is to accomodate clients which save references to Symbols created for use
  94         // within doclets or annotation processors, and which then attempt to use those
  95         // references after the tool exits, having closed any internally managed file manager.
  96         // Ideally, such clients should run the tool via the javax.tools API, providing their
  97         // own file manager, which can be closed by the client when all use of that file
  98         // manager is complete.
  99         // If the option has a numeric value, it will be interpreted as the duration,
 100         // in seconds, of the period of inactivity to wait for, before the file manager
 101         // is actually closed.
 102         // See also deferredClose().
 103         String s = options.get("fileManager.deferClose");
 104         if (s != null) {
 105             try {
 106                 deferredCloseTimeout = (int) (Float.parseFloat(s) * 1000);
 107             } catch (NumberFormatException e) {
 108                 deferredCloseTimeout = 60 * 1000;  // default: one minute, in millis
 109             }
 110         }
 111     }
 112 
 113     protected Locations createLocations() {
 114         return new Locations();
 115     }
 116 
 117     /**
 118      * The log to be used for error reporting.
 119      */
 120     public Log log;
 121 
 122     /**
 123      * User provided charset (through javax.tools).
 124      */
 125     protected Charset charset;
 126 
 127     protected Options options;
 128 
 129     protected String classLoaderClass;
 130 
 131     protected Locations locations;
 132 
 133     /**
 134      * A flag for clients to use to indicate that this file manager should
 135      * be closed when it is no longer required.
 136      */
 137     public boolean autoClose;
 138 
 139     /**
 140      * Wait for a period of inactivity before calling close().
 141      * The length of the period of inactivity is given by {@code deferredCloseTimeout}
 142      */
 143     protected void deferredClose() {
 144         Thread t = new Thread(getClass().getName() + " DeferredClose") {
 145             @Override
 146             public void run() {
 147                 try {
 148                     synchronized (BaseFileManager.this) {
 149                         long now = System.currentTimeMillis();
 150                         while (now < lastUsedTime + deferredCloseTimeout) {
 151                             BaseFileManager.this.wait(lastUsedTime + deferredCloseTimeout - now);
 152                             now = System.currentTimeMillis();
 153                         }
 154                         deferredCloseTimeout = 0;
 155                         close();
 156                     }
 157                 } catch (InterruptedException e) {
 158                 } catch (IOException e) {
 159                 }
 160             }
 161         };
 162         t.setDaemon(true);
 163         t.start();
 164     }
 165 
 166     synchronized void updateLastUsedTime() {
 167         if (deferredCloseTimeout > 0) { // avoid updating the time unnecessarily
 168             lastUsedTime = System.currentTimeMillis();
 169         }
 170     }
 171 
 172     private long lastUsedTime = System.currentTimeMillis();
 173     protected long deferredCloseTimeout = 0;
 174 
 175     protected ClassLoader getClassLoader(URL[] urls) {
 176         ClassLoader thisClassLoader = getClass().getClassLoader();
 177 
 178         // Allow the following to specify a closeable classloader
 179         // other than URLClassLoader.
 180 
 181         // 1: Allow client to specify the class to use via hidden option
 182         if (classLoaderClass != null) {
 183             try {
 184                 Class<? extends ClassLoader> loader =
 185                         Class.forName(classLoaderClass).asSubclass(ClassLoader.class);
 186                 Class<?>[] constrArgTypes = { URL[].class, ClassLoader.class };
 187                 Constructor<? extends ClassLoader> constr = loader.getConstructor(constrArgTypes);
 188                 return ensureReadable(constr.newInstance(urls, thisClassLoader));
 189             } catch (ReflectiveOperationException t) {
 190                 // ignore errors loading user-provided class loader, fall through
 191             }
 192         }
 193         return ensureReadable(new URLClassLoader(urls, thisClassLoader));
 194     }
 195 
 196     /**
 197      * Ensures that the unnamed module of the given classloader is readable to this
 198      * module.
 199      */
 200     private ClassLoader ensureReadable(ClassLoader targetLoader) {
 201         try {
 202             Method getModuleMethod = Class.class.getMethod("getModule");
 203             Object thisModule = getModuleMethod.invoke(this.getClass());
 204             Method getUnnamedModuleMethod = ClassLoader.class.getMethod("getUnnamedModule");
 205             Object targetModule = getUnnamedModuleMethod.invoke(targetLoader);
 206 
 207             Class<?> moduleClass = getModuleMethod.getReturnType();
 208             Method addReadsMethod = moduleClass.getMethod("addReads", moduleClass);
 209             addReadsMethod.invoke(thisModule, targetModule);
 210         } catch (NoSuchMethodException e) {
 211             // ignore
 212         } catch (Exception e) {
 213             throw new Abort(e);
 214         }
 215         return targetLoader;
 216     }
 217 
 218     public boolean isDefaultBootClassPath() {
 219         return locations.isDefaultBootClassPath();
 220     }
 221 
 222     // <editor-fold defaultstate="collapsed" desc="Option handling">
 223     @Override @DefinedBy(Api.COMPILER)
 224     public boolean handleOption(String current, Iterator<String> remaining) {
 225         OptionHelper helper = new GrumpyHelper(log) {
 226             @Override
 227             public String get(Option option) {
 228                 return options.get(option);
 229             }
 230 
 231             @Override
 232             public void put(String name, String value) {
 233                 options.put(name, value);
 234             }
 235 
 236             @Override
 237             public void remove(String name) {
 238                 options.remove(name);
 239             }
 240 
 241             @Override
 242             public boolean handleFileManagerOption(Option option, String value) {
 243                 return handleOption(option, value);
 244             }
 245         };
 246 
 247         Option o = Option.lookup(current, javacFileManagerOptions);
 248         if (o == null) {
 249             return false;
 250         }
 251 
 252         if (!o.handleOption(helper, current, remaining))
 253             throw new IllegalArgumentException(current);
 254 
 255         return true;
 256     }
 257     // where
 258         private static final Set<Option> javacFileManagerOptions =
 259             Option.getJavacFileManagerOptions();
 260 
 261     @Override @DefinedBy(Api.COMPILER)
 262     public int isSupportedOption(String option) {
 263         Option o = Option.lookup(option, javacFileManagerOptions);
 264         return (o == null) ? -1 : o.hasArg() ? 1 : 0;
 265     }
 266 
 267     protected String multiReleaseValue;
 268 
 269     /**
 270      * Common back end for OptionHelper handleFileManagerOption.
 271      * @param option the option whose value to be set
 272      * @param value the value for the option
 273      * @return true if successful, and false otherwise
 274      */
 275     public boolean handleOption(Option option, String value) {
 276         switch (option) {
 277             case ENCODING:
 278                 encodingName = value;
 279                 return true;
 280 
 281             case MULTIRELEASE:
 282                 multiReleaseValue = value;
 283                 locations.setMultiReleaseValue(value);
 284                 return true;
 285 
 286             default:
 287                 return locations.handleOption(option, value);
 288         }
 289     }
 290 
 291     /**
 292      * Call handleOption for collection of options and corresponding values.
 293      * @param map a collection of options and corresponding values
 294      * @return true if all the calls are successful
 295      */
 296     public boolean handleOptions(Map<Option, String> map) {
 297         boolean ok = true;
 298         for (Map.Entry<Option, String> e: map.entrySet()) {
 299             try {
 300                 ok = ok & handleOption(e.getKey(), e.getValue());
 301             } catch (IllegalArgumentException ex) {
 302                 log.error(Errors.IllegalArgumentForOption(e.getKey().getPrimaryName(), ex.getMessage()));
 303                 ok = false;
 304             }
 305         }
 306         return ok;
 307     }
 308 
 309     // </editor-fold>
 310 
 311     // <editor-fold defaultstate="collapsed" desc="Encoding">
 312     private String encodingName;
 313     private String defaultEncodingName;
 314     private String getDefaultEncodingName() {
 315         if (defaultEncodingName == null) {
 316             defaultEncodingName = Charset.defaultCharset().name();
 317         }
 318         return defaultEncodingName;
 319     }
 320 
 321     public String getEncodingName() {
 322         return (encodingName != null) ? encodingName : getDefaultEncodingName();
 323     }
 324 
 325     @SuppressWarnings("cast")
 326     public CharBuffer decode(ByteBuffer inbuf, boolean ignoreEncodingErrors) {
 327         String encodingName = getEncodingName();
 328         CharsetDecoder decoder;
 329         try {
 330             decoder = getDecoder(encodingName, ignoreEncodingErrors);
 331         } catch (IllegalCharsetNameException | UnsupportedCharsetException e) {
 332             log.error("unsupported.encoding", encodingName);
 333             return (CharBuffer)CharBuffer.allocate(1).flip();
 334         }
 335 
 336         // slightly overestimate the buffer size to avoid reallocation.
 337         float factor =
 338             decoder.averageCharsPerByte() * 0.8f +
 339             decoder.maxCharsPerByte() * 0.2f;
 340         CharBuffer dest = CharBuffer.
 341             allocate(10 + (int)(inbuf.remaining()*factor));
 342 
 343         while (true) {
 344             CoderResult result = decoder.decode(inbuf, dest, true);
 345             dest.flip();
 346 
 347             if (result.isUnderflow()) { // done reading
 348                 // make sure there is at least one extra character
 349                 if (dest.limit() == dest.capacity()) {
 350                     dest = CharBuffer.allocate(dest.capacity()+1).put(dest);
 351                     dest.flip();
 352                 }
 353                 return dest;
 354             } else if (result.isOverflow()) { // buffer too small; expand
 355                 int newCapacity =
 356                     10 + dest.capacity() +
 357                     (int)(inbuf.remaining()*decoder.maxCharsPerByte());
 358                 dest = CharBuffer.allocate(newCapacity).put(dest);
 359             } else if (result.isMalformed() || result.isUnmappable()) {
 360                 // bad character in input
 361                 StringBuilder unmappable = new StringBuilder();
 362                 int len = result.length();
 363 
 364                 for (int i = 0; i < len; i++) {
 365                     unmappable.append(String.format("%02X", inbuf.get()));
 366                 }
 367 
 368                 String charsetName = charset == null ? encodingName : charset.name();
 369 
 370                 log.error(dest.limit(),
 371                           Errors.IllegalCharForEncoding(unmappable.toString(), charsetName));
 372 
 373                 // undo the flip() to prepare the output buffer
 374                 // for more translation
 375                 dest.position(dest.limit());
 376                 dest.limit(dest.capacity());
 377                 dest.put((char)0xfffd); // backward compatible
 378             } else {
 379                 throw new AssertionError(result);
 380             }
 381         }
 382         // unreached
 383     }
 384 
 385     public CharsetDecoder getDecoder(String encodingName, boolean ignoreEncodingErrors) {
 386         Charset cs = (this.charset == null)
 387             ? Charset.forName(encodingName)
 388             : this.charset;
 389         CharsetDecoder decoder = cs.newDecoder();
 390 
 391         CodingErrorAction action;
 392         if (ignoreEncodingErrors)
 393             action = CodingErrorAction.REPLACE;
 394         else
 395             action = CodingErrorAction.REPORT;
 396 
 397         return decoder
 398             .onMalformedInput(action)
 399             .onUnmappableCharacter(action);
 400     }
 401     // </editor-fold>
 402 
 403     // <editor-fold defaultstate="collapsed" desc="ByteBuffers">
 404     /**
 405      * Make a byte buffer from an input stream.
 406      * @param in the stream
 407      * @return a byte buffer containing the contents of the stream
 408      * @throws IOException if an error occurred while reading the stream
 409      */
 410     @SuppressWarnings("cast")
 411     public ByteBuffer makeByteBuffer(InputStream in)
 412         throws IOException {
 413         int limit = in.available();
 414         if (limit < 1024) limit = 1024;
 415         ByteBuffer result = byteBufferCache.get(limit);
 416         int position = 0;
 417         while (in.available() != 0) {
 418             if (position >= limit)
 419                 // expand buffer
 420                 result = ByteBuffer.
 421                     allocate(limit <<= 1).
 422                     put((ByteBuffer)result.flip());
 423             int count = in.read(result.array(),
 424                 position,
 425                 limit - position);
 426             if (count < 0) break;
 427             result.position(position += count);
 428         }
 429         return (ByteBuffer)result.flip();
 430     }
 431 
 432     public void recycleByteBuffer(ByteBuffer bb) {
 433         byteBufferCache.put(bb);
 434     }
 435 
 436     /**
 437      * A single-element cache of direct byte buffers.
 438      */
 439     @SuppressWarnings("cast")
 440     private static class ByteBufferCache {
 441         private ByteBuffer cached;
 442         ByteBuffer get(int capacity) {
 443             if (capacity < 20480) capacity = 20480;
 444             ByteBuffer result =
 445                 (cached != null && cached.capacity() >= capacity)
 446                 ? (ByteBuffer)cached.clear()
 447                 : ByteBuffer.allocate(capacity + capacity>>1);
 448             cached = null;
 449             return result;
 450         }
 451         void put(ByteBuffer x) {
 452             cached = x;
 453         }
 454     }
 455 
 456     private final ByteBufferCache byteBufferCache;
 457     // </editor-fold>
 458 
 459     // <editor-fold defaultstate="collapsed" desc="Content cache">
 460     public CharBuffer getCachedContent(JavaFileObject file) {
 461         ContentCacheEntry e = contentCache.get(file);
 462         if (e == null)
 463             return null;
 464 
 465         if (!e.isValid(file)) {
 466             contentCache.remove(file);
 467             return null;
 468         }
 469 
 470         return e.getValue();
 471     }
 472 
 473     public void cache(JavaFileObject file, CharBuffer cb) {
 474         contentCache.put(file, new ContentCacheEntry(file, cb));
 475     }
 476 
 477     public void flushCache(JavaFileObject file) {
 478         contentCache.remove(file);
 479     }
 480 
 481     protected final Map<JavaFileObject, ContentCacheEntry> contentCache = new HashMap<>();
 482 
 483     protected static class ContentCacheEntry {
 484         final long timestamp;
 485         final SoftReference<CharBuffer> ref;
 486 
 487         ContentCacheEntry(JavaFileObject file, CharBuffer cb) {
 488             this.timestamp = file.getLastModified();
 489             this.ref = new SoftReference<>(cb);
 490         }
 491 
 492         boolean isValid(JavaFileObject file) {
 493             return timestamp == file.getLastModified();
 494         }
 495 
 496         CharBuffer getValue() {
 497             return ref.get();
 498         }
 499     }
 500     // </editor-fold>
 501 
 502     public static Kind getKind(Path path) {
 503         return getKind(path.getFileName().toString());
 504     }
 505 
 506     public static Kind getKind(String name) {
 507         if (name.endsWith(Kind.CLASS.extension))
 508             return Kind.CLASS;
 509         else if (name.endsWith(Kind.SOURCE.extension))
 510             return Kind.SOURCE;
 511         else if (name.endsWith(Kind.HTML.extension))
 512             return Kind.HTML;
 513         else
 514             return Kind.OTHER;
 515     }
 516 
 517     protected static <T> T nullCheck(T o) {
 518         return Objects.requireNonNull(o);
 519     }
 520 
 521     protected static <T> Collection<T> nullCheck(Collection<T> it) {
 522         for (T t : it)
 523             Objects.requireNonNull(t);
 524         return it;
 525     }
 526 }