< prev index next >

jdk/src/jdk.jlink/share/classes/jdk/tools/jlink/builder/DefaultImageBuilder.java

Print this page


   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


  42 import java.nio.charset.StandardCharsets;
  43 import java.nio.file.FileAlreadyExistsException;
  44 import java.nio.file.Files;
  45 import java.nio.file.Path;
  46 import java.nio.file.Paths;
  47 import java.nio.file.StandardOpenOption;
  48 import java.nio.file.attribute.PosixFilePermission;
  49 import java.util.Collections;
  50 import java.util.HashMap;
  51 import java.util.HashSet;
  52 import java.util.List;
  53 import java.util.Map;
  54 import java.util.Objects;
  55 import java.util.Optional;
  56 import java.util.Properties;
  57 import java.util.Set;
  58 import static java.util.stream.Collectors.*;
  59 
  60 import jdk.tools.jlink.internal.BasicImageWriter;
  61 import jdk.tools.jlink.internal.ExecutableImage;

  62 import jdk.tools.jlink.plugin.ResourcePool;
  63 import jdk.tools.jlink.plugin.ResourcePoolEntry;
  64 import jdk.tools.jlink.plugin.ResourcePoolEntry.Type;
  65 import jdk.tools.jlink.plugin.ResourcePoolModule;
  66 import jdk.tools.jlink.plugin.PluginException;
  67 
  68 /**
  69  *
  70  * Default Image Builder. This builder creates the default runtime image layout.
  71  */
  72 public final class DefaultImageBuilder implements ImageBuilder {
  73     // Top-level directory names in a modular runtime image
  74     public static final String BIN_DIRNAME      = "bin";
  75     public static final String CONF_DIRNAME     = "conf";
  76     public static final String INCLUDE_DIRNAME  = "include";
  77     public static final String LIB_DIRNAME      = "lib";
  78     public static final String LEGAL_DIRNAME    = "legal";
  79     public static final String MAN_DIRNAME      = "man";
  80 
  81     /**
  82      * The default java executable Image.
  83      */
  84     static final class DefaultExecutableImage implements ExecutableImage {
  85 


 116 
 117         @Override
 118         public List<String> getExecutionArgs() {
 119             return args;
 120         }
 121 
 122         @Override
 123         public void storeLaunchArgs(List<String> args) {
 124             try {
 125                 patchScripts(this, args);
 126             } catch (IOException ex) {
 127                 throw new UncheckedIOException(ex);
 128             }
 129         }
 130     }
 131 
 132     private final Path root;
 133     private final Map<String, String> launchers;
 134     private final Path mdir;
 135     private final Set<String> modules = new HashSet<>();
 136     private String targetOsName;
 137 
 138     /**
 139      * Default image builder constructor.
 140      *
 141      * @param root The image root directory.
 142      * @throws IOException
 143      */
 144     public DefaultImageBuilder(Path root, Map<String, String> launchers) throws IOException {
 145         this.root = Objects.requireNonNull(root);
 146         this.launchers = Objects.requireNonNull(launchers);
 147         this.mdir = root.resolve("lib");
 148         Files.createDirectories(mdir);
 149     }
 150 
 151     @Override
 152     public void storeFiles(ResourcePool files) {
 153         try {
 154             this.targetOsName = files.moduleView().
 155                 findModule("java.base").get().osName();
 156             if (this.targetOsName == null) {

 157                 throw new PluginException("ModuleTarget attribute is missing for java.base module");
 158             }

 159 
 160             checkResourcePool(files);
 161 
 162             Path bin = root.resolve(BIN_DIRNAME);
 163 
 164             // write non-classes resource files to the image
 165             files.entries()
 166                 .filter(f -> f.type() != ResourcePoolEntry.Type.CLASS_OR_RESOURCE)
 167                 .forEach(f -> {
 168                     try {
 169                         accept(f);
 170                     } catch (FileAlreadyExistsException e) {
 171                         // Should not happen! Duplicates checking already done!
 172                         throw new AssertionError("Duplicate entry!", e);
 173                     } catch (IOException ioExp) {
 174                         throw new UncheckedIOException(ioExp);
 175                     }
 176                 });
 177 
 178             files.moduleView().modules().forEach(m -> {


 459             try (BufferedWriter writer = Files.newBufferedWriter(dstFile)) {
 460                 writer.write(String.format("Please see %s%n", target.toString()));
 461             }
 462         }
 463     }
 464 
 465     private String nativeDir(String filename) {
 466         if (isWindows()) {
 467             if (filename.endsWith(".dll") || filename.endsWith(".diz")
 468                     || filename.endsWith(".pdb") || filename.endsWith(".map")) {
 469                 return BIN_DIRNAME;
 470             } else {
 471                 return LIB_DIRNAME;
 472             }
 473         } else {
 474             return LIB_DIRNAME;
 475         }
 476     }
 477 
 478     private boolean isWindows() {
 479         return targetOsName.startsWith("Windows");
 480     }
 481 
 482     /**
 483      * chmod ugo+x file
 484      */
 485     private void setExecutable(Path file) {
 486         try {
 487             Set<PosixFilePermission> perms = Files.getPosixFilePermissions(file);
 488             perms.add(PosixFilePermission.OWNER_EXECUTE);
 489             perms.add(PosixFilePermission.GROUP_EXECUTE);
 490             perms.add(PosixFilePermission.OTHERS_EXECUTE);
 491             Files.setPosixFilePermissions(file, perms);
 492         } catch (IOException ioe) {
 493             throw new UncheckedIOException(ioe);
 494         }
 495     }
 496 
 497     /**
 498      * chmod ugo-w file
 499      */


   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


  42 import java.nio.charset.StandardCharsets;
  43 import java.nio.file.FileAlreadyExistsException;
  44 import java.nio.file.Files;
  45 import java.nio.file.Path;
  46 import java.nio.file.Paths;
  47 import java.nio.file.StandardOpenOption;
  48 import java.nio.file.attribute.PosixFilePermission;
  49 import java.util.Collections;
  50 import java.util.HashMap;
  51 import java.util.HashSet;
  52 import java.util.List;
  53 import java.util.Map;
  54 import java.util.Objects;
  55 import java.util.Optional;
  56 import java.util.Properties;
  57 import java.util.Set;
  58 import static java.util.stream.Collectors.*;
  59 
  60 import jdk.tools.jlink.internal.BasicImageWriter;
  61 import jdk.tools.jlink.internal.ExecutableImage;
  62 import jdk.tools.jlink.internal.Platform;
  63 import jdk.tools.jlink.plugin.ResourcePool;
  64 import jdk.tools.jlink.plugin.ResourcePoolEntry;
  65 import jdk.tools.jlink.plugin.ResourcePoolEntry.Type;

  66 import jdk.tools.jlink.plugin.PluginException;
  67 
  68 /**
  69  *
  70  * Default Image Builder. This builder creates the default runtime image layout.
  71  */
  72 public final class DefaultImageBuilder implements ImageBuilder {
  73     // Top-level directory names in a modular runtime image
  74     public static final String BIN_DIRNAME      = "bin";
  75     public static final String CONF_DIRNAME     = "conf";
  76     public static final String INCLUDE_DIRNAME  = "include";
  77     public static final String LIB_DIRNAME      = "lib";
  78     public static final String LEGAL_DIRNAME    = "legal";
  79     public static final String MAN_DIRNAME      = "man";
  80 
  81     /**
  82      * The default java executable Image.
  83      */
  84     static final class DefaultExecutableImage implements ExecutableImage {
  85 


 116 
 117         @Override
 118         public List<String> getExecutionArgs() {
 119             return args;
 120         }
 121 
 122         @Override
 123         public void storeLaunchArgs(List<String> args) {
 124             try {
 125                 patchScripts(this, args);
 126             } catch (IOException ex) {
 127                 throw new UncheckedIOException(ex);
 128             }
 129         }
 130     }
 131 
 132     private final Path root;
 133     private final Map<String, String> launchers;
 134     private final Path mdir;
 135     private final Set<String> modules = new HashSet<>();
 136     private Platform targetPlatform;
 137 
 138     /**
 139      * Default image builder constructor.
 140      *
 141      * @param root The image root directory.
 142      * @throws IOException
 143      */
 144     public DefaultImageBuilder(Path root, Map<String, String> launchers) throws IOException {
 145         this.root = Objects.requireNonNull(root);
 146         this.launchers = Objects.requireNonNull(launchers);
 147         this.mdir = root.resolve("lib");
 148         Files.createDirectories(mdir);
 149     }
 150 
 151     @Override
 152     public void storeFiles(ResourcePool files) {
 153         try {
 154             String targetOsName = files.moduleView()
 155                                        .findModule("java.base").get()
 156                                        .osName();
 157             if (targetOsName == null) {
 158                 throw new PluginException("ModuleTarget attribute is missing for java.base module");
 159             }
 160             this.targetPlatform = Platform.toPlatform(targetOsName);
 161 
 162             checkResourcePool(files);
 163 
 164             Path bin = root.resolve(BIN_DIRNAME);
 165 
 166             // write non-classes resource files to the image
 167             files.entries()
 168                 .filter(f -> f.type() != ResourcePoolEntry.Type.CLASS_OR_RESOURCE)
 169                 .forEach(f -> {
 170                     try {
 171                         accept(f);
 172                     } catch (FileAlreadyExistsException e) {
 173                         // Should not happen! Duplicates checking already done!
 174                         throw new AssertionError("Duplicate entry!", e);
 175                     } catch (IOException ioExp) {
 176                         throw new UncheckedIOException(ioExp);
 177                     }
 178                 });
 179 
 180             files.moduleView().modules().forEach(m -> {


 461             try (BufferedWriter writer = Files.newBufferedWriter(dstFile)) {
 462                 writer.write(String.format("Please see %s%n", target.toString()));
 463             }
 464         }
 465     }
 466 
 467     private String nativeDir(String filename) {
 468         if (isWindows()) {
 469             if (filename.endsWith(".dll") || filename.endsWith(".diz")
 470                     || filename.endsWith(".pdb") || filename.endsWith(".map")) {
 471                 return BIN_DIRNAME;
 472             } else {
 473                 return LIB_DIRNAME;
 474             }
 475         } else {
 476             return LIB_DIRNAME;
 477         }
 478     }
 479 
 480     private boolean isWindows() {
 481         return targetPlatform == Platform.WINDOWS;
 482     }
 483 
 484     /**
 485      * chmod ugo+x file
 486      */
 487     private void setExecutable(Path file) {
 488         try {
 489             Set<PosixFilePermission> perms = Files.getPosixFilePermissions(file);
 490             perms.add(PosixFilePermission.OWNER_EXECUTE);
 491             perms.add(PosixFilePermission.GROUP_EXECUTE);
 492             perms.add(PosixFilePermission.OTHERS_EXECUTE);
 493             Files.setPosixFilePermissions(file, perms);
 494         } catch (IOException ioe) {
 495             throw new UncheckedIOException(ioe);
 496         }
 497     }
 498 
 499     /**
 500      * chmod ugo-w file
 501      */


< prev index next >