jdk/src/java.base/share/classes/java/lang/module/ModulePath.java
author redestad
Thu, 21 Apr 2016 13:39:53 +0200
changeset 37593 824750ada3d6
parent 36511 9d0388c6b336
child 37779 7c84df693837
permissions -rw-r--r--
8154231: Simplify access to System properties from JDK code Reviewed-by: rriggs, chegar, weijun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     1
/*
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     2
 * Copyright (c) 2014, 2016, Oracle and/or its affiliates. All rights reserved.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     4
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    10
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    15
 * accompanied this code).
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    16
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    20
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    23
 * questions.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    24
 */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    25
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    26
package java.lang.module;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    27
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    28
import java.io.BufferedInputStream;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    29
import java.io.BufferedReader;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    30
import java.io.File;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    31
import java.io.IOException;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    32
import java.io.InputStream;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    33
import java.io.InputStreamReader;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    34
import java.io.UncheckedIOException;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    35
import java.lang.module.ModuleDescriptor.Requires;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    36
import java.nio.file.DirectoryStream;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    37
import java.nio.file.Files;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    38
import java.nio.file.NoSuchFileException;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    39
import java.nio.file.Path;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    40
import java.nio.file.attribute.BasicFileAttributes;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    41
import java.util.Collections;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    42
import java.util.HashMap;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    43
import java.util.HashSet;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    44
import java.util.Map;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    45
import java.util.Objects;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    46
import java.util.Optional;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    47
import java.util.Set;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    48
import java.util.jar.Attributes;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    49
import java.util.jar.JarEntry;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    50
import java.util.jar.JarFile;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    51
import java.util.jar.Manifest;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    52
import java.util.regex.Matcher;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    53
import java.util.regex.Pattern;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    54
import java.util.stream.Collectors;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    55
import java.util.stream.Stream;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    56
import java.util.zip.ZipEntry;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    57
import java.util.zip.ZipFile;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    58
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    59
import jdk.internal.module.Checks;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    60
import jdk.internal.module.ConfigurableModuleFinder;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    61
import jdk.internal.perf.PerfCounter;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    62
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    63
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    64
/**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    65
 * A {@code ModuleFinder} that locates modules on the file system by searching
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    66
 * a sequence of directories or packaged modules.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    67
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    68
 * The {@code ModuleFinder} can be configured to work in either the run-time
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    69
 * or link-time phases. In both cases it locates modular JAR and exploded
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    70
 * modules. When configured for link-time then it additionally locates
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    71
 * modules in JMOD files.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    72
 */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    73
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    74
class ModulePath implements ConfigurableModuleFinder {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    75
    private static final String MODULE_INFO = "module-info.class";
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    76
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    77
    // the entries on this module path
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    78
    private final Path[] entries;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    79
    private int next;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    80
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    81
    // true if in the link phase
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    82
    private boolean isLinkPhase;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    83
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    84
    // map of module name to module reference map for modules already located
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    85
    private final Map<String, ModuleReference> cachedModules = new HashMap<>();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    86
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    87
    ModulePath(Path... entries) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    88
        this.entries = entries.clone();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    89
        for (Path entry : this.entries) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    90
            Objects.requireNonNull(entry);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    91
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    92
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    93
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    94
    @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    95
    public void configurePhase(Phase phase) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    96
        isLinkPhase = (phase == Phase.LINK_TIME);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    97
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    98
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    99
    @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   100
    public Optional<ModuleReference> find(String name) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   101
        Objects.requireNonNull(name);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   102
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   103
        // try cached modules
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   104
        ModuleReference m = cachedModules.get(name);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   105
        if (m != null)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   106
            return Optional.of(m);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   107
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   108
        // the module may not have been encountered yet
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   109
        while (hasNextEntry()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   110
            scanNextEntry();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   111
            m = cachedModules.get(name);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   112
            if (m != null)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   113
                return Optional.of(m);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   114
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   115
        return Optional.empty();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   116
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   117
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   118
    @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   119
    public Set<ModuleReference> findAll() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   120
        // need to ensure that all entries have been scanned
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   121
        while (hasNextEntry()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   122
            scanNextEntry();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   123
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   124
        return cachedModules.values().stream().collect(Collectors.toSet());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   125
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   126
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   127
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   128
     * Returns {@code true} if there are additional entries to scan
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   129
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   130
    private boolean hasNextEntry() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   131
        return next < entries.length;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   132
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   133
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   134
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   135
     * Scans the next entry on the module path. A no-op if all entries have
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   136
     * already been scanned.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   137
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   138
     * @throws FindException if an error occurs scanning the next entry
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   139
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   140
    private void scanNextEntry() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   141
        if (hasNextEntry()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   142
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   143
            long t0 = System.nanoTime();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   144
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   145
            Path entry = entries[next];
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   146
            Map<String, ModuleReference> modules = scan(entry);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   147
            next++;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   148
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   149
            // update cache, ignoring duplicates
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   150
            int initialSize = cachedModules.size();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   151
            for (Map.Entry<String, ModuleReference> e : modules.entrySet()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   152
                cachedModules.putIfAbsent(e.getKey(), e.getValue());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   153
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   154
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   155
            // update counters
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   156
            int added = cachedModules.size() - initialSize;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   157
            moduleCount.add(added);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   158
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   159
            scanTime.addElapsedTimeFrom(t0);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   160
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   161
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   162
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   163
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   164
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   165
     * Scan the given module path entry. If the entry is a directory then it is
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   166
     * a directory of modules or an exploded module. If the entry is a regular
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   167
     * file then it is assumed to be a packaged module.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   168
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   169
     * @throws FindException if an error occurs scanning the entry
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   170
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   171
    private Map<String, ModuleReference> scan(Path entry) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   172
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   173
        BasicFileAttributes attrs;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   174
        try {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   175
            attrs = Files.readAttributes(entry, BasicFileAttributes.class);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   176
        } catch (NoSuchFileException e) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   177
            return Collections.emptyMap();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   178
        } catch (IOException ioe) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   179
            throw new FindException(ioe);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   180
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   181
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   182
        try {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   183
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   184
            if (attrs.isDirectory()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   185
                Path mi = entry.resolve(MODULE_INFO);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   186
                if (!Files.exists(mi)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   187
                    // does not exist or unable to determine so assume a
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   188
                    // directory of modules
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   189
                    return scanDirectory(entry);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   190
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   191
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   192
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   193
            if (attrs.isRegularFile() || attrs.isDirectory()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   194
                // packaged or exploded module
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   195
                ModuleReference mref = readModule(entry, attrs);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   196
                if (mref != null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   197
                    String name = mref.descriptor().name();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   198
                    return Collections.singletonMap(name, mref);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   199
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   200
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   201
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   202
            // not recognized
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   203
            throw new FindException("Unrecognized module: " + entry);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   204
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   205
        } catch (IOException ioe) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   206
            throw new FindException(ioe);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   207
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   208
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   209
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   210
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   211
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   212
     * Scans the given directory for packaged or exploded modules.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   213
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   214
     * @return a map of module name to ModuleReference for the modules found
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   215
     *         in the directory
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   216
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   217
     * @throws IOException if an I/O error occurs
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   218
     * @throws FindException if an error occurs scanning the entry or the
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   219
     *         directory contains two or more modules with the same name
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   220
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   221
    private Map<String, ModuleReference> scanDirectory(Path dir)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   222
        throws IOException
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   223
    {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   224
        // The map of name -> mref of modules found in this directory.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   225
        Map<String, ModuleReference> nameToReference = new HashMap<>();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   226
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   227
        try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   228
            for (Path entry : stream) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   229
                BasicFileAttributes attrs;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   230
                try {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   231
                    attrs = Files.readAttributes(entry, BasicFileAttributes.class);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   232
                } catch (NoSuchFileException ignore) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   233
                    // file has been removed or moved, ignore for now
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   234
                    continue;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   235
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   236
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   237
                ModuleReference mref = readModule(entry, attrs);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   238
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   239
                // module found
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   240
                if (mref != null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   241
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   242
                    // can have at most one version of a module in the directory
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   243
                    String name = mref.descriptor().name();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   244
                    if (nameToReference.put(name, mref) != null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   245
                        throw new FindException("Two versions of module "
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   246
                                + name + " found in " + dir);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   247
                    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   248
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   249
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   250
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   251
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   252
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   253
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   254
        return nameToReference;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   255
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   256
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   257
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   258
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   259
     * Locates a packaged or exploded module, returning a {@code ModuleReference}
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   260
     * to the module. Returns {@code null} if the module is not recognized
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   261
     * as a packaged or exploded module.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   262
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   263
     * @throws IOException if an I/O error occurs
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   264
     * @throws FindException if an error occurs parsing the module descriptor
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   265
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   266
    private ModuleReference readModule(Path entry, BasicFileAttributes attrs)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   267
        throws IOException
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   268
    {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   269
        try {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   270
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   271
            ModuleReference mref = null;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   272
            if (attrs.isDirectory()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   273
                mref = readExplodedModule(entry);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   274
            } if (attrs.isRegularFile()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   275
                if (entry.toString().endsWith(".jar")) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   276
                    mref = readJar(entry);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   277
                } else if (isLinkPhase && entry.toString().endsWith(".jmod")) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   278
                    mref = readJMod(entry);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   279
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   280
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   281
            return mref;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   282
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   283
        } catch (InvalidModuleDescriptorException e) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   284
            throw new FindException("Error reading module: " + entry, e);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   285
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   286
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   287
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   288
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   289
    // -- jmod files --
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   290
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   291
    private Set<String> jmodPackages(ZipFile zf) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   292
        return zf.stream()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   293
            .filter(e -> e.getName().startsWith("classes/") &&
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   294
                    e.getName().endsWith(".class"))
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   295
            .map(e -> toPackageName(e))
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   296
            .filter(pkg -> pkg.length() > 0) // module-info
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   297
            .distinct()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   298
            .collect(Collectors.toSet());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   299
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   300
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   301
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   302
     * Returns a {@code ModuleReference} to a module in jmod file on the
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   303
     * file system.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   304
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   305
    private ModuleReference readJMod(Path file) throws IOException {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   306
        try (ZipFile zf = new ZipFile(file.toString())) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   307
            ZipEntry ze = zf.getEntry("classes/" + MODULE_INFO);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   308
            if (ze == null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   309
                throw new IOException(MODULE_INFO + " is missing: " + file);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   310
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   311
            ModuleDescriptor md;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   312
            try (InputStream in = zf.getInputStream(ze)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   313
                md = ModuleDescriptor.read(in, () -> jmodPackages(zf));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   314
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   315
            return ModuleReferences.newJModModule(md, file);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   316
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   317
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   318
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   319
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   320
    // -- JAR files --
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   321
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   322
    private static final String SERVICES_PREFIX = "META-INF/services/";
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   323
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   324
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   325
     * Returns a container with the service type corresponding to the name of
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   326
     * a services configuration file.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   327
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   328
     * For example, if called with "META-INF/services/p.S" then this method
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   329
     * returns a container with the value "p.S".
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   330
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   331
    private Optional<String> toServiceName(String cf) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   332
        assert cf.startsWith(SERVICES_PREFIX);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   333
        int index = cf.lastIndexOf("/") + 1;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   334
        if (index < cf.length()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   335
            String prefix = cf.substring(0, index);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   336
            if (prefix.equals(SERVICES_PREFIX)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   337
                String sn = cf.substring(index);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   338
                if (Checks.isJavaIdentifier(sn))
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   339
                    return Optional.of(sn);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   340
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   341
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   342
        return Optional.empty();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   343
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   344
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   345
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   346
     * Reads the next line from the given reader and trims it of comments and
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   347
     * leading/trailing white space.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   348
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   349
     * Returns null if the reader is at EOF.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   350
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   351
    private String nextLine(BufferedReader reader) throws IOException {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   352
        String ln = reader.readLine();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   353
        if (ln != null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   354
            int ci = ln.indexOf('#');
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   355
            if (ci >= 0)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   356
                ln = ln.substring(0, ci);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   357
            ln = ln.trim();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   358
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   359
        return ln;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   360
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   361
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   362
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   363
     * Treat the given JAR file as a module as follows:
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   364
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   365
     * 1. The module name (and optionally the version) is derived from the file
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   366
     *    name of the JAR file
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   367
     * 2. The packages of all .class files in the JAR file are exported
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   368
     * 3. It has no module-private/concealed packages
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   369
     * 4. The contents of any META-INF/services configuration files are mapped
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   370
     *    to "provides" declarations
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   371
     * 5. The Main-Class attribute in the main attributes of the JAR manifest
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   372
     *    is mapped to the module descriptor mainClass
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   373
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   374
     * @apiNote This needs to move to somewhere where it can be used by tools,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   375
     * maybe even a standard API if automatic modules are a Java SE feature.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   376
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   377
    private ModuleDescriptor deriveModuleDescriptor(JarFile jf)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   378
        throws IOException
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   379
    {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   380
        // Derive module name and version from JAR file name
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   381
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   382
        String fn = jf.getName();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   383
        int i = fn.lastIndexOf(File.separator);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   384
        if (i != -1)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   385
            fn = fn.substring(i+1);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   386
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   387
        // drop .jar
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   388
        String mn = fn.substring(0, fn.length()-4);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   389
        String vs = null;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   390
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   391
        // find first occurrence of -${NUMBER}. or -${NUMBER}$
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   392
        Matcher matcher = Pattern.compile("-(\\d+(\\.|$))").matcher(mn);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   393
        if (matcher.find()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   394
            int start = matcher.start();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   395
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   396
            // attempt to parse the tail as a version string
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   397
            try {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   398
                String tail = mn.substring(start+1);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   399
                ModuleDescriptor.Version.parse(tail);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   400
                vs = tail;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   401
            } catch (IllegalArgumentException ignore) { }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   402
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   403
            mn = mn.substring(0, start);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   404
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   405
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   406
        // finally clean up the module name
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   407
        mn =  mn.replaceAll("[^A-Za-z0-9]", ".")  // replace non-alphanumeric
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   408
                .replaceAll("(\\.)(\\1)+", ".")   // collapse repeating dots
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   409
                .replaceAll("^\\.", "")           // drop leading dots
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   410
                .replaceAll("\\.$", "");          // drop trailing dots
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   411
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   412
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   413
        // Builder throws IAE if module name is empty or invalid
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   414
        ModuleDescriptor.Builder builder
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   415
            = new ModuleDescriptor.Builder(mn, true)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   416
                .requires(Requires.Modifier.MANDATED, "java.base");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   417
        if (vs != null)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   418
            builder.version(vs);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   419
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   420
        // scan the entries in the JAR file to locate the .class and service
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   421
        // configuration file
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   422
        Stream<String> stream = jf.stream()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   423
            .map(e -> e.getName())
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   424
            .filter(e -> (e.endsWith(".class") || e.startsWith(SERVICES_PREFIX)))
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   425
            .distinct();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   426
        Map<Boolean, Set<String>> map
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   427
            = stream.collect(Collectors.partitioningBy(s -> s.endsWith(".class"),
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   428
                             Collectors.toSet()));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   429
        Set<String> classFiles = map.get(Boolean.TRUE);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   430
        Set<String> configFiles = map.get(Boolean.FALSE);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   431
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   432
        // all packages are exported
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   433
        classFiles.stream()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   434
            .map(c -> toPackageName(c))
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   435
            .distinct()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   436
            .forEach(p -> builder.exports(p));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   437
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   438
        // map names of service configuration files to service names
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   439
        Set<String> serviceNames = configFiles.stream()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   440
            .map(this::toServiceName)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   441
            .filter(Optional::isPresent)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   442
            .map(Optional::get)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   443
            .collect(Collectors.toSet());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   444
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   445
        // parse each service configuration file
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   446
        for (String sn : serviceNames) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   447
            JarEntry entry = jf.getJarEntry(SERVICES_PREFIX + sn);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   448
            Set<String> providerClasses = new HashSet<>();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   449
            try (InputStream in = jf.getInputStream(entry)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   450
                BufferedReader reader
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   451
                    = new BufferedReader(new InputStreamReader(in, "UTF-8"));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   452
                String cn;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   453
                while ((cn = nextLine(reader)) != null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   454
                    if (Checks.isJavaIdentifier(cn)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   455
                        providerClasses.add(cn);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   456
                    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   457
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   458
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   459
            if (!providerClasses.isEmpty())
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   460
                builder.provides(sn, providerClasses);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   461
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   462
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   463
        // Main-Class attribute if it exists
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   464
        Manifest man = jf.getManifest();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   465
        if (man != null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   466
            Attributes attrs = man.getMainAttributes();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   467
            String mainClass = attrs.getValue(Attributes.Name.MAIN_CLASS);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   468
            if (mainClass != null)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   469
                builder.mainClass(mainClass);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   470
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   471
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   472
        return builder.build();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   473
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   474
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   475
    private Set<String> jarPackages(JarFile jf) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   476
        return jf.stream()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   477
            .filter(e -> e.getName().endsWith(".class"))
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   478
            .map(e -> toPackageName(e))
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   479
            .filter(pkg -> pkg.length() > 0)   // module-info
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   480
            .distinct()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   481
            .collect(Collectors.toSet());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   482
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   483
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   484
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   485
     * Returns a {@code ModuleReference} to a module in modular JAR file on
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   486
     * the file system.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   487
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   488
    private ModuleReference readJar(Path file) throws IOException {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   489
        try (JarFile jf = new JarFile(file.toString())) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   490
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   491
            ModuleDescriptor md;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   492
            JarEntry entry = jf.getJarEntry(MODULE_INFO);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   493
            if (entry == null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   494
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   495
                // no module-info.class so treat it as automatic module
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   496
                try {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   497
                    md = deriveModuleDescriptor(jf);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   498
                } catch (IllegalArgumentException iae) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   499
                    throw new FindException(
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   500
                        "Unable to derive module descriptor for: "
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   501
                        + jf.getName(), iae);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   502
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   503
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   504
            } else {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   505
                md = ModuleDescriptor.read(jf.getInputStream(entry),
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   506
                                           () -> jarPackages(jf));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   507
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   508
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   509
            return ModuleReferences.newJarModule(md, file);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   510
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   511
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   512
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   513
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   514
    // -- exploded directories --
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   515
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   516
    private Set<String> explodedPackages(Path dir) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   517
        try {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   518
            return Files.find(dir, Integer.MAX_VALUE,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   519
                              ((path, attrs) -> attrs.isRegularFile() &&
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   520
                               path.toString().endsWith(".class")))
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   521
                .map(path -> toPackageName(dir.relativize(path)))
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   522
                .filter(pkg -> pkg.length() > 0)   // module-info
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   523
                .distinct()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   524
                .collect(Collectors.toSet());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   525
        } catch (IOException x) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   526
            throw new UncheckedIOException(x);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   527
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   528
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   529
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   530
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   531
     * Returns a {@code ModuleReference} to an exploded module on the file
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   532
     * system or {@code null} if {@code module-info.class} not found.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   533
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   534
    private ModuleReference readExplodedModule(Path dir) throws IOException {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   535
        Path mi = dir.resolve(MODULE_INFO);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   536
        ModuleDescriptor md;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   537
        try (InputStream in = Files.newInputStream(mi)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   538
            md = ModuleDescriptor.read(new BufferedInputStream(in),
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   539
                                       () -> explodedPackages(dir));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   540
        } catch (NoSuchFileException e) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   541
            // for now
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   542
            return null;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   543
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   544
        return ModuleReferences.newExplodedModule(md, dir);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   545
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   546
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   547
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   548
    //
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   549
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   550
    // p/q/T.class => p.q
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   551
    private String toPackageName(String cn) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   552
        assert cn.endsWith(".class");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   553
        int start = 0;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   554
        int index = cn.lastIndexOf("/");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   555
        if (index > start) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   556
            return cn.substring(start, index).replace('/', '.');
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   557
        } else {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   558
            return "";
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   559
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   560
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   561
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   562
    private String toPackageName(ZipEntry entry) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   563
        String name = entry.getName();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   564
        assert name.endsWith(".class");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   565
        // jmod classes in classes/, jar in /
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   566
        int start = name.startsWith("classes/") ? 8 : 0;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   567
        int index = name.lastIndexOf("/");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   568
        if (index > start) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   569
            return name.substring(start, index).replace('/', '.');
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   570
        } else {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   571
            return "";
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   572
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   573
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   574
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   575
    private String toPackageName(Path path) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   576
        String name = path.toString();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   577
        assert name.endsWith(".class");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   578
        int index = name.lastIndexOf(File.separatorChar);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   579
        if (index != -1) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   580
            return name.substring(0, index).replace(File.separatorChar, '.');
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   581
        } else {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   582
            return "";
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   583
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   584
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   585
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   586
    private static final PerfCounter scanTime
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   587
        = PerfCounter.newPerfCounter("jdk.module.finder.modulepath.scanTime");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   588
    private static final PerfCounter moduleCount
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   589
        = PerfCounter.newPerfCounter("jdk.module.finder.modulepath.modules");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   590
}