Profile.java revision 3170:dc017a37aac5
1230557Sjimharris/*
2230557Sjimharris * Copyright (c) 2013, 2014, Oracle and/or its affiliates. All rights reserved.
3230557Sjimharris * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4230557Sjimharris *
5230557Sjimharris * This code is free software; you can redistribute it and/or modify it
6230557Sjimharris * under the terms of the GNU General Public License version 2 only, as
7230557Sjimharris * published by the Free Software Foundation.  Oracle designates this
8230557Sjimharris * particular file as subject to the "Classpath" exception as provided
9230557Sjimharris * by Oracle in the LICENSE file that accompanied this code.
10230557Sjimharris *
11230557Sjimharris * This code is distributed in the hope that it will be useful, but WITHOUT
12230557Sjimharris * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13230557Sjimharris * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14230557Sjimharris * version 2 for more details (a copy is included in the LICENSE file that
15230557Sjimharris * accompanied this code).
16230557Sjimharris *
17230557Sjimharris * You should have received a copy of the GNU General Public License version
18230557Sjimharris * 2 along with this work; if not, write to the Free Software Foundation,
19230557Sjimharris * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20230557Sjimharris *
21230557Sjimharris * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22230557Sjimharris * or visit www.oracle.com if you need additional information or have any
23230557Sjimharris * questions.
24230557Sjimharris */
25230557Sjimharris
26230557Sjimharrispackage com.sun.tools.jdeps;
27230557Sjimharris
28230557Sjimharrisimport java.io.IOException;
29230557Sjimharrisimport java.util.*;
30230557Sjimharris
31230557Sjimharris/**
32230557Sjimharris * Build the profile information.
33230557Sjimharris */
34230557Sjimharrisenum Profile {
35230557Sjimharris    COMPACT1("compact1", 1, "java.compact1"),
36230557Sjimharris    COMPACT2("compact2", 2, "java.compact2"),
37230557Sjimharris    COMPACT3("compact3", 3, "java.compact3", "java.smartcardio", "jdk.sctp",
38230557Sjimharris                            "jdk.httpserver", "jdk.security.auth",
39230557Sjimharris                            "jdk.naming.dns", "jdk.naming.rmi",
40230557Sjimharris                            "jdk.management"),
41230557Sjimharris    FULL_JRE("Full JRE", 4, "java.se", "jdk.deploy.osx", "jdk.charsets",
42230557Sjimharris                            "jdk.crypto.ec", "jdk.crypto.pkcs11",
43230557Sjimharris                            "jdk.crypto.mscapi", "jdk.crypto.ucrypto", "jdk.jvmstat",
44230557Sjimharris                            "jdk.localedata", "jdk.scripting.nashorn", "jdk.zipfs");
45230557Sjimharris
46230557Sjimharris    final String name;
47230557Sjimharris    final int profile;
48230557Sjimharris    final String[] mnames;
49230557Sjimharris    final Set<Module> modules = new HashSet<>();
50230557Sjimharris
51230557Sjimharris    Profile(String name, int profile, String... mnames) {
52230557Sjimharris        this.name = name;
53230557Sjimharris        this.profile = profile;
54230557Sjimharris        this.mnames = mnames;
55230557Sjimharris    }
56230557Sjimharris
57230557Sjimharris    public String profileName() {
58230557Sjimharris        return name;
59230557Sjimharris    }
60230557Sjimharris
61230557Sjimharris    @Override
62230557Sjimharris    public String toString() {
63230557Sjimharris        return mnames[0];
64230557Sjimharris    }
65230557Sjimharris
66230557Sjimharris    public static int getProfileCount() {
67230557Sjimharris        return JDK.isEmpty() ? 0 : Profile.values().length;
68230557Sjimharris    }
69230557Sjimharris
70230557Sjimharris    /**
71230557Sjimharris     * Returns the Profile for the given package name; null if not found.
72230557Sjimharris     */
73230557Sjimharris    public static Profile getProfile(String pn) {
74230557Sjimharris        for (Profile p : Profile.values()) {
75230557Sjimharris            for (Module m : p.modules) {
76230557Sjimharris                if (m.packages().contains(pn)) {
77230557Sjimharris                    return p;
78230557Sjimharris                }
79230557Sjimharris            }
80230557Sjimharris        }
81230557Sjimharris        return null;
82230557Sjimharris    }
83230557Sjimharris
84230557Sjimharris    /*
85230557Sjimharris     * Returns the Profile for a given Module; null if not found.
86230557Sjimharris     */
87230557Sjimharris    public static Profile getProfile(Module m) {
88230557Sjimharris        for (Profile p : Profile.values()) {
89230557Sjimharris            if (p.modules.contains(m)) {
90230557Sjimharris                return p;
91230557Sjimharris            }
92230557Sjimharris        }
93230557Sjimharris        return null;
94230557Sjimharris    }
95230557Sjimharris
96230557Sjimharris    private final static Set<Module> JDK = new HashSet<>();
97230557Sjimharris    static void initProfiles(List<Archive> modules) {
98230557Sjimharris        // add all modules into  JDK
99230557Sjimharris        modules.forEach(m -> JDK.add((Module)m));
100230557Sjimharris
101230557Sjimharris        for (Profile p : Profile.values()) {
102230557Sjimharris            for (String mn : p.mnames) {
103230557Sjimharris                // this includes platform-dependent module that may not exist
104230557Sjimharris                Module m = PlatformClassPath.findModule(mn);
105230557Sjimharris                if (m != null) {
106230557Sjimharris                    p.addModule(m);
107230557Sjimharris                }
108230557Sjimharris            }
109230557Sjimharris        }
110230557Sjimharris    }
111230557Sjimharris
112230557Sjimharris    private void addModule(Module m) {
113230557Sjimharris        modules.add(m);
114230557Sjimharris        for (String n : m.requires().keySet()) {
115230557Sjimharris            Module d = PlatformClassPath.findModule(n);
116230557Sjimharris            if (d == null) {
117230557Sjimharris                throw new InternalError("module " + n + " required by " +
118230557Sjimharris                        m.name() + " doesn't exist");
119230557Sjimharris            }
120230557Sjimharris            modules.add(d);
121230557Sjimharris        }
122230557Sjimharris    }
123230557Sjimharris    // for debugging
124230557Sjimharris    public static void main(String[] args) throws IOException {
125230557Sjimharris        // find platform modules
126230557Sjimharris        PlatformClassPath.getModules(null);
127230557Sjimharris        if (Profile.getProfileCount() == 0) {
128230557Sjimharris            System.err.println("No profile is present in this JDK");
129230557Sjimharris        }
130230557Sjimharris        for (Profile p : Profile.values()) {
131230557Sjimharris            String profileName = p.name;
132230557Sjimharris            System.out.format("%2d: %-10s  %s%n", p.profile, profileName, p.modules);
133230557Sjimharris            for (Module m: p.modules) {
134230557Sjimharris                System.out.format("module %s%n", m.name());
135230557Sjimharris                System.out.format("   requires %s%n", m.requires());
136230557Sjimharris                for (Map.Entry<String,Set<String>> e: m.exports().entrySet()) {
137230557Sjimharris                    System.out.format("   exports %s %s%n", e.getKey(),
138230557Sjimharris                        e.getValue().isEmpty() ? "" : "to " + e.getValue());
139230557Sjimharris                }
140230557Sjimharris            }
141230557Sjimharris        }
142230557Sjimharris        System.out.println("All JDK modules:-");
143230557Sjimharris        JDK.stream().sorted(Comparator.comparing(Module::name))
144230557Sjimharris           .forEach(m -> System.out.println(m));
145230557Sjimharris    }
146230557Sjimharris}
147230557Sjimharris