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 package jdk.tools.jlink.internal; 26 27 import java.lang.module.ModuleDescriptor; 28 import java.nio.ByteBuffer; 29 import java.nio.ByteOrder; 30 import java.util.Collections; 31 import java.util.HashMap; 32 import java.util.HashSet; 33 import java.util.LinkedHashMap; 34 import java.util.Map; 35 import java.util.Objects; 36 import java.util.Optional; 37 import java.util.Set; 38 import java.util.function.Function; 39 import java.util.stream.Stream; 40 import jdk.internal.jimage.decompressor.CompressedResourceHeader; 41 import jdk.tools.jlink.plugin.ResourcePool; 42 import jdk.tools.jlink.plugin.ResourcePoolBuilder; 43 import jdk.tools.jlink.plugin.ResourcePoolEntry; 44 import jdk.tools.jlink.plugin.ResourcePoolModule; 45 import jdk.tools.jlink.plugin.ResourcePoolModuleView; 46 import jdk.tools.jlink.plugin.PluginException; 47 import jdk.tools.jlink.internal.plugins.FileCopierPlugin; 48 49 /** 50 * A manager for pool of resources. 51 */ 52 public class ResourcePoolManager { 53 // utility to read ModuleDescriptor of the given ResourcePoolModule 54 static ModuleDescriptor readModuleDescriptor(ResourcePoolModule mod) { 55 String p = "/" + mod.name() + "/module-info.class"; 56 Optional<ResourcePoolEntry> content = mod.findEntry(p); 57 if (!content.isPresent()) { 58 throw new PluginException("No module-info for " + mod.name() 59 + " module"); 60 } 61 ByteBuffer bb = ByteBuffer.wrap(content.get().contentBytes()); 62 return ModuleDescriptor.read(bb); 63 } 64 65 class ResourcePoolModuleImpl implements ResourcePoolModule { 66 67 final Map<String, ResourcePoolEntry> moduleContent = new LinkedHashMap<>(); 83 if (!path.startsWith("/")) { 84 path = "/" + path; 85 } 86 if (!path.startsWith("/" + name + "/")) { 87 path = "/" + name + path; // path already starts with '/' 88 } 89 return Optional.ofNullable(moduleContent.get(path)); 90 } 91 92 @Override 93 public ModuleDescriptor descriptor() { 94 if (descriptor == null) { 95 descriptor = readModuleDescriptor(this); 96 } 97 return descriptor; 98 } 99 100 @Override 101 public Set<String> packages() { 102 Set<String> pkgs = new HashSet<>(); 103 moduleContent.values().stream().filter(m -> m.type(). 104 equals(ResourcePoolEntry.Type.CLASS_OR_RESOURCE)).forEach(res -> { 105 // Module metadata only contains packages with .class files 106 if (ImageFileCreator.isClassPackage(res.path())) { 107 String[] split = ImageFileCreator.splitPath(res.path()); 108 String pkg = split[1]; 109 if (pkg != null && !pkg.isEmpty()) { 110 pkgs.add(pkg); 111 } 112 } 113 }); 114 return pkgs; 115 } 116 117 @Override 118 public String toString() { 119 return name(); 120 } 121 122 @Override 123 public Stream<ResourcePoolEntry> entries() { 124 return moduleContent.values().stream(); 125 } 126 127 @Override 128 public int entryCount() { 129 return moduleContent.values().size(); | 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 package jdk.tools.jlink.internal; 26 27 import java.lang.module.ModuleDescriptor; 28 import java.nio.ByteBuffer; 29 import java.nio.ByteOrder; 30 import java.util.HashSet; 31 import java.util.LinkedHashMap; 32 import java.util.Map; 33 import java.util.Objects; 34 import java.util.Optional; 35 import java.util.Set; 36 import java.util.stream.Stream; 37 import jdk.internal.jimage.decompressor.CompressedResourceHeader; 38 import jdk.tools.jlink.plugin.ResourcePool; 39 import jdk.tools.jlink.plugin.ResourcePoolBuilder; 40 import jdk.tools.jlink.plugin.ResourcePoolEntry; 41 import jdk.tools.jlink.plugin.ResourcePoolModule; 42 import jdk.tools.jlink.plugin.ResourcePoolModuleView; 43 import jdk.tools.jlink.plugin.PluginException; 44 45 /** 46 * A manager for pool of resources. 47 */ 48 public class ResourcePoolManager { 49 // utility to read ModuleDescriptor of the given ResourcePoolModule 50 static ModuleDescriptor readModuleDescriptor(ResourcePoolModule mod) { 51 String p = "/" + mod.name() + "/module-info.class"; 52 Optional<ResourcePoolEntry> content = mod.findEntry(p); 53 if (!content.isPresent()) { 54 throw new PluginException("No module-info for " + mod.name() 55 + " module"); 56 } 57 ByteBuffer bb = ByteBuffer.wrap(content.get().contentBytes()); 58 return ModuleDescriptor.read(bb); 59 } 60 61 class ResourcePoolModuleImpl implements ResourcePoolModule { 62 63 final Map<String, ResourcePoolEntry> moduleContent = new LinkedHashMap<>(); 79 if (!path.startsWith("/")) { 80 path = "/" + path; 81 } 82 if (!path.startsWith("/" + name + "/")) { 83 path = "/" + name + path; // path already starts with '/' 84 } 85 return Optional.ofNullable(moduleContent.get(path)); 86 } 87 88 @Override 89 public ModuleDescriptor descriptor() { 90 if (descriptor == null) { 91 descriptor = readModuleDescriptor(this); 92 } 93 return descriptor; 94 } 95 96 @Override 97 public Set<String> packages() { 98 Set<String> pkgs = new HashSet<>(); 99 moduleContent.values().stream() 100 .filter(m -> m.type() == ResourcePoolEntry.Type.CLASS_OR_RESOURCE) 101 .forEach(res -> { 102 String name = ImageFileCreator.resourceName(res.path()); 103 if (name.endsWith(".class") && !name.endsWith("module-info.class")) { 104 String pkg = ImageFileCreator.toPackage(name); 105 if (!pkg.isEmpty()) { 106 pkgs.add(pkg); 107 } 108 } 109 }); 110 return pkgs; 111 } 112 113 @Override 114 public String toString() { 115 return name(); 116 } 117 118 @Override 119 public Stream<ResourcePoolEntry> entries() { 120 return moduleContent.values().stream(); 121 } 122 123 @Override 124 public int entryCount() { 125 return moduleContent.values().size(); |