📄 beancontextsupport.java
字号:
/* * @(#)BeanContextSupport.java 1.46 03/01/13 * * Copyright 2003 Sun Microsystems, Inc. All rights reserved. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. */package java.beans.beancontext;import java.awt.Component;import java.awt.Container;import java.beans.Beans;import java.beans.AppletInitializer;import java.beans.DesignMode;import java.beans.PropertyChangeEvent;import java.beans.PropertyChangeListener;import java.beans.PropertyChangeSupport;import java.beans.VetoableChangeListener;import java.beans.VetoableChangeSupport;import java.beans.PropertyVetoException;import java.beans.Visibility;import java.io.IOException;import java.io.InputStream;import java.io.ObjectInputStream;import java.io.ObjectOutputStream;import java.io.Serializable;import java.net.URL;import java.util.ArrayList;import java.util.Collection;import java.util.HashMap;import java.util.Iterator;import java.util.Locale;import java.util.Map;/** * This helper class provides a utility implementation of the * java.beans.beancontext.BeanContext interface. * </p> * <p> * Since this class directly implements the BeanContext interface, the class * can, and is intended to be used either by subclassing this implementation, * or via ad-hoc delegation of an instance of this class from another. * </p> * * @author Laurence P. G. Cable * @version 1.46, 01/13/03 * @since 1.2 */public class BeanContextSupport extends BeanContextChildSupport implements BeanContext, Serializable, PropertyChangeListener, VetoableChangeListener { // Fix for bug 4282900 to pass JCK regression test static final long serialVersionUID = -4879613978649577204L; /** * * Construct a BeanContextSupport instance * * * @param peer The peer <tt>BeanContext</tt> we are * supplying an implementation for, * or <tt>null</tt> * if this object is its own peer * @param lcle The current Locale for this BeanContext. If * <tt>lcle</tt> is <tt>null</tt>, the default locale * is assigned to the <tt>BeanContext</tt> instance. * @param dTime The initial state, * <tt>true</tt> if in design mode, * <tt>false</tt> if runtime. * @param visible The initial visibility. * @see java.util.Locale#getDefault() * @see java.util.Locale#setDefault(java.util.Locale) */ public BeanContextSupport(BeanContext peer, Locale lcle, boolean dTime, boolean visible) { super(peer); locale = lcle != null ? lcle : Locale.getDefault(); designTime = dTime; okToUseGui = visible; initialize(); } /** * Create an instance using the specified Locale and design mode. * * @param peer The peer <tt>BeanContext</tt> we * are supplying an implementation for, * or <tt>null</tt> if this object is its own peer * @param lcle The current Locale for this <tt>BeanContext</tt>. If * <tt>lcle</tt> is <tt>null</tt>, the default locale * is assigned to the <tt>BeanContext</tt> instance. * @param dtime The initial state, <tt>true</tt> * if in design mode, * <tt>false</tt> if runtime. * @see java.util.Locale#getDefault() * @see java.util.Locale#setDefault(java.util.Locale) */ public BeanContextSupport(BeanContext 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, * or <tt>null</tt> if this object * is its own peer * @param lcle The current Locale for this * <tt>BeanContext</tt>. If * <tt>lcle</tt> is <tt>null</tt>, * the default locale * is assigned to the <tt>BeanContext</tt> * instance. * @see java.util.Locale#getDefault() * @see java.util.Locale#setDefault(java.util.Locale) */ public BeanContextSupport(BeanContext peer, Locale lcle) { this (peer, lcle, false, true); } /** * Create an instance using with a default locale * * @param peer The peer <tt>BeanContext</tt> we are * supplying an implementation for, * or <tt>null</tt> if this object * is its own peer */ public BeanContextSupport(BeanContext peer) { this (peer, null, false, true); } /** * Create an instance that is not a delegate of another object */ public BeanContextSupport() { this (null, null, false, true); } /** * Gets the instance of <tt>BeanContext</tt> that * this object is providing the implementation for. * @return the BeanContext instance */ public BeanContext getBeanContextPeer() { return (BeanContext)getBeanContextChildPeer(); } /** * <p> * The instantiateChild method is a convenience hook * in BeanContext to simplify * the task of instantiating a Bean, nested, * into a <tt>BeanContext</tt>. * </p> * <p> * The semantics of the beanName parameter are defined by java.beans.Beans.instantate. * </p> * * @param beanName the name of the Bean to instantiate within this BeanContext * @throws IOException if there is an I/O error when the bean is being deserialized * @throws ClassNotFoundException if the class * identified by the beanName parameter is not found * @return the new object */ public Object instantiateChild(String beanName) throws IOException, ClassNotFoundException { BeanContext bc = getBeanContextPeer(); return Beans.instantiate(bc.getClass().getClassLoader(), beanName, bc); } /** * Gets the number of children currently nested in * this BeanContext. * * @return number of children */ public int size() { synchronized(children) { return children.size(); } } /** * Reports whether or not this * <tt>BeanContext</tt> is empty. * A <tt>BeanContext</tt> is considered * empty when it contains zero * nested children. * @return if there are not children */ public boolean isEmpty() { synchronized(children) { return children.isEmpty(); } } /** * Determines whether or not the specified object * is currently a child of this <tt>BeanContext</tt>. * @param o the Object in question * @return if this object is a child */ public boolean contains(Object o) { synchronized(children) { return children.containsKey(o); } } /** * Determines whether or not the specified object * is currently a child of this <tt>BeanContext</tt>. * @param o the Object in question * @return if this object is a child */ public boolean containsKey(Object o) { synchronized(children) { return children.containsKey(o); } } /** * Gets all JavaBean or <tt>BeanContext</tt> instances * currently nested in this <tt>BeanContext</tt>. * @return an <tt>Iterator</tt> of the nested children */ public Iterator iterator() { synchronized(children) { return new BCSIterator(children.keySet().iterator()); } } /** * Gets all JavaBean or <tt>BeanContext</tt> * instances currently nested in this BeanContext. */ public Object[] toArray() { synchronized(children) { return children.keySet().toArray(); } } /** * Gets an array containing all children of * this <tt>BeanContext</tt> that match * the types contained in arry. * @param arry The array of object * types that are of interest. * @return an array of children */ public Object[] toArray(Object[] arry) { synchronized(children) { return children.keySet().toArray(arry); } } /************************************************************************/ /** * protected final subclass that encapsulates an iterator but implements * a noop remove() method. */ protected static final class BCSIterator implements Iterator { BCSIterator(Iterator i) { super(); src = i; } public boolean hasNext() { return src.hasNext(); } public Object next() { return src.next(); } public void remove() { /* do nothing */ } private Iterator src; } /************************************************************************/ /* * 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 BCSChild implements Serializable { BCSChild(Object bcc, Object peer) { super(); child = bcc; proxyPeer = peer; } Object getChild() { return child; } void setRemovePending(boolean v) { removePending = v; } boolean isRemovePending() { return removePending; } boolean isProxyPeer() { return proxyPeer != null; } Object getProxyPeer() { return proxyPeer; } /* * fields */ private Object child; private Object proxyPeer; private transient boolean removePending; } /** * <p> * Subclasses can override this method to insert their own subclass * of Child without having to override add() or the other Collection * methods that add children to the set. * </p> * * @param targetChild the child to create the Child on behalf of * @param peer the peer if the tragetChild and the peer are related by an implementation of BeanContextProxy */ protected BCSChild createBCSChild(Object targetChild, Object peer) { return new BCSChild(targetChild, peer); } /************************************************************************/ /** * Adds/nests a child within this <tt>BeanContext</tt>. * <p> * Invoked as a side effect of java.beans.Beans.instantiate(). * If the child object is not valid for adding then this method * throws an IllegalStateException. * </p> * * * @param targetChild The child objects to nest * within this <tt>BeanContext</tt> * @return true if the child was added successfully. * @see #validatePendingAdd */ public boolean add(Object targetChild) { if (targetChild == null) throw new IllegalArgumentException(); // The specification requires that we do nothing if the child // is already nested herein. if (children.containsKey(targetChild)) return false; // test before locking synchronized(BeanContext.globalHierarchyLock) { if (children.containsKey(targetChild)) return false; // check again if (!validatePendingAdd(targetChild)) { throw new IllegalStateException(); } // The specification requires that we invoke setBeanContext() on the // newly added child if it implements the java.beans.beancontext.BeanContextChild interface BeanContextChild cbcc = getChildBeanContextChild(targetChild); BeanContextChild bccp = null; synchronized(targetChild) { if (targetChild instanceof BeanContextProxy) { bccp = ((BeanContextProxy)targetChild).getBeanContextProxy(); if (bccp == null) throw new NullPointerException("BeanContextPeer.getBeanContextProxy()"); } BCSChild bcsc = createBCSChild(targetChild, bccp); BCSChild pbcsc = null; synchronized (children) { children.put(targetChild, bcsc); if (bccp != null) children.put(bccp, pbcsc = createBCSChild(bccp, targetChild)); } if (cbcc != null) synchronized(cbcc) { try { cbcc.setBeanContext(getBeanContextPeer()); } catch (PropertyVetoException pve) { synchronized (children) { children.remove(targetChild); if (bccp != null) children.remove(bccp); } throw new IllegalStateException(); } cbcc.addPropertyChangeListener("beanContext", childPCL); cbcc.addVetoableChangeListener("beanContext", childVCL); } Visibility v = getChildVisibility(targetChild); if (v != null) { if (okToUseGui) v.okToUseGui(); else v.dontUseGui(); } if (getChildSerializable(targetChild) != null) serializable++; childJustAddedHook(targetChild, bcsc); if (bccp != null) { v = getChildVisibility(bccp); if (v != null) { if (okToUseGui) v.okToUseGui(); else v.dontUseGui(); } if (getChildSerializable(bccp) != null) serializable++; childJustAddedHook(bccp, pbcsc); } } // The specification requires that we fire a notification of the change fireChildrenAdded(new BeanContextMembershipEvent(getBeanContextPeer(), bccp == null ? new Object[] { targetChild } : new Object[] { targetChild, bccp } )); } return true; } /** * Removes a child from this BeanContext. If the child object is not
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -