📄 beancontextservicessupport.java
字号:
/* * @(#)BeanContextServicesSupport.java 1.19 03/01/23 * * Copyright 2003 Sun Microsystems, Inc. All rights reserved. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. */package java.beans.beancontext;import java.util.ArrayList;import java.util.Collection;import java.util.HashMap;import java.util.HashSet;import java.util.Iterator;import java.util.Map;import java.util.Map.Entry;import java.io.IOException;import java.io.ObjectInputStream;import java.io.ObjectOutputStream;import java.io.Serializable;import java.util.TooManyListenersException;import java.util.Locale;/** * <p> * This helper class provides a utility implementation of the * java.beans.beancontext.BeanContextServices interface. * </p> * <p> * Since this class directly implements the BeanContextServices interface, * the class can, and is intended to be used either by subclassing this * implementation, or via delegation of an instance of this class * from another through the BeanContextProxy interface. * </p> * * @author Laurence P. G. Cable * @version 1.19, 01/23/03 * @since 1.2 */public class BeanContextServicesSupport extends BeanContextSupport implements BeanContextServices { /** * <p> * Construct a BeanContextServicesSupport instance * </p> * * @param peer The peer BeanContext we are supplying an implementation for, if null the this object is its own peer * @param lcle The current Locale for this BeanContext. * @param dtime The initial state, true if in design mode, false if runtime. * @param visible The initial visibility. * */ public BeanContextServicesSupport(BeanContextServices peer, Locale lcle, boolean dTime, boolean visible) { super(peer, lcle, dTime, visible); } /** * Create an instance using the specified Locale and design mode. * * @param peer The peer BeanContext we are supplying an implementation for, if null the this object is its own peer * @param lcle The current Locale for this BeanContext. * @param dtime The initial state, true if in design mode, false if runtime. */ public BeanContextServicesSupport(BeanContextServices peer, Locale lcle, boolean dtime) { this (peer, lcle, dtime, true); } /** * Create an instance using the specified locale * * @param peer The peer BeanContext we are supplying an implementation for, if null the this object is its own peer * @param lcle The current Locale for this BeanContext. */ public BeanContextServicesSupport(BeanContextServices peer, Locale lcle) { this (peer, lcle, false, true); } /** * Create an instance with a peer * * @param peer The peer BeanContext we are supplying an implementation for, if null the this object is its own peer */ public BeanContextServicesSupport(BeanContextServices peer) { this (peer, null, false, true); } /** * Create an instance that is not a delegate of another object */ public BeanContextServicesSupport() { this (null, null, false, true); } /** * called by BeanContextSupport superclass during construction and * deserialization to initialize subclass transient state. * * subclasses may envelope this method, but should not override it or * call it directly. */ public void initialize() { super.initialize(); services = new HashMap(serializable + 1); bcsListeners = new ArrayList(1); } /** * Gets the <tt>BeanContextServices</tt> associated with this * <tt>BeanContextServicesSupport</tt>. * * @return the instance of <tt>BeanContext</tt> * this object is providing the implementation for. */ public BeanContextServices getBeanContextServicesPeer() { return (BeanContextServices)getBeanContextChildPeer(); } /************************************************************************/ /* * protected nested class containing per child information, an instance * of which is associated with each child in the "children" hashtable. * subclasses can extend this class to include their own per-child state. * * Note that this 'value' is serialized with the corresponding child 'key' * when the BeanContextSupport is serialized. */ protected class BCSSChild extends BeanContextSupport.BCSChild { /* * private nested class to map serviceClass to Provider and requestors * listeners. */ class BCSSCServiceClassRef { // create an instance of a service ref BCSSCServiceClassRef(Class sc, BeanContextServiceProvider bcsp, boolean delegated) { super(); serviceClass = sc; if (delegated) delegateProvider = bcsp; else serviceProvider = bcsp; } // add a requestor and assoc listener void addRequestor(Object requestor, BeanContextServiceRevokedListener bcsrl) throws TooManyListenersException { BeanContextServiceRevokedListener cbcsrl = (BeanContextServiceRevokedListener)requestors.get(requestor); if (cbcsrl != null && !cbcsrl.equals(bcsrl)) throw new TooManyListenersException(); requestors.put(requestor, bcsrl); } // remove a requestor void removeRequestor(Object requestor) { requestors.remove(requestor); } // check a requestors listener void verifyRequestor(Object requestor, BeanContextServiceRevokedListener bcsrl) throws TooManyListenersException { BeanContextServiceRevokedListener cbcsrl = (BeanContextServiceRevokedListener)requestors.get(requestor); if (cbcsrl != null && !cbcsrl.equals(bcsrl)) throw new TooManyListenersException(); } void verifyAndMaybeSetProvider(BeanContextServiceProvider bcsp, boolean isDelegated) { BeanContextServiceProvider current; if (isDelegated) { // the provider is delegated current = delegateProvider; if (current == null || bcsp == null) { delegateProvider = bcsp; return; } } else { // the provider is registered with this BCS current = serviceProvider; if (current == null || bcsp == null) { serviceProvider = bcsp; return; } } if (!current.equals(bcsp)) throw new UnsupportedOperationException("existing service reference obtained from different BeanContextServiceProvider not supported"); } Iterator cloneOfEntries() { return ((HashMap)requestors.clone()).entrySet().iterator(); } Iterator entries() { return requestors.entrySet().iterator(); } boolean isEmpty() { return requestors.isEmpty(); } Class getServiceClass() { return serviceClass; } BeanContextServiceProvider getServiceProvider() { return serviceProvider; } BeanContextServiceProvider getDelegateProvider() { return delegateProvider; } boolean isDelegated() { return delegateProvider != null; } void addRef(boolean delegated) { if (delegated) delegateRefs++; else serviceRefs++; } void releaseRef(boolean delegated) { if (delegated) if (--delegateRefs == 0) delegateProvider = null; else if (--serviceRefs <= 0) serviceProvider = null; } int getRefs() { return serviceRefs + delegateRefs; } int getDelegateRefs() { return delegateRefs; } int getServiceRefs() { return serviceRefs; } /* * fields */ Class serviceClass; BeanContextServiceProvider serviceProvider; int serviceRefs; BeanContextServiceProvider delegateProvider; // proxy int delegateRefs; HashMap requestors = new HashMap(1); } /* * per service reference info ... */ class BCSSCServiceRef { BCSSCServiceRef(BCSSCServiceClassRef scref, boolean isDelegated) { serviceClassRef = scref; delegated = isDelegated; } void addRef() { refCnt++; } int release() { return --refCnt; } BCSSCServiceClassRef getServiceClassRef() { return serviceClassRef; } boolean isDelegated() { return delegated; } /* * fields */ BCSSCServiceClassRef serviceClassRef; int refCnt = 1; boolean delegated = false; } BCSSChild(Object bcc, Object peer) { super(bcc, peer); } // note usage of service per requestor, per service synchronized void usingService(Object requestor, Object service, Class serviceClass, BeanContextServiceProvider bcsp, boolean isDelegated, BeanContextServiceRevokedListener bcsrl) throws TooManyListenersException, UnsupportedOperationException { // first, process mapping from serviceClass to requestor(s) BCSSCServiceClassRef serviceClassRef = null; if (serviceClasses == null) serviceClasses = new HashMap(1); else serviceClassRef = (BCSSCServiceClassRef)serviceClasses.get(serviceClass); if (serviceClassRef == null) { // new service being used ... serviceClassRef = new BCSSCServiceClassRef(serviceClass, bcsp, isDelegated); serviceClasses.put(serviceClass, serviceClassRef); } else { // existing service ... serviceClassRef.verifyAndMaybeSetProvider(bcsp, isDelegated); // throws serviceClassRef.verifyRequestor(requestor, bcsrl); // throws } serviceClassRef.addRequestor(requestor, bcsrl); serviceClassRef.addRef(isDelegated); // now handle mapping from requestor to service(s) BCSSCServiceRef serviceRef = null; Map services = null; if (serviceRequestors == null) { serviceRequestors = new HashMap(1); } else { services = (Map)serviceRequestors.get(requestor); } if (services == null) { services = new HashMap(1); serviceRequestors.put(requestor, services); } else serviceRef = (BCSSCServiceRef)services.get(service); if (serviceRef == null) { serviceRef = new BCSSCServiceRef(serviceClassRef, isDelegated); services.put(service, serviceRef); } else { serviceRef.addRef(); } } // release a service reference synchronized void releaseService(Object requestor, Object service) { if (serviceRequestors == null) return; Map services = (Map)serviceRequestors.get(requestor); if (services == null) return; // oops its not there anymore! BCSSCServiceRef serviceRef = (BCSSCServiceRef)services.get(service); if (serviceRef == null) return; // oops its not there anymore! BCSSCServiceClassRef serviceClassRef = serviceRef.getServiceClassRef(); boolean isDelegated = serviceRef.isDelegated(); BeanContextServiceProvider bcsp = isDelegated ? serviceClassRef.getDelegateProvider() : serviceClassRef.getServiceProvider(); bcsp.releaseService(BeanContextServicesSupport.this.getBeanContextServicesPeer(), requestor, service); serviceClassRef.releaseRef(isDelegated); if (serviceRef.release() == 0) { services.remove(service); if (services.isEmpty()) { serviceRequestors.remove(requestor); serviceClassRef.removeRequestor(requestor); } if (serviceRequestors.isEmpty()) { serviceRequestors = null; } if (serviceClassRef.isEmpty()) { serviceClasses.remove(serviceClassRef.getServiceClass()); } if (serviceClasses.isEmpty()) serviceClasses = null; } } // revoke a service synchronized void revokeService(Class serviceClass, boolean isDelegated, boolean revokeNow) { if (serviceClasses == null) return; BCSSCServiceClassRef serviceClassRef = (BCSSCServiceClassRef)serviceClasses.get(serviceClass); if (serviceClassRef == null) return; Iterator i = serviceClassRef.cloneOfEntries(); BeanContextServiceRevokedEvent bcsre = new BeanContextServiceRevokedEvent(BeanContextServicesSupport.this.getBeanContextServicesPeer(), serviceClass, revokeNow); boolean noMoreRefs = false;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -