LoaderPool.java revision 13901:b2a69d66dc65
1/*
2 * Copyright (c) 2015, 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 jdk.internal.loader;
27
28import java.lang.module.Configuration;
29import java.lang.module.ModuleReference;
30import java.lang.module.ResolvedModule;
31import java.lang.reflect.Layer;
32import java.util.HashMap;
33import java.util.Map;
34import java.util.stream.Stream;
35
36/**
37 * A pool of class loaders.
38 *
39 * @see Layer#defineModulesWithManyLoaders
40 */
41
42public final class LoaderPool {
43
44    // maps module names to class loaders
45    private final Map<String, Loader> loaders;
46
47
48    /**
49     * Creates a pool of class loaders. Each module in the given configuration
50     * will be loaded its own class loader in the pool. The class loader is
51     * created with the given parent class loader as its parent.
52     */
53    public LoaderPool(Configuration cf,
54                      Layer parentLayer,
55                      ClassLoader parentLoader)
56    {
57        Map<String, Loader> loaders = new HashMap<>();
58        for (ResolvedModule resolvedModule : cf.modules()) {
59            Loader loader = new Loader(resolvedModule, this, parentLoader);
60            String mn = resolvedModule.name();
61            loaders.put(mn, loader);
62        }
63        this.loaders = loaders;
64
65        // complete the initialization
66        loaders.values().forEach(l -> l.initRemotePackageMap(cf, parentLayer));
67    }
68
69
70    /**
71     * Returns the class loader for the named module
72     */
73    public Loader loaderFor(String name) {
74        Loader loader = loaders.get(name);
75        assert loader != null;
76        return loader;
77    }
78
79    /**
80     * Returns a stream of the loaders in this pool.
81     */
82    public Stream<Loader> loaders() {
83        return loaders.values().stream();
84    }
85
86}
87
88