< prev index next >

test/java/security/modules/JigsawSecurityUtils.java

Print this page




   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 }


   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 }
< prev index next >