1 /*
   2  * Copyright (c) 2013, 2016, 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 com.sun.tools.jdeps;
  27 
  28 import java.io.IOException;
  29 import java.lang.module.ModuleDescriptor;
  30 import java.util.Arrays;
  31 import java.util.Comparator;
  32 import java.util.HashMap;
  33 import java.util.HashSet;
  34 import java.util.Map;
  35 import java.util.Optional;
  36 import java.util.Set;
  37 
  38 /**
  39  * Build the profile information.
  40  */
  41 enum Profile {
  42     COMPACT1("compact1", 1, "java.logging",
  43                             "java.scripting"),
  44     COMPACT2("compact2", 2, "java.rmi",
  45                             "java.sql",
  46                             "java.xml",
  47                             "jdk.xml.dom",
  48                             "jdk.httpserver"),
  49     COMPACT3("compact3", 3, "java.smartcardio",
  50                             "java.compiler",
  51                             "java.instrument",
  52                             "java.management",
  53                             "java.naming",
  54                             "java.prefs",
  55                             "java.security.jgss",
  56                             "java.security.sasl",
  57                             "java.sql.rowset",
  58                             "java.xml.crypto",
  59                             "jdk.management",
  60                             "jdk.naming.dns",
  61                             "jdk.naming.rmi",
  62                             "jdk.sctp",
  63                             "jdk.security.auth");
  64 
  65     final String name;
  66     final int profile;
  67     final String[] mnames;
  68     final Map<String, Module> modules = new HashMap<>();
  69 
  70     Profile(String name, int profile, String... mnames) {
  71         this.name = name;
  72         this.profile = profile;
  73         this.mnames = mnames;
  74     }
  75 
  76     public String profileName() {
  77         return name;
  78     }
  79 
  80     @Override
  81     public String toString() {
  82         return mnames[0];
  83     }
  84 
  85     public static int getProfileCount() {
  86         return JDK.isEmpty() ? 0 : Profile.values().length;
  87     }
  88 
  89     /**
  90      * Returns the Profile for the given package name; null if not found.
  91      */
  92     public static Profile getProfile(String pn) {
  93         for (Profile p : Profile.values()) {
  94             for (Module m : p.modules.values()) {
  95                 if (m.packages().contains(pn)) {
  96                     return p;
  97                 }
  98             }
  99         }
 100         return null;
 101     }
 102 
 103     /*
 104      * Returns the Profile for a given Module; null if not found.
 105      */
 106     public static Profile getProfile(Module m) {
 107         for (Profile p : Profile.values()) {
 108             if (p.modules.containsValue(m)) {
 109                 return p;
 110             }
 111         }
 112         return null;
 113     }
 114 
 115     private final static Set<Module> JDK = new HashSet<>();
 116     static synchronized void init(Map<String, Module> systemModules) {
 117         Arrays.stream(Profile.values()).forEach(p ->
 118             // this includes platform-dependent module that may not exist
 119             Arrays.stream(p.mnames)
 120                   .filter(systemModules::containsKey)
 121                   .map(systemModules::get)
 122                   .forEach(m -> p.addModule(systemModules, m)));
 123 
 124         // JDK modules should include full JRE plus other jdk.* modules
 125         // Just include all installed modules.  Assume jdeps is running
 126         // in JDK image
 127         JDK.addAll(systemModules.values());
 128     }
 129 
 130     private void addModule(Map<String, Module> systemModules, Module module) {
 131         modules.put(module.name(), module);
 132         module.descriptor().requires().stream()
 133               .map(ModuleDescriptor.Requires::name)
 134               .map(systemModules::get)
 135               .forEach(m -> modules.put(m.name(), m));
 136     }
 137 
 138     // for debugging
 139     public static void main(String[] args) throws IOException {
 140         // initialize Profiles
 141         new JdepsConfiguration.Builder().allModules().build();
 142 
 143         // find platform modules
 144         if (Profile.getProfileCount() == 0) {
 145             System.err.println("No profile is present in this JDK");
 146         }
 147         for (Profile p : Profile.values()) {
 148             String profileName = p.name;
 149             System.out.format("%2d: %-10s  %s%n", p.profile, profileName, p.modules);
 150         }
 151         System.out.println("All JDK modules:-");
 152         JDK.stream().sorted(Comparator.comparing(Module::name))
 153            .forEach(m -> System.out.println(m));
 154     }
 155 }