View Javadoc

1   /**
2    * Copyright 2007 Björn Voß
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5    * use this file except in compliance with the License. You may obtain a copy of
6    * the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations under
14   * the License.
15   */
16  package net.sf.oxclient.factory;
17  
18  import java.beans.PropertyDescriptor;
19  import java.lang.reflect.InvocationTargetException;
20  import java.lang.reflect.Method;
21  
22  import net.sf.oval.constraint.NotNull;
23  import net.sf.oval.guard.Guarded;
24  
25  import org.apache.commons.lang.StringUtils;
26  import org.springframework.beans.BeanUtils;
27  import org.springframework.beans.factory.annotation.Required;
28  
29  /**
30   * @author Björn Voß
31   *
32   */
33  @Guarded
34  public class BeanManager implements IBeanFactory, IBeanHelper {
35  
36  	private IBeanFactory beanFactory = null;
37  	private IUniversalConverter converter;
38  
39  	/* (non-Javadoc)
40  	 * @see net.sf.oxclient.factory.IBeanFactory#createNewInstance(java.lang.Class)
41  	 */
42  	public <E> E createNewInstance(final Class<E> clazz) {
43  		return this.beanFactory.createNewInstance(clazz);
44  	}
45  
46  	/* (non-Javadoc)
47  	 * @see net.sf.oxclient.factory.IBeanHelper#setProperty(java.lang.Object, java.lang.String, java.lang.Object)
48  	 */
49  	public void setProperty(
50  			@NotNull final Object bean,
51  			@NotNull final String property,
52  			final Object value) {
53  		if (StringUtils.contains(property, '.')) {
54  			final String nearestProperty = StringUtils.substringBefore(property, ".");
55  			final PropertyDescriptor pd = getPropertyDescriptor(bean, nearestProperty);
56  			final Class<?> propertyClass = pd.getPropertyType();
57  			final Object instance = this.beanFactory.createNewInstance(propertyClass);
58  			final String otherProperty = StringUtils.substringAfter(property, ".");
59  			setProperty(instance, otherProperty	, value);
60  			doSetProperty(bean, nearestProperty, instance);
61  		} else {
62  			doSetProperty(bean, property, value);
63  		}
64  	}
65  
66  	private void doSetProperty(
67  			final Object bean,
68  			final String property,
69  			final Object value) {
70  		try {
71  			final PropertyDescriptor pd = getPropertyDescriptor(bean, property);
72  			final Object myValue;
73  			if (!pd.getPropertyType().isInstance(value)) {
74  				myValue = this.converter.convert(pd.getPropertyType(), value);
75  			} else {
76  				myValue = value;
77  			}
78  			final Method m = getWriteMethode(bean, property, pd);
79  			m.invoke(bean, myValue);
80  		} catch (final IllegalAccessException e) {
81  			throw new BeanAccessException(e, bean.getClass());
82  		} catch (final InvocationTargetException e) {
83  			throw new BeanPropertyException(e, bean.getClass());
84  		}
85  	}
86  
87  	private Method getWriteMethode(
88  			final Object bean,
89  			final String property,
90  			final PropertyDescriptor pd) {
91  		final Method m = pd.getWriteMethod();
92  		if (m == null) {
93  			throw new BeanPropertyException(
94  					"property '" + property + "' can't be written",
95  					bean.getClass());
96  		}
97  		return m;
98  	}
99  
100 	private PropertyDescriptor getPropertyDescriptor(
101 			final Object bean,
102 			final String property) {
103 		final PropertyDescriptor pd =
104 			BeanUtils.getPropertyDescriptor(bean.getClass(), property);
105 		if (pd == null) {
106 			throw new BeanPropertyException(
107 					"property '" + property + "' not found",
108 					bean.getClass());
109 		}
110 		return pd;
111 	}
112 
113 	@Required
114 	public void setBeanFactory(@NotNull final IBeanFactory beanFactory) {
115 		this.beanFactory = beanFactory;
116 	}
117 
118 	@Required
119 	public void setConverter(@NotNull final IUniversalConverter converter) {
120 		this.converter = converter;
121 	}
122 
123 }