langtools/src/share/classes/com/sun/tools/javac/file/CacheFSInfo.java
author jjg
Mon, 24 Jan 2011 16:38:56 -0800
changeset 8223 638daa596494
parent 5847 1908176fd6e3
child 8422 b2ac1acf3937
permissions -rw-r--r--
6988106: javac report 'java.lang.IllegalMonitorStateException' Reviewed-by: ksrini
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1208
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
     1
/*
8223
638daa596494 6988106: javac report 'java.lang.IllegalMonitorStateException'
jjg
parents: 5847
diff changeset
     2
 * Copyright (c) 2005, 2011, Oracle and/or its affiliates. All rights reserved.
1208
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
     4
 *
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 3380
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
1208
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 3380
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
1208
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    10
 *
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    15
 * accompanied this code).
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    16
 *
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    20
 *
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 3380
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 3380
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 3380
diff changeset
    23
 * questions.
1208
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    24
 */
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    25
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    26
package com.sun.tools.javac.file;
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    27
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    28
import java.io.File;
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    29
import java.io.IOException;
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    30
import java.util.List;
8223
638daa596494 6988106: javac report 'java.lang.IllegalMonitorStateException'
jjg
parents: 5847
diff changeset
    31
import java.util.Map;
638daa596494 6988106: javac report 'java.lang.IllegalMonitorStateException'
jjg
parents: 5847
diff changeset
    32
import java.util.concurrent.ConcurrentHashMap;
1208
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    33
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    34
import com.sun.tools.javac.util.Context;
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    35
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    36
/**
3380
a6c2bcab0fec 6865399: some javac files are missing Sun internal API comment
jjg
parents: 1208
diff changeset
    37
 * Caching implementation of FSInfo.
a6c2bcab0fec 6865399: some javac files are missing Sun internal API comment
jjg
parents: 1208
diff changeset
    38
 *
5847
1908176fd6e3 6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents: 5520
diff changeset
    39
 * <p><b>This is NOT part of any supported API.
3380
a6c2bcab0fec 6865399: some javac files are missing Sun internal API comment
jjg
parents: 1208
diff changeset
    40
 * If you write code that depends on this, you do so at your own risk.
a6c2bcab0fec 6865399: some javac files are missing Sun internal API comment
jjg
parents: 1208
diff changeset
    41
 * This code and its internal interfaces are subject to change or
a6c2bcab0fec 6865399: some javac files are missing Sun internal API comment
jjg
parents: 1208
diff changeset
    42
 * deletion without notice.</b>
1208
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    43
 */
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    44
public class CacheFSInfo extends FSInfo {
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    45
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    46
    /**
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    47
     * Register a Context.Factory to create a singleton CacheFSInfo.
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    48
     */
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    49
    public static void preRegister(final Context context) {
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    50
        context.put(FSInfo.class, new Context.Factory<FSInfo>() {
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    51
            public FSInfo make() {
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    52
                if (singleton == null)
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    53
                    singleton = new CacheFSInfo();
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    54
                context.put(FSInfo.class, singleton);
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    55
                return singleton;
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    56
            }
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    57
        });
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    58
    }
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    59
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    60
    static CacheFSInfo singleton;
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    61
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    62
    public void clearCache() {
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    63
        cache.clear();
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    64
    }
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    65
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    66
    @Override
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    67
    public File getCanonicalFile(File file) {
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    68
        Entry e = getEntry(file);
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    69
        return e.canonicalFile;
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    70
    }
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    71
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    72
    @Override
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    73
    public boolean exists(File file) {
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    74
        Entry e = getEntry(file);
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    75
        return e.exists;
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    76
    }
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    77
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    78
    @Override
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    79
    public boolean isDirectory(File file) {
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    80
        Entry e = getEntry(file);
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    81
        return e.isDirectory;
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    82
    }
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    83
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    84
    @Override
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    85
    public boolean isFile(File file) {
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    86
        Entry e = getEntry(file);
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    87
        return e.isFile;
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    88
    }
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    89
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    90
    @Override
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    91
    public List<File> getJarClassPath(File file) throws IOException {
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    92
        // don't bother to lock the cache, because it is thread-safe, and
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    93
        // because the worst that can happen would be to create two identical
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    94
        // jar class paths together and have one overwrite the other.
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    95
        Entry e = getEntry(file);
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    96
        if (e.jarClassPath == null)
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    97
            e.jarClassPath = super.getJarClassPath(file);
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    98
        return e.jarClassPath;
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    99
    }
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
   100
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
   101
    private Entry getEntry(File file) {
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
   102
        // don't bother to lock the cache, because it is thread-safe, and
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
   103
        // because the worst that can happen would be to create two identical
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
   104
        // entries together and have one overwrite the other.
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
   105
        Entry e = cache.get(file);
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
   106
        if (e == null) {
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
   107
            e = new Entry();
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
   108
            e.canonicalFile = super.getCanonicalFile(file);
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
   109
            e.exists = super.exists(file);
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
   110
            e.isDirectory = super.isDirectory(file);
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
   111
            e.isFile = super.isFile(file);
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
   112
            cache.put(file, e);
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
   113
        }
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
   114
        return e;
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
   115
    }
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
   116
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
   117
    // could also be a Map<File,SoftReference<Entry>> ?
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
   118
    private Map<File,Entry> cache = new ConcurrentHashMap<File,Entry>();
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
   119
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
   120
    private static class Entry {
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
   121
        File canonicalFile;
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
   122
        boolean exists;
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
   123
        boolean isFile;
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
   124
        boolean isDirectory;
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
   125
        List<File> jarClassPath;
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
   126
    }
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
   127
}