author | henryjen |
Fri, 21 Feb 2014 15:28:39 -0800 | |
changeset 23313 | f100f0d49e0b |
parent 22959 | 2d9d076cee41 |
child 23328 | 4c53a6ebc779 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
5506 | 2 |
* Copyright (c) 2000, 2007, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 24 |
*/ |
25 |
||
26 |
package javax.imageio.spi; |
|
27 |
||
28 |
import java.io.File; |
|
29 |
import java.util.ArrayList; |
|
30 |
import java.util.HashMap; |
|
31 |
import java.util.Iterator; |
|
32 |
import java.util.List; |
|
33 |
import java.util.Map; |
|
34 |
import java.util.NoSuchElementException; |
|
35 |
import java.util.Set; |
|
36 |
import java.util.ServiceLoader; |
|
37 |
||
38 |
/** |
|
39 |
* A registry for service provider instances. |
|
40 |
* |
|
41 |
* <p> A <i>service</i> is a well-known set of interfaces and (usually |
|
42 |
* abstract) classes. A <i>service provider</i> is a specific |
|
43 |
* implementation of a service. The classes in a provider typically |
|
44 |
* implement the interface or subclass the class defined by the |
|
45 |
* service itself. |
|
46 |
* |
|
47 |
* <p> Service providers are stored in one or more <i>categories</i>, |
|
48 |
* each of which is defined by a class of interface (described by a |
|
49 |
* <code>Class</code> object) that all of its members must implement. |
|
50 |
* The set of categories may be changed dynamically. |
|
51 |
* |
|
52 |
* <p> Only a single instance of a given leaf class (that is, the |
|
53 |
* actual class returned by <code>getClass()</code>, as opposed to any |
|
54 |
* inherited classes or interfaces) may be registered. That is, |
|
55 |
* suppose that the |
|
56 |
* <code>com.mycompany.mypkg.GreenServiceProvider</code> class |
|
57 |
* implements the <code>com.mycompany.mypkg.MyService</code> |
|
58 |
* interface. If a <code>GreenServiceProvider</code> instance is |
|
59 |
* registered, it will be stored in the category defined by the |
|
60 |
* <code>MyService</code> class. If a new instance of |
|
61 |
* <code>GreenServiceProvider</code> is registered, it will replace |
|
62 |
* the previous instance. In practice, service provider objects are |
|
63 |
* usually singletons so this behavior is appropriate. |
|
64 |
* |
|
65 |
* <p> To declare a service provider, a <code>services</code> |
|
66 |
* subdirectory is placed within the <code>META-INF</code> directory |
|
67 |
* that is present in every JAR file. This directory contains a file |
|
68 |
* for each service provider interface that has one or more |
|
69 |
* implementation classes present in the JAR file. For example, if |
|
70 |
* the JAR file contained a class named |
|
71 |
* <code>com.mycompany.mypkg.MyServiceImpl</code> which implements the |
|
72 |
* <code>javax.someapi.SomeService</code> interface, the JAR file |
|
73 |
* would contain a file named: <pre> |
|
74 |
* META-INF/services/javax.someapi.SomeService </pre> |
|
75 |
* |
|
76 |
* containing the line: |
|
77 |
* |
|
78 |
* <pre> |
|
79 |
* com.mycompany.mypkg.MyService |
|
80 |
* </pre> |
|
81 |
* |
|
82 |
* <p> The service provider classes should be to be lightweight and |
|
83 |
* quick to load. Implementations of these interfaces should avoid |
|
84 |
* complex dependencies on other classes and on native code. The usual |
|
85 |
* pattern for more complex services is to register a lightweight |
|
86 |
* proxy for the heavyweight service. |
|
87 |
* |
|
88 |
* <p> An application may customize the contents of a registry as it |
|
89 |
* sees fit, so long as it has the appropriate runtime permission. |
|
90 |
* |
|
91 |
* <p> For more details on declaring service providers, and the JAR |
|
92 |
* format in general, see the <a |
|
93 |
* href="../../../../technotes/guides/jar/jar.html"> |
|
94 |
* JAR File Specification</a>. |
|
95 |
* |
|
96 |
* @see RegisterableService |
|
97 |
* |
|
98 |
*/ |
|
99 |
public class ServiceRegistry { |
|
100 |
||
101 |
// Class -> Registry |
|
23313
f100f0d49e0b
8035487: Fix raw and unchecked lint warnings in javax.imageio.spi
henryjen
parents:
22959
diff
changeset
|
102 |
private Map<Class<?>, SubRegistry> categoryMap = new HashMap<>(); |
2 | 103 |
|
104 |
/** |
|
105 |
* Constructs a <code>ServiceRegistry</code> instance with a |
|
106 |
* set of categories taken from the <code>categories</code> |
|
107 |
* argument. |
|
108 |
* |
|
109 |
* @param categories an <code>Iterator</code> containing |
|
110 |
* <code>Class</code> objects to be used to define categories. |
|
111 |
* |
|
112 |
* @exception IllegalArgumentException if |
|
113 |
* <code>categories</code> is <code>null</code>. |
|
114 |
*/ |
|
115 |
public ServiceRegistry(Iterator<Class<?>> categories) { |
|
116 |
if (categories == null) { |
|
117 |
throw new IllegalArgumentException("categories == null!"); |
|
118 |
} |
|
119 |
while (categories.hasNext()) { |
|
23313
f100f0d49e0b
8035487: Fix raw and unchecked lint warnings in javax.imageio.spi
henryjen
parents:
22959
diff
changeset
|
120 |
Class<?> category = categories.next(); |
2 | 121 |
SubRegistry reg = new SubRegistry(this, category); |
122 |
categoryMap.put(category, reg); |
|
123 |
} |
|
124 |
} |
|
125 |
||
126 |
/** |
|
127 |
* Searches for implementations of a particular service class |
|
128 |
* using the given class loader. |
|
129 |
* |
|
130 |
* <p> This method transforms the name of the given service class |
|
131 |
* into a provider-configuration filename as described in the |
|
132 |
* class comment and then uses the <code>getResources</code> |
|
133 |
* method of the given class loader to find all available files |
|
134 |
* with that name. These files are then read and parsed to |
|
135 |
* produce a list of provider-class names. The iterator that is |
|
136 |
* returned uses the given class loader to look up and then |
|
137 |
* instantiate each element of the list. |
|
138 |
* |
|
139 |
* <p> Because it is possible for extensions to be installed into |
|
140 |
* a running Java virtual machine, this method may return |
|
141 |
* different results each time it is invoked. |
|
142 |
* |
|
143 |
* @param providerClass a <code>Class</code>object indicating the |
|
144 |
* class or interface of the service providers being detected. |
|
145 |
* |
|
146 |
* @param loader the class loader to be used to load |
|
147 |
* provider-configuration files and instantiate provider classes, |
|
148 |
* or <code>null</code> if the system class loader (or, failing that |
|
149 |
* the bootstrap class loader) is to be used. |
|
150 |
* |
|
19168 | 151 |
* @param <T> the type of the providerClass. |
152 |
* |
|
2 | 153 |
* @return An <code>Iterator</code> that yields provider objects |
154 |
* for the given service, in some arbitrary order. The iterator |
|
155 |
* will throw an <code>Error</code> if a provider-configuration |
|
156 |
* file violates the specified format or if a provider class |
|
157 |
* cannot be found and instantiated. |
|
158 |
* |
|
159 |
* @exception IllegalArgumentException if |
|
160 |
* <code>providerClass</code> is <code>null</code>. |
|
161 |
*/ |
|
162 |
public static <T> Iterator<T> lookupProviders(Class<T> providerClass, |
|
163 |
ClassLoader loader) |
|
164 |
{ |
|
165 |
if (providerClass == null) { |
|
166 |
throw new IllegalArgumentException("providerClass == null!"); |
|
167 |
} |
|
168 |
return ServiceLoader.load(providerClass, loader).iterator(); |
|
169 |
} |
|
170 |
||
171 |
/** |
|
172 |
* Locates and incrementally instantiates the available providers |
|
173 |
* of a given service using the context class loader. This |
|
174 |
* convenience method is equivalent to: |
|
175 |
* |
|
176 |
* <pre> |
|
177 |
* ClassLoader cl = Thread.currentThread().getContextClassLoader(); |
|
178 |
* return Service.providers(service, cl); |
|
179 |
* </pre> |
|
180 |
* |
|
181 |
* @param providerClass a <code>Class</code>object indicating the |
|
182 |
* class or interface of the service providers being detected. |
|
183 |
* |
|
19168 | 184 |
* @param <T> the type of the providerClass. |
185 |
* |
|
2 | 186 |
* @return An <code>Iterator</code> that yields provider objects |
187 |
* for the given service, in some arbitrary order. The iterator |
|
188 |
* will throw an <code>Error</code> if a provider-configuration |
|
189 |
* file violates the specified format or if a provider class |
|
190 |
* cannot be found and instantiated. |
|
191 |
* |
|
192 |
* @exception IllegalArgumentException if |
|
193 |
* <code>providerClass</code> is <code>null</code>. |
|
194 |
*/ |
|
195 |
public static <T> Iterator<T> lookupProviders(Class<T> providerClass) { |
|
196 |
if (providerClass == null) { |
|
197 |
throw new IllegalArgumentException("providerClass == null!"); |
|
198 |
} |
|
199 |
return ServiceLoader.load(providerClass).iterator(); |
|
200 |
} |
|
201 |
||
202 |
/** |
|
203 |
* Returns an <code>Iterator</code> of <code>Class</code> objects |
|
204 |
* indicating the current set of categories. The iterator will be |
|
205 |
* empty if no categories exist. |
|
206 |
* |
|
207 |
* @return an <code>Iterator</code> containing |
|
208 |
* <code>Class</code>objects. |
|
209 |
*/ |
|
210 |
public Iterator<Class<?>> getCategories() { |
|
23313
f100f0d49e0b
8035487: Fix raw and unchecked lint warnings in javax.imageio.spi
henryjen
parents:
22959
diff
changeset
|
211 |
Set<Class<?>> keySet = categoryMap.keySet(); |
2 | 212 |
return keySet.iterator(); |
213 |
} |
|
214 |
||
215 |
/** |
|
216 |
* Returns an Iterator containing the subregistries to which the |
|
217 |
* provider belongs. |
|
218 |
*/ |
|
23313
f100f0d49e0b
8035487: Fix raw and unchecked lint warnings in javax.imageio.spi
henryjen
parents:
22959
diff
changeset
|
219 |
private Iterator<SubRegistry> getSubRegistries(Object provider) { |
f100f0d49e0b
8035487: Fix raw and unchecked lint warnings in javax.imageio.spi
henryjen
parents:
22959
diff
changeset
|
220 |
List<SubRegistry> l = new ArrayList<>(); |
f100f0d49e0b
8035487: Fix raw and unchecked lint warnings in javax.imageio.spi
henryjen
parents:
22959
diff
changeset
|
221 |
Iterator<Class<?>> iter = categoryMap.keySet().iterator(); |
2 | 222 |
while (iter.hasNext()) { |
23313
f100f0d49e0b
8035487: Fix raw and unchecked lint warnings in javax.imageio.spi
henryjen
parents:
22959
diff
changeset
|
223 |
Class<?> c = iter.next(); |
2 | 224 |
if (c.isAssignableFrom(provider.getClass())) { |
23313
f100f0d49e0b
8035487: Fix raw and unchecked lint warnings in javax.imageio.spi
henryjen
parents:
22959
diff
changeset
|
225 |
l.add(categoryMap.get(c)); |
2 | 226 |
} |
227 |
} |
|
228 |
return l.iterator(); |
|
229 |
} |
|
230 |
||
231 |
/** |
|
232 |
* Adds a service provider object to the registry. The provider |
|
233 |
* is associated with the given category. |
|
234 |
* |
|
235 |
* <p> If <code>provider</code> implements the |
|
236 |
* <code>RegisterableService</code> interface, its |
|
237 |
* <code>onRegistration</code> method will be called. Its |
|
238 |
* <code>onDeregistration</code> method will be called each time |
|
239 |
* it is deregistered from a category, for example if a |
|
240 |
* category is removed or the registry is garbage collected. |
|
241 |
* |
|
242 |
* @param provider the service provide object to be registered. |
|
243 |
* @param category the category under which to register the |
|
244 |
* provider. |
|
19168 | 245 |
* @param <T> the type of the provider. |
2 | 246 |
* |
247 |
* @return true if no provider of the same class was previously |
|
248 |
* registered in the same category category. |
|
249 |
* |
|
250 |
* @exception IllegalArgumentException if <code>provider</code> is |
|
251 |
* <code>null</code>. |
|
252 |
* @exception IllegalArgumentException if there is no category |
|
253 |
* corresponding to <code>category</code>. |
|
254 |
* @exception ClassCastException if provider does not implement |
|
255 |
* the <code>Class</code> defined by <code>category</code>. |
|
256 |
*/ |
|
257 |
public <T> boolean registerServiceProvider(T provider, |
|
258 |
Class<T> category) { |
|
259 |
if (provider == null) { |
|
260 |
throw new IllegalArgumentException("provider == null!"); |
|
261 |
} |
|
23313
f100f0d49e0b
8035487: Fix raw and unchecked lint warnings in javax.imageio.spi
henryjen
parents:
22959
diff
changeset
|
262 |
SubRegistry reg = categoryMap.get(category); |
2 | 263 |
if (reg == null) { |
264 |
throw new IllegalArgumentException("category unknown!"); |
|
265 |
} |
|
266 |
if (!category.isAssignableFrom(provider.getClass())) { |
|
267 |
throw new ClassCastException(); |
|
268 |
} |
|
269 |
||
270 |
return reg.registerServiceProvider(provider); |
|
271 |
} |
|
272 |
||
273 |
/** |
|
274 |
* Adds a service provider object to the registry. The provider |
|
275 |
* is associated within each category present in the registry |
|
276 |
* whose <code>Class</code> it implements. |
|
277 |
* |
|
278 |
* <p> If <code>provider</code> implements the |
|
279 |
* <code>RegisterableService</code> interface, its |
|
280 |
* <code>onRegistration</code> method will be called once for each |
|
281 |
* category it is registered under. Its |
|
282 |
* <code>onDeregistration</code> method will be called each time |
|
283 |
* it is deregistered from a category or when the registry is |
|
284 |
* finalized. |
|
285 |
* |
|
286 |
* @param provider the service provider object to be registered. |
|
287 |
* |
|
288 |
* @exception IllegalArgumentException if |
|
289 |
* <code>provider</code> is <code>null</code>. |
|
290 |
*/ |
|
291 |
public void registerServiceProvider(Object provider) { |
|
292 |
if (provider == null) { |
|
293 |
throw new IllegalArgumentException("provider == null!"); |
|
294 |
} |
|
23313
f100f0d49e0b
8035487: Fix raw and unchecked lint warnings in javax.imageio.spi
henryjen
parents:
22959
diff
changeset
|
295 |
Iterator<SubRegistry> regs = getSubRegistries(provider); |
2 | 296 |
while (regs.hasNext()) { |
23313
f100f0d49e0b
8035487: Fix raw and unchecked lint warnings in javax.imageio.spi
henryjen
parents:
22959
diff
changeset
|
297 |
SubRegistry reg = regs.next(); |
2 | 298 |
reg.registerServiceProvider(provider); |
299 |
} |
|
300 |
} |
|
301 |
||
302 |
/** |
|
303 |
* Adds a set of service provider objects, taken from an |
|
304 |
* <code>Iterator</code> to the registry. Each provider is |
|
305 |
* associated within each category present in the registry whose |
|
306 |
* <code>Class</code> it implements. |
|
307 |
* |
|
308 |
* <p> For each entry of <code>providers</code> that implements |
|
309 |
* the <code>RegisterableService</code> interface, its |
|
310 |
* <code>onRegistration</code> method will be called once for each |
|
311 |
* category it is registered under. Its |
|
312 |
* <code>onDeregistration</code> method will be called each time |
|
313 |
* it is deregistered from a category or when the registry is |
|
314 |
* finalized. |
|
315 |
* |
|
316 |
* @param providers an Iterator containing service provider |
|
317 |
* objects to be registered. |
|
318 |
* |
|
319 |
* @exception IllegalArgumentException if <code>providers</code> |
|
320 |
* is <code>null</code> or contains a <code>null</code> entry. |
|
321 |
*/ |
|
322 |
public void registerServiceProviders(Iterator<?> providers) { |
|
323 |
if (providers == null) { |
|
324 |
throw new IllegalArgumentException("provider == null!"); |
|
325 |
} |
|
326 |
while (providers.hasNext()) { |
|
327 |
registerServiceProvider(providers.next()); |
|
328 |
} |
|
329 |
} |
|
330 |
||
331 |
/** |
|
332 |
* Removes a service provider object from the given category. If |
|
333 |
* the provider was not previously registered, nothing happens and |
|
334 |
* <code>false</code> is returned. Otherwise, <code>true</code> |
|
335 |
* is returned. If an object of the same class as |
|
336 |
* <code>provider</code> but not equal (using <code>==</code>) to |
|
337 |
* <code>provider</code> is registered, it will not be |
|
338 |
* deregistered. |
|
339 |
* |
|
340 |
* <p> If <code>provider</code> implements the |
|
341 |
* <code>RegisterableService</code> interface, its |
|
342 |
* <code>onDeregistration</code> method will be called. |
|
343 |
* |
|
344 |
* @param provider the service provider object to be deregistered. |
|
345 |
* @param category the category from which to deregister the |
|
346 |
* provider. |
|
19168 | 347 |
* @param <T> the type of the provider. |
2 | 348 |
* |
349 |
* @return <code>true</code> if the provider was previously |
|
350 |
* registered in the same category category, |
|
351 |
* <code>false</code> otherwise. |
|
352 |
* |
|
353 |
* @exception IllegalArgumentException if <code>provider</code> is |
|
354 |
* <code>null</code>. |
|
355 |
* @exception IllegalArgumentException if there is no category |
|
356 |
* corresponding to <code>category</code>. |
|
357 |
* @exception ClassCastException if provider does not implement |
|
358 |
* the class defined by <code>category</code>. |
|
359 |
*/ |
|
360 |
public <T> boolean deregisterServiceProvider(T provider, |
|
361 |
Class<T> category) { |
|
362 |
if (provider == null) { |
|
363 |
throw new IllegalArgumentException("provider == null!"); |
|
364 |
} |
|
23313
f100f0d49e0b
8035487: Fix raw and unchecked lint warnings in javax.imageio.spi
henryjen
parents:
22959
diff
changeset
|
365 |
SubRegistry reg = categoryMap.get(category); |
2 | 366 |
if (reg == null) { |
367 |
throw new IllegalArgumentException("category unknown!"); |
|
368 |
} |
|
369 |
if (!category.isAssignableFrom(provider.getClass())) { |
|
370 |
throw new ClassCastException(); |
|
371 |
} |
|
372 |
return reg.deregisterServiceProvider(provider); |
|
373 |
} |
|
374 |
||
375 |
/** |
|
376 |
* Removes a service provider object from all categories that |
|
377 |
* contain it. |
|
378 |
* |
|
379 |
* @param provider the service provider object to be deregistered. |
|
380 |
* |
|
381 |
* @exception IllegalArgumentException if <code>provider</code> is |
|
382 |
* <code>null</code>. |
|
383 |
*/ |
|
384 |
public void deregisterServiceProvider(Object provider) { |
|
385 |
if (provider == null) { |
|
386 |
throw new IllegalArgumentException("provider == null!"); |
|
387 |
} |
|
23313
f100f0d49e0b
8035487: Fix raw and unchecked lint warnings in javax.imageio.spi
henryjen
parents:
22959
diff
changeset
|
388 |
Iterator<SubRegistry> regs = getSubRegistries(provider); |
2 | 389 |
while (regs.hasNext()) { |
23313
f100f0d49e0b
8035487: Fix raw and unchecked lint warnings in javax.imageio.spi
henryjen
parents:
22959
diff
changeset
|
390 |
SubRegistry reg = regs.next(); |
2 | 391 |
reg.deregisterServiceProvider(provider); |
392 |
} |
|
393 |
} |
|
394 |
||
395 |
/** |
|
396 |
* Returns <code>true</code> if <code>provider</code> is currently |
|
397 |
* registered. |
|
398 |
* |
|
399 |
* @param provider the service provider object to be queried. |
|
400 |
* |
|
401 |
* @return <code>true</code> if the given provider has been |
|
402 |
* registered. |
|
403 |
* |
|
404 |
* @exception IllegalArgumentException if <code>provider</code> is |
|
405 |
* <code>null</code>. |
|
406 |
*/ |
|
407 |
public boolean contains(Object provider) { |
|
408 |
if (provider == null) { |
|
409 |
throw new IllegalArgumentException("provider == null!"); |
|
410 |
} |
|
23313
f100f0d49e0b
8035487: Fix raw and unchecked lint warnings in javax.imageio.spi
henryjen
parents:
22959
diff
changeset
|
411 |
Iterator<SubRegistry> regs = getSubRegistries(provider); |
2 | 412 |
while (regs.hasNext()) { |
23313
f100f0d49e0b
8035487: Fix raw and unchecked lint warnings in javax.imageio.spi
henryjen
parents:
22959
diff
changeset
|
413 |
SubRegistry reg = regs.next(); |
2 | 414 |
if (reg.contains(provider)) { |
415 |
return true; |
|
416 |
} |
|
417 |
} |
|
418 |
||
419 |
return false; |
|
420 |
} |
|
421 |
||
422 |
/** |
|
423 |
* Returns an <code>Iterator</code> containing all registered |
|
424 |
* service providers in the given category. If |
|
425 |
* <code>useOrdering</code> is <code>false</code>, the iterator |
|
426 |
* will return all of the server provider objects in an arbitrary |
|
427 |
* order. Otherwise, the ordering will respect any pairwise |
|
428 |
* orderings that have been set. If the graph of pairwise |
|
429 |
* orderings contains cycles, any providers that belong to a cycle |
|
430 |
* will not be returned. |
|
431 |
* |
|
432 |
* @param category the category to be retrieved from. |
|
433 |
* @param useOrdering <code>true</code> if pairwise orderings |
|
434 |
* should be taken account in ordering the returned objects. |
|
19168 | 435 |
* @param <T> the type of the category. |
2 | 436 |
* |
437 |
* @return an <code>Iterator</code> containing service provider |
|
438 |
* objects from the given category, possibly in order. |
|
439 |
* |
|
440 |
* @exception IllegalArgumentException if there is no category |
|
441 |
* corresponding to <code>category</code>. |
|
442 |
*/ |
|
443 |
public <T> Iterator<T> getServiceProviders(Class<T> category, |
|
444 |
boolean useOrdering) { |
|
23313
f100f0d49e0b
8035487: Fix raw and unchecked lint warnings in javax.imageio.spi
henryjen
parents:
22959
diff
changeset
|
445 |
SubRegistry reg = categoryMap.get(category); |
2 | 446 |
if (reg == null) { |
447 |
throw new IllegalArgumentException("category unknown!"); |
|
448 |
} |
|
23313
f100f0d49e0b
8035487: Fix raw and unchecked lint warnings in javax.imageio.spi
henryjen
parents:
22959
diff
changeset
|
449 |
@SuppressWarnings("unchecked") |
f100f0d49e0b
8035487: Fix raw and unchecked lint warnings in javax.imageio.spi
henryjen
parents:
22959
diff
changeset
|
450 |
Iterator<T> it = (Iterator<T>)reg.getServiceProviders(useOrdering); |
f100f0d49e0b
8035487: Fix raw and unchecked lint warnings in javax.imageio.spi
henryjen
parents:
22959
diff
changeset
|
451 |
return it; |
2 | 452 |
} |
453 |
||
454 |
/** |
|
455 |
* A simple filter interface used by |
|
456 |
* <code>ServiceRegistry.getServiceProviders</code> to select |
|
457 |
* providers matching an arbitrary criterion. Classes that |
|
458 |
* implement this interface should be defined in order to make use |
|
459 |
* of the <code>getServiceProviders</code> method of |
|
460 |
* <code>ServiceRegistry</code> that takes a <code>Filter</code>. |
|
461 |
* |
|
462 |
* @see ServiceRegistry#getServiceProviders(Class, ServiceRegistry.Filter, boolean) |
|
463 |
*/ |
|
464 |
public interface Filter { |
|
465 |
||
466 |
/** |
|
467 |
* Returns <code>true</code> if the given |
|
468 |
* <code>provider</code> object matches the criterion defined |
|
469 |
* by this <code>Filter</code>. |
|
470 |
* |
|
471 |
* @param provider a service provider <code>Object</code>. |
|
472 |
* |
|
473 |
* @return true if the provider matches the criterion. |
|
474 |
*/ |
|
475 |
boolean filter(Object provider); |
|
476 |
} |
|
477 |
||
478 |
/** |
|
479 |
* Returns an <code>Iterator</code> containing service provider |
|
480 |
* objects within a given category that satisfy a criterion |
|
481 |
* imposed by the supplied <code>ServiceRegistry.Filter</code> |
|
482 |
* object's <code>filter</code> method. |
|
483 |
* |
|
484 |
* <p> The <code>useOrdering</code> argument controls the |
|
485 |
* ordering of the results using the same rules as |
|
486 |
* <code>getServiceProviders(Class, boolean)</code>. |
|
487 |
* |
|
488 |
* @param category the category to be retrieved from. |
|
489 |
* @param filter an instance of <code>ServiceRegistry.Filter</code> |
|
490 |
* whose <code>filter</code> method will be invoked. |
|
491 |
* @param useOrdering <code>true</code> if pairwise orderings |
|
492 |
* should be taken account in ordering the returned objects. |
|
19168 | 493 |
* @param <T> the type of the category. |
2 | 494 |
* |
495 |
* @return an <code>Iterator</code> containing service provider |
|
496 |
* objects from the given category, possibly in order. |
|
497 |
* |
|
498 |
* @exception IllegalArgumentException if there is no category |
|
499 |
* corresponding to <code>category</code>. |
|
500 |
*/ |
|
501 |
public <T> Iterator<T> getServiceProviders(Class<T> category, |
|
502 |
Filter filter, |
|
503 |
boolean useOrdering) { |
|
23313
f100f0d49e0b
8035487: Fix raw and unchecked lint warnings in javax.imageio.spi
henryjen
parents:
22959
diff
changeset
|
504 |
SubRegistry reg = categoryMap.get(category); |
2 | 505 |
if (reg == null) { |
506 |
throw new IllegalArgumentException("category unknown!"); |
|
507 |
} |
|
23313
f100f0d49e0b
8035487: Fix raw and unchecked lint warnings in javax.imageio.spi
henryjen
parents:
22959
diff
changeset
|
508 |
Iterator<T> iter = getServiceProviders(category, useOrdering); |
f100f0d49e0b
8035487: Fix raw and unchecked lint warnings in javax.imageio.spi
henryjen
parents:
22959
diff
changeset
|
509 |
return new FilterIterator<>(iter, filter); |
2 | 510 |
} |
511 |
||
512 |
/** |
|
513 |
* Returns the currently registered service provider object that |
|
514 |
* is of the given class type. At most one object of a given |
|
515 |
* class is allowed to be registered at any given time. If no |
|
516 |
* registered object has the desired class type, <code>null</code> |
|
517 |
* is returned. |
|
518 |
* |
|
519 |
* @param providerClass the <code>Class</code> of the desired |
|
520 |
* service provider object. |
|
19168 | 521 |
* @param <T> the type of the provider. |
2 | 522 |
* |
523 |
* @return a currently registered service provider object with the |
|
524 |
* desired <code>Class</code>type, or <code>null</code> is none is |
|
525 |
* present. |
|
526 |
* |
|
527 |
* @exception IllegalArgumentException if <code>providerClass</code> is |
|
528 |
* <code>null</code>. |
|
529 |
*/ |
|
530 |
public <T> T getServiceProviderByClass(Class<T> providerClass) { |
|
531 |
if (providerClass == null) { |
|
532 |
throw new IllegalArgumentException("providerClass == null!"); |
|
533 |
} |
|
23313
f100f0d49e0b
8035487: Fix raw and unchecked lint warnings in javax.imageio.spi
henryjen
parents:
22959
diff
changeset
|
534 |
Iterator<Class<?>> iter = categoryMap.keySet().iterator(); |
2 | 535 |
while (iter.hasNext()) { |
23313
f100f0d49e0b
8035487: Fix raw and unchecked lint warnings in javax.imageio.spi
henryjen
parents:
22959
diff
changeset
|
536 |
Class<?> c = iter.next(); |
2 | 537 |
if (c.isAssignableFrom(providerClass)) { |
23313
f100f0d49e0b
8035487: Fix raw and unchecked lint warnings in javax.imageio.spi
henryjen
parents:
22959
diff
changeset
|
538 |
SubRegistry reg = categoryMap.get(c); |
2 | 539 |
T provider = reg.getServiceProviderByClass(providerClass); |
540 |
if (provider != null) { |
|
541 |
return provider; |
|
542 |
} |
|
543 |
} |
|
544 |
} |
|
545 |
return null; |
|
546 |
} |
|
547 |
||
548 |
/** |
|
549 |
* Sets a pairwise ordering between two service provider objects |
|
550 |
* within a given category. If one or both objects are not |
|
551 |
* currently registered within the given category, or if the |
|
552 |
* desired ordering is already set, nothing happens and |
|
553 |
* <code>false</code> is returned. If the providers previously |
|
554 |
* were ordered in the reverse direction, that ordering is |
|
555 |
* removed. |
|
556 |
* |
|
557 |
* <p> The ordering will be used by the |
|
558 |
* <code>getServiceProviders</code> methods when their |
|
559 |
* <code>useOrdering</code> argument is <code>true</code>. |
|
560 |
* |
|
561 |
* @param category a <code>Class</code> object indicating the |
|
562 |
* category under which the preference is to be established. |
|
563 |
* @param firstProvider the preferred provider. |
|
564 |
* @param secondProvider the provider to which |
|
565 |
* <code>firstProvider</code> is preferred. |
|
19168 | 566 |
* @param <T> the type of the category. |
2 | 567 |
* |
568 |
* @return <code>true</code> if a previously unset ordering |
|
569 |
* was established. |
|
570 |
* |
|
571 |
* @exception IllegalArgumentException if either provider is |
|
572 |
* <code>null</code> or they are the same object. |
|
573 |
* @exception IllegalArgumentException if there is no category |
|
574 |
* corresponding to <code>category</code>. |
|
575 |
*/ |
|
576 |
public <T> boolean setOrdering(Class<T> category, |
|
577 |
T firstProvider, |
|
578 |
T secondProvider) { |
|
579 |
if (firstProvider == null || secondProvider == null) { |
|
580 |
throw new IllegalArgumentException("provider is null!"); |
|
581 |
} |
|
582 |
if (firstProvider == secondProvider) { |
|
583 |
throw new IllegalArgumentException("providers are the same!"); |
|
584 |
} |
|
23313
f100f0d49e0b
8035487: Fix raw and unchecked lint warnings in javax.imageio.spi
henryjen
parents:
22959
diff
changeset
|
585 |
SubRegistry reg = categoryMap.get(category); |
2 | 586 |
if (reg == null) { |
587 |
throw new IllegalArgumentException("category unknown!"); |
|
588 |
} |
|
589 |
if (reg.contains(firstProvider) && |
|
590 |
reg.contains(secondProvider)) { |
|
591 |
return reg.setOrdering(firstProvider, secondProvider); |
|
592 |
} |
|
593 |
return false; |
|
594 |
} |
|
595 |
||
596 |
/** |
|
597 |
* Sets a pairwise ordering between two service provider objects |
|
598 |
* within a given category. If one or both objects are not |
|
599 |
* currently registered within the given category, or if no |
|
600 |
* ordering is currently set between them, nothing happens |
|
601 |
* and <code>false</code> is returned. |
|
602 |
* |
|
603 |
* <p> The ordering will be used by the |
|
604 |
* <code>getServiceProviders</code> methods when their |
|
605 |
* <code>useOrdering</code> argument is <code>true</code>. |
|
606 |
* |
|
607 |
* @param category a <code>Class</code> object indicating the |
|
608 |
* category under which the preference is to be disestablished. |
|
609 |
* @param firstProvider the formerly preferred provider. |
|
610 |
* @param secondProvider the provider to which |
|
611 |
* <code>firstProvider</code> was formerly preferred. |
|
19168 | 612 |
* @param <T> the type of the category. |
2 | 613 |
* |
614 |
* @return <code>true</code> if a previously set ordering was |
|
615 |
* disestablished. |
|
616 |
* |
|
617 |
* @exception IllegalArgumentException if either provider is |
|
618 |
* <code>null</code> or they are the same object. |
|
619 |
* @exception IllegalArgumentException if there is no category |
|
620 |
* corresponding to <code>category</code>. |
|
621 |
*/ |
|
622 |
public <T> boolean unsetOrdering(Class<T> category, |
|
623 |
T firstProvider, |
|
624 |
T secondProvider) { |
|
625 |
if (firstProvider == null || secondProvider == null) { |
|
626 |
throw new IllegalArgumentException("provider is null!"); |
|
627 |
} |
|
628 |
if (firstProvider == secondProvider) { |
|
629 |
throw new IllegalArgumentException("providers are the same!"); |
|
630 |
} |
|
23313
f100f0d49e0b
8035487: Fix raw and unchecked lint warnings in javax.imageio.spi
henryjen
parents:
22959
diff
changeset
|
631 |
SubRegistry reg = categoryMap.get(category); |
2 | 632 |
if (reg == null) { |
633 |
throw new IllegalArgumentException("category unknown!"); |
|
634 |
} |
|
635 |
if (reg.contains(firstProvider) && |
|
636 |
reg.contains(secondProvider)) { |
|
637 |
return reg.unsetOrdering(firstProvider, secondProvider); |
|
638 |
} |
|
639 |
return false; |
|
640 |
} |
|
641 |
||
642 |
/** |
|
643 |
* Deregisters all service provider object currently registered |
|
644 |
* under the given category. |
|
645 |
* |
|
646 |
* @param category the category to be emptied. |
|
647 |
* |
|
648 |
* @exception IllegalArgumentException if there is no category |
|
649 |
* corresponding to <code>category</code>. |
|
650 |
*/ |
|
651 |
public void deregisterAll(Class<?> category) { |
|
23313
f100f0d49e0b
8035487: Fix raw and unchecked lint warnings in javax.imageio.spi
henryjen
parents:
22959
diff
changeset
|
652 |
SubRegistry reg = categoryMap.get(category); |
2 | 653 |
if (reg == null) { |
654 |
throw new IllegalArgumentException("category unknown!"); |
|
655 |
} |
|
656 |
reg.clear(); |
|
657 |
} |
|
658 |
||
659 |
/** |
|
660 |
* Deregisters all currently registered service providers from all |
|
661 |
* categories. |
|
662 |
*/ |
|
663 |
public void deregisterAll() { |
|
23313
f100f0d49e0b
8035487: Fix raw and unchecked lint warnings in javax.imageio.spi
henryjen
parents:
22959
diff
changeset
|
664 |
Iterator<SubRegistry> iter = categoryMap.values().iterator(); |
2 | 665 |
while (iter.hasNext()) { |
23313
f100f0d49e0b
8035487: Fix raw and unchecked lint warnings in javax.imageio.spi
henryjen
parents:
22959
diff
changeset
|
666 |
SubRegistry reg = iter.next(); |
2 | 667 |
reg.clear(); |
668 |
} |
|
669 |
} |
|
670 |
||
671 |
/** |
|
672 |
* Finalizes this object prior to garbage collection. The |
|
673 |
* <code>deregisterAll</code> method is called to deregister all |
|
674 |
* currently registered service providers. This method should not |
|
675 |
* be called from application code. |
|
676 |
* |
|
677 |
* @exception Throwable if an error occurs during superclass |
|
678 |
* finalization. |
|
679 |
*/ |
|
680 |
public void finalize() throws Throwable { |
|
681 |
deregisterAll(); |
|
682 |
super.finalize(); |
|
683 |
} |
|
684 |
} |
|
685 |
||
686 |
||
687 |
/** |
|
688 |
* A portion of a registry dealing with a single superclass or |
|
689 |
* interface. |
|
690 |
*/ |
|
691 |
class SubRegistry { |
|
692 |
||
693 |
ServiceRegistry registry; |
|
694 |
||
23313
f100f0d49e0b
8035487: Fix raw and unchecked lint warnings in javax.imageio.spi
henryjen
parents:
22959
diff
changeset
|
695 |
Class<?> category; |
2 | 696 |
|
697 |
// Provider Objects organized by partial oridering |
|
23313
f100f0d49e0b
8035487: Fix raw and unchecked lint warnings in javax.imageio.spi
henryjen
parents:
22959
diff
changeset
|
698 |
PartiallyOrderedSet<Object> poset = new PartiallyOrderedSet<>(); |
2 | 699 |
|
700 |
// Class -> Provider Object of that class |
|
23313
f100f0d49e0b
8035487: Fix raw and unchecked lint warnings in javax.imageio.spi
henryjen
parents:
22959
diff
changeset
|
701 |
// No way to express heterogeneous map, we want |
f100f0d49e0b
8035487: Fix raw and unchecked lint warnings in javax.imageio.spi
henryjen
parents:
22959
diff
changeset
|
702 |
// Map<Class<T>, T>, where T is ? |
f100f0d49e0b
8035487: Fix raw and unchecked lint warnings in javax.imageio.spi
henryjen
parents:
22959
diff
changeset
|
703 |
Map<Class<?>, Object> map = new HashMap<>(); |
2 | 704 |
|
23313
f100f0d49e0b
8035487: Fix raw and unchecked lint warnings in javax.imageio.spi
henryjen
parents:
22959
diff
changeset
|
705 |
public SubRegistry(ServiceRegistry registry, Class<?> category) { |
2 | 706 |
this.registry = registry; |
707 |
this.category = category; |
|
708 |
} |
|
709 |
||
710 |
public boolean registerServiceProvider(Object provider) { |
|
711 |
Object oprovider = map.get(provider.getClass()); |
|
712 |
boolean present = oprovider != null; |
|
713 |
||
714 |
if (present) { |
|
715 |
deregisterServiceProvider(oprovider); |
|
716 |
} |
|
717 |
map.put(provider.getClass(), provider); |
|
718 |
poset.add(provider); |
|
719 |
if (provider instanceof RegisterableService) { |
|
720 |
RegisterableService rs = (RegisterableService)provider; |
|
721 |
rs.onRegistration(registry, category); |
|
722 |
} |
|
723 |
||
724 |
return !present; |
|
725 |
} |
|
726 |
||
727 |
/** |
|
728 |
* If the provider was not previously registered, do nothing. |
|
729 |
* |
|
730 |
* @return true if the provider was previously registered. |
|
731 |
*/ |
|
732 |
public boolean deregisterServiceProvider(Object provider) { |
|
733 |
Object oprovider = map.get(provider.getClass()); |
|
734 |
||
735 |
if (provider == oprovider) { |
|
736 |
map.remove(provider.getClass()); |
|
737 |
poset.remove(provider); |
|
738 |
if (provider instanceof RegisterableService) { |
|
739 |
RegisterableService rs = (RegisterableService)provider; |
|
740 |
rs.onDeregistration(registry, category); |
|
741 |
} |
|
742 |
||
743 |
return true; |
|
744 |
} |
|
745 |
return false; |
|
746 |
} |
|
747 |
||
748 |
public boolean contains(Object provider) { |
|
749 |
Object oprovider = map.get(provider.getClass()); |
|
750 |
return oprovider == provider; |
|
751 |
} |
|
752 |
||
753 |
public boolean setOrdering(Object firstProvider, |
|
754 |
Object secondProvider) { |
|
755 |
return poset.setOrdering(firstProvider, secondProvider); |
|
756 |
} |
|
757 |
||
758 |
public boolean unsetOrdering(Object firstProvider, |
|
759 |
Object secondProvider) { |
|
760 |
return poset.unsetOrdering(firstProvider, secondProvider); |
|
761 |
} |
|
762 |
||
23313
f100f0d49e0b
8035487: Fix raw and unchecked lint warnings in javax.imageio.spi
henryjen
parents:
22959
diff
changeset
|
763 |
public Iterator<Object> getServiceProviders(boolean useOrdering) { |
2 | 764 |
if (useOrdering) { |
765 |
return poset.iterator(); |
|
766 |
} else { |
|
767 |
return map.values().iterator(); |
|
768 |
} |
|
769 |
} |
|
770 |
||
23313
f100f0d49e0b
8035487: Fix raw and unchecked lint warnings in javax.imageio.spi
henryjen
parents:
22959
diff
changeset
|
771 |
@SuppressWarnings("unchecked") |
2 | 772 |
public <T> T getServiceProviderByClass(Class<T> providerClass) { |
773 |
return (T)map.get(providerClass); |
|
774 |
} |
|
775 |
||
776 |
public void clear() { |
|
23313
f100f0d49e0b
8035487: Fix raw and unchecked lint warnings in javax.imageio.spi
henryjen
parents:
22959
diff
changeset
|
777 |
Iterator<Object> iter = map.values().iterator(); |
2 | 778 |
while (iter.hasNext()) { |
779 |
Object provider = iter.next(); |
|
780 |
iter.remove(); |
|
781 |
||
782 |
if (provider instanceof RegisterableService) { |
|
783 |
RegisterableService rs = (RegisterableService)provider; |
|
784 |
rs.onDeregistration(registry, category); |
|
785 |
} |
|
786 |
} |
|
787 |
poset.clear(); |
|
788 |
} |
|
789 |
||
790 |
public void finalize() { |
|
791 |
clear(); |
|
792 |
} |
|
793 |
} |
|
794 |
||
795 |
||
796 |
/** |
|
797 |
* A class for wrapping <code>Iterators</code> with a filter function. |
|
798 |
* This provides an iterator for a subset without duplication. |
|
799 |
*/ |
|
800 |
class FilterIterator<T> implements Iterator<T> { |
|
801 |
||
23313
f100f0d49e0b
8035487: Fix raw and unchecked lint warnings in javax.imageio.spi
henryjen
parents:
22959
diff
changeset
|
802 |
private Iterator<? extends T> iter; |
2 | 803 |
private ServiceRegistry.Filter filter; |
804 |
||
805 |
private T next = null; |
|
806 |
||
23313
f100f0d49e0b
8035487: Fix raw and unchecked lint warnings in javax.imageio.spi
henryjen
parents:
22959
diff
changeset
|
807 |
public FilterIterator(Iterator<? extends T> iter, |
2 | 808 |
ServiceRegistry.Filter filter) { |
809 |
this.iter = iter; |
|
810 |
this.filter = filter; |
|
811 |
advance(); |
|
812 |
} |
|
813 |
||
814 |
private void advance() { |
|
815 |
while (iter.hasNext()) { |
|
816 |
T elt = iter.next(); |
|
817 |
if (filter.filter(elt)) { |
|
818 |
next = elt; |
|
819 |
return; |
|
820 |
} |
|
821 |
} |
|
822 |
||
823 |
next = null; |
|
824 |
} |
|
825 |
||
826 |
public boolean hasNext() { |
|
827 |
return next != null; |
|
828 |
} |
|
829 |
||
830 |
public T next() { |
|
831 |
if (next == null) { |
|
832 |
throw new NoSuchElementException(); |
|
833 |
} |
|
834 |
T o = next; |
|
835 |
advance(); |
|
836 |
return o; |
|
837 |
} |
|
838 |
||
839 |
public void remove() { |
|
840 |
throw new UnsupportedOperationException(); |
|
841 |
} |
|
842 |
} |