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.lang.module.ModuleDescriptor;
  30 import static java.lang.module.ModuleDescriptor.Builder;
  31 
  32 /**
  33  * Jigsaw utility methods are part of this class. It exposes methods to generate
  34  * module descriptor object and create service descriptor inside META-INF folder
  35  * etc.
  36  */
  37 public class JigsawSecurityUtils {
  38 
  39     /**
  40      * Enum represents all supported module types in JDK9.
  41      */
  42     public enum MODULE_TYPE {
  43 
  44         EXPLICIT, AUTO, UNNAMED;
  45     }
  46 
  47     public static final String SPACE = " ";
  48 
  49     /**
  50      * Constructs a Java Command line string based on modular structure followed
  51      * by modular client and service.
  52      */
  53     public static String[] getJavaCommand(Path modulePath,
  54             StringBuilder classPath, String clientModuleName,
  55             String mainClass, Map<String, String> vmArgs) throws IOException {
  56 
  57         final StringBuilder command = new StringBuilder();
  58         vmArgs.forEach((key, value) -> command.append(key + value + SPACE));
  59         if (modulePath != null) {
  60             command.append("-mp" + SPACE + modulePath.toRealPath() + SPACE);
  61         }
  62         if (classPath != null && classPath.length() > 0) {
  63             command.append("-cp" + SPACE + classPath + SPACE);
  64         }
  65         if (clientModuleName != null && clientModuleName.length() > 0) {
  66             command.append("-m" + SPACE + clientModuleName + "/");
  67         }
  68         command.append(mainClass);
  69         return command.toString().trim().split("[\\s]+");
  70     }
  71 
  72     /**
  73      * Generate ModuleDescriptor object for explicit/auto based client/service
  74      * modules.
  75      */
  76     public static ModuleDescriptor generateModuleDescriptor(
  77             boolean service, MODULE_TYPE moduleType, String moduleName,
  78             String pkg, String serviceInterface,
  79             String serviceImpl, String serviceModuleName,
  80             List<String> requiredModules, boolean depends) {
  81         final Builder builder;
  82         if (moduleType == MODULE_TYPE.EXPLICIT) {
  83             System.out.println("Generating ModuleDescriptor object");
  84             builder = new Builder(moduleName).exports(pkg);
  85             if (service) {
  86                 builder.provides(serviceInterface, serviceImpl);
  87             } else {
  88                 builder.uses(serviceInterface);
  89                 if (depends) {
  90                     builder.requires(serviceModuleName);
  91                 }
  92             }
  93         } else {
  94             System.out.println("ModuleDescriptor object not required.");
  95             return null;
  96         }
  97         requiredModules.stream().forEach(reqMod -> builder.requires(reqMod));
  98 
  99         return builder.build();
 100     }
 101 
 102     /**
 103      * Generates service descriptor inside META-INF folder.
 104      */
 105     public static boolean createMetaInfServiceDescriptor(
 106             Path serviceDescriptorFile, String serviceImpl) {
 107         boolean created = true;
 108         System.out.println(String.format("Creating META-INF service descriptor"
 109                 + " for '%s' at path '%s'", serviceImpl,
 110                 serviceDescriptorFile));
 111         try {
 112             Path parent = serviceDescriptorFile.getParent();
 113             if (parent != null) {
 114                 Files.createDirectories(parent);
 115             }
 116             Files.write(serviceDescriptorFile, serviceImpl.getBytes("UTF-8"));
 117             System.out.println(String.format(
 118                     "META-INF service descriptor generated successfully"));
 119         } catch (IOException e) {
 120             e.printStackTrace(System.out);
 121             created = false;
 122         }
 123         return created;
 124     }
 125 
 126 }