langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/file/JRTIndex.java
author jjg
Thu, 04 Dec 2014 14:57:40 -0800
changeset 27859 f5bd37460a09
parent 27579 d1a63c99cdd5
child 29780 8f8e54a1fa20
permissions -rw-r--r--
8066731: javac does not work on exploded image Reviewed-by: jjg, mchung Contributed-by: alan.bateman@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
27579
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
     1
/*
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
     2
 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
     4
 *
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    10
 *
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    15
 * accompanied this code).
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    16
 *
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    20
 *
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    23
 * questions.
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    24
 */
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    25
package com.sun.tools.javac.file;
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    26
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    27
import java.io.IOException;
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    28
import java.io.UncheckedIOException;
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    29
import java.lang.ref.SoftReference;
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    30
import java.net.URI;
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    31
import java.nio.file.DirectoryStream;
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    32
import java.nio.file.FileSystem;
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    33
import java.nio.file.FileSystems;
27859
f5bd37460a09 8066731: javac does not work on exploded image
jjg
parents: 27579
diff changeset
    34
import java.nio.file.FileSystemNotFoundException;
27579
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    35
import java.nio.file.Files;
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    36
import java.nio.file.Path;
27859
f5bd37460a09 8066731: javac does not work on exploded image
jjg
parents: 27579
diff changeset
    37
import java.nio.file.ProviderNotFoundException;
27579
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    38
import java.nio.file.spi.FileSystemProvider;
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    39
import java.util.Collections;
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    40
import java.util.HashMap;
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    41
import java.util.LinkedHashMap;
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    42
import java.util.LinkedHashSet;
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    43
import java.util.Map;
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    44
import java.util.MissingResourceException;
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    45
import java.util.ResourceBundle;
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    46
import java.util.Set;
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    47
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    48
import javax.tools.FileObject;
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    49
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    50
import com.sun.tools.javac.file.RelativePath.RelativeDirectory;
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    51
import com.sun.tools.javac.nio.PathFileObject;
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    52
import com.sun.tools.javac.util.Context;
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    53
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    54
/**
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    55
 * A package-oriented index into the jrt: filesystem.
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    56
 */
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    57
public class JRTIndex {
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    58
    /** Get a shared instance of the cache. */
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    59
    private static JRTIndex sharedInstance;
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    60
    public synchronized static JRTIndex getSharedInstance() {
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    61
        if (sharedInstance == null) {
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    62
            try {
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    63
                sharedInstance = new JRTIndex();
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    64
            } catch (IOException e) {
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    65
                throw new UncheckedIOException(e);
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    66
            }
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    67
        }
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    68
        return sharedInstance;
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    69
    }
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    70
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    71
    /** Get a context-specific instance of a cache. */
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    72
    public static JRTIndex instance(Context context) {
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    73
        try {
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    74
            JRTIndex instance = context.get(JRTIndex.class);
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    75
            if (instance == null)
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    76
                context.put(JRTIndex.class, instance = new JRTIndex());
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    77
            return instance;
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    78
        } catch (IOException e) {
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    79
            throw new UncheckedIOException(e);
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    80
        }
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    81
    }
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    82
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    83
    public static boolean isAvailable() {
27859
f5bd37460a09 8066731: javac does not work on exploded image
jjg
parents: 27579
diff changeset
    84
        try {
f5bd37460a09 8066731: javac does not work on exploded image
jjg
parents: 27579
diff changeset
    85
            FileSystems.getFileSystem(URI.create("jrt:/"));
f5bd37460a09 8066731: javac does not work on exploded image
jjg
parents: 27579
diff changeset
    86
            return true;
f5bd37460a09 8066731: javac does not work on exploded image
jjg
parents: 27579
diff changeset
    87
        } catch (ProviderNotFoundException | FileSystemNotFoundException e) {
f5bd37460a09 8066731: javac does not work on exploded image
jjg
parents: 27579
diff changeset
    88
            return false;
27579
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    89
        }
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    90
    }
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    91
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    92
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    93
    /**
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    94
     * The jrt: file system.
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    95
     */
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    96
    private final FileSystem jrtfs;
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    97
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    98
    /**
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    99
     * The set of module directories within the jrt: file system.
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   100
     */
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   101
    private final Set<Path> jrtModules;
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   102
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   103
    /**
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   104
     * A lazily evaluated set of entries about the contents of the jrt: file system.
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   105
     */
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   106
    private final Map<RelativeDirectory, SoftReference<Entry>> entries;
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   107
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   108
    /**
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   109
     * An entry provides cached info about a specific package directory within jrt:.
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   110
     */
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   111
    class Entry {
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   112
        /**
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   113
         * The regular files for this package.
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   114
         * For now, assume just one instance of each file across all modules.
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   115
         */
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   116
        final Map<String, Path> files;
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   117
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   118
        /**
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   119
         * The set of subdirectories in jrt: for this package.
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   120
         */
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   121
        final Set<RelativeDirectory> subdirs;
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   122
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   123
        /**
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   124
         * The info that used to be in ct.sym for classes in this package.
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   125
         */
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   126
        final CtSym ctSym;
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   127
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   128
        private Entry(Map<String, Path> files, Set<RelativeDirectory> subdirs, CtSym ctSym) {
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   129
            this.files = files;
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   130
            this.subdirs = subdirs;
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   131
            this.ctSym = ctSym;
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   132
        }
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   133
    }
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   134
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   135
    /**
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   136
     * The info that used to be in ct.sym for classes in a package.
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   137
     */
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   138
    public static class CtSym {
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   139
        /**
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   140
         * The classes in this package are internal and not visible.
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   141
         */
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   142
        public final boolean hidden;
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   143
        /**
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   144
         * The classes in this package are proprietary and will generate a warning.
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   145
         */
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   146
        public final boolean proprietary;
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   147
        /**
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   148
         * The minimum profile in which classes in this package are available.
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   149
         */
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   150
        public final String minProfile;
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   151
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   152
        CtSym(boolean hidden, boolean proprietary, String minProfile) {
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   153
            this.hidden = hidden;
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   154
            this.proprietary = proprietary;
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   155
            this.minProfile = minProfile;
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   156
        }
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   157
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   158
        @Override
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   159
        public String toString() {
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   160
            StringBuilder sb = new StringBuilder("CtSym[");
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   161
            boolean needSep = false;
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   162
            if (hidden) {
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   163
                sb.append("hidden");
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   164
                needSep = true;
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   165
            }
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   166
            if (proprietary) {
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   167
                if (needSep) sb.append(",");
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   168
                sb.append("proprietary");
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   169
                needSep = true;
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   170
            }
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   171
            if (minProfile != null) {
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   172
                if (needSep) sb.append(",");
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   173
                sb.append(minProfile);
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   174
            }
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   175
            sb.append("]");
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   176
            return sb.toString();
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   177
        }
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   178
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   179
        static final CtSym EMPTY = new CtSym(false, false, null);
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   180
    }
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   181
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   182
    /**
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   183
     * Create and initialize the index.
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   184
     */
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   185
    private JRTIndex() throws IOException {
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   186
        jrtfs = FileSystems.getFileSystem(URI.create("jrt:/"));
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   187
        jrtModules = new LinkedHashSet<>();
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   188
        Path root = jrtfs.getPath("/");
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   189
        try (DirectoryStream<Path> stream = Files.newDirectoryStream(root)) {
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   190
            for (Path entry: stream) {
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   191
                if (Files.isDirectory(entry))
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   192
                    jrtModules.add(entry);
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   193
            }
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   194
        }
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   195
        entries = new HashMap<>();
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   196
    }
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   197
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   198
    public CtSym getCtSym(CharSequence packageName) throws IOException {
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   199
        return getEntry(RelativeDirectory.forPackage(packageName)).ctSym;
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   200
    }
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   201
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   202
    synchronized Entry getEntry(RelativeDirectory rd) throws IOException {
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   203
        SoftReference<Entry> ref = entries.get(rd);
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   204
        Entry e = (ref == null) ? null : ref.get();
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   205
        if (e == null) {
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   206
            Map<String, Path> files = new LinkedHashMap<>();
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   207
            Set<RelativeDirectory> subdirs = new LinkedHashSet<>();
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   208
            for (Path module: jrtModules) {
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   209
                Path p = rd.getFile(module);
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   210
                if (!Files.exists(p))
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   211
                    continue;
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   212
                try (DirectoryStream<Path> stream = Files.newDirectoryStream(p)) {
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   213
                    for (Path entry: stream) {
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   214
                        String name = entry.getFileName().toString();
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   215
                        if (Files.isRegularFile(entry)) {
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   216
                            // TODO: consider issue of files with same name in different modules
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   217
                            files.put(name, entry);
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   218
                        } else if (Files.isDirectory(entry)) {
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   219
                            subdirs.add(new RelativeDirectory(rd, name));
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   220
                        }
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   221
                    }
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   222
                }
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   223
            }
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   224
            e = new Entry(Collections.unmodifiableMap(files),
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   225
                    Collections.unmodifiableSet(subdirs),
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   226
                    getCtInfo(rd));
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   227
            entries.put(rd, new SoftReference<>(e));
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   228
        }
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   229
        return e;
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   230
    }
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   231
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   232
    public boolean isInJRT(FileObject fo) {
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   233
        if (fo instanceof PathFileObject) {
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   234
            Path path = ((PathFileObject) fo).getPath();
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   235
            return (path.getFileSystem() == jrtfs);
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   236
        } else {
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   237
            return false;
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   238
        }
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   239
    }
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   240
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   241
    private CtSym getCtInfo(RelativeDirectory dir) {
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   242
        if (dir.path.isEmpty())
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   243
            return CtSym.EMPTY;
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   244
        // It's a side-effect of the default build rules that ct.properties
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   245
        // ends up as a resource bundle.
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   246
        if (ctBundle == null) {
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   247
            final String bundleName = "com.sun.tools.javac.resources.ct";
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   248
            ctBundle = ResourceBundle.getBundle(bundleName);
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   249
        }
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   250
        try {
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   251
            String attrs = ctBundle.getString(dir.path.replace('/', '.') + '*');
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   252
            boolean hidden = false;
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   253
            boolean proprietary = false;
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   254
            String minProfile = null;
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   255
            for (String attr: attrs.split(" +", 0)) {
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   256
                switch (attr) {
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   257
                    case "hidden":
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   258
                        hidden = true;
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   259
                        break;
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   260
                    case "proprietary":
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   261
                        proprietary = true;
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   262
                        break;
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   263
                    default:
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   264
                        minProfile = attr;
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   265
                }
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   266
            }
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   267
            return new CtSym(hidden, proprietary, minProfile);
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   268
        } catch (MissingResourceException e) {
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   269
            return CtSym.EMPTY;
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   270
        }
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   271
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   272
    }
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   273
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   274
    private ResourceBundle ctBundle;
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   275
}