< prev index next >

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

Print this page




  78         private static List<String> createArgs(Path home) {
  79             Objects.requireNonNull(home);
  80             List<String> javaArgs = new ArrayList<>();
  81             javaArgs.add(home.resolve("bin").
  82                     resolve(getJavaProcessName()).toString());
  83             return javaArgs;
  84         }
  85 
  86         @Override
  87         public void storeLaunchArgs(List<String> args) {
  88             try {
  89                 patchScripts(this, args);
  90             } catch (IOException ex) {
  91                 throw new UncheckedIOException(ex);
  92             }
  93         }
  94     }
  95 
  96     private final Path root;
  97     private final Path mdir;
  98     private final boolean genBom;
  99     private final Set<String> modules = new HashSet<>();
 100 
 101     /**
 102      * Default image builder constructor.
 103      *
 104      * @param genBom true, generates a bom file.
 105      * @param root The image root directory.
 106      * @throws IOException
 107      */
 108     public DefaultImageBuilder(boolean genBom, Path root) throws IOException {
 109         Objects.requireNonNull(root);
 110 
 111         this.genBom = genBom;
 112 
 113         this.root = root;
 114         this.mdir = root.resolve("lib");
 115         Files.createDirectories(mdir);
 116     }
 117 
 118     private void storeFiles(Set<String> modules, String bom, Properties release) throws IOException {
 119         if (release != null) {
 120             addModules(release, modules);
 121             File r = new File(root.toFile(), "release");
 122             try (FileOutputStream fo = new FileOutputStream(r)) {
 123                 release.store(fo, null);
 124             }
 125         }
 126         // Generate bom
 127         if (genBom) {
 128             File bomFile = new File(root.toFile(), "bom");
 129             createUtf8File(bomFile, bom);
 130         }
 131     }
 132 
 133     private void addModules(Properties release, Set<String> modules) throws IOException {
 134         StringBuilder builder = new StringBuilder();
 135         int i = 0;
 136         for (String m : modules) {
 137             builder.append(m);
 138             if (i < modules.size() - 1) {
 139                 builder.append(",");
 140             }
 141             i++;
 142         }
 143         release.setProperty("MODULES", builder.toString());
 144     }
 145 
 146     @Override
 147     public void storeFiles(Pool files, String bom, Properties release) {
 148         try {
 149             for (ModuleData f : files.getContent()) {
 150                if (!f.getType().equals(Pool.ModuleDataType.CLASS_OR_RESOURCE)) {
 151                     accept(f);
 152                 }
 153             }
 154             for (Module m : files.getModules()) {
 155                 // Only add modules that contain packages
 156                 if (!m.getAllPackages().isEmpty()) {
 157                     // Skip the fake module used by FileCopierPlugin when copying files.
 158                     if (m.getName().equals(FileCopierPlugin.FAKE_MODULE)) {
 159                        continue;
 160                     }
 161                     modules.add(m.getName());
 162                 }
 163             }
 164             storeFiles(modules, bom, release);
 165 
 166             if (Files.getFileStore(root).supportsFileAttributeView(PosixFileAttributeView.class)) {
 167                 // launchers in the bin directory need execute permission
 168                 Path bin = root.resolve("bin");
 169                 if (Files.isDirectory(bin)) {
 170                     Files.list(bin)
 171                             .filter(f -> !f.toString().endsWith(".diz"))
 172                             .filter(f -> Files.isRegularFile(f))
 173                             .forEach(this::setExecutable);
 174                 }
 175 
 176                 // jspawnhelper is in lib or lib/<arch>
 177                 Path lib = root.resolve("lib");
 178                 if (Files.isDirectory(lib)) {
 179                     Files.find(lib, 2, (path, attrs) -> {
 180                         return path.getFileName().toString().equals("jspawnhelper") ||
 181                                path.getFileName().toString().equals("jexec");
 182                     }).forEach(this::setExecutable);
 183                 }
 184             }
 185 
 186             prepareApplicationFiles(files, modules);
 187         } catch (IOException ex) {
 188             throw new PluginException(ex);
 189         }
 190     }
 191 
 192     @Override
 193     public void storeFiles(Pool files, String bom) {
 194         storeFiles(files, bom, new Properties());
 195     }
 196 
 197     /**
 198      * Generates launcher scripts.
 199      * @param imageContent The image content.
 200      * @param modules The set of modules that the runtime image contains.
 201      * @throws IOException
 202      */
 203     protected void prepareApplicationFiles(Pool imageContent, Set<String> modules) throws IOException {
 204         // generate launch scripts for the modules with a main class
 205         for (String module : modules) {
 206             String path = "/" + module + "/module-info.class";
 207             ModuleData res = imageContent.get(path);
 208             if (res == null) {
 209                 throw new IOException("module-info.class not found for " + module + " module");
 210             }
 211             Optional<String> mainClass;
 212             ByteArrayInputStream stream = new ByteArrayInputStream(res.getBytes());
 213             mainClass = ModuleDescriptor.read(stream).mainClass();
 214             if (mainClass.isPresent()) {




  78         private static List<String> createArgs(Path home) {
  79             Objects.requireNonNull(home);
  80             List<String> javaArgs = new ArrayList<>();
  81             javaArgs.add(home.resolve("bin").
  82                     resolve(getJavaProcessName()).toString());
  83             return javaArgs;
  84         }
  85 
  86         @Override
  87         public void storeLaunchArgs(List<String> args) {
  88             try {
  89                 patchScripts(this, args);
  90             } catch (IOException ex) {
  91                 throw new UncheckedIOException(ex);
  92             }
  93         }
  94     }
  95 
  96     private final Path root;
  97     private final Path mdir;

  98     private final Set<String> modules = new HashSet<>();
  99 
 100     /**
 101      * Default image builder constructor.
 102      *

 103      * @param root The image root directory.
 104      * @throws IOException
 105      */
 106     public DefaultImageBuilder(Path root) throws IOException {
 107         Objects.requireNonNull(root);
 108 


 109         this.root = root;
 110         this.mdir = root.resolve("lib");
 111         Files.createDirectories(mdir);
 112     }
 113 
 114     private void storeFiles(Set<String> modules, Properties release) throws IOException {
 115         if (release != null) {
 116             addModules(release, modules);
 117             File r = new File(root.toFile(), "release");
 118             try (FileOutputStream fo = new FileOutputStream(r)) {
 119                 release.store(fo, null);
 120             }
 121         }





 122     }
 123 
 124     private void addModules(Properties release, Set<String> modules) throws IOException {
 125         StringBuilder builder = new StringBuilder();
 126         int i = 0;
 127         for (String m : modules) {
 128             builder.append(m);
 129             if (i < modules.size() - 1) {
 130                 builder.append(",");
 131             }
 132             i++;
 133         }
 134         release.setProperty("MODULES", builder.toString());
 135     }
 136 
 137     @Override
 138     public void storeFiles(Pool files, Properties release) {
 139         try {
 140             for (ModuleData f : files.getContent()) {
 141                if (!f.getType().equals(Pool.ModuleDataType.CLASS_OR_RESOURCE)) {
 142                     accept(f);
 143                 }
 144             }
 145             for (Module m : files.getModules()) {
 146                 // Only add modules that contain packages
 147                 if (!m.getAllPackages().isEmpty()) {
 148                     // Skip the fake module used by FileCopierPlugin when copying files.
 149                     if (m.getName().equals(FileCopierPlugin.FAKE_MODULE)) {
 150                        continue;
 151                     }
 152                     modules.add(m.getName());
 153                 }
 154             }
 155             storeFiles(modules, release);
 156 
 157             if (Files.getFileStore(root).supportsFileAttributeView(PosixFileAttributeView.class)) {
 158                 // launchers in the bin directory need execute permission
 159                 Path bin = root.resolve("bin");
 160                 if (Files.isDirectory(bin)) {
 161                     Files.list(bin)
 162                             .filter(f -> !f.toString().endsWith(".diz"))
 163                             .filter(f -> Files.isRegularFile(f))
 164                             .forEach(this::setExecutable);
 165                 }
 166 
 167                 // jspawnhelper is in lib or lib/<arch>
 168                 Path lib = root.resolve("lib");
 169                 if (Files.isDirectory(lib)) {
 170                     Files.find(lib, 2, (path, attrs) -> {
 171                         return path.getFileName().toString().equals("jspawnhelper") ||
 172                                path.getFileName().toString().equals("jexec");
 173                     }).forEach(this::setExecutable);
 174                 }
 175             }
 176 
 177             prepareApplicationFiles(files, modules);
 178         } catch (IOException ex) {
 179             throw new PluginException(ex);
 180         }
 181     }
 182 
 183     @Override
 184     public void storeFiles(Pool files) {
 185         storeFiles(files, new Properties());
 186     }
 187 
 188     /**
 189      * Generates launcher scripts.
 190      * @param imageContent The image content.
 191      * @param modules The set of modules that the runtime image contains.
 192      * @throws IOException
 193      */
 194     protected void prepareApplicationFiles(Pool imageContent, Set<String> modules) throws IOException {
 195         // generate launch scripts for the modules with a main class
 196         for (String module : modules) {
 197             String path = "/" + module + "/module-info.class";
 198             ModuleData res = imageContent.get(path);
 199             if (res == null) {
 200                 throw new IOException("module-info.class not found for " + module + " module");
 201             }
 202             Optional<String> mainClass;
 203             ByteArrayInputStream stream = new ByteArrayInputStream(res.getBytes());
 204             mainClass = ModuleDescriptor.read(stream).mainClass();
 205             if (mainClass.isPresent()) {


< prev index next >