📄 beancontextsupport.java
字号:
boolean prev = serializing; serializing = true; int count = 0; synchronized(children) { Iterator i = children.entrySet().iterator(); while (i.hasNext() && count < serializable) { Map.Entry entry = (Map.Entry)i.next(); if (entry.getKey() instanceof Serializable) { try { oos.writeObject(entry.getKey()); // child oos.writeObject(entry.getValue()); // BCSChild } catch (IOException ioe) { serializing = prev; throw ioe; } count++; } } } serializing = prev; if (count != serializable) { throw new IOException("wrote different number of children than expected"); } } /** * Serialize the BeanContextSupport, if this instance has a distinct * peer (that is this object is acting as a delegate for another) then * the children of this instance are not serialized here due to a * 'chicken and egg' problem that occurs on deserialization of the * children at the same time as this instance. * * Therefore in situations where there is a distinct peer to this instance * it should always call writeObject() followed by writeChildren() and * readObject() followed by readChildren(). * * @param oos the ObjectOutputStream */ private synchronized void writeObject(ObjectOutputStream oos) throws IOException, ClassNotFoundException { serializing = true; synchronized (BeanContext.globalHierarchyLock) { try { oos.defaultWriteObject(); // serialize the BeanContextSupport object bcsPreSerializationHook(oos); if (serializable > 0 && this.equals(getBeanContextPeer())) writeChildren(oos); serialize(oos, (Collection)bcmListeners); } finally { serializing = false; } } } /** * When an instance of this class is used as a delegate for the * implementation of the BeanContext protocols (and its subprotocols) * there exists a 'chicken and egg' problem during deserialization */ public final void readChildren(ObjectInputStream ois) throws IOException, ClassNotFoundException { int count = serializable; while (count-- > 0) { Object child = null; BeanContextSupport.BCSChild bscc = null; try { child = ois.readObject(); bscc = (BeanContextSupport.BCSChild)ois.readObject(); } catch (IOException ioe) { continue; } catch (ClassNotFoundException cnfe) { continue; } synchronized(child) { BeanContextChild bcc = null; try { bcc = (BeanContextChild)child; } catch (ClassCastException cce) { // do nothing; } if (bcc != null) { try { bcc.setBeanContext(getBeanContextPeer()); bcc.addPropertyChangeListener("beanContext", childPCL); bcc.addVetoableChangeListener("beanContext", childVCL); } catch (PropertyVetoException pve) { continue; } } childDeserializedHook(child, bscc); } } } /** * deserialize contents ... if this instance has a distinct peer the * children are *not* serialized here, the peer's readObject() must call * readChildren() after deserializing this instance. */ private synchronized void readObject(ObjectInputStream ois) throws IOException, ClassNotFoundException { synchronized(BeanContext.globalHierarchyLock) { ois.defaultReadObject(); initialize(); bcsPreDeserializationHook(ois); if (serializable > 0 && this.equals(getBeanContextPeer())) readChildren(ois); deserialize(ois, bcmListeners = new ArrayList(1)); } } /** * subclasses may envelope to monitor veto child property changes. */ public void vetoableChange(PropertyChangeEvent pce) throws PropertyVetoException { String propertyName = pce.getPropertyName(); Object source = pce.getSource(); synchronized(children) { if ("beanContext".equals(propertyName) && containsKey(source) && !getBeanContextPeer().equals(pce.getNewValue()) ) { if (!validatePendingRemove(source)) { throw new PropertyVetoException("current BeanContext vetoes setBeanContext()", pce); } else ((BCSChild)children.get(source)).setRemovePending(true); } } } /** * subclasses may envelope to monitor child property changes. */ public void propertyChange(PropertyChangeEvent pce) { String propertyName = pce.getPropertyName(); Object source = pce.getSource(); synchronized(children) { if ("beanContext".equals(propertyName) && containsKey(source) && ((BCSChild)children.get(source)).isRemovePending()) { BeanContext bc = getBeanContextPeer(); if (bc.equals(pce.getOldValue()) && !bc.equals(pce.getNewValue())) { remove(source, false); } else { ((BCSChild)children.get(source)).setRemovePending(false); } } } } /** * <p> * Subclasses of this class may override, or envelope, this method to * add validation behavior for the BeanContext to examine child objects * immediately prior to their being added to the BeanContext. * </p> * * @return true iff the child may be added to this BeanContext, otherwise false. */ protected boolean validatePendingAdd(Object targetChild) { return true; } /** * <p> * Subclasses of this class may override, or envelope, this method to * add validation behavior for the BeanContext to examine child objects * immediately prior to their being removed from the BeanContext. * </p> * * @return true iff the child may be removed from this BeanContext, otherwise false. */ protected boolean validatePendingRemove(Object targetChild) { return true; } /** * subclasses may override this method to simply extend add() semantics * after the child has been added and before the event notification has * occurred. The method is called with the child synchronized. */ protected void childJustAddedHook(Object child, BCSChild bcsc) { } /** * subclasses may override this method to simply extend remove() semantics * after the child has been removed and before the event notification has * occurred. The method is called with the child synchronized. */ protected void childJustRemovedHook(Object child, BCSChild bcsc) { } /** * Gets the Component (if any) associated with the specified child. * @param child the specified child * @return the Component (if any) associated with the specified child. */ protected static final Visibility getChildVisibility(Object child) { try { return (Visibility)child; } catch (ClassCastException cce) { return null; } } /** * Gets the Serializable (if any) associated with the specified Child * @param child the specified child * @return the Serializable (if any) associated with the specified Child */ protected static final Serializable getChildSerializable(Object child) { try { return (Serializable)child; } catch (ClassCastException cce) { return null; } } /** * Gets the PropertyChangeListener * (if any) of the specified child * @param child the specified child * @return the PropertyChangeListener (if any) of the specified child */ protected static final PropertyChangeListener getChildPropertyChangeListener(Object child) { try { return (PropertyChangeListener)child; } catch (ClassCastException cce) { return null; } } /** * Gets the VetoableChangeListener * (if any) of the specified child * @param child the specified child * @return the VetoableChangeListener (if any) of the specified child */ protected static final VetoableChangeListener getChildVetoableChangeListener(Object child) { try { return (VetoableChangeListener)child; } catch (ClassCastException cce) { return null; } } /** * Gets the BeanContextMembershipListener * (if any) of the specified child * @param child the specified child * @return the BeanContextMembershipListener (if any) of the specified child */ protected static final BeanContextMembershipListener getChildBeanContextMembershipListener(Object child) { try { return (BeanContextMembershipListener)child; } catch (ClassCastException cce) { return null; } } /** * Gets the BeanContextChild (if any) of the specified child * @param child the specified child * @return the BeanContextChild (if any) of the specified child * @throws IllegalArgumentException if child implements both BeanContextChild and BeanContextProxy */ protected static final BeanContextChild getChildBeanContextChild(Object child) { try { BeanContextChild bcc = (BeanContextChild)child; if (child instanceof BeanContextChild && child instanceof BeanContextProxy) throw new IllegalArgumentException("child cannot implement both BeanContextChild and BeanContextProxy"); else return bcc; } catch (ClassCastException cce) { try { return ((BeanContextProxy)child).getBeanContextProxy(); } catch (ClassCastException cce1) { return null; } } } /** * Fire a BeanContextshipEvent on the BeanContextMembershipListener interface */ protected final void fireChildrenAdded(BeanContextMembershipEvent bcme) { Object[] copy; synchronized(bcmListeners) { copy = bcmListeners.toArray(); } for (int i = 0; i < copy.length; i++) ((BeanContextMembershipListener)copy[i]).childrenAdded(bcme); } /** * Fire a BeanContextshipEvent on the BeanContextMembershipListener interface */ protected final void fireChildrenRemoved(BeanContextMembershipEvent bcme) { Object[] copy; synchronized(bcmListeners) { copy = bcmListeners.toArray(); } for (int i = 0; i < copy.length; i++) ((BeanContextMembershipListener)copy[i]).childrenRemoved(bcme); } /** * protected method called from constructor and readObject to initialize * transient state of BeanContextSupport instance. * * This class uses this method to instantiate inner class listeners used * to monitor PropertyChange and VetoableChange events on children. * * subclasses may envelope this method to add their own initialization * behavior */ protected synchronized void initialize() { children = new HashMap(serializable + 1); bcmListeners = new ArrayList(1); childPCL = new PropertyChangeListener() { /* * this adaptor is used by the BeanContextSupport class to forward * property changes from a child to the BeanContext, avoiding * accidential serialization of the BeanContext by a badly * behaved Serializable child. */ public void propertyChange(PropertyChangeEvent pce) { BeanContextSupport.this.propertyChange(pce); } }; childVCL = new VetoableChangeListener() { /* * this adaptor is used by the BeanContextSupport class to forward * vetoable changes from a child to the BeanContext, avoiding * accidential serialization of the BeanContext by a badly * behaved Serializable child. */ public void vetoableChange(PropertyChangeEvent pce) throws PropertyVetoException { BeanContextSupport.this.vetoableChange(pce); } }; } /** * Gets a copy of the this BeanContext's children. * @return a copy of the current nested children */ protected final Object[] copyChildren() { synchronized(children) { return children.keySet().toArray(); } } /** * Tests to see if two class objects, * or their names are equal. * @param first the first object * @param second the second object * @return true if equal, false if not */ protected static final boolean classEquals(Class first, Class second) { return first.equals(second) || first.getName().equals(second.getName()); } /* * fields */ /** * all accesses to the <code> protected HashMap children </code> field * shall be synchronized on that object. */ protected transient HashMap children; private int serializable = 0; // children serializable /** * all accesses to the <code> protected ArrayList bcmListeners </code> field * shall be synchronized on that object. */ protected transient ArrayList bcmListeners; // /** * The current locale of this BeanContext. */ protected Locale locale; /** * A <tt>boolean</tt> indicating if this * instance may now render a GUI. */ protected boolean okToUseGui; /** * A <tt>boolean</tt> indicating whether or not * this object is currently in design time mode. */ protected boolean designTime; /* * transient */ private transient PropertyChangeListener childPCL; private transient VetoableChangeListener childVCL; private transient boolean serializing;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -