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