src/share/classes/com/sun/tools/javac/file/CacheFSInfo.java

Tue, 25 Sep 2012 13:11:05 -0700

author
jjg
date
Tue, 25 Sep 2012 13:11:05 -0700
changeset 1340
99d23c0ef8ee
parent 893
8f0dcb9499db
child 2525
2eb010b6cb22
permissions
-rw-r--r--

7196464: upgrade JavaCompiler.shouldStopPolicy to accomodate policies in face of error and no error
Reviewed-by: mcimadamore

     1 /*
     2  * Copyright (c) 2005, 2011, 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  */
    26 package com.sun.tools.javac.file;
    28 import java.io.File;
    29 import java.io.IOException;
    30 import java.util.List;
    31 import java.util.Map;
    32 import java.util.concurrent.ConcurrentHashMap;
    34 import com.sun.tools.javac.util.Context;
    36 /**
    37  * Caching implementation of FSInfo.
    38  *
    39  * <p><b>This is NOT part of any supported API.
    40  * If you write code that depends on this, you do so at your own risk.
    41  * This code and its internal interfaces are subject to change or
    42  * deletion without notice.</b>
    43  */
    44 public class CacheFSInfo extends FSInfo {
    46     /**
    47      * Register a Context.Factory to create a CacheFSInfo.
    48      */
    49     public static void preRegister(Context context) {
    50         context.put(FSInfo.class, new Context.Factory<FSInfo>() {
    51             public FSInfo make(Context c) {
    52                 FSInfo instance = new CacheFSInfo();
    53                 c.put(FSInfo.class, instance);
    54                 return instance;
    55             }
    56         });
    57     }
    59     public void clearCache() {
    60         cache.clear();
    61     }
    63     @Override
    64     public File getCanonicalFile(File file) {
    65         Entry e = getEntry(file);
    66         return e.canonicalFile;
    67     }
    69     @Override
    70     public boolean exists(File file) {
    71         Entry e = getEntry(file);
    72         return e.exists;
    73     }
    75     @Override
    76     public boolean isDirectory(File file) {
    77         Entry e = getEntry(file);
    78         return e.isDirectory;
    79     }
    81     @Override
    82     public boolean isFile(File file) {
    83         Entry e = getEntry(file);
    84         return e.isFile;
    85     }
    87     @Override
    88     public List<File> getJarClassPath(File file) throws IOException {
    89         // don't bother to lock the cache, because it is thread-safe, and
    90         // because the worst that can happen would be to create two identical
    91         // jar class paths together and have one overwrite the other.
    92         Entry e = getEntry(file);
    93         if (e.jarClassPath == null)
    94             e.jarClassPath = super.getJarClassPath(file);
    95         return e.jarClassPath;
    96     }
    98     private Entry getEntry(File file) {
    99         // don't bother to lock the cache, because it is thread-safe, and
   100         // because the worst that can happen would be to create two identical
   101         // entries together and have one overwrite the other.
   102         Entry e = cache.get(file);
   103         if (e == null) {
   104             e = new Entry();
   105             e.canonicalFile = super.getCanonicalFile(file);
   106             e.exists = super.exists(file);
   107             e.isDirectory = super.isDirectory(file);
   108             e.isFile = super.isFile(file);
   109             cache.put(file, e);
   110         }
   111         return e;
   112     }
   114     // could also be a Map<File,SoftReference<Entry>> ?
   115     private Map<File,Entry> cache = new ConcurrentHashMap<File,Entry>();
   117     private static class Entry {
   118         File canonicalFile;
   119         boolean exists;
   120         boolean isFile;
   121         boolean isDirectory;
   122         List<File> jarClassPath;
   123     }
   124 }

mercurial