001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package java.beans.beancontext;
019:
020: import java.beans.PropertyChangeEvent;
021: import java.beans.PropertyChangeListener;
022: import java.beans.PropertyChangeSupport;
023: import java.beans.PropertyVetoException;
024: import java.beans.VetoableChangeListener;
025: import java.beans.VetoableChangeSupport;
026: import java.io.IOException;
027: import java.io.ObjectInputStream;
028: import java.io.ObjectOutputStream;
029: import java.io.Serializable;
030:
031: import org.apache.harmony.beans.internal.nls.Messages;
032:
033: public class BeanContextChildSupport implements BeanContextChild,
034: BeanContextServicesListener, Serializable {
035:
036: private static final long serialVersionUID = 6328947014421475877L;
037:
038: static final String BEAN_CONTEXT = "beanContext"; //$NON-NLS-1$
039:
040: protected transient BeanContext beanContext;
041:
042: public BeanContextChild beanContextChildPeer;
043:
044: protected PropertyChangeSupport pcSupport;
045:
046: protected transient boolean rejectedSetBCOnce;
047:
048: protected VetoableChangeSupport vcSupport;
049:
050: private transient BeanContext lastVetoedContext;
051:
052: public BeanContextChildSupport() {
053: // This class implements the JavaBean component itself
054: this (null);
055: }
056:
057: public BeanContextChildSupport(BeanContextChild bcc) {
058: // If 'bcc' parameter is not null the JavaBean component itself
059: // implements BeanContextChild
060: this .beanContextChildPeer = (bcc == null ? this : bcc);
061:
062: // Initialize necessary fields for later use
063: pcSupport = new PropertyChangeSupport(this .beanContextChildPeer);
064: vcSupport = new VetoableChangeSupport(this .beanContextChildPeer);
065: this .rejectedSetBCOnce = false;
066: }
067:
068: public void addPropertyChangeListener(String name,
069: PropertyChangeListener pcl) {
070: // Do nothing if name or listener is null
071: if ((name == null) || (pcl == null)) {
072: return;
073: }
074:
075: this .pcSupport.addPropertyChangeListener(name, pcl);
076: }
077:
078: public void addVetoableChangeListener(String name,
079: VetoableChangeListener vcl) {
080: // Do nothing if name or listener is null
081: if ((name == null) || (vcl == null)) {
082: return;
083: }
084:
085: this .vcSupport.addVetoableChangeListener(name, vcl);
086: this .lastVetoedContext = null;
087: }
088:
089: public void firePropertyChange(String name, Object oldValue,
090: Object newValue) {
091: this .pcSupport.firePropertyChange(name, oldValue, newValue);
092: }
093:
094: public void fireVetoableChange(String name, Object oldValue,
095: Object newValue) throws PropertyVetoException {
096:
097: this .vcSupport.fireVetoableChange(name, oldValue, newValue);
098: }
099:
100: public synchronized BeanContext getBeanContext() {
101: return this .beanContext;
102: }
103:
104: public BeanContextChild getBeanContextChildPeer() {
105: return this .beanContextChildPeer;
106: }
107:
108: protected void initializeBeanContextResources() {
109: }
110:
111: public boolean isDelegated() {
112: return (!this .beanContextChildPeer.equals(this ));
113: }
114:
115: private void readObject(ObjectInputStream ois) throws IOException,
116: ClassNotFoundException {
117:
118: ois.defaultReadObject();
119: }
120:
121: protected void releaseBeanContextResources() {
122: }
123:
124: public void removePropertyChangeListener(String name,
125: PropertyChangeListener pcl) {
126:
127: this .pcSupport.removePropertyChangeListener(name, pcl);
128: }
129:
130: public void removeVetoableChangeListener(String name,
131: VetoableChangeListener vcl) {
132:
133: this .vcSupport.removeVetoableChangeListener(name, vcl);
134: this .lastVetoedContext = null;
135: }
136:
137: public void serviceAvailable(BeanContextServiceAvailableEvent bcsae) {
138: if (isDelegated()) {
139: ((BeanContextServicesListener) beanContextChildPeer)
140: .serviceAvailable(bcsae);
141: }
142: }
143:
144: public void serviceRevoked(BeanContextServiceRevokedEvent bcsre) {
145: if (isDelegated()) {
146: ((BeanContextServicesListener) beanContextChildPeer)
147: .serviceRevoked(bcsre);
148: }
149: }
150:
151: public synchronized void setBeanContext(BeanContext bc)
152: throws PropertyVetoException {
153:
154: // Do nothing if the old and new values are equal
155: if ((this .beanContext == null) && (bc == null)) {
156: return;
157: }
158:
159: if ((this .beanContext != null) && this .beanContext.equals(bc)) {
160: return;
161: }
162:
163: // Children are not allowed to repeatedly veto this operation.
164: // So, we set rejectedSetBCOnce flag to true if veto occurs
165: // and never veto the change again
166: if (!(this .rejectedSetBCOnce && this .lastVetoedContext == bc)) {
167: this .lastVetoedContext = bc;
168: this .rejectedSetBCOnce = true;
169: // Validate the new BeanContext value and throw
170: // PropertyVetoException if it was not successful
171: if (!validatePendingSetBeanContext(bc)) {
172: throw new PropertyVetoException(Messages
173: .getString("beans.0F"), //$NON-NLS-1$
174: new PropertyChangeEvent(
175: this .beanContextChildPeer,
176: BEAN_CONTEXT, this .beanContext, bc));
177: }
178: fireVetoableChange(BEAN_CONTEXT, this .beanContext, bc);
179: }
180:
181: this .rejectedSetBCOnce = false;
182:
183: releaseBeanContextResources();
184:
185: // We have to notify all listeners about "beanContext"
186: // property change
187: firePropertyChange(BEAN_CONTEXT, this .beanContext, bc);
188: this .beanContext = bc;
189: initializeBeanContextResources();
190: //}
191: }
192:
193: public boolean validatePendingSetBeanContext(BeanContext newValue) {
194: return true;
195: }
196:
197: private void writeObject(ObjectOutputStream oos) throws IOException {
198: oos.defaultWriteObject();
199: }
200: }
|