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