1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423
|
/*
* Copyright (c) 2013, 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package toolbox;
import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOError;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URI;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;
import java.util.Set;
import java.util.jar.Attributes;
import java.util.jar.JarEntry;
import java.util.jar.JarOutputStream;
import java.util.jar.Manifest;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import javax.tools.FileObject;
import javax.tools.JavaFileManager;
import javax.tools.JavaFileObject;
import static toolbox.ToolBox.currDir;
/**
* A task to configure and run the jar file utility.
*/
public class JarTask extends AbstractTask<JarTask> {
private Path jar;
private Manifest manifest;
private String classpath;
private String mainClass;
private Path baseDir;
private List<Path> paths;
private Set<FileObject> fileObjects;
/**
* Creates a task to write jar files, using API mode.
* @param toolBox the {@code ToolBox} to use
*/
public JarTask(ToolBox toolBox) {
super(toolBox, Task.Mode.API);
paths = Collections.emptyList();
fileObjects = new LinkedHashSet<>();
}
/**
* Creates a JarTask for use with a given jar file.
* @param toolBox the {@code ToolBox} to use
* @param path the file
*/
public JarTask(ToolBox toolBox, String path) {
this(toolBox);
jar = Paths.get(path);
}
/**
* Creates a JarTask for use with a given jar file.
* @param toolBox the {@code ToolBox} to use
* @param path the file
*/
public JarTask(ToolBox toolBox, Path path) {
this(toolBox);
jar = path;
}
/**
* Sets a manifest for the jar file.
* @param manifest the manifest
* @return this task object
*/
public JarTask manifest(Manifest manifest) {
this.manifest = manifest;
return this;
}
/**
* Sets a manifest for the jar file.
* @param manifest a string containing the contents of the manifest
* @return this task object
* @throws IOException if there is a problem creating the manifest
*/
public JarTask manifest(String manifest) throws IOException {
this.manifest = new Manifest(new ByteArrayInputStream(manifest.getBytes()));
return this;
}
/**
* Sets the classpath to be written to the {@code Class-Path}
* entry in the manifest.
* @param classpath the classpath
* @return this task object
*/
public JarTask classpath(String classpath) {
this.classpath = classpath;
return this;
}
/**
* Sets the class to be written to the {@code Main-Class}
* entry in the manifest..
* @param mainClass the name of the main class
* @return this task object
*/
public JarTask mainClass(String mainClass) {
this.mainClass = mainClass;
return this;
}
/**
* Sets the base directory for files to be written into the jar file.
* @param baseDir the base directory
* @return this task object
*/
public JarTask baseDir(String baseDir) {
this.baseDir = Paths.get(baseDir);
return this;
}
/**
* Sets the base directory for files to be written into the jar file.
* @param baseDir the base directory
* @return this task object
*/
public JarTask baseDir(Path baseDir) {
this.baseDir = baseDir;
return this;
}
/**
* Sets the files to be written into the jar file.
* @param files the files
* @return this task object
*/
public JarTask files(String... files) {
this.paths = Stream.of(files)
.map(file -> Paths.get(file))
.collect(Collectors.toList());
return this;
}
/**
* Adds a set of file objects to be written into the jar file, by copying them
* from a Location in a JavaFileManager.
* The file objects to be written are specified by a series of paths;
* each path can be in one of the following forms:
* <ul>
* <li>The name of a class. For example, java.lang.Object.
* In this case, the corresponding .class file will be written to the jar file.
* <li>the name of a package followed by {@code .*}. For example, {@code java.lang.*}.
* In this case, all the class files in the specified package will be written to
* the jar file.
* <li>the name of a package followed by {@code .**}. For example, {@code java.lang.**}.
* In this case, all the class files in the specified package, and any subpackages
* will be written to the jar file.
* </ul>
*
* @param fm the file manager in which to find the file objects
* @param l the location in which to find the file objects
* @param paths the paths specifying the file objects to be copied
* @return this task object
* @throws IOException if errors occur while determining the set of file objects
*/
public JarTask files(JavaFileManager fm, JavaFileManager.Location l, String... paths)
throws IOException {
for (String p : paths) {
if (p.endsWith(".**"))
addPackage(fm, l, p.substring(0, p.length() - 3), true);
else if (p.endsWith(".*"))
addPackage(fm, l, p.substring(0, p.length() - 2), false);
else
addFile(fm, l, p);
}
return this;
}
private void addPackage(JavaFileManager fm, JavaFileManager.Location l, String pkg, boolean recurse)
throws IOException {
for (JavaFileObject fo : fm.list(l, pkg, EnumSet.allOf(JavaFileObject.Kind.class), recurse)) {
fileObjects.add(fo);
}
}
private void addFile(JavaFileManager fm, JavaFileManager.Location l, String path) throws IOException {
JavaFileObject fo = fm.getJavaFileForInput(l, path, JavaFileObject.Kind.CLASS);
fileObjects.add(fo);
}
/**
* Provides limited jar command-like functionality.
* The supported commands are:
* <ul>
* <li> jar cf jarfile -C dir files...
* <li> jar cfm jarfile manifestfile -C dir files...
* </ul>
* Any values specified by other configuration methods will be ignored.
* @param args arguments in the style of those for the jar command
* @return a Result object containing the results of running the task
*/
public Task.Result run(String... args) {
if (args.length < 2)
throw new IllegalArgumentException();
ListIterator<String> iter = Arrays.asList(args).listIterator();
String first = iter.next();
switch (first) {
case "cf":
jar = Paths.get(iter.next());
break;
case "cfm":
jar = Paths.get(iter.next());
try (InputStream in = Files.newInputStream(Paths.get(iter.next()))) {
manifest = new Manifest(in);
} catch (IOException e) {
throw new IOError(e);
}
break;
}
if (iter.hasNext()) {
if (iter.next().equals("-C"))
baseDir = Paths.get(iter.next());
else
iter.previous();
}
paths = new ArrayList<>();
while (iter.hasNext())
paths.add(Paths.get(iter.next()));
return run();
}
/**
* {@inheritDoc}
* @return the name "jar"
*/
@Override
public String name() {
return "jar";
}
/**
* Creates a jar file with the arguments as currently configured.
* @return a Result object indicating the outcome of the compilation
* and the content of any output written to stdout, stderr, or the
* main stream by the compiler.
* @throws TaskError if the outcome of the task is not as expected.
*/
@Override
public Task.Result run() {
Manifest m = (manifest == null) ? new Manifest() : manifest;
Attributes mainAttrs = m.getMainAttributes();
if (mainClass != null)
mainAttrs.put(Attributes.Name.MAIN_CLASS, mainClass);
if (classpath != null)
mainAttrs.put(Attributes.Name.CLASS_PATH, classpath);
AbstractTask.StreamOutput sysOut = new AbstractTask.StreamOutput(System.out, System::setOut);
AbstractTask.StreamOutput sysErr = new AbstractTask.StreamOutput(System.err, System::setErr);
Map<Task.OutputKind, String> outputMap = new HashMap<>();
try (OutputStream os = Files.newOutputStream(jar);
JarOutputStream jos = openJar(os, m)) {
writeFiles(jos);
writeFileObjects(jos);
} catch (IOException e) {
error("Exception while opening " + jar, e);
} finally {
outputMap.put(Task.OutputKind.STDOUT, sysOut.close());
outputMap.put(Task.OutputKind.STDERR, sysErr.close());
}
return checkExit(new Task.Result(toolBox, this, (errors == 0) ? 0 : 1, outputMap));
}
private JarOutputStream openJar(OutputStream os, Manifest m) throws IOException {
if (m == null || m.getMainAttributes().isEmpty() && m.getEntries().isEmpty()) {
return new JarOutputStream(os);
} else {
if (m.getMainAttributes().get(Attributes.Name.MANIFEST_VERSION) == null)
m.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0");
return new JarOutputStream(os, m);
}
}
private void writeFiles(JarOutputStream jos) throws IOException {
Path base = (baseDir == null) ? currDir : baseDir;
for (Path path : paths) {
Files.walkFileTree(base.resolve(path), new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
try {
String p = base.relativize(file)
.normalize()
.toString()
.replace(File.separatorChar, '/');
JarEntry e = new JarEntry(p);
jos.putNextEntry(e);
try {
jos.write(Files.readAllBytes(file));
} finally {
jos.closeEntry();
}
return FileVisitResult.CONTINUE;
} catch (IOException e) {
error("Exception while adding " + file + " to jar file", e);
return FileVisitResult.TERMINATE;
}
}
});
}
}
private void writeFileObjects(JarOutputStream jos) throws IOException {
for (FileObject fo : fileObjects) {
String p = guessPath(fo);
JarEntry e = new JarEntry(p);
jos.putNextEntry(e);
try {
byte[] buf = new byte[1024];
try (BufferedInputStream in = new BufferedInputStream(fo.openInputStream())) {
int n;
while ((n = in.read(buf)) > 0)
jos.write(buf, 0, n);
} catch (IOException ex) {
error("Exception while adding " + fo.getName() + " to jar file", ex);
}
} finally {
jos.closeEntry();
}
}
}
/*
* A jar: URL is of the form jar:URL!/<entry> where URL is a URL for the .jar file itself.
* In Symbol files (i.e. ct.sym) the underlying entry is prefixed META-INF/sym/<base>.
*/
private final Pattern jarEntry = Pattern.compile(".*!/(?:META-INF/sym/[^/]+/)?(.*)");
/*
* A jrt: URL is of the form jrt:/modules/<module>/<package>/<file>
*/
private final Pattern jrtEntry = Pattern.compile("/modules/([^/]+)/(.*)");
/*
* A file: URL is of the form file:/path/to/{modules,patches}/<module>/<package>/<file>
*/
private final Pattern fileEntry = Pattern.compile(".*/(?:modules|patches)/([^/]+)/(.*)");
private String guessPath(FileObject fo) {
URI u = fo.toUri();
switch (u.getScheme()) {
case "jar": {
Matcher m = jarEntry.matcher(u.getSchemeSpecificPart());
if (m.matches()) {
return m.group(1);
}
break;
}
case "jrt": {
Matcher m = jrtEntry.matcher(u.getSchemeSpecificPart());
if (m.matches()) {
return m.group(2);
}
break;
}
case "file": {
Matcher m = fileEntry.matcher(u.getSchemeSpecificPart());
if (m.matches()) {
return m.group(2);
}
break;
}
}
throw new IllegalArgumentException(fo.getName() + "--" + fo.toUri());
}
private void error(String message, Throwable t) {
toolBox.out.println("Error: " + message + ": " + t);
errors++;
}
private int errors;
}
|