BeanContextServicesSupport.java 26.5 KB
Newer Older
Tom Tromey committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
/* BeanContextServicesSupport.java --
   Copyright (C) 2003, 2005  Free Software Foundation, Inc.

This file is part of GNU Classpath.

GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

GNU Classpath is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.

You should have received a copy of the GNU General Public License
along with GNU Classpath; see the file COPYING.  If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA.

Linking this library statically or dynamically with other modules is
making a combined work based on this library.  Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.

As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module.  An independent module is a module which is not derived from
or based on this library.  If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so.  If you do not wish to do so, delete this
exception statement from your version. */


package java.beans.beancontext;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
47
import java.util.HashSet;
Tom Tromey committed
48
import java.util.Iterator;
49
import java.util.List;
Tom Tromey committed
50
import java.util.Locale;
51
import java.util.Set;
Tom Tromey committed
52 53 54
import java.util.TooManyListenersException;

/**
55 56 57 58 59
 * This is a helper class for implementing a bean context which
 * supplies services.  It is intended to be used either by
 * subclassing or by calling methods of this implementation
 * from another.
 *
Tom Tromey committed
60
 * @author Michael Koch
61
 * @author Andrew John Hughes (gnu_andrew@member.fsf.org)
Tom Tromey committed
62 63 64 65 66 67 68 69 70 71 72
 * @since 1.2
 */
public class BeanContextServicesSupport
  extends BeanContextSupport
  implements BeanContextServices
{
  private static final long serialVersionUID = -8494482757288719206L;
  
  protected class BCSSChild
    extends BeanContextSupport.BCSChild
  {
73
    private static final long serialVersionUID = -3263851306889194873L;
74 75 76 77 78

    BCSSChild(Object targetChild, Object peer)
    {
      super(targetChild, peer);
    }
Tom Tromey committed
79 80 81 82 83 84 85
  }

  protected class BCSSProxyServiceProvider
    implements BeanContextServiceProvider,
    BeanContextServiceRevokedListener
  {
    private static final long serialVersionUID = 7078212910685744490L;
86

87 88
    private BeanContextServiceProvider provider;

Tom Tromey committed
89 90 91
    public Iterator getCurrentServiceSelectors (BeanContextServices bcs,
                                                Class serviceClass)
    {
92
      return provider.getCurrentServiceSelectors(bcs, serviceClass);
Tom Tromey committed
93 94 95 96 97 98 99
    }

    public Object getService (BeanContextServices bcs,
                              Object requestor,
                              Class serviceClass,
                              Object serviceSelector)
    {
100 101
      return provider.getService(bcs, requestor, serviceClass,
				 serviceSelector);
Tom Tromey committed
102 103 104 105 106 107
    }

    public void releaseService (BeanContextServices bcs,
                                Object requestor,
                                Object service)
    {
108
      provider.releaseService(bcs, requestor, service);
Tom Tromey committed
109 110 111 112
    }

    public void serviceRevoked (BeanContextServiceRevokedEvent bcsre)
    {
113 114
      if (provider instanceof BeanContextServiceRevokedListener)
	((BeanContextServiceRevokedListener) provider).serviceRevoked(bcsre);
Tom Tromey committed
115 116 117 118 119 120 121 122 123 124
    }
  }

  protected static class BCSSServiceProvider
    implements Serializable
  {
    private static final long serialVersionUID = 861278251667444782L;

    protected BeanContextServiceProvider serviceProvider;

125 126 127 128
    private Class serviceClass;

    private BCSSServiceProvider(Class serviceClass,
				BeanContextServiceProvider provider)
129
    {
130 131
      this.serviceClass = serviceClass;
      serviceProvider = provider;
132 133
    }

Tom Tromey committed
134 135 136 137
    protected BeanContextServiceProvider getServiceProvider()
    {
      return serviceProvider;
    }
138 139 140 141 142 143

    private Class getServiceClass()
    {
      return serviceClass;
    }

Tom Tromey committed
144 145
  }

146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
  /**
   * Represents a request for a service.  This is
   * a common superclass used by the classes which maintain
   * the listener-requestor and service-requestor relationships.
   *
   * @author Andrew John Hughes (gnu_andrew@member.fsf.org)
   */
  private static abstract class Request
  {
    private Object requestor;

    public Request(Object requestor)
    {
      this.requestor = requestor;
    }

    public boolean equals(Object obj)
    {
      if (obj instanceof Request)
	{
	  Request req = (Request) obj;
	  return req.getRequestor().equals(requestor);
	}
      return false;
    }

    public Object getRequestor()
    {
      return requestor;
    }

  }

  /**
   * Represents a relationship between a service requestor
   * and a revocation listener.
   *
   * @author Andrew John Hughes (gnu_andrew@member.fsf.org)
   */
  private static class ServiceRequest
    extends Request
  {

    private BeanContextServiceRevokedListener listener;

    public ServiceRequest(Object requestor,
			  BeanContextServiceRevokedListener listener)
    {
      super(requestor);
      this.listener = listener;
    }

    public boolean equals(Object obj)
    {
      if (obj instanceof ServiceRequest)
	{
	  ServiceRequest sr = (ServiceRequest) obj;
	  return (super.equals(obj) &&
		  sr.getListener().equals(listener));
	}
      return false;
    }

    public BeanContextServiceRevokedListener getListener()
    {
      return listener;
    }
  }

  /**
   * Represents a relationship between a service requestor
   * and a service instance.
   *
   * @author Andrew John Hughes (gnu_andrew@member.fsf.org)
   */
  private static class ServiceLease
    extends Request
  {
Tom Tromey committed
224

225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
    private Object service;

    public ServiceLease(Object requestor, Object service)
    {
      super(requestor);
      this.service = service;
    }

    public boolean equals(Object obj)
    {
      if (obj instanceof ServiceLease)
	{
	  ServiceLease sl = (ServiceLease) obj;
	  return (super.equals(obj) &&
		  sl.getService().equals(service));
	}
      return false;
    }

    public Object getService()
    {
      return service;
    }
  }

  /**
   * A collection of listeners who receive availability
   * and revocation notifications.
   */
  protected transient ArrayList bcsListeners;
    
Tom Tromey committed
256 257
  protected transient BCSSProxyServiceProvider proxy;

258 259 260
  /**
   * The number of serializable service providers.
   */
Tom Tromey committed
261 262
  protected transient int serializable;

263 264 265 266
  /**
   * A map of registered services, linking the service
   * class to its associated {@link BCSSServiceProvider}.
   */
Tom Tromey committed
267 268
  protected transient HashMap services;

269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
  /**
   * A map of children to a list of services they
   * have obtained.
   */
  private transient HashMap serviceUsers;

  /**
   * A map of services to {@link ServiceRequest}s.
   */
  private transient HashMap serviceRequests;

  /**
   * A map of {@link ServiceLease}s to providers.
   */
  private transient HashMap serviceLeases;

  /**
   * Construct a {@link BeanContextServicesSupport} instance.
   */
Tom Tromey committed
288 289 290 291 292
  public BeanContextServicesSupport ()
  {
    super();
  }

293 294 295 296 297
  /**
   * Construct a {@link BeanContextServicesSupport} instance.
   * 
   * @param peer the bean context services peer (<code>null</code> permitted).
   */
Tom Tromey committed
298 299 300 301 302
  public BeanContextServicesSupport (BeanContextServices peer)
  {
    super(peer);
  }

303 304 305 306 307 308 309
  /**
   * Construct a {@link BeanContextServicesSupport} instance.
   * 
   * @param peer the bean context peer (<code>null</code> permitted).
   * @param locale the locale (<code>null</code> permitted, equivalent to 
   *     the default locale).
   */
Tom Tromey committed
310 311 312 313 314
  public BeanContextServicesSupport(BeanContextServices peer, Locale locale)
  {
    super(peer, locale);
  }

315 316 317 318 319 320 321 322 323
  /**
   * Construct a {@link BeanContextServicesSupport} instance.
   * 
   * @param peer  the bean context peer (<code>null</code> permitted).
   * @param locale  the locale (<code>null</code> permitted, equivalent to 
   *     the default locale).
   * @param dtime  a flag indicating whether or not the bean context is in
   *     design time mode.
   */
Tom Tromey committed
324 325 326 327 328 329
  public BeanContextServicesSupport(BeanContextServices peer, Locale locale,
                                    boolean dtime)
  {
    super(peer, locale, dtime);
  }

330 331 332 333 334 335 336 337 338 339
  /**
   * Construct a {@link BeanContextServicesSupport} instance.
   * 
   * @param peer  the bean context peer (<code>null</code> permitted).
   * @param locale  the locale (<code>null</code> permitted, equivalent to 
   *     the default locale).
   * @param dtime  a flag indicating whether or not the bean context is in
   *     design time mode.
   * @param visible  initial value of the <code>okToUseGui</code> flag.
   */
Tom Tromey committed
340 341 342 343 344
  public BeanContextServicesSupport(BeanContextServices peer, Locale locale,
                                    boolean dtime, boolean visible)
  {
    super(peer, locale, dtime, visible);
  }
345 346 347 348 349 350 351
  
  /**
   * Adds a new listener for service availability and
   * revocation events.
   *
   * @param listener the listener to add.
   */
Tom Tromey committed
352 353 354
  public void addBeanContextServicesListener
    (BeanContextServicesListener listener)
  {
355 356 357 358 359
    synchronized (bcsListeners)
      {
        if (! bcsListeners.contains(listener))
          bcsListeners.add(listener);
      }
Tom Tromey committed
360 361
  }

362 363 364 365 366 367 368 369 370 371 372 373 374
  /**
   * Registers a new service from the specified service provider.
   * The service is internally associated with the service provider
   * and a <code>BeanContextServiceAvailableEvent</code> is fired.  If
   * the service is already registered, then this method instead
   * returns <code>false</code>.  This is equivalent to calling
   * <code>addService(serviceClass, bcsp, true)</code>.
   *
   * @param serviceClass the class of the service to be registered.
   * @param bcsp the provider of the given service.
   * @return true if the service was registered successfully.
   * @see #addService(Class, BeanContextServiceProvider, boolean)
   */
375 376
  public boolean addService (Class serviceClass,
                             BeanContextServiceProvider bcsp)
Tom Tromey committed
377
  {
378
    return addService(serviceClass, bcsp, true);
Tom Tromey committed
379 380
  }

381 382 383 384 385 386 387 388 389 390 391 392 393 394
  /**
   * Registers a new service from the specified service provider.
   * The service is internally associated with the service provider
   * and (if <code>fireEvent</code> is true) a
   * <code>BeanContextServiceAvailableEvent</code> is fired.  If
   * the service is already registered, then this method instead
   * returns <code>false</code>.
   *
   * @param serviceClass the class of the service to be registered.
   * @param bcsp the provider of the given service.
   * @param fireEvent true if a service availability event should
   *                  be fired.
   * @return true if the service was registered successfully.
   */
Tom Tromey committed
395 396 397 398
  protected boolean addService (Class serviceClass,
                                BeanContextServiceProvider bcsp,
                                boolean fireEvent)
  {
399
    synchronized (globalHierarchyLock)
400
      {
401 402 403 404 405 406 407 408 409 410 411 412
	synchronized (services)
	  {
	    if (services.containsKey(serviceClass))
	      return false;
	    services.put(serviceClass,
			 createBCSSServiceProvider(serviceClass, bcsp));
	    if (bcsp instanceof Serializable)
	      ++serializable;
	    if (fireEvent)
	      fireServiceAdded(serviceClass);
	    return true;
	  }
413
      }
Tom Tromey committed
414 415
  }
  
416 417 418 419 420 421 422
  /**
   * Deserializes any service providers which are serializable.  This
   * method is called by the <code>readObject</code> method of
   * {@link BeanContextSupport} prior to deserialization of the children.
   * Subclasses may envelope its behaviour in order to read further
   * serialized data to the stream.
   *
423
   * @param ois the stream from which data is being deserialized.
424 425 426 427
   * @throws IOException if an I/O error occurs.
   * @throws ClassNotFoundException if the class of a deserialized object
   *                                can not be found.
   */
Tom Tromey committed
428
  protected void bcsPreDeserializationHook (ObjectInputStream ois)
429
    throws ClassNotFoundException, IOException
Tom Tromey committed
430
  {
431 432 433 434 435 436
    serializable = ois.readInt();
    for (int a = 0; a < serializable; ++a)
      {
	BCSSServiceProvider bcsssp = (BCSSServiceProvider) ois.readObject();
	addService(bcsssp.getServiceClass(), bcsssp.getServiceProvider());
      }
Tom Tromey committed
437 438
  }

439 440 441 442 443 444 445 446 447 448
  /**
   * Serializes any service providers which are serializable.  This
   * method is called by the <code>writeObject</code> method of
   * {@link BeanContextSupport} prior to serialization of the children.
   * Subclasses may envelope its behaviour in order to add further
   * serialized data to the stream.
   *
   * @param oos the stream to which data is being serialized.
   * @throws IOException if an I/O error occurs.
   */
Tom Tromey committed
449
  protected void bcsPreSerializationHook (ObjectOutputStream oos) 
450
    throws IOException
Tom Tromey committed
451
  {
452 453 454 455 456 457 458 459 460 461 462
    oos.writeInt(serializable);
    synchronized (services)
      {
	Iterator i = services.values().iterator();
	while (i.hasNext())
	  {
	    BCSSServiceProvider bcsssp = (BCSSServiceProvider) i.next();
	    if (bcsssp.getServiceProvider() instanceof Serializable)
	      oos.writeObject(bcsssp);
	  }
      }
Tom Tromey committed
463
  }
464 465 466 467 468 469 470 471 472 473 474

  /**
   * Revokes any services used by a child that has just been removed.
   * The superclass ({@link BeanContextSupport}) calls this method
   * when a child has just been successfully removed.  Subclasses can
   * extend this method in order to perform additional operations
   * on child removal.
   *
   * @param child the child being removed.
   * @param bcsc the support object for the child.
   */ 
Tom Tromey committed
475 476 477
  protected void childJustRemovedHook (Object child,
                                       BeanContextSupport.BCSChild bcsc)
  {
478 479 480 481 482 483 484 485
    if (child instanceof BeanContextChild)
      {
	BeanContextChild bcchild = (BeanContextChild) child;
	Iterator childServices = ((List) serviceUsers.get(bcchild)).iterator();
	while (childServices.hasNext())
	  releaseService(bcchild, this, childServices.next());
	serviceUsers.remove(bcchild);
      }
Tom Tromey committed
486 487
  }

488 489 490 491 492 493 494 495
  /**
   * Overrides the {@link BeanContextSupport#createBCSChild} method
   * so as to use a {@link BCSSChild} instead.
   *
   * @param targetChild the child to create the child for.
   * @param peer the peer which relates to the child if a proxy is used.
   * @return a new instance of {@link BCSSChild}.
   */
Tom Tromey committed
496
  protected BeanContextSupport.BCSChild createBCSChild (Object targetChild,
497
                                                        Object peer)
Tom Tromey committed
498
  {
499
    return new BCSSChild(targetChild, peer);
Tom Tromey committed
500 501
  }

502 503 504 505 506 507 508 509 510 511
  /**
   * Provides a hook so that subclasses can replace the
   * {@link BCSSServiceProvider} class, used to store registered
   * service providers, with a subclass without replacing the
   * {@link #addService(Class, BeanContextServiceProvider)} method.
   *
   * @param sc the class of service being registered.
   * @param bcsp the provider of the service.
   * @return a instance of {@link BCSSServiceProvider} wrapping the provider.
   */
Tom Tromey committed
512 513 514
  protected BeanContextServicesSupport.BCSSServiceProvider
  createBCSSServiceProvider (Class sc, BeanContextServiceProvider bcsp)
  {
515
    return new BCSSServiceProvider(sc, bcsp);
Tom Tromey committed
516 517
  }

518 519 520 521 522 523
  /**
   * Sends a <code>BeanContextServiceAvailableEvent</code> to all
   * registered listeners.
   *
   * @param bcssae the event to send.
   */
Tom Tromey committed
524 525
  protected final void fireServiceAdded (BeanContextServiceAvailableEvent bcssae)
  {
526 527 528 529 530 531 532 533 534 535
    synchronized (bcsListeners)
      {
        int size = bcsListeners.size();
        for (int i = 0; i < size; ++i)
          {
            BeanContextServicesListener bcsl
              = (BeanContextServicesListener) bcsListeners.get(i);
            bcsl.serviceAvailable(bcssae);
          }
      }
Tom Tromey committed
536 537
  }

538 539 540 541 542 543 544
  /**
   * Sends a <code>BeanContextServiceAvailableEvent</code> to all
   * registered listeners.
   *
   * @param serviceClass the service that is now available.
   * @see #fireServiceAdded(BeanContextServiceAvailableEvent)
   */
545
  protected final void fireServiceAdded (Class serviceClass)
Tom Tromey committed
546
  {
547 548
    fireServiceAdded(new BeanContextServiceAvailableEvent(this,
                                                          serviceClass));
Tom Tromey committed
549 550
  }

551 552 553 554 555 556
  /**
   * Sends a <code>BeanContextServiceRevokedEvent</code> to all
   * registered listeners.
   *
   * @param event the event to send.
   */
Tom Tromey committed
557 558
  protected final void fireServiceRevoked(BeanContextServiceRevokedEvent event)
  {
559 560 561 562 563 564 565 566 567
    synchronized (bcsListeners)
      {
        int size = bcsListeners.size();
        for (int i = 0; i < size; ++i)
          {
            BeanContextServicesListener bcsl
              = (BeanContextServicesListener) bcsListeners.get(i);
            bcsl.serviceRevoked(event);
          }
568 569 570 571 572 573 574 575 576 577
	List requests = (List) serviceRequests.get(event.getServiceClass());
	if (requests != null)
	  {
	    Iterator i = requests.iterator();
	    while (i.hasNext())
	      {
		ServiceRequest r = (ServiceRequest) i.next();
		r.getListener().serviceRevoked(event);
	      }
	  }
578
      }
Tom Tromey committed
579 580
  }

581 582 583 584 585 586 587
  /**
   * Sends a <code>BeanContextServiceRevokedEvent</code> to all
   * registered listeners.
   *
   * @param serviceClass the service that has been revoked.
   * @see #fireServiceRevoked(BeanContextServiceRevokedEvent)
   */
Tom Tromey committed
588 589 590
  protected final void fireServiceRevoked (Class serviceClass,
                                           boolean revokeNow)
  {
591 592
    fireServiceRevoked(new BeanContextServiceRevokedEvent(this, serviceClass,
                                                          revokeNow));
Tom Tromey committed
593 594
  }

595 596 597 598 599 600
  /**
   * Returns the services peer given at construction time,
   * or <code>null</code> if no peer was given.
   *
   * @return the {@link BeanContextServices} peer.
   */
Tom Tromey committed
601 602
  public BeanContextServices getBeanContextServicesPeer ()
  {
603
    return (BeanContextServices) beanContextChildPeer;
Tom Tromey committed
604 605
  }

606 607 608 609 610 611 612 613 614
  /**
   * Returns <code>child</code> as an instance of 
   * {@link BeanContextServicesListener}, or <code>null</code> if 
   * <code>child</code> does not implement that interface.
   * 
   * @param child  the child (<code>null</code> permitted).
   * 
   * @return The child cast to {@link BeanContextServicesListener}.
   */
Tom Tromey committed
615
  protected static final BeanContextServicesListener
616
      getChildBeanContextServicesListener(Object child)
Tom Tromey committed
617
  {
618 619 620 621
    if (child instanceof BeanContextServicesListener) 
      return (BeanContextServicesListener) child;
    else 
      return null;
Tom Tromey committed
622 623
  }

624 625 626 627 628 629
  /**
   * Returns an iterator over the currently available
   * services.
   *
   * @return an iterator over the currently available services.
   */
630
  public Iterator getCurrentServiceClasses ()
Tom Tromey committed
631
  {
632
    synchronized (globalHierarchyLock)
633
      {
634 635 636 637
	synchronized (services)
	  {
	    return services.keySet().iterator();
	  }
638
      }
Tom Tromey committed
639 640
  }

641 642 643 644 645 646 647 648 649 650 651 652 653
  /**
   * Returns an iterator over the service selectors of the service
   * provider for the given service.  The iterator is actually
   * obtained by calling the
   * {@link BeanContextServiceProvider#getCurrentServiceSelectors}
   * of the provider itself.  If the specified service is not available,
   * <code>null</code> is returned.
   *
   * @param serviceClass the service whose provider's selectors should
   *                     be iterated over.
   * @return an {@link Iterator} over the service selectors of the
   *         provider of the given service.
   */
654
  public Iterator getCurrentServiceSelectors (Class serviceClass)
Tom Tromey committed
655
  {
656
    synchronized (globalHierarchyLock)
657
      {
658 659 660 661 662 663 664 665 666 667
	synchronized (services)
	  {
	    BeanContextServiceProvider bcsp
	      = ((BCSSServiceProvider)
		 services.get(serviceClass)).getServiceProvider();
	    if (bcsp == null)
	      return null;
	    else
	      return bcsp.getCurrentServiceSelectors(this, serviceClass);
	  }
668
      }
Tom Tromey committed
669 670
  }

671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689
  /**
   * Retrieves the specified service.  If a provider for the service
   * is registered in this context, then the request is passed on to
   * the provider and the service returned.  Otherwise, the request
   * is delegated to a parent {@link BeanContextServices}, if possible.
   * If the service can not be found at all, then <code>null</code>
   * is returned.
   *
   * @param child the child obtaining the reference.
   * @param requestor the requestor of the service, which may be the
   *                  child itself.
   * @param serviceClass the service being requested.
   * @param serviceSelector an additional service-dependent parameter
   *                        (may be <code>null</code> if not appropriate).
   * @param bcsrl a listener used to notify the requestor that the service
   *              has since been revoked.
   * @return a reference to the service requested, or <code>null</code>.
   * @throws TooManyListenersException according to Sun's documentation.
   */
Tom Tromey committed
690 691 692
  public Object getService (BeanContextChild child, Object requestor,
                            Class serviceClass, Object serviceSelector,
                            BeanContextServiceRevokedListener bcsrl)
693
    throws TooManyListenersException
Tom Tromey committed
694
  {
695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738
    synchronized (globalHierarchyLock)
      {
	synchronized (services)
	  {
	    Object service;
	    BeanContextServiceProvider provider = ((BCSSServiceProvider)
	      services.get(serviceClass)).getServiceProvider();
	    if (provider != null)
	      {
		service = provider.getService(this, requestor, serviceClass,
					      serviceSelector);
		List childServices = (List) serviceUsers.get(child);
		if (childServices == null)
		  {
		    childServices = new ArrayList();
		    serviceUsers.put(child, childServices);
		  }
		childServices.add(serviceClass);
	      }
	    else 
	      {
		BeanContextServices peer = getBeanContextServicesPeer();
		if (peer != null)
		  service = peer.getService(child, requestor, serviceClass,
					    serviceSelector, bcsrl);
		else
		  service = null;
	      }    								    
	    if (service != null)
	      {
		ServiceRequest request = new ServiceRequest(requestor, bcsrl);
		Set requests = (Set) serviceRequests.get(serviceClass);
		if (requests == null)
		  {
		    requests = new HashSet();
		    serviceRequests.put(serviceClass, requests);
		  }
		requests.add(request);
		ServiceLease lease = new ServiceLease(requestor, service);
		serviceLeases.put(lease, provider);
	      }
	    return service;
	  }
      }
Tom Tromey committed
739 740
  }

741 742 743 744 745 746
  /**
   * Returns true if the specified service is available.
   *
   * @param serviceClass the service to check for.
   * @return true if the service is available.
   */
Tom Tromey committed
747 748
  public boolean hasService (Class serviceClass)
  {
749
    synchronized (globalHierarchyLock)
750
      {
751 752 753 754
	synchronized (services)
	  {
	    return services.containsKey(serviceClass);
	  }
755
      }
Tom Tromey committed
756 757 758 759 760 761 762 763
  }

  public void initialize ()
  {
    super.initialize();

    bcsListeners = new ArrayList();
    services = new HashMap();
764 765 766
    serviceUsers = new HashMap();
    serviceRequests = new HashMap();
    serviceLeases = new HashMap();
Tom Tromey committed
767 768
  }

769 770 771 772 773
  /**
   * Subclasses may override this method to allocate resources
   * from the nesting bean context.
   */
  protected  void initializeBeanContextResources()
Tom Tromey committed
774
  {
775
    /* Purposefully left empty */
Tom Tromey committed
776 777
  }

778 779 780 781 782 783 784
  /**
   * Relinquishes any resources obtained from the parent context.
   * Specifically, those services obtained from the parent are revoked.
   * Subclasses may override this method to deallocate resources
   * from the nesting bean context.  
   */
  protected void releaseBeanContextResources()
Tom Tromey committed
785
  {
786
    /* Purposefully left empty */
Tom Tromey committed
787 788
  }

789 790 791 792 793 794 795 796 797 798
  /**
   * Releases the reference to a service held by a
   * {@link BeanContextChild} (or an arbitrary object associated
   * with it).  It simply calls the appropriate method on the
   * underlying provider.
   *
   * @param child the child who holds the reference.
   * @param requestor the object that requested the reference.
   * @param service the service being released.
   */
Tom Tromey committed
799 800 801
  public void releaseService (BeanContextChild child, Object requestor,
                              Object service)
  {
802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819
    synchronized (globalHierarchyLock)
      {
	synchronized (services)
	  {
	    ServiceLease lease = new ServiceLease(requestor, service);
	    BeanContextServiceProvider provider = (BeanContextServiceProvider)
	      serviceLeases.get(lease);
	    if (provider != null)
	      provider.releaseService(this, requestor, service);
	    else
	      {
		BeanContextServices peer = getBeanContextServicesPeer();
		if (peer != null)
		  peer.releaseService(child, requestor, service);
	      }
	    serviceLeases.remove(lease);
	  }
      }
Tom Tromey committed
820 821 822 823 824
  }

  public void removeBeanContextServicesListener
    (BeanContextServicesListener listener)
  {
825 826
    synchronized (bcsListeners)
      {
827
	bcsListeners.remove(listener);
828
      }
Tom Tromey committed
829 830
  }

831 832 833 834 835 836 837 838 839 840 841 842
  /**
   * Revokes the given service.  A {@link BeanContextServiceRevokedEvent} is
   * emitted to all registered {@link BeanContextServiceRevokedListener}s
   * and {@link BeanContextServiceListener}s.  If <code>revokeCurrentServicesNow</code>
   * is true, termination of the service is immediate.  Otherwise, prior
   * acquisitions of the service by requestors remain valid.
   *
   * @param serviceClass the service to revoke.
   * @param bcsp the provider of the revoked service.
   * @param revokeCurrentServicesNow true if this is an exceptional circumstance
   *                                 where service should be immediately revoked.
   */
Tom Tromey committed
843
  public void revokeService (Class serviceClass, BeanContextServiceProvider bcsp,
844
                             boolean revokeCurrentServicesNow)
Tom Tromey committed
845
  {
846 847 848 849 850 851 852 853 854 855
    synchronized (globalHierarchyLock)
      {
	synchronized (services)
	  {
	    fireServiceRevoked(serviceClass, revokeCurrentServicesNow);
	    services.remove(serviceClass);
	    if (bcsp instanceof Serializable)
	      --serializable;
	  }
      }
Tom Tromey committed
856 857
  }

858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889
  public void serviceAvailable (BeanContextServiceAvailableEvent bcssae)
  {
    synchronized (services)
      {
        Class klass = bcssae.getServiceClass();
        if (services.containsKey(klass))
          return;
        Iterator it = bcsChildren();
        while (it.hasNext())
          {
            Object obj = it.next();
            if (obj instanceof BeanContextServices)
              ((BeanContextServices) obj).serviceAvailable(bcssae);
          }
      }
  }

  public void serviceRevoked (BeanContextServiceRevokedEvent bcssre)
  {
    synchronized (services)
      {
        Class klass = bcssre.getServiceClass();
        if (services.containsKey(klass))
          return;
        Iterator it = bcsChildren();
        while (it.hasNext())
          {
            Object obj = it.next();
            if (obj instanceof BeanContextServices)
              ((BeanContextServices) obj).serviceRevoked(bcssre);
          }
      }
Tom Tromey committed
890 891
  }
}