ServiceRegistry.java 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962
  1. /* ServiceRegistry.java -- A simple registry for service providers.
  2. Copyright (C) 2004, 2005 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package javax.imageio.spi;
  32. import gnu.classpath.ServiceFactory;
  33. import java.util.ArrayList;
  34. import java.util.Collection;
  35. import java.util.Collections;
  36. import java.util.Comparator;
  37. import java.util.HashSet;
  38. import java.util.IdentityHashMap;
  39. import java.util.Iterator;
  40. import java.util.LinkedList;
  41. import java.util.Map;
  42. import java.util.NoSuchElementException;
  43. import java.util.Set;
  44. /**
  45. * A registry for service providers.
  46. *
  47. * @since 1.4
  48. *
  49. * @author Michael Koch (konqueror@gmx.de)
  50. * @author Sascha Brawer (brawer@dandelis.ch)
  51. */
  52. public class ServiceRegistry
  53. {
  54. // Package-private to avoid a trampoline.
  55. /**
  56. * The service categories of this registry.
  57. *
  58. * <p>Note that we expect that only very few categories will
  59. * typically be used with a registry. The most common case will be
  60. * one, it seems unlikely that any registry would contain more than
  61. * five or six categories. Therefore, we intentionally avoid the
  62. * overhead of a HashMap.
  63. *
  64. * @see #providers
  65. */
  66. final Class[] categories;
  67. /**
  68. * The registered providers for each service category, indexed by
  69. * the same index as the {@link #categories} array. If no provider
  70. * is registered for a category, the array entry will be
  71. * <code>null</code>.
  72. *
  73. * <p>Note that we expect that only very few providers will
  74. * typically be registered for a category. The most common case will
  75. * be one or two. Therefore, we intentionally avoid the overhead of
  76. * a HashMap.
  77. */
  78. private final LinkedList[] providers;
  79. /**
  80. * The ordring constaints for each service category, indexed by the
  81. * same index as the {@link #categories} array. The constraints for
  82. * a service category are stored as a <code>Map&lt;Object,
  83. * Set&lt;Object&gt;&gt;</code>, where the Map&#x2019;s values are
  84. * those providers that need to come after the key. If no
  85. * constraints are imposed on the providers of a category, the array
  86. * entry will be <code>null</code>. If no constraints have been set
  87. * whatsoever, <code>constraints</code> will be <code>null</code>.
  88. *
  89. * <p>Note that we expect that only very few constraints will
  90. * typically be imposed on a category. The most common case will
  91. * be zero.
  92. */
  93. private IdentityHashMap[] constraints;
  94. /**
  95. * Constructs a <code>ServiceRegistry</code> for the specified
  96. * service categories.
  97. *
  98. * @param categories the categories to support
  99. *
  100. * @throws IllegalArgumentException if <code>categories</code> is
  101. * <code>null</code>, or if its {@link Iterator#next()} method
  102. * returns <code>null</code>.
  103. *
  104. * @throws ClassCastException if <code>categories</code> does not
  105. * iterate over instances of {@link java.lang.Class}.
  106. */
  107. public ServiceRegistry(Iterator<Class<?>> categories)
  108. {
  109. ArrayList cats = new ArrayList(/* expected size */ 10);
  110. if (categories == null)
  111. throw new IllegalArgumentException();
  112. while (categories.hasNext())
  113. {
  114. Class cat = (Class) categories.next();
  115. if (cat == null)
  116. throw new IllegalArgumentException();
  117. cats.add(cat);
  118. }
  119. int numCats = cats.size();
  120. this.categories = (Class[]) cats.toArray(new Class[numCats]);
  121. this.providers = new LinkedList[numCats];
  122. }
  123. /**
  124. * Finds service providers that are implementing the specified
  125. * Service Provider Interface.
  126. *
  127. * <p><b>On-demand loading:</b> Loading and initializing service
  128. * providers is delayed as much as possible. The rationale is that
  129. * typical clients will iterate through the set of installed service
  130. * providers until one is found that matches some criteria (like
  131. * supported formats, or quality of service). In such scenarios, it
  132. * might make sense to install only the frequently needed service
  133. * providers on the local machine. More exotic providers can be put
  134. * onto a server; the server will only be contacted when no suitable
  135. * service could be found locally.</p>
  136. *
  137. * <p><b>Security considerations:</b> Any loaded service providers
  138. * are loaded through the specified ClassLoader, or the system
  139. * ClassLoader if <code>classLoader</code> is
  140. * <code>null</code>. When <code>lookupProviders</code> is called,
  141. * the current {@link java.security.AccessControlContext} gets
  142. * recorded. This captured security context will determine the
  143. * permissions when services get loaded via the <code>next()</code>
  144. * method of the returned <code>Iterator</code>.</p>
  145. *
  146. * @param spi the service provider interface which must be
  147. * implemented by any loaded service providers.
  148. *
  149. * @param loader the class loader that will be used to load the
  150. * service providers, or <code>null</code> for the system class
  151. * loader. For using the context class loader, see {@link
  152. * #lookupProviders(Class)}.
  153. *
  154. * @return an iterator over instances of <code>spi</code>.
  155. *
  156. * @throws IllegalArgumentException if <code>spi</code> is
  157. * <code>null</code>.
  158. */
  159. public static <T> Iterator<T> lookupProviders(Class<T> spi,
  160. ClassLoader loader)
  161. {
  162. return ServiceFactory.lookupProviders(spi, loader);
  163. }
  164. /**
  165. * Finds service providers that are implementing the specified
  166. * Service Provider Interface, using the context class loader
  167. * for loading providers.
  168. *
  169. * @param spi the service provider interface which must be
  170. * implemented by any loaded service providers.
  171. *
  172. * @return an iterator over instances of <code>spi</code>.
  173. *
  174. * @throws IllegalArgumentException if <code>spi</code> is
  175. * <code>null</code>.
  176. *
  177. * @see #lookupProviders(Class, ClassLoader)
  178. */
  179. public static <T> Iterator<T> lookupProviders(Class<T> spi)
  180. {
  181. return ServiceFactory.lookupProviders(spi);
  182. }
  183. /**
  184. * Returns an iterator over all service categories.
  185. *
  186. * @return an unmodifiable {@link
  187. * java.util.Iterator}&lt;{@link java.lang.Class}&gt;.
  188. */
  189. public Iterator<Class<?>> getCategories()
  190. {
  191. return new Iterator()
  192. {
  193. int index = -1;
  194. public boolean hasNext()
  195. {
  196. return index < categories.length - 1;
  197. }
  198. public Object next()
  199. {
  200. if (!hasNext())
  201. throw new NoSuchElementException();
  202. return categories[++index];
  203. }
  204. public void remove()
  205. {
  206. throw new UnsupportedOperationException();
  207. }
  208. };
  209. }
  210. /**
  211. * Registers a provider for a service category which is specified by
  212. * the class-internal category ID.
  213. *
  214. * @param provider the service provider to be registered.
  215. *
  216. * @param cat the service category, which is identified by an index
  217. * into the {@link #categories} array.
  218. *
  219. * @return <code>true</code> if <code>provider</code> is the first
  220. * provider that gets registered for the specified service category;
  221. * <code>false</code> if other providers have already been
  222. * registered for the same servide category.
  223. *
  224. * @throws IllegalArgumentException if <code>provider</code> is
  225. * <code>null</code>.
  226. *
  227. * @throws ClassCastException if <code>provider</code> does not
  228. * implement the specified service provider interface.
  229. */
  230. private synchronized boolean registerServiceProvider(Object provider,
  231. int cat)
  232. {
  233. LinkedList provs;
  234. boolean result;
  235. Class category;
  236. if (provider == null)
  237. throw new IllegalArgumentException();
  238. category = categories[cat];
  239. if (!category.isInstance(provider))
  240. throw new ClassCastException(category.getName());
  241. provs = providers[cat];
  242. if (provs == null)
  243. {
  244. result = true;
  245. provs = providers[cat] = new LinkedList();
  246. }
  247. else
  248. result = false;
  249. provs.add(provider);
  250. if (provider instanceof RegisterableService)
  251. ((RegisterableService) provider).onRegistration(this, category);
  252. return result;
  253. }
  254. /**
  255. * Registers a provider for the specified service category.
  256. *
  257. * <p>If <code>provider</code> implements the {@link
  258. * RegisterableService} interface, its {@link
  259. * RegisterableService#onRegistration onRegistration} method is
  260. * invoked in order to inform the provider about the addition to
  261. * this registry.
  262. *
  263. * @param provider the service provider to be registered.
  264. *
  265. * @param category the service category under which
  266. * <code>provider</code> shall be registered.
  267. *
  268. * @return <code>true</code> if <code>provider</code> is the first
  269. * provider that gets registered for the specified service category;
  270. * <code>false</code> if other providers have already been
  271. * registered for the same servide category.
  272. *
  273. * @throws IllegalArgumentException if <code>provider</code> is
  274. * <code>null</code>, or if <code>category</code> is not among the
  275. * categories passed to the {@linkplain #ServiceRegistry(Iterator)
  276. * constructor} of this ServiceRegistry.
  277. *
  278. * @throws ClassCastException if <code>provider</code> does not
  279. * implement <code>category</code>.
  280. */
  281. public synchronized <T> boolean registerServiceProvider(T provider,
  282. Class<T> category)
  283. {
  284. for (int i = 0; i < categories.length; i++)
  285. if (categories[i] == category)
  286. return registerServiceProvider(provider, i);
  287. throw new IllegalArgumentException();
  288. }
  289. /**
  290. * Registers a provider under all service categories it
  291. * implements.
  292. *
  293. * <p>If <code>provider</code> implements the {@link
  294. * RegisterableService} interface, its {@link
  295. * RegisterableService#onRegistration onRegistration} method is
  296. * invoked in order to inform the provider about the addition to
  297. * this registry. If <code>provider</code> implements several
  298. * service categories, <code>onRegistration</code> gets called
  299. * multiple times.
  300. *
  301. * @param provider the service provider to be registered.
  302. *
  303. * @throws IllegalArgumentException if <code>provider</code> is
  304. * <code>null</code>, or if <code>provider</code> does not implement
  305. * any of the service categories passed to the {@linkplain
  306. * #ServiceRegistry(Iterator) constructor} of this ServiceRegistry.
  307. */
  308. public synchronized void registerServiceProvider(Object provider)
  309. {
  310. boolean ok = false;
  311. if (provider == null)
  312. throw new IllegalArgumentException();
  313. for (int i = 0; i < categories.length; i++)
  314. if (categories[i].isInstance(provider))
  315. {
  316. ok = true;
  317. registerServiceProvider(provider, i);
  318. }
  319. if (!ok)
  320. throw new IllegalArgumentException();
  321. }
  322. /**
  323. * Registers a number of providers under all service categories they
  324. * implement.
  325. *
  326. * <p>If a provider implements the {@link RegisterableService}
  327. * interface, its {@link RegisterableService#onRegistration
  328. * onRegistration} method is invoked in order to inform the provider
  329. * about the addition to this registry. If <code>provider</code>
  330. * implements several service categories,
  331. * <code>onRegistration</code> gets called multiple times.
  332. *
  333. * @throws IllegalArgumentException if <code>providers</code> is
  334. * <code>null</code>, if any iterated provider is <code>null</code>,
  335. * or if some iterated provider does not implement any of the
  336. * service categories passed to the {@linkplain
  337. * #ServiceRegistry(Iterator) constructor} of this
  338. * <code>ServiceRegistry</code>.
  339. */
  340. public synchronized void registerServiceProviders(Iterator<?> providers)
  341. {
  342. if (providers == null)
  343. throw new IllegalArgumentException();
  344. while (providers.hasNext())
  345. registerServiceProvider(providers.next());
  346. }
  347. /**
  348. * De-registers a provider for a service category which is specified
  349. * by the class-internal category ID.
  350. *
  351. * @param provider the service provider to be registered.
  352. *
  353. * @param cat the service category, which is identified by an index
  354. * into the {@link #categories} array.
  355. *
  356. * @return <code>true</code> if <code>provider</code> was previously
  357. * registered for the specified service category; <code>false</code>
  358. * if if the provider had not been registered.
  359. *
  360. * @throws IllegalArgumentException if <code>provider</code> is
  361. * <code>null</code>.
  362. *
  363. * @throws ClassCastException if <code>provider</code> does not
  364. * implement the specified service provider interface.
  365. */
  366. private synchronized boolean deregisterServiceProvider(Object provider,
  367. int cat)
  368. {
  369. LinkedList provs;
  370. boolean result;
  371. Class category;
  372. if (provider == null)
  373. throw new IllegalArgumentException();
  374. category = categories[cat];
  375. if (!category.isInstance(provider))
  376. throw new ClassCastException(category.getName());
  377. provs = providers[cat];
  378. if (provs == null)
  379. return false;
  380. result = provs.remove(provider);
  381. if (provs.isEmpty())
  382. providers[cat] = null;
  383. if (result && (provider instanceof RegisterableService))
  384. ((RegisterableService) provider).onDeregistration(this, category);
  385. return result;
  386. }
  387. /**
  388. * De-registers a provider for the specified service category.
  389. *
  390. * <p>If <code>provider</code> implements the {@link
  391. * RegisterableService} interface, its {@link
  392. * RegisterableService#onDeregistration onDeregistration} method is
  393. * invoked in order to inform the provider about the removal from
  394. * this registry.
  395. *
  396. * @param provider the service provider to be de-registered.
  397. *
  398. * @param category the service category from which
  399. * <code>provider</code> shall be de-registered.
  400. *
  401. * @return <code>true</code> if <code>provider</code> was previously
  402. * registered for the specified service category; <code>false</code>
  403. * if if the provider had not been registered.
  404. *
  405. * @throws IllegalArgumentException if <code>provider</code> is
  406. * <code>null</code>, or if <code>category</code> is not among the
  407. * categories passed to the {@linkplain #ServiceRegistry(Iterator)
  408. * constructor} of this ServiceRegistry.
  409. *
  410. * @throws ClassCastException if <code>provider</code> does not
  411. * implement <code>category</code>.
  412. */
  413. public synchronized <T> boolean deregisterServiceProvider(T provider,
  414. Class<T> category)
  415. {
  416. for (int i = 0; i < categories.length; i++)
  417. if (categories[i] == category)
  418. return deregisterServiceProvider(provider, i);
  419. throw new IllegalArgumentException();
  420. }
  421. /**
  422. * De-registers a provider from all service categories it
  423. * implements.
  424. *
  425. * <p>If <code>provider</code> implements the {@link
  426. * RegisterableService} interface, its {@link
  427. * RegisterableService#onDeregistration onDeregistration} method is
  428. * invoked in order to inform the provider about the removal from
  429. * this registry. If <code>provider</code> implements several
  430. * service categories, <code>onDeregistration</code> gets called
  431. * multiple times.</p>
  432. *
  433. * @param provider the service provider to be de-registered.
  434. *
  435. * @throws IllegalArgumentException if <code>provider</code> is
  436. * <code>null</code>, or if <code>provider</code> does not implement
  437. * any of the service categories passed to the {@linkplain
  438. * #ServiceRegistry(Iterator) constructor} of this
  439. * <code>ServiceRegistry</code>.
  440. */
  441. public synchronized void deregisterServiceProvider(Object provider)
  442. {
  443. boolean ok = false;
  444. if (provider == null)
  445. throw new IllegalArgumentException();
  446. for (int i = 0; i < categories.length; i++)
  447. if (categories[i].isInstance(provider))
  448. {
  449. ok = true;
  450. deregisterServiceProvider(provider, i);
  451. }
  452. if (!ok)
  453. throw new IllegalArgumentException();
  454. }
  455. /**
  456. * De-registers all providers which have been registered for the
  457. * specified service category.
  458. *
  459. * <p>If a provider implements the {@link RegisterableService}
  460. * interface, its {@link RegisterableService#onDeregistration
  461. * onDeregistration} method is invoked in order to inform the
  462. * provider about the removal from this registry. If the provider
  463. * implements several service categories,
  464. * <code>onDeregistration</code> gets called multiple times.
  465. *
  466. * @param category the category whose registered providers will be
  467. * de-registered.
  468. *
  469. * @throws IllegalArgumentException if <code>category</code> is not
  470. * among the categories passed to the {@linkplain
  471. * #ServiceRegistry(Iterator) constructor} of this
  472. * <code>ServiceRegistry</code>.
  473. */
  474. public synchronized void deregisterAll(Class<?> category)
  475. {
  476. boolean ok = false;
  477. for (int i = 0; i < categories.length; i++)
  478. {
  479. if (categories[i] != category)
  480. continue;
  481. ok = true;
  482. while (providers[i] != null)
  483. deregisterServiceProvider(providers[i].get(0), i);
  484. }
  485. if (!ok)
  486. throw new IllegalArgumentException();
  487. }
  488. /**
  489. * De-registers all service providers.
  490. *
  491. * <p>If a provider implements the {@link RegisterableService}
  492. * interface, its {@link RegisterableService#onDeregistration
  493. * onDeregistration} method is invoked in order to inform the
  494. * provider about the removal from this registry. If the provider
  495. * implements several service categories,
  496. * <code>onDeregistration</code> gets called multiple times.
  497. */
  498. public synchronized void deregisterAll()
  499. {
  500. for (int i = 0; i < categories.length; i++)
  501. while (providers[i] != null)
  502. deregisterServiceProvider(providers[i].get(0), i);
  503. }
  504. /**
  505. * Called by the Virtual Machine when it detects that this
  506. * <code>ServiceRegistry</code> has become garbage. De-registers all
  507. * service providers, which will cause those that implement {@link
  508. * RegisterableService} to receive a {@link
  509. * RegisterableService#onDeregistration onDeregistration}
  510. * notification.
  511. */
  512. public void finalize()
  513. throws Throwable
  514. {
  515. super.finalize();
  516. deregisterAll();
  517. }
  518. /**
  519. * Determines whether a provider has been registered with this
  520. * registry.
  521. *
  522. * @return <code>true</code> if <code>provider</code> has been
  523. * registered under any service category; <code>false</code> if
  524. * it is not registered.
  525. *
  526. * @throws IllegalArgumentException if <code>provider</code> is
  527. * <code>null</code>.
  528. */
  529. public synchronized boolean contains(Object provider)
  530. {
  531. if (provider == null)
  532. throw new IllegalArgumentException();
  533. // Note that contains is rather unlikely to be ever called,
  534. // so it would be wasteful to keep a special data structure
  535. // (such as a HashSet) for making it a fast operation.
  536. for (int i = 0; i < providers.length; i++)
  537. {
  538. // If provider does not implement categories[i],
  539. // it would not have been possible to register it there.
  540. // In that case, it would be pointless to look there.
  541. if (!categories[i].isInstance(provider))
  542. continue;
  543. // But if the list of registered providers contains provider,
  544. // we have found it.
  545. LinkedList p = providers[i];
  546. if (p != null && p.contains(provider))
  547. return true;
  548. }
  549. return false;
  550. }
  551. /**
  552. * Returns the index in {@link #categories} occupied by the
  553. * specified service category.
  554. *
  555. * @throws IllegalArgumentException if <code>category</code> is not
  556. * among the categories passed to the {@linkplain
  557. * #ServiceRegistry(Iterator) constructor} of this ServiceRegistry.
  558. */
  559. private int getCategoryID(Class category)
  560. {
  561. for (int i = 0; i < categories.length; i++)
  562. if (categories[i] == category)
  563. return i;
  564. throw new IllegalArgumentException();
  565. }
  566. /**
  567. * Retrieves all providers that have been registered for the
  568. * specified service category.
  569. *
  570. * @param category the service category whose providers are
  571. * to be retrieved.
  572. *
  573. * @param useOrdering <code>true</code> in order to retrieve the
  574. * providers in an order imposed by the {@linkplain #setOrdering
  575. * ordering constraints}; <code>false</code> in order to retrieve
  576. * the providers in any order.
  577. *
  578. * @throws IllegalArgumentException if <code>category</code> is not
  579. * among the categories passed to the {@linkplain
  580. * #ServiceRegistry(Iterator) constructor} of this
  581. * <code>ServiceRegistry</code>.
  582. *
  583. * @see #getServiceProviders(Class, Filter, boolean)
  584. */
  585. public <T> Iterator<T> getServiceProviders(Class<T> category,
  586. boolean useOrdering)
  587. {
  588. return getServiceProviders(category, null, useOrdering);
  589. }
  590. /**
  591. * Retrieves all providers that have been registered for the
  592. * specified service category and that satisfy the criteria
  593. * of a custom filter.
  594. *
  595. * @param category the service category whose providers are
  596. * to be retrieved.
  597. *
  598. * @param filter a custom filter, or <code>null</code> to
  599. * retrieve all registered providers for the specified
  600. * category.
  601. *
  602. * @param useOrdering <code>true</code> in order to retrieve the
  603. * providers in an order imposed by the {@linkplain #setOrdering
  604. * ordering constraints}; <code>false</code> in order to retrieve
  605. * the providers in any order.
  606. *
  607. * @throws IllegalArgumentException if <code>category</code> is not
  608. * among the categories passed to the {@linkplain
  609. * #ServiceRegistry(Iterator) constructor} of this
  610. * <code>ServiceRegistry</code>.
  611. */
  612. public synchronized <T> Iterator<T> getServiceProviders(Class<T> category,
  613. Filter filter,
  614. boolean useOrdering)
  615. {
  616. int catid;
  617. LinkedList provs;
  618. ArrayList result;
  619. catid = getCategoryID(category);
  620. provs = providers[catid];
  621. if (provs == null)
  622. return Collections.EMPTY_LIST.iterator();
  623. result = new ArrayList(provs.size());
  624. for (Iterator iter = provs.iterator(); iter.hasNext();)
  625. {
  626. Object provider = iter.next();
  627. if (filter == null || filter.filter(provider))
  628. result.add(provider);
  629. }
  630. // If we are supposed to obey ordering constraints, and
  631. // if any constraints have been imposed on the specified
  632. // service category, sort the result.
  633. if (useOrdering && constraints != null)
  634. {
  635. final Map cons = constraints[catid];
  636. if (cons != null)
  637. Collections.sort(result, new Comparator()
  638. {
  639. public int compare(Object o1, Object o2)
  640. {
  641. Set s;
  642. if (o1 == o2)
  643. return 0;
  644. s = (Set) cons.get(o1);
  645. if (s != null && s.contains(o2))
  646. return -1; // o1 < o2
  647. s = (Set) cons.get(o2);
  648. if (s != null && s.contains(o1))
  649. return 1; // o1 > o2
  650. return 0; // o1 == o2
  651. }
  652. });
  653. }
  654. return result.iterator();
  655. }
  656. /**
  657. * Returns one of the service providers that is a subclass of the
  658. * specified class.
  659. *
  660. * @param providerClass a class to search for.
  661. */
  662. public synchronized <T> T getServiceProviderByClass(Class<T> providerClass)
  663. {
  664. if (providerClass == null)
  665. throw new IllegalArgumentException();
  666. // Note that the method getServiceProviderByClass is rather
  667. // unlikely to be ever called, so it would be wasteful to keep a
  668. // special data structure for making it a fast operation.
  669. for (int cat = 0; cat < categories.length; cat++)
  670. {
  671. if (!categories[cat].isAssignableFrom(providerClass))
  672. continue;
  673. LinkedList provs = providers[cat];
  674. if (provs == null)
  675. continue;
  676. for (Iterator iter = provs.iterator(); iter.hasNext();)
  677. {
  678. Object provider = iter.next();
  679. if (providerClass.isInstance(provider))
  680. return (T) provider;
  681. }
  682. }
  683. return null;
  684. }
  685. /**
  686. * Adds an ordering constraint on service providers.
  687. *
  688. * @param category the service category to which an ordering
  689. * constraint is to be added.
  690. *
  691. * @param firstProvider the provider which is supposed to come before
  692. * <code>second</code>.
  693. *
  694. * @param secondProvider the provider which is supposed to come after
  695. * <code>first</code>.
  696. *
  697. * @throws IllegalArgumentException if <code>first</code> and
  698. * <code>second</code> are referring to the same object, or if one
  699. * of them is <code>null</code>.
  700. *
  701. * @see #unsetOrdering
  702. * @see #getServiceProviders(Class, Filter, boolean)
  703. */
  704. public synchronized <T> boolean setOrdering(Class<T> category,
  705. T firstProvider,
  706. T secondProvider)
  707. {
  708. return addConstraint(getCategoryID(category), firstProvider,
  709. secondProvider);
  710. }
  711. /**
  712. * Removes an ordering constraint on service providers.
  713. *
  714. * @param category the service category from which an ordering
  715. * constraint is to be removed.
  716. *
  717. * @param firstProvider the provider which is supposed to come before
  718. * <code>second</code>.
  719. *
  720. * @param secondProvider the provider which is supposed to come after
  721. * <code>first</code>.
  722. *
  723. * @throws IllegalArgumentException if <code>first</code> and
  724. * <code>second</code> are referring to the same object, or if one
  725. * of them is <code>null</code>.
  726. *
  727. * @see #setOrdering
  728. */
  729. public synchronized <T> boolean unsetOrdering(Class<T> category,
  730. T firstProvider,
  731. T secondProvider)
  732. {
  733. return removeConstraint(getCategoryID(category),
  734. firstProvider, secondProvider);
  735. }
  736. /**
  737. * Adds an ordering constraint on service providers.
  738. *
  739. * @param catid the service category ID, which is the
  740. * category&#x2019;s index into the {@link #categories} array.
  741. *
  742. * @param first the provider which is supposed to come before
  743. * <code>second</code>.
  744. *
  745. * @param second the provider which is supposed to come after
  746. * <code>first</code>.
  747. *
  748. * @throws IllegalArgumentException if <code>first</code> and
  749. * <code>second</code> are referring to the same object, or if one
  750. * of them is <code>null</code>.
  751. */
  752. private boolean addConstraint(int catid, Object first, Object second)
  753. {
  754. Set s;
  755. IdentityHashMap cons;
  756. // Also checks argument validity.
  757. removeConstraint(catid, second, first);
  758. if (constraints == null)
  759. constraints = new IdentityHashMap[categories.length];
  760. cons = constraints[catid];
  761. if (cons == null)
  762. cons = constraints[catid] = new IdentityHashMap();
  763. s = (Set) cons.get(first);
  764. if (s == null)
  765. cons.put(first, s = new HashSet());
  766. return s.add(second);
  767. }
  768. /**
  769. * Removes an ordering constraint on service providers.
  770. *
  771. * @param catid the service category ID, which is the
  772. * category&#x2019;s index into the {@link #categories} array.
  773. *
  774. * @param first the provider which is supposed to come before
  775. * <code>second</code>.
  776. *
  777. * @param second the provider which is supposed to come after
  778. * <code>first</code>.
  779. *
  780. * @throws IllegalArgumentException if <code>first</code> and
  781. * <code>second</code> are referring to the same object, or if one
  782. * of them is <code>null</code>.
  783. */
  784. private boolean removeConstraint(int catid, Object first, Object second)
  785. {
  786. Collection s;
  787. IdentityHashMap cons;
  788. if (first == null || second == null || first == second)
  789. throw new IllegalArgumentException();
  790. if (constraints == null)
  791. return false;
  792. cons = constraints[catid];
  793. if (cons == null)
  794. return false;
  795. s = (Collection) cons.get(first);
  796. if (s == null)
  797. return false;
  798. if (!s.remove(second))
  799. return false;
  800. // If we removed the last constraint for a service category,
  801. // we can get free some memory.
  802. if (cons.isEmpty())
  803. {
  804. constraints[catid] = null;
  805. boolean anyConstraints = false;
  806. for (int i = 0; i < constraints.length; i++)
  807. {
  808. if (constraints[i] != null)
  809. {
  810. anyConstraints = true;
  811. break;
  812. }
  813. }
  814. if (!anyConstraints)
  815. constraints = null;
  816. }
  817. return true;
  818. }
  819. /**
  820. * A filter for selecting service providers that match custom
  821. * criteria.
  822. *
  823. * @see ServiceRegistry#getServiceProviders(Class, Filter,
  824. * boolean)
  825. *
  826. * @since 1.4
  827. *
  828. * @author Michael Koch (konqueror@gmx.de)
  829. * @author Sascha Brawer (brawer@dandelis.ch)
  830. */
  831. public static interface Filter
  832. {
  833. /**
  834. * Checks whether the specified service provider matches the
  835. * constraints of this Filter.
  836. *
  837. * @param provider the service provider in question.
  838. *
  839. * @return <code>true</code> if <code>provider</code> matches the
  840. * criteria; <code>false</code> if it does not match.
  841. */
  842. boolean filter(Object provider);
  843. }
  844. }