langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/file/BaseFileManager.java
changeset 35807 2eb1d877da0f
parent 29780 8f8e54a1fa20
child 36155 1555ce1fcb4f
equal deleted inserted replaced
35739:db483b34fa71 35807:2eb1d877da0f
     1 /*
     1 /*
     2  * Copyright (c) 2009, 2014, Oracle and/or its affiliates. All rights reserved.
     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.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     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
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     7  * published by the Free Software Foundation.  Oracle designates this
    53 import javax.tools.JavaFileObject;
    53 import javax.tools.JavaFileObject;
    54 import javax.tools.JavaFileObject.Kind;
    54 import javax.tools.JavaFileObject.Kind;
    55 
    55 
    56 import com.sun.tools.javac.code.Lint;
    56 import com.sun.tools.javac.code.Lint;
    57 import com.sun.tools.javac.code.Source;
    57 import com.sun.tools.javac.code.Source;
    58 import com.sun.tools.javac.file.FSInfo;
       
    59 import com.sun.tools.javac.file.Locations;
       
    60 import com.sun.tools.javac.main.Option;
    58 import com.sun.tools.javac.main.Option;
    61 import com.sun.tools.javac.main.OptionHelper;
    59 import com.sun.tools.javac.main.OptionHelper;
    62 import com.sun.tools.javac.main.OptionHelper.GrumpyHelper;
    60 import com.sun.tools.javac.main.OptionHelper.GrumpyHelper;
    63 import com.sun.tools.javac.util.Context;
    61 import com.sun.tools.javac.util.Context;
    64 import com.sun.tools.javac.util.DefinedBy;
    62 import com.sun.tools.javac.util.DefinedBy;
    86     public void setContext(Context context) {
    84     public void setContext(Context context) {
    87         log = Log.instance(context);
    85         log = Log.instance(context);
    88         options = Options.instance(context);
    86         options = Options.instance(context);
    89         classLoaderClass = options.get("procloader");
    87         classLoaderClass = options.get("procloader");
    90         locations.update(log, Lint.instance(context), FSInfo.instance(context));
    88         locations.update(log, Lint.instance(context), FSInfo.instance(context));
       
    89 
       
    90         // Setting this option is an indication that close() should defer actually closing
       
    91         // the file manager until after a specified period of inactivity.
       
    92         // This is to accomodate clients which save references to Symbols created for use
       
    93         // within doclets or annotation processors, and which then attempt to use those
       
    94         // references after the tool exits, having closed any internally managed file manager.
       
    95         // Ideally, such clients should run the tool via the javax.tools API, providing their
       
    96         // own file manager, which can be closed by the client when all use of that file
       
    97         // manager is complete.
       
    98         // If the option has a numeric value, it will be interpreted as the duration,
       
    99         // in seconds, of the period of inactivity to wait for, before the file manager
       
   100         // is actually closed.
       
   101         // See also deferredClose().
       
   102         String s = options.get("fileManager.deferClose");
       
   103         if (s != null) {
       
   104             try {
       
   105                 deferredCloseTimeout = (int) (Float.parseFloat(s) * 1000);
       
   106             } catch (NumberFormatException e) {
       
   107                 deferredCloseTimeout = 60 * 1000;  // default: one minute, in millis
       
   108             }
       
   109         }
    91     }
   110     }
    92 
   111 
    93     protected Locations createLocations() {
   112     protected Locations createLocations() {
    94         return new Locations();
   113         return new Locations();
    95     }
   114     }
   113     /**
   132     /**
   114      * A flag for clients to use to indicate that this file manager should
   133      * A flag for clients to use to indicate that this file manager should
   115      * be closed when it is no longer required.
   134      * be closed when it is no longer required.
   116      */
   135      */
   117     public boolean autoClose;
   136     public boolean autoClose;
       
   137 
       
   138     /**
       
   139      * Wait for a period of inactivity before calling close().
       
   140      * The length of the period of inactivity is given by {@code deferredCloseTimeout}
       
   141      */
       
   142     protected void deferredClose() {
       
   143         Thread t = new Thread(getClass().getName() + " DeferredClose") {
       
   144             @Override
       
   145             public void run() {
       
   146                 try {
       
   147                     synchronized (BaseFileManager.this) {
       
   148                         long now = System.currentTimeMillis();
       
   149                         while (now < lastUsedTime + deferredCloseTimeout) {
       
   150                             BaseFileManager.this.wait(lastUsedTime + deferredCloseTimeout - now);
       
   151                             now = System.currentTimeMillis();
       
   152                         }
       
   153                         deferredCloseTimeout = 0;
       
   154                         close();
       
   155                     }
       
   156                 } catch (InterruptedException e) {
       
   157                 } catch (IOException e) {
       
   158                 }
       
   159             }
       
   160         };
       
   161         t.setDaemon(true);
       
   162         t.start();
       
   163     }
       
   164 
       
   165     synchronized void updateLastUsedTime() {
       
   166         if (deferredCloseTimeout > 0) { // avoid updating the time unnecessarily
       
   167             lastUsedTime = System.currentTimeMillis();
       
   168         }
       
   169     }
       
   170 
       
   171     private long lastUsedTime = System.currentTimeMillis();
       
   172     protected long deferredCloseTimeout = 0;
   118 
   173 
   119     protected Source getSource() {
   174     protected Source getSource() {
   120         String sourceName = options.get(Option.SOURCE);
   175         String sourceName = options.get(Option.SOURCE);
   121         Source source = null;
   176         Source source = null;
   122         if (sourceName != null)
   177         if (sourceName != null)