src/jdk.jlink/share/classes/jdk/tools/jlink/internal/ResourcePoolConfiguration.java
author pchilanomate
Fri, 31 Aug 2018 10:22:04 -0400
changeset 51610 cdef4df6b0e7
parent 47216 71c04702a3d5
permissions -rw-r--r--
8206424: Use locking for cleaning ProtectionDomainTable Summary: ServiceThread is now in charge of cleaning ProtectionDomainTable entries Reviewed-by: coleenp, iklam
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
40556
27f68990ba9d 8147491: module graph consistency checks after jlink plugins operate on module pool
sundar
parents:
diff changeset
     1
/*
27f68990ba9d 8147491: module graph consistency checks after jlink plugins operate on module pool
sundar
parents:
diff changeset
     2
 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
27f68990ba9d 8147491: module graph consistency checks after jlink plugins operate on module pool
sundar
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
27f68990ba9d 8147491: module graph consistency checks after jlink plugins operate on module pool
sundar
parents:
diff changeset
     4
 *
27f68990ba9d 8147491: module graph consistency checks after jlink plugins operate on module pool
sundar
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
27f68990ba9d 8147491: module graph consistency checks after jlink plugins operate on module pool
sundar
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
27f68990ba9d 8147491: module graph consistency checks after jlink plugins operate on module pool
sundar
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
27f68990ba9d 8147491: module graph consistency checks after jlink plugins operate on module pool
sundar
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
27f68990ba9d 8147491: module graph consistency checks after jlink plugins operate on module pool
sundar
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
27f68990ba9d 8147491: module graph consistency checks after jlink plugins operate on module pool
sundar
parents:
diff changeset
    10
 *
27f68990ba9d 8147491: module graph consistency checks after jlink plugins operate on module pool
sundar
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
27f68990ba9d 8147491: module graph consistency checks after jlink plugins operate on module pool
sundar
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
27f68990ba9d 8147491: module graph consistency checks after jlink plugins operate on module pool
sundar
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
27f68990ba9d 8147491: module graph consistency checks after jlink plugins operate on module pool
sundar
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
27f68990ba9d 8147491: module graph consistency checks after jlink plugins operate on module pool
sundar
parents:
diff changeset
    15
 * accompanied this code).
27f68990ba9d 8147491: module graph consistency checks after jlink plugins operate on module pool
sundar
parents:
diff changeset
    16
 *
27f68990ba9d 8147491: module graph consistency checks after jlink plugins operate on module pool
sundar
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
27f68990ba9d 8147491: module graph consistency checks after jlink plugins operate on module pool
sundar
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
27f68990ba9d 8147491: module graph consistency checks after jlink plugins operate on module pool
sundar
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
27f68990ba9d 8147491: module graph consistency checks after jlink plugins operate on module pool
sundar
parents:
diff changeset
    20
 *
27f68990ba9d 8147491: module graph consistency checks after jlink plugins operate on module pool
sundar
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
27f68990ba9d 8147491: module graph consistency checks after jlink plugins operate on module pool
sundar
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
27f68990ba9d 8147491: module graph consistency checks after jlink plugins operate on module pool
sundar
parents:
diff changeset
    23
 * questions.
27f68990ba9d 8147491: module graph consistency checks after jlink plugins operate on module pool
sundar
parents:
diff changeset
    24
 */
27f68990ba9d 8147491: module graph consistency checks after jlink plugins operate on module pool
sundar
parents:
diff changeset
    25
27f68990ba9d 8147491: module graph consistency checks after jlink plugins operate on module pool
sundar
parents:
diff changeset
    26
package jdk.tools.jlink.internal;
27f68990ba9d 8147491: module graph consistency checks after jlink plugins operate on module pool
sundar
parents:
diff changeset
    27
27f68990ba9d 8147491: module graph consistency checks after jlink plugins operate on module pool
sundar
parents:
diff changeset
    28
import java.lang.module.Configuration;
27f68990ba9d 8147491: module graph consistency checks after jlink plugins operate on module pool
sundar
parents:
diff changeset
    29
import java.lang.module.ModuleDescriptor;
27f68990ba9d 8147491: module graph consistency checks after jlink plugins operate on module pool
sundar
parents:
diff changeset
    30
import java.lang.module.ModuleFinder;
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
    31
import java.lang.module.ModuleReader;
40556
27f68990ba9d 8147491: module graph consistency checks after jlink plugins operate on module pool
sundar
parents:
diff changeset
    32
import java.lang.module.ModuleReference;
27f68990ba9d 8147491: module graph consistency checks after jlink plugins operate on module pool
sundar
parents:
diff changeset
    33
import java.io.IOException;
27f68990ba9d 8147491: module graph consistency checks after jlink plugins operate on module pool
sundar
parents:
diff changeset
    34
import java.io.UncheckedIOException;
27f68990ba9d 8147491: module graph consistency checks after jlink plugins operate on module pool
sundar
parents:
diff changeset
    35
import java.nio.ByteBuffer;
27f68990ba9d 8147491: module graph consistency checks after jlink plugins operate on module pool
sundar
parents:
diff changeset
    36
import java.util.Collection;
27f68990ba9d 8147491: module graph consistency checks after jlink plugins operate on module pool
sundar
parents:
diff changeset
    37
import java.util.HashSet;
27f68990ba9d 8147491: module graph consistency checks after jlink plugins operate on module pool
sundar
parents:
diff changeset
    38
import java.util.Map;
27f68990ba9d 8147491: module graph consistency checks after jlink plugins operate on module pool
sundar
parents:
diff changeset
    39
import java.util.Optional;
27f68990ba9d 8147491: module graph consistency checks after jlink plugins operate on module pool
sundar
parents:
diff changeset
    40
import java.util.Set;
27f68990ba9d 8147491: module graph consistency checks after jlink plugins operate on module pool
sundar
parents:
diff changeset
    41
import java.util.stream.Collectors;
27f68990ba9d 8147491: module graph consistency checks after jlink plugins operate on module pool
sundar
parents:
diff changeset
    42
import jdk.tools.jlink.plugin.PluginException;
27f68990ba9d 8147491: module graph consistency checks after jlink plugins operate on module pool
sundar
parents:
diff changeset
    43
import jdk.tools.jlink.plugin.ResourcePool;
27f68990ba9d 8147491: module graph consistency checks after jlink plugins operate on module pool
sundar
parents:
diff changeset
    44
import jdk.tools.jlink.plugin.ResourcePoolEntry;
27f68990ba9d 8147491: module graph consistency checks after jlink plugins operate on module pool
sundar
parents:
diff changeset
    45
import jdk.tools.jlink.plugin.ResourcePoolModule;
27f68990ba9d 8147491: module graph consistency checks after jlink plugins operate on module pool
sundar
parents:
diff changeset
    46
27f68990ba9d 8147491: module graph consistency checks after jlink plugins operate on module pool
sundar
parents:
diff changeset
    47
final class ResourcePoolConfiguration {
27f68990ba9d 8147491: module graph consistency checks after jlink plugins operate on module pool
sundar
parents:
diff changeset
    48
    private ResourcePoolConfiguration() {}
27f68990ba9d 8147491: module graph consistency checks after jlink plugins operate on module pool
sundar
parents:
diff changeset
    49
27f68990ba9d 8147491: module graph consistency checks after jlink plugins operate on module pool
sundar
parents:
diff changeset
    50
    private static ModuleDescriptor descriptorOf(ResourcePoolModule mod) {
27f68990ba9d 8147491: module graph consistency checks after jlink plugins operate on module pool
sundar
parents:
diff changeset
    51
        ModuleDescriptor md = mod.descriptor();
27f68990ba9d 8147491: module graph consistency checks after jlink plugins operate on module pool
sundar
parents:
diff changeset
    52
27f68990ba9d 8147491: module graph consistency checks after jlink plugins operate on module pool
sundar
parents:
diff changeset
    53
        // drop hashes
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
    54
        ModuleDescriptor.Builder builder = ModuleDescriptor.newModule(md.name());
40556
27f68990ba9d 8147491: module graph consistency checks after jlink plugins operate on module pool
sundar
parents:
diff changeset
    55
        md.requires().stream()
27f68990ba9d 8147491: module graph consistency checks after jlink plugins operate on module pool
sundar
parents:
diff changeset
    56
          .forEach(builder::requires);
27f68990ba9d 8147491: module graph consistency checks after jlink plugins operate on module pool
sundar
parents:
diff changeset
    57
        md.exports().stream()
27f68990ba9d 8147491: module graph consistency checks after jlink plugins operate on module pool
sundar
parents:
diff changeset
    58
          .forEach(builder::exports);
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40556
diff changeset
    59
        md.opens().stream()
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40556
diff changeset
    60
          .forEach(builder::opens);
40556
27f68990ba9d 8147491: module graph consistency checks after jlink plugins operate on module pool
sundar
parents:
diff changeset
    61
        md.uses().stream()
27f68990ba9d 8147491: module graph consistency checks after jlink plugins operate on module pool
sundar
parents:
diff changeset
    62
          .forEach(builder::uses);
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40556
diff changeset
    63
        md.provides().stream()
40556
27f68990ba9d 8147491: module graph consistency checks after jlink plugins operate on module pool
sundar
parents:
diff changeset
    64
          .forEach(builder::provides);
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
    65
        builder.packages(md.packages());
40556
27f68990ba9d 8147491: module graph consistency checks after jlink plugins operate on module pool
sundar
parents:
diff changeset
    66
27f68990ba9d 8147491: module graph consistency checks after jlink plugins operate on module pool
sundar
parents:
diff changeset
    67
        md.version().ifPresent(builder::version);
27f68990ba9d 8147491: module graph consistency checks after jlink plugins operate on module pool
sundar
parents:
diff changeset
    68
        md.mainClass().ifPresent(builder::mainClass);
27f68990ba9d 8147491: module graph consistency checks after jlink plugins operate on module pool
sundar
parents:
diff changeset
    69
27f68990ba9d 8147491: module graph consistency checks after jlink plugins operate on module pool
sundar
parents:
diff changeset
    70
        return builder.build();
27f68990ba9d 8147491: module graph consistency checks after jlink plugins operate on module pool
sundar
parents:
diff changeset
    71
    }
27f68990ba9d 8147491: module graph consistency checks after jlink plugins operate on module pool
sundar
parents:
diff changeset
    72
27f68990ba9d 8147491: module graph consistency checks after jlink plugins operate on module pool
sundar
parents:
diff changeset
    73
    private static ModuleReference moduleReference(ModuleDescriptor desc) {
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
    74
        return new ModuleReference(desc, null) {
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
    75
            @Override
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
    76
            public ModuleReader open() {
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
    77
                throw new UnsupportedOperationException();
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
    78
            }
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
    79
        };
40556
27f68990ba9d 8147491: module graph consistency checks after jlink plugins operate on module pool
sundar
parents:
diff changeset
    80
    }
27f68990ba9d 8147491: module graph consistency checks after jlink plugins operate on module pool
sundar
parents:
diff changeset
    81
27f68990ba9d 8147491: module graph consistency checks after jlink plugins operate on module pool
sundar
parents:
diff changeset
    82
    private static Map<String, ModuleReference> allModRefs(ResourcePool pool) {
27f68990ba9d 8147491: module graph consistency checks after jlink plugins operate on module pool
sundar
parents:
diff changeset
    83
        return pool.moduleView().modules().
27f68990ba9d 8147491: module graph consistency checks after jlink plugins operate on module pool
sundar
parents:
diff changeset
    84
            collect(Collectors.toMap(ResourcePoolModule::name,
27f68990ba9d 8147491: module graph consistency checks after jlink plugins operate on module pool
sundar
parents:
diff changeset
    85
                m -> moduleReference(descriptorOf(m))));
27f68990ba9d 8147491: module graph consistency checks after jlink plugins operate on module pool
sundar
parents:
diff changeset
    86
    }
27f68990ba9d 8147491: module graph consistency checks after jlink plugins operate on module pool
sundar
parents:
diff changeset
    87
27f68990ba9d 8147491: module graph consistency checks after jlink plugins operate on module pool
sundar
parents:
diff changeset
    88
    private static void checkPackages(ResourcePool pool) {
27f68990ba9d 8147491: module graph consistency checks after jlink plugins operate on module pool
sundar
parents:
diff changeset
    89
        // check that each resource pool module's packages()
27f68990ba9d 8147491: module graph consistency checks after jlink plugins operate on module pool
sundar
parents:
diff changeset
    90
        // returns a set that is consistent with the module
27f68990ba9d 8147491: module graph consistency checks after jlink plugins operate on module pool
sundar
parents:
diff changeset
    91
        // descriptor of that module.
27f68990ba9d 8147491: module graph consistency checks after jlink plugins operate on module pool
sundar
parents:
diff changeset
    92
27f68990ba9d 8147491: module graph consistency checks after jlink plugins operate on module pool
sundar
parents:
diff changeset
    93
        pool.moduleView().modules().forEach(m -> {
27f68990ba9d 8147491: module graph consistency checks after jlink plugins operate on module pool
sundar
parents:
diff changeset
    94
            ModuleDescriptor desc = m.descriptor();
27f68990ba9d 8147491: module graph consistency checks after jlink plugins operate on module pool
sundar
parents:
diff changeset
    95
            if (!desc.packages().equals(m.packages())) {
27f68990ba9d 8147491: module graph consistency checks after jlink plugins operate on module pool
sundar
parents:
diff changeset
    96
                throw new RuntimeException("Module " + m.name() +
43796
c204d2d789dc 8174718: "Module <name>'s descriptor returns inconsistent package set" confusing
sundar
parents: 43712
diff changeset
    97
                   "'s descriptor indicates the set of packages is : " +
c204d2d789dc 8174718: "Module <name>'s descriptor returns inconsistent package set" confusing
sundar
parents: 43712
diff changeset
    98
                   desc.packages() + ", but module contains packages: " +
c204d2d789dc 8174718: "Module <name>'s descriptor returns inconsistent package set" confusing
sundar
parents: 43712
diff changeset
    99
                   m.packages());
40556
27f68990ba9d 8147491: module graph consistency checks after jlink plugins operate on module pool
sundar
parents:
diff changeset
   100
            }
27f68990ba9d 8147491: module graph consistency checks after jlink plugins operate on module pool
sundar
parents:
diff changeset
   101
        });
27f68990ba9d 8147491: module graph consistency checks after jlink plugins operate on module pool
sundar
parents:
diff changeset
   102
    }
27f68990ba9d 8147491: module graph consistency checks after jlink plugins operate on module pool
sundar
parents:
diff changeset
   103
27f68990ba9d 8147491: module graph consistency checks after jlink plugins operate on module pool
sundar
parents:
diff changeset
   104
    static Configuration validate(ResourcePool pool) {
27f68990ba9d 8147491: module graph consistency checks after jlink plugins operate on module pool
sundar
parents:
diff changeset
   105
        checkPackages(pool);
27f68990ba9d 8147491: module graph consistency checks after jlink plugins operate on module pool
sundar
parents:
diff changeset
   106
        final Map<String, ModuleReference> nameToModRef = allModRefs(pool);
27f68990ba9d 8147491: module graph consistency checks after jlink plugins operate on module pool
sundar
parents:
diff changeset
   107
        final Set<ModuleReference> allRefs = new HashSet<>(nameToModRef.values());
27f68990ba9d 8147491: module graph consistency checks after jlink plugins operate on module pool
sundar
parents:
diff changeset
   108
27f68990ba9d 8147491: module graph consistency checks after jlink plugins operate on module pool
sundar
parents:
diff changeset
   109
        final ModuleFinder finder = new ModuleFinder() {
27f68990ba9d 8147491: module graph consistency checks after jlink plugins operate on module pool
sundar
parents:
diff changeset
   110
            @Override
27f68990ba9d 8147491: module graph consistency checks after jlink plugins operate on module pool
sundar
parents:
diff changeset
   111
            public Optional<ModuleReference> find(String name) {
27f68990ba9d 8147491: module graph consistency checks after jlink plugins operate on module pool
sundar
parents:
diff changeset
   112
                return Optional.ofNullable(nameToModRef.get(name));
27f68990ba9d 8147491: module graph consistency checks after jlink plugins operate on module pool
sundar
parents:
diff changeset
   113
            }
27f68990ba9d 8147491: module graph consistency checks after jlink plugins operate on module pool
sundar
parents:
diff changeset
   114
27f68990ba9d 8147491: module graph consistency checks after jlink plugins operate on module pool
sundar
parents:
diff changeset
   115
            @Override
27f68990ba9d 8147491: module graph consistency checks after jlink plugins operate on module pool
sundar
parents:
diff changeset
   116
            public Set<ModuleReference> findAll() {
27f68990ba9d 8147491: module graph consistency checks after jlink plugins operate on module pool
sundar
parents:
diff changeset
   117
                return allRefs;
27f68990ba9d 8147491: module graph consistency checks after jlink plugins operate on module pool
sundar
parents:
diff changeset
   118
            }
27f68990ba9d 8147491: module graph consistency checks after jlink plugins operate on module pool
sundar
parents:
diff changeset
   119
        };
27f68990ba9d 8147491: module graph consistency checks after jlink plugins operate on module pool
sundar
parents:
diff changeset
   120
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   121
        return Configuration.empty().resolve(
40556
27f68990ba9d 8147491: module graph consistency checks after jlink plugins operate on module pool
sundar
parents:
diff changeset
   122
            finder, ModuleFinder.of(), nameToModRef.keySet());
27f68990ba9d 8147491: module graph consistency checks after jlink plugins operate on module pool
sundar
parents:
diff changeset
   123
    }
27f68990ba9d 8147491: module graph consistency checks after jlink plugins operate on module pool
sundar
parents:
diff changeset
   124
}