1/*
2 * Copyright (c) 2014, 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 jdk.internal.module;
27
28import java.lang.module.ModuleDescriptor;
29import java.lang.module.ModuleDescriptor.Provides;
30import java.util.ArrayList;
31import java.util.Collections;
32import java.util.List;
33import java.util.Map;
34import java.util.Objects;
35import java.util.concurrent.ConcurrentHashMap;
36import java.util.concurrent.CopyOnWriteArrayList;
37
38import jdk.internal.loader.ClassLoaderValue;
39
40/**
41 * A <em>services catalog</em>. Each {@code ClassLoader} and {@code Layer} has
42 * an optional {@code ServicesCatalog} for modules that provide services.
43 *
44 * @apiNote This class will be replaced once the ServiceLoader is further
45 *          specified
46 */
47public final class ServicesCatalog {
48
49    /**
50     * Represents a service provider in the services catalog.
51     */
52    public final class ServiceProvider {
53        private final Module module;
54        private final String providerName;
55
56        public ServiceProvider(Module module, String providerName) {
57            this.module = module;
58            this.providerName = providerName;
59        }
60
61        public Module module() {
62            return module;
63        }
64
65        public String providerName() {
66            return providerName;
67        }
68
69        @Override
70        public int hashCode() {
71            return Objects.hash(module, providerName);
72        }
73
74        @Override
75        public boolean equals(Object ob) {
76            if (!(ob instanceof ServiceProvider))
77                return false;
78            ServiceProvider that = (ServiceProvider)ob;
79            return Objects.equals(this.module, that.module)
80                    && Objects.equals(this.providerName, that.providerName);
81        }
82    }
83
84    // service name -> list of providers
85    private final Map<String, List<ServiceProvider>> map = new ConcurrentHashMap<>();
86
87    private ServicesCatalog() { }
88
89    /**
90     * Creates a ServicesCatalog that supports concurrent registration and
91     * and lookup
92     */
93    public static ServicesCatalog create() {
94        return new ServicesCatalog();
95    }
96
97    /**
98     * Returns the list of service provides for the given service type
99     * name, creating it if needed.
100     */
101    private List<ServiceProvider> providers(String service) {
102        // avoid computeIfAbsent here
103        List<ServiceProvider> list = map.get(service);
104        if (list == null) {
105            list = new CopyOnWriteArrayList<>();
106            List<ServiceProvider> prev = map.putIfAbsent(service, list);
107            if (prev != null)
108                list = prev;  // someone else got there
109        }
110        return list;
111    }
112
113    /**
114     * Registers the providers in the given module in this services catalog.
115     */
116    public void register(Module module) {
117        ModuleDescriptor descriptor = module.getDescriptor();
118        for (Provides provides : descriptor.provides()) {
119            String service = provides.service();
120            List<String> providerNames = provides.providers();
121            int count = providerNames.size();
122            if (count == 1) {
123                String pn = providerNames.get(0);
124                providers(service).add(new ServiceProvider(module, pn));
125            } else {
126                List<ServiceProvider> list = new ArrayList<>(count);
127                for (String pn : providerNames) {
128                    list.add(new ServiceProvider(module, pn));
129                }
130                providers(service).addAll(list);
131            }
132        }
133    }
134
135    /**
136     * Add a provider in the given module to this services catalog
137     *
138     * @apiNote This method is for use by java.lang.instrument
139     */
140    public void addProvider(Module module, Class<?> service, Class<?> impl) {
141        List<ServiceProvider> list = providers(service.getName());
142        list.add(new ServiceProvider(module, impl.getName()));
143    }
144
145    /**
146     * Returns the (possibly empty) list of service providers that implement
147     * the given service type.
148     */
149    public List<ServiceProvider> findServices(String service) {
150        return map.getOrDefault(service, Collections.emptyList());
151    }
152
153    /**
154     * Returns the ServicesCatalog for the given class loader or {@code null}
155     * if there is none.
156     */
157    public static ServicesCatalog getServicesCatalogOrNull(ClassLoader loader) {
158        return CLV.get(loader);
159    }
160
161    /**
162     * Returns the ServicesCatalog for the given class loader, creating it if
163     * needed.
164     */
165    public static ServicesCatalog getServicesCatalog(ClassLoader loader) {
166        // CLV.computeIfAbsent(loader, (cl, clv) -> create());
167        ServicesCatalog catalog = CLV.get(loader);
168        if (catalog == null) {
169            catalog = create();
170            ServicesCatalog previous = CLV.putIfAbsent(loader, catalog);
171            if (previous != null) catalog = previous;
172        }
173        return catalog;
174    }
175
176    // the ServicesCatalog registered to a class loader
177    private static final ClassLoaderValue<ServicesCatalog> CLV = new ClassLoaderValue<>();
178}