View Javadoc

1   /*
2    * Copyright (c) 2005 Regents of the University of California (Regents). Created
3    * by TELS, Graduate School of Education, University of California at Berkeley.
4    *
5    * This software is distributed under the GNU Lesser General Public License, v2.
6    *
7    * Permission is hereby granted, without written agreement and without license
8    * or royalty fees, to use, copy, modify, and distribute this software and its
9    * documentation for any purpose, provided that the above copyright notice and
10   * the following two paragraphs appear in all copies of this software.
11   *
12   * REGENTS SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
13   * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
14   * PURPOSE. THE SOFTWAREAND ACCOMPANYING DOCUMENTATION, IF ANY, PROVIDED
15   * HEREUNDER IS PROVIDED "AS IS". REGENTS HAS NO OBLIGATION TO PROVIDE
16   * MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
17   *
18   * IN NO EVENT SHALL REGENTS BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT,
19   * SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS,
20   * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
21   * REGENTS HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
22   */
23  package net.sf.sail.core.beans.assembly;
24  
25  import java.beans.DefaultPersistenceDelegate;
26  import java.lang.reflect.InvocationTargetException;
27  
28  import org.apache.commons.beanutils.PropertyUtils;
29  
30  /**
31   * Reference to a bean's property that maintains over long-term persistance.
32   * 
33   * @author turadg
34   */
35  public class PropertyRef {
36  
37  	final static DefaultPersistenceDelegate PR_DELEGATE = new DefaultPersistenceDelegate(
38  			new String[] { "target", "propertyName" });
39  
40  	Object target;
41  
42  	String propertyName;
43  
44  	/**
45  	 * @param target the target bean
46  	 * @param propertyName the name of the property of target bean that this references
47  	 */
48  	public PropertyRef(Object target, String propertyName) {
49  		this.target = target;
50  		this.propertyName = propertyName;
51  	}
52  
53  	public Object getTarget() {
54  		return target;
55  	}
56  
57  	public String getPropertyName() {
58  		return propertyName;
59  	}
60  
61  	public void set(Object value) throws IllegalAccessException,
62  			InvocationTargetException, NoSuchMethodException {
63  		PropertyUtils.setSimpleProperty(target, propertyName, value);
64  	}
65  
66  	public Object getCurrentValue() throws IllegalAccessException,
67  			InvocationTargetException, NoSuchMethodException {
68  		return PropertyUtils.getSimpleProperty(target, propertyName);
69  	}
70  
71  	/* (non-Javadoc)
72  	 * @see java.lang.Object#toString()
73  	 */
74  	@Override
75  	public String toString() {
76  		return "PropertyRef(\"" + target + "\".\"" + propertyName+"\")";
77  	}
78  }