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