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.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 import java.io.IOException;
  25 import java.nio.file.Files;
  26 import java.nio.file.Path;
  27 import java.util.List;
  28 import java.util.Map;
  29 import java.util.Arrays;
  30 import java.lang.module.ModuleDescriptor;
  31 import static java.lang.module.ModuleDescriptor.Builder;
  32 
  33 /**
  34  * Jigsaw utility methods are part of this class. It exposes methods to generate
  35  * module descriptor object and create service descriptor inside META-INF folder
  36  * etc.
  37  */
  38 public class JigsawSecurityUtils {
  39 
  40     /**
  41      * Enum represents all supported module types in JDK9.
  42      */
  43     public enum MODULE_TYPE {
  44 
  45         EXPLICIT, AUTO, UNNAMED;
  46     }
  47 
  48     public static final String SPACE = " ";
  49 
  50     /**
  51      * Constructs a Java Command line string based on modular structure followed
  52      * by modular client and service.
  53      */
  54     public static String[] getJavaCommand(Path modulePath,
  55             StringBuilder classPath, String clientModuleName,
  56             String mainClass, Map<String, String> vmArgs,
  57             String... options) throws IOException {
  58 
  59         final StringBuilder command = new StringBuilder();
  60         vmArgs.forEach((key, value) -> command.append(key + value + SPACE));
  61         if (modulePath != null) {
  62             command.append("-mp" + SPACE + modulePath.toRealPath() + SPACE);
  63         }
  64         if (classPath != null && classPath.length() > 0) {
  65             command.append("-cp" + SPACE + classPath + SPACE);
  66         }
  67         if (clientModuleName != null && clientModuleName.length() > 0) {
  68             command.append("-m" + SPACE + clientModuleName + "/");
  69         }
  70         command.append(mainClass);
  71         Arrays.asList(options).stream().forEach(o -> command.append(SPACE + o));
  72         return command.toString().trim().split("[\\s]+");
  73     }
  74 
  75     /**
  76      * Generate ModuleDescriptor object for explicit/auto based client/service
  77      * modules.
  78      */
  79     public static ModuleDescriptor generateModuleDescriptor(
  80             boolean service, MODULE_TYPE moduleType, String moduleName,
  81             String pkg, String serviceInterface,
  82             String serviceImpl, String serviceModuleName,
  83             List<String> requiredModules, boolean depends) {
  84         final Builder builder;
  85         if (moduleType == MODULE_TYPE.EXPLICIT) {
  86             System.out.println("Generating ModuleDescriptor object");
  87             builder = new Builder(moduleName).exports(pkg);
  88             if (service) {
  89                 builder.provides(serviceInterface, serviceImpl);
  90             } else {
  91                 builder.uses(serviceInterface);
  92                 if (depends) {
  93                     builder.requires(serviceModuleName);
  94                 }
  95             }
  96         } else {
  97             System.out.println("ModuleDescriptor object not required.");
  98             return null;
  99         }
 100         requiredModules.stream().forEach(reqMod -> builder.requires(reqMod));
 101 
 102         return builder.build();
 103     }
 104 
 105     /**
 106      * Generates service descriptor inside META-INF folder.
 107      */
 108     public static boolean createMetaInfServiceDescriptor(
 109             Path serviceDescriptorFile, String serviceImpl) {
 110         boolean created = true;
 111         System.out.println(String.format(
 112                 "Creating META-INF service descriptor for '%s' "
 113                 + "at path '%s'", serviceImpl, serviceDescriptorFile));
 114         try {
 115             Path parent = serviceDescriptorFile.getParent();
 116             if (parent != null) {
 117                 Files.createDirectories(parent);
 118             }
 119             Files.write(serviceDescriptorFile, serviceImpl.getBytes("UTF-8"));
 120             System.out.println(String.format(
 121                     "META-INF service descriptor generated successfully"));
 122         } catch (IOException e) {
 123             e.printStackTrace(System.out);
 124             created = false;
 125         }
 126         return created;
 127     }
 128 
 129 }