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.Configuration; 28 import java.lang.module.ModuleFinder; 29 import java.nio.ByteOrder; 30 import java.nio.file.Path; 31 import java.util.ArrayList; 32 import java.util.Collections; 33 import java.util.List; 34 import java.util.Map; 35 import java.util.Objects; 36 import java.util.Set; 37 38 import jdk.internal.module.ModulePath; 39 import jdk.tools.jlink.plugin.Plugin; 40 import jdk.tools.jlink.plugin.PluginException; 41 import jdk.tools.jlink.builder.ImageBuilder; 42 43 /** 44 * API to call jlink. 45 */ 46 public final class Jlink { 47 48 /** 49 * Create a plugin. 50 * 129 @Override 130 public String toString() { 131 StringBuilder builder = new StringBuilder(); 132 builder.append("imagebuilder=").append(imageBuilder).append("\n"); 133 StringBuilder pluginsBuilder = new StringBuilder(); 134 for (Plugin p : plugins) { 135 pluginsBuilder.append(p).append(","); 136 } 137 builder.append("plugins=").append(pluginsBuilder).append("\n"); 138 builder.append("lastsorter=").append(lastSorterPluginName).append("\n"); 139 140 return builder.toString(); 141 } 142 } 143 144 /** 145 * Jlink configuration. Instances of this class are used to configure jlink. 146 */ 147 public static final class JlinkConfiguration { 148 149 private final List<Path> modulepaths; 150 private final Path output; 151 private final Set<String> modules; 152 private final Set<String> limitmods; 153 private final ByteOrder endian; 154 private final ModuleFinder finder; 155 156 /** 157 * jlink configuration, 158 * 159 * @param output Output directory, must not exist. 160 * @param modulepaths Modules paths 161 * @param modules The possibly-empty set of root modules to resolve 162 * @param limitmods Limit the universe of observable modules 163 * @param endian Jimage byte order. Native order by default 164 */ 165 public JlinkConfiguration(Path output, 166 List<Path> modulepaths, 167 Set<String> modules, 168 Set<String> limitmods, 169 ByteOrder endian) { 170 if (Objects.requireNonNull(modulepaths).isEmpty()) { 171 throw new IllegalArgumentException("Empty module path"); 172 } 173 174 this.output = output; 175 this.modulepaths = modulepaths; 176 this.modules = Objects.requireNonNull(modules); 177 this.limitmods = Objects.requireNonNull(limitmods); 178 this.endian = Objects.requireNonNull(endian); 179 this.finder = moduleFinder(); 180 } 181 182 /** 183 * @return the modulepaths 184 */ 185 public List<Path> getModulepaths() { 186 return modulepaths; 187 } 188 189 /** 190 * @return the byte ordering 191 */ 192 public ByteOrder getByteOrder() { 193 return endian; 194 } 195 196 /** 197 * @return the output 198 */ 199 public Path getOutput() { 200 return output; 201 } 202 203 /** 204 * @return the modules 205 */ 206 public Set<String> getModules() { 207 return modules; 208 } 209 210 /** 211 * @return the limitmods 212 */ 213 public Set<String> getLimitmods() { 214 return limitmods; 215 } 216 217 /** 218 * Returns {@link ModuleFinder} that finds all observable modules 219 * for this jlink configuration. 220 */ 221 public ModuleFinder finder() { 222 return finder; 223 } 224 225 /** 226 * Returns a {@link Configuration} of the given module path, 227 * root modules with full service binding. 228 */ 229 public Configuration resolveAndBind() 230 { 231 return Configuration.empty().resolveAndBind(finder, 232 ModuleFinder.of(), 233 modules); 234 } 235 236 /** 237 * Returns a {@link Configuration} of the given module path, 238 * root modules with no service binding. 239 */ 240 public Configuration resolve() 241 { 242 return Configuration.empty().resolve(finder, 243 ModuleFinder.of(), 244 modules); 245 } 246 247 private ModuleFinder moduleFinder() { 248 Path[] entries = modulepaths.toArray(new Path[0]); 249 ModuleFinder finder = ModulePath.of(Runtime.version(), true, entries); 250 if (!limitmods.isEmpty()) { 251 finder = JlinkTask.limitFinder(finder, limitmods, modules); 252 } 253 return finder; 254 } 255 256 @Override 257 public String toString() { 258 StringBuilder builder = new StringBuilder(); 259 260 builder.append("output=").append(output).append("\n"); 261 StringBuilder pathsBuilder = new StringBuilder(); 262 for (Path p : modulepaths) { 263 pathsBuilder.append(p).append(","); 264 } 265 builder.append("modulepaths=").append(pathsBuilder).append("\n"); 266 267 StringBuilder modsBuilder = new StringBuilder(); 268 for (String p : modules) { 269 modsBuilder.append(p).append(","); 270 } 271 builder.append("modules=").append(modsBuilder).append("\n"); 272 273 StringBuilder limitsBuilder = new StringBuilder(); 274 for (String p : limitmods) { 275 limitsBuilder.append(p).append(","); 276 } 277 builder.append("limitmodules=").append(limitsBuilder).append("\n"); 278 builder.append("endian=").append(endian).append("\n"); 279 return builder.toString(); 280 } 281 } 282 283 /** 284 * Jlink instance constructor, if a security manager is set, the jlink 285 * permission is checked. 286 */ 287 public Jlink() { 288 if (System.getSecurityManager() != null) { 289 System.getSecurityManager(). 290 checkPermission(new JlinkPermission("jlink")); 291 } 292 } 293 294 /** 295 * Build the image. 296 * 297 * @param config Jlink config, must not be null. | 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.Configuration; 28 import java.lang.module.ModuleFinder; 29 import java.nio.ByteOrder; 30 import java.nio.file.Files; 31 import java.nio.file.Path; 32 import java.nio.file.Paths; 33 import java.util.ArrayList; 34 import java.util.Collections; 35 import java.util.List; 36 import java.util.Map; 37 import java.util.Objects; 38 import java.util.Set; 39 40 import jdk.internal.module.ModulePath; 41 import jdk.tools.jlink.plugin.Plugin; 42 import jdk.tools.jlink.plugin.PluginException; 43 import jdk.tools.jlink.builder.ImageBuilder; 44 45 /** 46 * API to call jlink. 47 */ 48 public final class Jlink { 49 50 /** 51 * Create a plugin. 52 * 131 @Override 132 public String toString() { 133 StringBuilder builder = new StringBuilder(); 134 builder.append("imagebuilder=").append(imageBuilder).append("\n"); 135 StringBuilder pluginsBuilder = new StringBuilder(); 136 for (Plugin p : plugins) { 137 pluginsBuilder.append(p).append(","); 138 } 139 builder.append("plugins=").append(pluginsBuilder).append("\n"); 140 builder.append("lastsorter=").append(lastSorterPluginName).append("\n"); 141 142 return builder.toString(); 143 } 144 } 145 146 /** 147 * Jlink configuration. Instances of this class are used to configure jlink. 148 */ 149 public static final class JlinkConfiguration { 150 151 private final Path output; 152 private final Set<String> modules; 153 private final ByteOrder endian; 154 private final ModuleFinder finder; 155 156 /** 157 * jlink configuration, 158 * 159 * @param output Output directory, must not exist. 160 * @param modules The possibly-empty set of root modules to resolve 161 * @param endian Jimage byte order. Native order by default 162 * @param finder the ModuleFinder for this configuration 163 */ 164 public JlinkConfiguration(Path output, 165 Set<String> modules, 166 ByteOrder endian, 167 ModuleFinder finder) { 168 this.output = output; 169 this.modules = Objects.requireNonNull(modules); 170 this.endian = Objects.requireNonNull(endian); 171 this.finder = finder; 172 } 173 174 /** 175 * @return the byte ordering 176 */ 177 public ByteOrder getByteOrder() { 178 return endian; 179 } 180 181 /** 182 * @return the output 183 */ 184 public Path getOutput() { 185 return output; 186 } 187 188 /** 189 * @return the modules 190 */ 191 public Set<String> getModules() { 192 return modules; 193 } 194 195 /** 196 * Returns {@link ModuleFinder} that finds all observable modules 197 * for this jlink configuration. 198 */ 199 public ModuleFinder finder() { 200 return finder; 201 } 202 203 /** 204 * Returns a {@link Configuration} of the given module path, 205 * root modules with full service binding. 206 */ 207 public Configuration resolveAndBind() 208 { 209 return Configuration.empty().resolveAndBind(finder, 210 ModuleFinder.of(), 211 modules); 212 } 213 214 /** 215 * Returns a {@link Configuration} of the given module path, 216 * root modules with no service binding. 217 */ 218 public Configuration resolve() 219 { 220 return Configuration.empty().resolve(finder, 221 ModuleFinder.of(), 222 modules); 223 } 224 225 @Override 226 public String toString() { 227 StringBuilder builder = new StringBuilder(); 228 229 builder.append("output=").append(output).append("\n"); 230 StringBuilder modsBuilder = new StringBuilder(); 231 for (String p : modules) { 232 modsBuilder.append(p).append(","); 233 } 234 builder.append("modules=").append(modsBuilder).append("\n"); 235 builder.append("endian=").append(endian).append("\n"); 236 return builder.toString(); 237 } 238 } 239 240 /** 241 * Jlink instance constructor, if a security manager is set, the jlink 242 * permission is checked. 243 */ 244 public Jlink() { 245 if (System.getSecurityManager() != null) { 246 System.getSecurityManager(). 247 checkPermission(new JlinkPermission("jlink")); 248 } 249 } 250 251 /** 252 * Build the image. 253 * 254 * @param config Jlink config, must not be null. |