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 
  26 package build.tools.module;
  27 
  28 import java.io.BufferedReader;
  29 import java.io.BufferedWriter;
  30 import java.io.IOException;
  31 import java.io.InputStream;
  32 import java.io.InputStreamReader;
  33 import java.io.PrintWriter;
  34 import java.nio.charset.StandardCharsets;
  35 import java.nio.file.Files;
  36 import java.nio.file.Path;
  37 import java.nio.file.Paths;
  38 import java.util.Arrays;
  39 import java.util.Set;
  40 import java.util.stream.Collectors;
  41 import java.util.stream.Stream;
  42 
  43 public class GenModuleLoaderMap {
  44     private static final String USAGE =
  45         "GenModuleLoaderMap -o <output-file> -boot m1[,m2]* -platform m3[,m4]* <original-source>";
  46 
  47     public static void main(String... args) throws Exception {
  48         // default set of boot modules and ext modules
  49         Stream<String> bootModules = Stream.empty();
  50         Stream<String> platformModules = Stream.empty();
  51         Path outfile = null;
  52         Path source = null;
  53         for (int i=0; i < args.length; i++) {
  54             String option = args[i];
  55             if (option.startsWith("-")) {
  56                 String arg = args[++i];
  57                 if (option.equals("-boot")) {
  58                     String[] mns = arg.split(",");
  59                     bootModules = Stream.concat(bootModules, Arrays.stream(mns));
  60                 } else if (option.equals("-platform")) {
  61                     String[] mns = arg.split(",");
  62                     platformModules = Stream.concat(platformModules, Arrays.stream(mns));
  63                 } else if (option.equals("-o")) {
  64                     outfile = Paths.get(arg);
  65                 } else {
  66                     throw new IllegalArgumentException("invalid option: " + option);
  67                 }
  68             } else {
  69                 source = Paths.get(option);
  70             }
  71         }
  72 
  73         if (outfile == null) {
  74             throw new IllegalArgumentException("-o must be specified");
  75         }
  76         if (Files.notExists(source)) {
  77             throw new IllegalArgumentException(source + " not exist");
  78         }
  79 
  80         try (BufferedWriter bw = Files.newBufferedWriter(outfile, StandardCharsets.UTF_8);
  81              PrintWriter writer = new PrintWriter(bw)) {
  82             for (String line : Files.readAllLines(source)) {
  83                 if (line.contains("@@BOOT_MODULE_NAMES@@")) {
  84                     line = patch(line, "@@BOOT_MODULE_NAMES@@", bootModules);
  85                 } else if (line.contains("@@PLATFORM_MODULE_NAMES@@")) {
  86                     line = patch(line, "@@PLATFORM_MODULE_NAMES@@", platformModules);
  87                 }
  88                 writer.println(line);
  89             }
  90         }
  91     }
  92 
  93     private static String patch(String s, String tag, Stream<String> stream) {
  94         String mns = stream.sorted()
  95             .collect(Collectors.joining("\",\n            \""));
  96         return s.replace(tag, mns);
  97     }
  98 
  99     /**
 100      * Reads the contents of the given modules file.
 101      */
 102     private static Set<String> readModuleSet(String name) throws IOException {
 103         try (InputStream is = GenModuleLoaderMap.class.getResourceAsStream(name);
 104              BufferedReader reader = new BufferedReader(new InputStreamReader(is))) {
 105             return reader.lines().collect(Collectors.toSet());
 106         }
 107     }
 108 }