BeanContextServicesSupport.java 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897
  1. /* BeanContextServicesSupport.java --
  2. Copyright (C) 2003, 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 java.beans.beancontext;
  32. import java.io.IOException;
  33. import java.io.ObjectInputStream;
  34. import java.io.ObjectOutputStream;
  35. import java.io.Serializable;
  36. import java.util.ArrayList;
  37. import java.util.HashMap;
  38. import java.util.HashSet;
  39. import java.util.Iterator;
  40. import java.util.List;
  41. import java.util.Locale;
  42. import java.util.Set;
  43. import java.util.TooManyListenersException;
  44. /**
  45. * This is a helper class for implementing a bean context which
  46. * supplies services. It is intended to be used either by
  47. * subclassing or by calling methods of this implementation
  48. * from another.
  49. *
  50. * @author Michael Koch
  51. * @author Andrew John Hughes (gnu_andrew@member.fsf.org)
  52. * @since 1.2
  53. */
  54. public class BeanContextServicesSupport
  55. extends BeanContextSupport
  56. implements BeanContextServices
  57. {
  58. private static final long serialVersionUID = -8494482757288719206L;
  59. protected class BCSSChild
  60. extends BeanContextSupport.BCSChild
  61. {
  62. private static final long serialVersionUID = -3263851306889194873L;
  63. BCSSChild(Object targetChild, Object peer)
  64. {
  65. super(targetChild, peer);
  66. }
  67. }
  68. protected class BCSSProxyServiceProvider
  69. implements BeanContextServiceProvider,
  70. BeanContextServiceRevokedListener
  71. {
  72. private static final long serialVersionUID = 7078212910685744490L;
  73. private BeanContextServiceProvider provider;
  74. BCSSProxyServiceProvider(BeanContextServiceProvider p)
  75. {
  76. provider = p;
  77. }
  78. public Iterator getCurrentServiceSelectors (BeanContextServices bcs,
  79. Class serviceClass)
  80. {
  81. return provider.getCurrentServiceSelectors(bcs, serviceClass);
  82. }
  83. public Object getService (BeanContextServices bcs,
  84. Object requestor,
  85. Class serviceClass,
  86. Object serviceSelector)
  87. {
  88. return provider.getService(bcs, requestor, serviceClass,
  89. serviceSelector);
  90. }
  91. public void releaseService (BeanContextServices bcs,
  92. Object requestor,
  93. Object service)
  94. {
  95. provider.releaseService(bcs, requestor, service);
  96. }
  97. public void serviceRevoked (BeanContextServiceRevokedEvent bcsre)
  98. {
  99. if (provider instanceof BeanContextServiceRevokedListener)
  100. ((BeanContextServiceRevokedListener) provider).serviceRevoked(bcsre);
  101. }
  102. }
  103. protected static class BCSSServiceProvider
  104. implements Serializable
  105. {
  106. private static final long serialVersionUID = 861278251667444782L;
  107. protected BeanContextServiceProvider serviceProvider;
  108. private Class serviceClass;
  109. private BCSSServiceProvider(Class serviceClass,
  110. BeanContextServiceProvider provider)
  111. {
  112. this.serviceClass = serviceClass;
  113. serviceProvider = provider;
  114. }
  115. protected BeanContextServiceProvider getServiceProvider()
  116. {
  117. return serviceProvider;
  118. }
  119. private Class getServiceClass()
  120. {
  121. return serviceClass;
  122. }
  123. }
  124. /**
  125. * Represents a request for a service. This is
  126. * a common superclass used by the classes which maintain
  127. * the listener-requestor and service-requestor relationships.
  128. *
  129. * @author Andrew John Hughes (gnu_andrew@member.fsf.org)
  130. */
  131. private static abstract class Request
  132. {
  133. private Object requestor;
  134. public Request(Object requestor)
  135. {
  136. this.requestor = requestor;
  137. }
  138. public boolean equals(Object obj)
  139. {
  140. if (obj instanceof Request)
  141. {
  142. Request req = (Request) obj;
  143. return req.getRequestor().equals(requestor);
  144. }
  145. return false;
  146. }
  147. public Object getRequestor()
  148. {
  149. return requestor;
  150. }
  151. }
  152. /**
  153. * Represents a relationship between a service requestor
  154. * and a revocation listener.
  155. *
  156. * @author Andrew John Hughes (gnu_andrew@member.fsf.org)
  157. */
  158. private static class ServiceRequest
  159. extends Request
  160. {
  161. private BeanContextServiceRevokedListener listener;
  162. public ServiceRequest(Object requestor,
  163. BeanContextServiceRevokedListener listener)
  164. {
  165. super(requestor);
  166. this.listener = listener;
  167. }
  168. public boolean equals(Object obj)
  169. {
  170. if (obj instanceof ServiceRequest)
  171. {
  172. ServiceRequest sr = (ServiceRequest) obj;
  173. return (super.equals(obj) &&
  174. sr.getListener().equals(listener));
  175. }
  176. return false;
  177. }
  178. public BeanContextServiceRevokedListener getListener()
  179. {
  180. return listener;
  181. }
  182. }
  183. /**
  184. * Represents a relationship between a service requestor
  185. * and a service instance.
  186. *
  187. * @author Andrew John Hughes (gnu_andrew@member.fsf.org)
  188. */
  189. private static class ServiceLease
  190. extends Request
  191. {
  192. private Object service;
  193. public ServiceLease(Object requestor, Object service)
  194. {
  195. super(requestor);
  196. this.service = service;
  197. }
  198. public boolean equals(Object obj)
  199. {
  200. if (obj instanceof ServiceLease)
  201. {
  202. ServiceLease sl = (ServiceLease) obj;
  203. return (super.equals(obj) &&
  204. sl.getService().equals(service));
  205. }
  206. return false;
  207. }
  208. public Object getService()
  209. {
  210. return service;
  211. }
  212. }
  213. /**
  214. * A collection of listeners who receive availability
  215. * and revocation notifications.
  216. */
  217. protected transient ArrayList bcsListeners;
  218. protected transient BCSSProxyServiceProvider proxy;
  219. /**
  220. * The number of serializable service providers.
  221. */
  222. protected transient int serializable;
  223. /**
  224. * A map of registered services, linking the service
  225. * class to its associated {@link BCSSServiceProvider}.
  226. */
  227. protected transient HashMap services;
  228. /**
  229. * A map of children to a list of services they
  230. * have obtained.
  231. */
  232. private transient HashMap serviceUsers;
  233. /**
  234. * A map of services to {@link ServiceRequest}s.
  235. */
  236. private transient HashMap serviceRequests;
  237. /**
  238. * A map of {@link ServiceLease}s to providers.
  239. */
  240. private transient HashMap serviceLeases;
  241. /**
  242. * Construct a {@link BeanContextServicesSupport} instance.
  243. */
  244. public BeanContextServicesSupport ()
  245. {
  246. super();
  247. }
  248. /**
  249. * Construct a {@link BeanContextServicesSupport} instance.
  250. *
  251. * @param peer the bean context services peer (<code>null</code> permitted).
  252. */
  253. public BeanContextServicesSupport (BeanContextServices peer)
  254. {
  255. super(peer);
  256. }
  257. /**
  258. * Construct a {@link BeanContextServicesSupport} instance.
  259. *
  260. * @param peer the bean context peer (<code>null</code> permitted).
  261. * @param locale the locale (<code>null</code> permitted, equivalent to
  262. * the default locale).
  263. */
  264. public BeanContextServicesSupport(BeanContextServices peer, Locale locale)
  265. {
  266. super(peer, locale);
  267. }
  268. /**
  269. * Construct a {@link BeanContextServicesSupport} instance.
  270. *
  271. * @param peer the bean context peer (<code>null</code> permitted).
  272. * @param locale the locale (<code>null</code> permitted, equivalent to
  273. * the default locale).
  274. * @param dtime a flag indicating whether or not the bean context is in
  275. * design time mode.
  276. */
  277. public BeanContextServicesSupport(BeanContextServices peer, Locale locale,
  278. boolean dtime)
  279. {
  280. super(peer, locale, dtime);
  281. }
  282. /**
  283. * Construct a {@link BeanContextServicesSupport} instance.
  284. *
  285. * @param peer the bean context peer (<code>null</code> permitted).
  286. * @param locale the locale (<code>null</code> permitted, equivalent to
  287. * the default locale).
  288. * @param dtime a flag indicating whether or not the bean context is in
  289. * design time mode.
  290. * @param visible initial value of the <code>okToUseGui</code> flag.
  291. */
  292. public BeanContextServicesSupport(BeanContextServices peer, Locale locale,
  293. boolean dtime, boolean visible)
  294. {
  295. super(peer, locale, dtime, visible);
  296. }
  297. /**
  298. * Adds a new listener for service availability and
  299. * revocation events.
  300. *
  301. * @param listener the listener to add.
  302. */
  303. public void addBeanContextServicesListener
  304. (BeanContextServicesListener listener)
  305. {
  306. synchronized (bcsListeners)
  307. {
  308. if (! bcsListeners.contains(listener))
  309. bcsListeners.add(listener);
  310. }
  311. }
  312. /**
  313. * Registers a new service from the specified service provider.
  314. * The service is internally associated with the service provider
  315. * and a <code>BeanContextServiceAvailableEvent</code> is fired. If
  316. * the service is already registered, then this method instead
  317. * returns <code>false</code>. This is equivalent to calling
  318. * <code>addService(serviceClass, bcsp, true)</code>.
  319. *
  320. * @param serviceClass the class of the service to be registered.
  321. * @param bcsp the provider of the given service.
  322. * @return true if the service was registered successfully.
  323. * @see #addService(Class, BeanContextServiceProvider, boolean)
  324. */
  325. public boolean addService (Class serviceClass,
  326. BeanContextServiceProvider bcsp)
  327. {
  328. return addService(serviceClass, bcsp, true);
  329. }
  330. /**
  331. * Registers a new service from the specified service provider.
  332. * The service is internally associated with the service provider
  333. * and (if <code>fireEvent</code> is true) a
  334. * <code>BeanContextServiceAvailableEvent</code> is fired. If
  335. * the service is already registered, then this method instead
  336. * returns <code>false</code>.
  337. *
  338. * @param serviceClass the class of the service to be registered.
  339. * @param bcsp the provider of the given service.
  340. * @param fireEvent true if a service availability event should
  341. * be fired.
  342. * @return true if the service was registered successfully.
  343. */
  344. protected boolean addService (Class serviceClass,
  345. BeanContextServiceProvider bcsp,
  346. boolean fireEvent)
  347. {
  348. synchronized (globalHierarchyLock)
  349. {
  350. synchronized (services)
  351. {
  352. if (services.containsKey(serviceClass))
  353. return false;
  354. services.put(serviceClass,
  355. createBCSSServiceProvider(serviceClass, bcsp));
  356. if (bcsp instanceof Serializable)
  357. ++serializable;
  358. if (fireEvent)
  359. fireServiceAdded(serviceClass);
  360. return true;
  361. }
  362. }
  363. }
  364. /**
  365. * Deserializes any service providers which are serializable. This
  366. * method is called by the <code>readObject</code> method of
  367. * {@link BeanContextSupport} prior to deserialization of the children.
  368. * Subclasses may envelope its behaviour in order to read further
  369. * serialized data to the stream.
  370. *
  371. * @param ois the stream from which data is being deserialized.
  372. * @throws IOException if an I/O error occurs.
  373. * @throws ClassNotFoundException if the class of a deserialized object
  374. * can not be found.
  375. */
  376. protected void bcsPreDeserializationHook (ObjectInputStream ois)
  377. throws ClassNotFoundException, IOException
  378. {
  379. serializable = ois.readInt();
  380. for (int a = 0; a < serializable; ++a)
  381. {
  382. BCSSServiceProvider bcsssp = (BCSSServiceProvider) ois.readObject();
  383. addService(bcsssp.getServiceClass(), bcsssp.getServiceProvider());
  384. }
  385. }
  386. /**
  387. * Serializes any service providers which are serializable. This
  388. * method is called by the <code>writeObject</code> method of
  389. * {@link BeanContextSupport} prior to serialization of the children.
  390. * Subclasses may envelope its behaviour in order to add further
  391. * serialized data to the stream.
  392. *
  393. * @param oos the stream to which data is being serialized.
  394. * @throws IOException if an I/O error occurs.
  395. */
  396. protected void bcsPreSerializationHook (ObjectOutputStream oos)
  397. throws IOException
  398. {
  399. oos.writeInt(serializable);
  400. synchronized (services)
  401. {
  402. Iterator i = services.values().iterator();
  403. while (i.hasNext())
  404. {
  405. BCSSServiceProvider bcsssp = (BCSSServiceProvider) i.next();
  406. if (bcsssp.getServiceProvider() instanceof Serializable)
  407. oos.writeObject(bcsssp);
  408. }
  409. }
  410. }
  411. /**
  412. * Revokes any services used by a child that has just been removed.
  413. * The superclass ({@link BeanContextSupport}) calls this method
  414. * when a child has just been successfully removed. Subclasses can
  415. * extend this method in order to perform additional operations
  416. * on child removal.
  417. *
  418. * @param child the child being removed.
  419. * @param bcsc the support object for the child.
  420. */
  421. protected void childJustRemovedHook (Object child,
  422. BeanContextSupport.BCSChild bcsc)
  423. {
  424. if (child instanceof BeanContextChild)
  425. {
  426. BeanContextChild bcchild = (BeanContextChild) child;
  427. Iterator childServices = ((List) serviceUsers.get(bcchild)).iterator();
  428. while (childServices.hasNext())
  429. releaseService(bcchild, this, childServices.next());
  430. serviceUsers.remove(bcchild);
  431. }
  432. }
  433. /**
  434. * Overrides the {@link BeanContextSupport#createBCSChild} method
  435. * so as to use a {@link BCSSChild} instead.
  436. *
  437. * @param targetChild the child to create the child for.
  438. * @param peer the peer which relates to the child if a proxy is used.
  439. * @return a new instance of {@link BCSSChild}.
  440. */
  441. protected BeanContextSupport.BCSChild createBCSChild (Object targetChild,
  442. Object peer)
  443. {
  444. return new BCSSChild(targetChild, peer);
  445. }
  446. /**
  447. * Provides a hook so that subclasses can replace the
  448. * {@link BCSSServiceProvider} class, used to store registered
  449. * service providers, with a subclass without replacing the
  450. * {@link #addService(Class, BeanContextServiceProvider)} method.
  451. *
  452. * @param sc the class of service being registered.
  453. * @param bcsp the provider of the service.
  454. * @return a instance of {@link BCSSServiceProvider} wrapping the provider.
  455. */
  456. protected BeanContextServicesSupport.BCSSServiceProvider
  457. createBCSSServiceProvider (Class sc, BeanContextServiceProvider bcsp)
  458. {
  459. return new BCSSServiceProvider(sc, bcsp);
  460. }
  461. /**
  462. * Sends a <code>BeanContextServiceAvailableEvent</code> to all
  463. * registered listeners.
  464. *
  465. * @param bcssae the event to send.
  466. */
  467. protected final void fireServiceAdded (BeanContextServiceAvailableEvent bcssae)
  468. {
  469. synchronized (bcsListeners)
  470. {
  471. int size = bcsListeners.size();
  472. for (int i = 0; i < size; ++i)
  473. {
  474. BeanContextServicesListener bcsl
  475. = (BeanContextServicesListener) bcsListeners.get(i);
  476. bcsl.serviceAvailable(bcssae);
  477. }
  478. }
  479. }
  480. /**
  481. * Sends a <code>BeanContextServiceAvailableEvent</code> to all
  482. * registered listeners.
  483. *
  484. * @param serviceClass the service that is now available.
  485. * @see #fireServiceAdded(BeanContextServiceAvailableEvent)
  486. */
  487. protected final void fireServiceAdded (Class serviceClass)
  488. {
  489. fireServiceAdded(new BeanContextServiceAvailableEvent(this,
  490. serviceClass));
  491. }
  492. /**
  493. * Sends a <code>BeanContextServiceRevokedEvent</code> to all
  494. * registered listeners.
  495. *
  496. * @param event the event to send.
  497. */
  498. protected final void fireServiceRevoked(BeanContextServiceRevokedEvent event)
  499. {
  500. synchronized (bcsListeners)
  501. {
  502. int size = bcsListeners.size();
  503. for (int i = 0; i < size; ++i)
  504. {
  505. BeanContextServicesListener bcsl
  506. = (BeanContextServicesListener) bcsListeners.get(i);
  507. bcsl.serviceRevoked(event);
  508. }
  509. List requests = (List) serviceRequests.get(event.getServiceClass());
  510. if (requests != null)
  511. {
  512. Iterator i = requests.iterator();
  513. while (i.hasNext())
  514. {
  515. ServiceRequest r = (ServiceRequest) i.next();
  516. r.getListener().serviceRevoked(event);
  517. }
  518. }
  519. }
  520. }
  521. /**
  522. * Sends a <code>BeanContextServiceRevokedEvent</code> to all
  523. * registered listeners.
  524. *
  525. * @param serviceClass the service that has been revoked.
  526. * @see #fireServiceRevoked(BeanContextServiceRevokedEvent)
  527. */
  528. protected final void fireServiceRevoked (Class serviceClass,
  529. boolean revokeNow)
  530. {
  531. fireServiceRevoked(new BeanContextServiceRevokedEvent(this, serviceClass,
  532. revokeNow));
  533. }
  534. /**
  535. * Returns the services peer given at construction time,
  536. * or <code>null</code> if no peer was given.
  537. *
  538. * @return the {@link BeanContextServices} peer.
  539. */
  540. public BeanContextServices getBeanContextServicesPeer ()
  541. {
  542. return (BeanContextServices) beanContextChildPeer;
  543. }
  544. /**
  545. * Returns <code>child</code> as an instance of
  546. * {@link BeanContextServicesListener}, or <code>null</code> if
  547. * <code>child</code> does not implement that interface.
  548. *
  549. * @param child the child (<code>null</code> permitted).
  550. *
  551. * @return The child cast to {@link BeanContextServicesListener}.
  552. */
  553. protected static final BeanContextServicesListener
  554. getChildBeanContextServicesListener(Object child)
  555. {
  556. if (child instanceof BeanContextServicesListener)
  557. return (BeanContextServicesListener) child;
  558. else
  559. return null;
  560. }
  561. /**
  562. * Returns an iterator over the currently available
  563. * services.
  564. *
  565. * @return an iterator over the currently available services.
  566. */
  567. public Iterator getCurrentServiceClasses ()
  568. {
  569. synchronized (globalHierarchyLock)
  570. {
  571. synchronized (services)
  572. {
  573. return services.keySet().iterator();
  574. }
  575. }
  576. }
  577. /**
  578. * Returns an iterator over the service selectors of the service
  579. * provider for the given service. The iterator is actually
  580. * obtained by calling the
  581. * {@link BeanContextServiceProvider#getCurrentServiceSelectors}
  582. * of the provider itself. If the specified service is not available,
  583. * <code>null</code> is returned.
  584. *
  585. * @param serviceClass the service whose provider's selectors should
  586. * be iterated over.
  587. * @return an {@link Iterator} over the service selectors of the
  588. * provider of the given service.
  589. */
  590. public Iterator getCurrentServiceSelectors (Class serviceClass)
  591. {
  592. synchronized (globalHierarchyLock)
  593. {
  594. synchronized (services)
  595. {
  596. BeanContextServiceProvider bcsp
  597. = ((BCSSServiceProvider)
  598. services.get(serviceClass)).getServiceProvider();
  599. if (bcsp == null)
  600. return null;
  601. else
  602. return bcsp.getCurrentServiceSelectors(this, serviceClass);
  603. }
  604. }
  605. }
  606. /**
  607. * Retrieves the specified service. If a provider for the service
  608. * is registered in this context, then the request is passed on to
  609. * the provider and the service returned. Otherwise, the request
  610. * is delegated to a parent {@link BeanContextServices}, if possible.
  611. * If the service can not be found at all, then <code>null</code>
  612. * is returned.
  613. *
  614. * @param child the child obtaining the reference.
  615. * @param requestor the requestor of the service, which may be the
  616. * child itself.
  617. * @param serviceClass the service being requested.
  618. * @param serviceSelector an additional service-dependent parameter
  619. * (may be <code>null</code> if not appropriate).
  620. * @param bcsrl a listener used to notify the requestor that the service
  621. * has since been revoked.
  622. * @return a reference to the service requested, or <code>null</code>.
  623. * @throws TooManyListenersException according to Sun's documentation.
  624. */
  625. public Object getService (BeanContextChild child, Object requestor,
  626. Class serviceClass, Object serviceSelector,
  627. BeanContextServiceRevokedListener bcsrl)
  628. throws TooManyListenersException
  629. {
  630. synchronized (globalHierarchyLock)
  631. {
  632. synchronized (services)
  633. {
  634. Object service;
  635. BeanContextServiceProvider provider = ((BCSSServiceProvider)
  636. services.get(serviceClass)).getServiceProvider();
  637. if (provider != null)
  638. {
  639. service = provider.getService(this, requestor, serviceClass,
  640. serviceSelector);
  641. List childServices = (List) serviceUsers.get(child);
  642. if (childServices == null)
  643. {
  644. childServices = new ArrayList();
  645. serviceUsers.put(child, childServices);
  646. }
  647. childServices.add(serviceClass);
  648. }
  649. else
  650. {
  651. BeanContextServices peer = getBeanContextServicesPeer();
  652. if (peer != null)
  653. service = peer.getService(child, requestor, serviceClass,
  654. serviceSelector, bcsrl);
  655. else
  656. service = null;
  657. }
  658. if (service != null)
  659. {
  660. ServiceRequest request = new ServiceRequest(requestor, bcsrl);
  661. Set requests = (Set) serviceRequests.get(serviceClass);
  662. if (requests == null)
  663. {
  664. requests = new HashSet();
  665. serviceRequests.put(serviceClass, requests);
  666. }
  667. requests.add(request);
  668. ServiceLease lease = new ServiceLease(requestor, service);
  669. serviceLeases.put(lease, provider);
  670. }
  671. return service;
  672. }
  673. }
  674. }
  675. /**
  676. * Returns true if the specified service is available.
  677. *
  678. * @param serviceClass the service to check for.
  679. * @return true if the service is available.
  680. */
  681. public boolean hasService (Class serviceClass)
  682. {
  683. synchronized (globalHierarchyLock)
  684. {
  685. synchronized (services)
  686. {
  687. return services.containsKey(serviceClass);
  688. }
  689. }
  690. }
  691. public void initialize ()
  692. {
  693. super.initialize();
  694. bcsListeners = new ArrayList();
  695. services = new HashMap();
  696. serviceUsers = new HashMap();
  697. serviceRequests = new HashMap();
  698. serviceLeases = new HashMap();
  699. }
  700. /**
  701. * Subclasses may override this method to allocate resources
  702. * from the nesting bean context.
  703. */
  704. protected void initializeBeanContextResources()
  705. {
  706. /* Purposefully left empty */
  707. }
  708. /**
  709. * Relinquishes any resources obtained from the parent context.
  710. * Specifically, those services obtained from the parent are revoked.
  711. * Subclasses may override this method to deallocate resources
  712. * from the nesting bean context.
  713. */
  714. protected void releaseBeanContextResources()
  715. {
  716. /* Purposefully left empty */
  717. }
  718. /**
  719. * Releases the reference to a service held by a
  720. * {@link BeanContextChild} (or an arbitrary object associated
  721. * with it). It simply calls the appropriate method on the
  722. * underlying provider.
  723. *
  724. * @param child the child who holds the reference.
  725. * @param requestor the object that requested the reference.
  726. * @param service the service being released.
  727. */
  728. public void releaseService (BeanContextChild child, Object requestor,
  729. Object service)
  730. {
  731. synchronized (globalHierarchyLock)
  732. {
  733. synchronized (services)
  734. {
  735. ServiceLease lease = new ServiceLease(requestor, service);
  736. BeanContextServiceProvider provider = (BeanContextServiceProvider)
  737. serviceLeases.get(lease);
  738. if (provider != null)
  739. provider.releaseService(this, requestor, service);
  740. else
  741. {
  742. BeanContextServices peer = getBeanContextServicesPeer();
  743. if (peer != null)
  744. peer.releaseService(child, requestor, service);
  745. }
  746. serviceLeases.remove(lease);
  747. }
  748. }
  749. }
  750. public void removeBeanContextServicesListener
  751. (BeanContextServicesListener listener)
  752. {
  753. synchronized (bcsListeners)
  754. {
  755. bcsListeners.remove(listener);
  756. }
  757. }
  758. /**
  759. * Revokes the given service. A {@link BeanContextServiceRevokedEvent} is
  760. * emitted to all registered {@link BeanContextServiceRevokedListener}s
  761. * and {@link BeanContextServiceListener}s. If <code>revokeCurrentServicesNow</code>
  762. * is true, termination of the service is immediate. Otherwise, prior
  763. * acquisitions of the service by requestors remain valid.
  764. *
  765. * @param serviceClass the service to revoke.
  766. * @param bcsp the provider of the revoked service.
  767. * @param revokeCurrentServicesNow true if this is an exceptional circumstance
  768. * where service should be immediately revoked.
  769. */
  770. public void revokeService (Class serviceClass, BeanContextServiceProvider bcsp,
  771. boolean revokeCurrentServicesNow)
  772. {
  773. synchronized (globalHierarchyLock)
  774. {
  775. synchronized (services)
  776. {
  777. fireServiceRevoked(serviceClass, revokeCurrentServicesNow);
  778. services.remove(serviceClass);
  779. if (bcsp instanceof Serializable)
  780. --serializable;
  781. }
  782. }
  783. }
  784. public void serviceAvailable (BeanContextServiceAvailableEvent bcssae)
  785. {
  786. synchronized (services)
  787. {
  788. Class klass = bcssae.getServiceClass();
  789. if (services.containsKey(klass))
  790. return;
  791. Iterator it = bcsChildren();
  792. while (it.hasNext())
  793. {
  794. Object obj = it.next();
  795. if (obj instanceof BeanContextServices)
  796. ((BeanContextServices) obj).serviceAvailable(bcssae);
  797. }
  798. }
  799. }
  800. public void serviceRevoked (BeanContextServiceRevokedEvent bcssre)
  801. {
  802. synchronized (services)
  803. {
  804. Class klass = bcssre.getServiceClass();
  805. if (services.containsKey(klass))
  806. return;
  807. Iterator it = bcsChildren();
  808. while (it.hasNext())
  809. {
  810. Object obj = it.next();
  811. if (obj instanceof BeanContextServices)
  812. ((BeanContextServices) obj).serviceRevoked(bcssre);
  813. }
  814. }
  815. }
  816. }