1 /* 2 * Copyright (c) 2015, 2018, 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.jpackager.internal; 27 28 import java.io.BufferedReader; 29 import java.io.File; 30 import java.io.IOException; 31 import java.io.InputStream; 32 import java.io.InputStreamReader; 33 import java.io.UnsupportedEncodingException; 34 import java.util.Optional; 35 import java.lang.module.ModuleDescriptor; 36 import java.lang.module.ModuleFinder; 37 import java.lang.module.ModuleReference; 38 import java.lang.module.ModuleReader; 39 import java.nio.file.Path; 40 import java.util.HashSet; 41 import java.util.LinkedHashSet; 42 import java.util.List; 43 import java.util.NoSuchElementException; 44 import java.util.Set; 45 46 import jdk.tools.jlink.internal.packager.AppRuntimeImageBuilder; 47 48 public final class RedistributableModules { 49 private static final String JDK_JPACKAGER_MODULE = "jdk.jpackager"; 50 51 private RedistributableModules() {} 52 53 public static String stripComments(String line) { 54 String result = line.trim(); 55 int i = result.indexOf(";"); 56 57 if (i >= 0) { 58 result = result.substring(0, i); 59 result = result.trim(); 60 } 61 62 return result; 63 } 64 65 public static Set<String> getRedistributableModules(List<Path> modulePath, 66 String filename) { 67 Set<String> result = null; 68 69 Set<String> addModules = new HashSet<>(); 70 Set<String> limitModules = new HashSet<>(); 71 ModuleFinder finder = AppRuntimeImageBuilder.moduleFinder( 72 modulePath, addModules, limitModules); 73 Optional<ModuleReference> mref = finder.find(JDK_JPACKAGER_MODULE); 74 75 if (mref.isPresent()) { 76 ModuleReader reader = null; 77 78 try { 79 reader = mref.get().open(); 80 } catch (NoSuchElementException | IOException ex) { 81 } 82 83 if (reader != null) { 84 Optional<InputStream> stream = null; 85 86 try { 87 stream = reader.open(filename); 88 } catch (IOException ex) { 89 } 90 91 if (stream != null) { 92 if (stream.isPresent()) { 93 BufferedReader br = null; 94 95 try { 96 br = new BufferedReader(new InputStreamReader( 97 stream.get(), "UTF-8")); 98 } catch (UnsupportedEncodingException ex) { 99 } 100 101 if (br != null) { 102 result = new LinkedHashSet<String>(); 103 String line; 104 105 try { 106 while ((line = br.readLine()) != null) { 107 String module = stripComments(line); 108 109 if (!module.isEmpty()) { 110 result.add(module); 111 } 112 } 113 } catch (IOException ex) { 114 } 115 } 116 } 117 } 118 } 119 } 120 121 return result; 122 } 123 124 public static String getModuleVersion(File moduleFile, 125 List<Path> modulePath, Set<String> addModules, 126 Set<String> limitModules) { 127 String result = ""; 128 129 ModFile modFile = new ModFile(moduleFile); 130 ModuleFinder finder = AppRuntimeImageBuilder.moduleFinder(modulePath, 131 addModules, limitModules); 132 Optional<ModuleReference> mref = finder.find(modFile.getModName()); 133 134 if (mref.isPresent()) { 135 ModuleDescriptor descriptor = mref.get().descriptor(); 136 137 if (descriptor != null) { 138 Optional<ModuleDescriptor.Version> version = 139 descriptor.version(); 140 141 if (version.isPresent()) { 142 result = version.get().toString(); 143 } 144 } 145 } 146 147 return result; 148 } 149 }