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