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