1 /*
   2  * Copyright (c) 2013, 2014, 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.util.*;
  30 
  31 /**
  32  * Build the profile information.
  33  */
  34 enum Profile {
  35     COMPACT1("compact1", 1, "java.compact1"),
  36     COMPACT2("compact2", 2, "java.compact2"),
  37     COMPACT3("compact3", 3, "java.compact3", "java.smartcardio", "jdk.sctp",
  38                             "jdk.httpserver", "jdk.security.auth",
  39                             "jdk.naming.dns", "jdk.naming.rmi",
  40                             "jdk.management"),
  41     FULL_JRE("Full JRE", 4, "java.se", "jdk.deploy.osx", "jdk.charsets",
  42                             "jdk.crypto.ec", "jdk.crypto.pkcs11",
  43                             "jdk.crypto.mscapi", "jdk.crypto.ucrypto", "jdk.jvmstat",
  44                             "jdk.localedata", "jdk.scripting.nashorn", "jdk.zipfs");
  45 
  46     final String name;
  47     final int profile;
  48     final String[] mnames;
  49     final Set<Module> modules = new HashSet<>();
  50 
  51     Profile(String name, int profile, String... mnames) {
  52         this.name = name;
  53         this.profile = profile;
  54         this.mnames = mnames;
  55     }
  56 
  57     public String profileName() {
  58         return name;
  59     }
  60 
  61     @Override
  62     public String toString() {
  63         return mnames[0];
  64     }
  65 
  66     public static int getProfileCount() {
  67         return JDK.isEmpty() ? 0 : Profile.values().length;
  68     }
  69 
  70     /**
  71      * Returns the Profile for the given package name; null if not found.
  72      */
  73     public static Profile getProfile(String pn) {
  74         for (Profile p : Profile.values()) {
  75             for (Module m : p.modules) {
  76                 if (m.packages().contains(pn)) {
  77                     return p;
  78                 }
  79             }
  80         }
  81         return null;
  82     }
  83 
  84     /*
  85      * Returns the Profile for a given Module; null if not found.
  86      */
  87     public static Profile getProfile(Module m) {
  88         for (Profile p : Profile.values()) {
  89             if (p.modules.contains(m)) {
  90                 return p;
  91             }
  92         }
  93         return null;
  94     }
  95 
  96     private final static Set<Module> JDK = new HashSet<>();
  97     static void initProfiles(List<Archive> modules) {
  98         // add all modules into  JDK
  99         modules.forEach(m -> JDK.add((Module)m));
 100 
 101         for (Profile p : Profile.values()) {
 102             for (String mn : p.mnames) {
 103                 // this includes platform-dependent module that may not exist
 104                 Module m = PlatformClassPath.findModule(mn);
 105                 if (m != null) {
 106                     p.addModule(m);
 107                 }
 108             }
 109         }
 110     }
 111 
 112     private void addModule(Module m) {
 113         modules.add(m);
 114         for (String n : m.requires().keySet()) {
 115             Module d = PlatformClassPath.findModule(n);
 116             if (d == null) {
 117                 throw new InternalError("module " + n + " required by " +
 118                         m.name() + " doesn't exist");
 119             }
 120             modules.add(d);
 121         }
 122     }
 123     // for debugging
 124     public static void main(String[] args) throws IOException {
 125         // find platform modules
 126         PlatformClassPath.getModules(null);
 127         if (Profile.getProfileCount() == 0) {
 128             System.err.println("No profile is present in this JDK");
 129         }
 130         for (Profile p : Profile.values()) {
 131             String profileName = p.name;
 132             System.out.format("%2d: %-10s  %s%n", p.profile, profileName, p.modules);
 133             for (Module m: p.modules) {
 134                 System.out.format("module %s%n", m.name());
 135                 System.out.format("   requires %s%n", m.requires());
 136                 for (Map.Entry<String,Set<String>> e: m.exports().entrySet()) {
 137                     System.out.format("   exports %s %s%n", e.getKey(),
 138                         e.getValue().isEmpty() ? "" : "to " + e.getValue());
 139                 }
 140             }
 141         }
 142         System.out.println("All JDK modules:-");
 143         JDK.stream().sorted(Comparator.comparing(Module::name))
 144            .forEach(m -> System.out.println(m));
 145     }
 146 }