View Javadoc

1   /*
2    * EL4J, the Extension Library for the J2EE, adds incremental enhancements to
3    * the spring framework, http://el4j.sf.net
4    * Copyright (C) 2005 by ELCA Informatique SA, Av. de la Harpe 22-24,
5    * 1000 Lausanne, Switzerland, http://www.elca.ch
6    *
7    * EL4J is published under the GNU Lesser General Public License (LGPL)
8    * Version 2.1. See http://www.gnu.org/licenses/
9    *
10   * This program is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13   * GNU Lesser General Public License for more details.
14   *
15   * For alternative licensing, please contact info@elca.ch
16   */
17  package ch.elca.el4j.util.objectwrapper.impl;
18  
19  import java.io.Serializable;
20  import java.lang.reflect.InvocationTargetException;
21  import java.lang.reflect.Method;
22  import java.util.Locale;
23  
24  import ch.elca.el4j.util.objectwrapper.ObjectWrapperRTException;
25  import ch.elca.el4j.util.objectwrapper.interfaces.KeyedVersioned;
26  
27  
28  /**
29   * Implementation of KeyedVersioned that uses reflection on properties "key" and "version". 
30   *
31   * @svnLink $Revision: 3879 $;$Date: 2009-08-04 15:13:46 +0200 (Di, 04. Aug 2009) $;$Author: swismer $;$URL: https://el4j.svn.sourceforge.net/svnroot/el4j/branches/el4j_3_1/el4j/framework/modules/hibernate/src/main/java/ch/elca/el4j/util/objectwrapper/impl/KeyedVersionedReflectionImpl.java $
32   *
33   * @author David Bernhard (DBD)
34   */
35  public class KeyedVersionedReflectionImpl extends AbstractWrapper implements
36  	KeyedVersioned {
37  
38  	/** getKey(). */
39  	private Method m_keyGetter;
40  	
41  	/** setKey(). */
42  	private Method m_keySetter;
43  	
44  	/** getVersion(). */
45  	private Method m_versionGetter;
46  	
47  	/** setVersion(). */
48  	private Method m_versionSetter;
49  	
50  	/** The key class. */
51  	private Class<?> m_keyClass;
52  	
53  	/** The version class. */
54  	private Class<?> m_versionClass;
55  	
56  	/** The key property name. */
57  	private String m_keyName;
58  	
59  	/** The version property name. */
60  	private String m_versionName;
61  	
62  	/**
63  	 * Empty constructor which uses "key" and "version" as properties. 
64  	 */
65  	public KeyedVersionedReflectionImpl() {
66  		this("key", "version");
67  	}
68  	
69  	/**
70  	 * Create the wrapper class providing property names for key and version.
71  	 * @param keyName The key property name.
72  	 * @param versionName The version property name.
73  	 */
74  	public KeyedVersionedReflectionImpl(String keyName, String versionName) {
75  		m_keyName = keyName.substring(0, 1).toUpperCase(Locale.getDefault())
76  			+ keyName.substring(1);
77  		m_versionName = versionName.substring(0, 1).toUpperCase(Locale.getDefault())
78  			+ versionName.substring(1);
79  	}
80  	
81  	/** {@inheritDoc} */
82  	@Override
83  	public void create() {
84  		Class<?> cls = m_target.getClass();
85  		try {
86  			m_keyGetter = cls.getMethod("get" + m_keyName, new Class<?>[0]);
87  			m_keyClass = m_keyGetter.getReturnType();
88  			m_keySetter = cls.getMethod("set" + m_keyName, m_keyClass);
89  			m_versionGetter = cls.getMethod("get" + m_versionName, new Class<?>[0]);
90  			m_versionClass = m_versionGetter.getReturnType();
91  			m_versionSetter = cls.getMethod("set" + m_versionName, m_versionClass);
92  		} catch (NoSuchMethodException ex) {
93  			throw new ObjectWrapperRTException("Failed to get method via reflection.", ex);
94  		}
95  	}
96  
97  	/** {@inheritDoc} */
98  	public Serializable getKey() {
99  		try {
100 			return (Serializable) m_keyGetter.invoke(m_target, new Object[0]);
101 		} catch (IllegalAccessException e) {
102 			throw new ObjectWrapperRTException(e);
103 		} catch (InvocationTargetException e) {
104 			throw new ObjectWrapperRTException(e);
105 		}
106 	}
107 
108 	/** {@inheritDoc} */
109 	public Serializable getVersion() {
110 		try {
111 			return (Serializable) m_versionGetter.invoke(m_target, new Object[0]);
112 		} catch (IllegalAccessException e) {
113 			throw new ObjectWrapperRTException(e);
114 		} catch (InvocationTargetException e) {
115 			throw new ObjectWrapperRTException(e);
116 		}
117 	}
118 
119 	/** {@inheritDoc} */
120 	public void setKey(Serializable key) {
121 		try {
122 			m_keySetter.invoke(m_target, key);
123 		} catch (IllegalAccessException e) {
124 			throw new ObjectWrapperRTException(e);
125 		} catch (InvocationTargetException e) {
126 			throw new ObjectWrapperRTException(e);
127 		}
128 
129 	}
130 
131 	/** {@inheritDoc} */
132 	public void setVersion(Serializable version) {
133 		try {
134 			m_versionSetter.invoke(m_target, version);
135 		} catch (IllegalAccessException e) {
136 			throw new ObjectWrapperRTException(e);
137 		} catch (InvocationTargetException e) {
138 			throw new ObjectWrapperRTException(e);
139 		}
140 
141 	}
142 
143 	/** {@inheritDoc} */
144 	public Class<?> getKeyClass() {
145 		return m_keyClass;
146 	}
147 
148 	/** {@inheritDoc} */
149 	public Class<?> getVersionClass() {
150 		return m_versionClass;
151 	}
152 	
153 	
154 
155 }