Profile.java revision 2942:08092deced3f
129088Smarkm/*
229088Smarkm * Copyright (c) 2013, 2014, Oracle and/or its affiliates. All rights reserved.
329088Smarkm * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
429088Smarkm *
529088Smarkm * This code is free software; you can redistribute it and/or modify it
629088Smarkm * under the terms of the GNU General Public License version 2 only, as
729088Smarkm * published by the Free Software Foundation.  Oracle designates this
829088Smarkm * particular file as subject to the "Classpath" exception as provided
929088Smarkm * by Oracle in the LICENSE file that accompanied this code.
1029088Smarkm *
1129088Smarkm * This code is distributed in the hope that it will be useful, but WITHOUT
1229088Smarkm * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1329088Smarkm * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
1429088Smarkm * version 2 for more details (a copy is included in the LICENSE file that
1529088Smarkm * accompanied this code).
1629088Smarkm *
1729088Smarkm * You should have received a copy of the GNU General Public License version
1829088Smarkm * 2 along with this work; if not, write to the Free Software Foundation,
1929088Smarkm * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
2029088Smarkm *
2129088Smarkm * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2229088Smarkm * or visit www.oracle.com if you need additional information or have any
2329088Smarkm * questions.
2429088Smarkm */
2529088Smarkmpackage com.sun.tools.jdeps;
2629088Smarkm
2729088Smarkmimport java.io.IOException;
2829088Smarkmimport java.util.*;
2929088Smarkm
3029088Smarkm/**
3129088Smarkm * Build the profile information.
3229088Smarkm */
3329088Smarkmenum Profile {
34114630Sobrien    COMPACT1("compact1", 1, "java.compact1"),
3529088Smarkm    COMPACT2("compact2", 2, "java.compact2"),
3629181Smarkm    COMPACT3("compact3", 3, "java.compact3", "java.smartcardio", "jdk.sctp",
3763248Speter                            "jdk.httpserver", "jdk.security.auth",
38114630Sobrien                            "jdk.naming.dns", "jdk.naming.rmi",
39114630Sobrien                            "jdk.management"),
40114630Sobrien    FULL_JRE("Full JRE", 4, "java.se", "jdk.deploy.osx", "jdk.charsets",
4129088Smarkm                            "jdk.crypto.ec", "jdk.crypto.pkcs11",
4229088Smarkm                            "jdk.crypto.mscapi", "jdk.crypto.ucrypto", "jdk.jvmstat",
4329088Smarkm                            "jdk.localedata", "jdk.scripting.nashorn", "jdk.zipfs");
4429088Smarkm
4529088Smarkm    final String name;
4629088Smarkm    final int profile;
4729088Smarkm    final String[] mnames;
4829088Smarkm    final Set<Module> modules = new HashSet<>();
4929088Smarkm
5087139Smarkm    Profile(String name, int profile, String... mnames) {
5187139Smarkm        this.name = name;
5229181Smarkm        this.profile = profile;
5387139Smarkm        this.mnames = mnames;
5429181Smarkm    }
5587139Smarkm
5629181Smarkm    public String profileName() {
5729088Smarkm        return name;
5829088Smarkm    }
5929088Smarkm
6029088Smarkm    @Override
6129088Smarkm    public String toString() {
6229088Smarkm        return mnames[0];
6329088Smarkm    }
6487139Smarkm
6529181Smarkm    public static int getProfileCount() {
6629181Smarkm        return JDK.isEmpty() ? 0 : Profile.values().length;
6787139Smarkm    }
6829181Smarkm
6929181Smarkm    /**
7029181Smarkm     * Returns the Profile for the given package name; null if not found.
7129088Smarkm     */
7229088Smarkm    public static Profile getProfile(String pn) {
7329088Smarkm        for (Profile p : Profile.values()) {
7429088Smarkm            for (Module m : p.modules) {
7529088Smarkm                if (m.packages().contains(pn)) {
7629088Smarkm                    return p;
7729088Smarkm                }
7829088Smarkm            }
7929088Smarkm        }
8029088Smarkm        return null;
8129088Smarkm    }
8229088Smarkm
8329088Smarkm    /*
8429088Smarkm     * Returns the Profile for a given Module; null if not found.
8529088Smarkm     */
8629088Smarkm    public static Profile getProfile(Module m) {
8729088Smarkm        for (Profile p : Profile.values()) {
8829088Smarkm            if (p.modules.contains(m)) {
8929088Smarkm                return p;
9029088Smarkm            }
9129088Smarkm        }
9229088Smarkm        return null;
9329088Smarkm    }
9429088Smarkm
9529088Smarkm    private final static Set<Module> JDK = new HashSet<>();
9629088Smarkm    static void initProfiles(List<Archive> modules) {
9729088Smarkm        // add all modules into  JDK
98114911Smarkm        modules.forEach(m -> JDK.add((Module)m));
9929088Smarkm
10029088Smarkm        for (Profile p : Profile.values()) {
10129088Smarkm            for (String mn : p.mnames) {
10229088Smarkm                // this includes platform-dependent module that may not exist
10329088Smarkm                Module m = PlatformClassPath.findModule(mn);
10429088Smarkm                if (m != null) {
10529088Smarkm                    p.addModule(m);
10629088Smarkm                }
10729088Smarkm            }
10829088Smarkm        }
10929088Smarkm    }
11029088Smarkm
11129088Smarkm    private void addModule(Module m) {
11229088Smarkm        modules.add(m);
11329181Smarkm        for (String n : m.requires().keySet()) {
11447973Sru            Module d = PlatformClassPath.findModule(n);
11529181Smarkm            if (d == null) {
11629088Smarkm                throw new InternalError("module " + n + " required by " +
11729088Smarkm                        m.name() + " doesn't exist");
11881965Smarkm            }
11976616Speter            modules.add(d);
12081965Smarkm        }
12129088Smarkm    }
12229088Smarkm    // for debugging
12329088Smarkm    public static void main(String[] args) throws IOException {
12429088Smarkm        // find platform modules
12529088Smarkm        PlatformClassPath.getModules(null);
12629088Smarkm        if (Profile.getProfileCount() == 0) {
12729088Smarkm            System.err.println("No profile is present in this JDK");
12829088Smarkm        }
12929088Smarkm        for (Profile p : Profile.values()) {
13029088Smarkm            String profileName = p.name;
13129088Smarkm            System.out.format("%2d: %-10s  %s%n", p.profile, profileName, p.modules);
13229088Smarkm            for (Module m: p.modules) {
13329088Smarkm                System.out.format("module %s%n", m.name());
13429088Smarkm                System.out.format("   requires %s%n", m.requires());
13529088Smarkm                for (Map.Entry<String,Set<String>> e: m.exports().entrySet()) {
13629088Smarkm                    System.out.format("   exports %s %s%n", e.getKey(),
13729088Smarkm                        e.getValue().isEmpty() ? "" : "to " + e.getValue());
13829088Smarkm                }
13929088Smarkm            }
14029088Smarkm        }
14129088Smarkm        System.out.println("All JDK modules:-");
14229088Smarkm        JDK.stream().sorted(Comparator.comparing(Module::name))
14329088Smarkm           .forEach(m -> System.out.println(m));
14429088Smarkm    }
14529088Smarkm}
14629088Smarkm