View Javadoc
1   /**
2    * Copyright 2010-2014 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
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,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.common.util.property.processor;
17  
18  import java.lang.reflect.InvocationTargetException;
19  import java.util.Properties;
20  
21  import org.apache.commons.beanutils.BeanUtils;
22  
23  /**
24   * Copy a String property value into a bean
25   */
26  public class CopyStringProcessor implements PropertyProcessor {
27  
28  	Object bean;
29  	String beanProperty;
30  	String propertyKey;
31  
32  	public CopyStringProcessor() {
33  		this(null, null, null);
34  	}
35  
36  	public CopyStringProcessor(Object bean, String beanProperty, String propertyKey) {
37  		super();
38  		this.bean = bean;
39  		this.beanProperty = beanProperty;
40  		this.propertyKey = propertyKey;
41  	}
42  
43  	@Override
44  	public void process(Properties properties) {
45  		copyProperty(bean, beanProperty, properties, propertyKey);
46  	}
47  
48  	protected void copyProperty(Object bean, String beanProperty, Properties properties, String propertyKey) {
49  		try {
50  			String value = properties.getProperty(propertyKey);
51  			BeanUtils.copyProperty(bean, beanProperty, value);
52  		} catch (InvocationTargetException e) {
53  			throw new IllegalArgumentException(e);
54  		} catch (IllegalAccessException e) {
55  			throw new IllegalArgumentException(e);
56  		}
57  	}
58  
59  	public Object getBean() {
60  		return bean;
61  	}
62  
63  	public void setBean(Object bean) {
64  		this.bean = bean;
65  	}
66  
67  	public String getBeanProperty() {
68  		return beanProperty;
69  	}
70  
71  	public void setBeanProperty(String beanProperty) {
72  		this.beanProperty = beanProperty;
73  	}
74  
75  	public String getPropertyKey() {
76  		return propertyKey;
77  	}
78  
79  	public void setPropertyKey(String propertyKey) {
80  		this.propertyKey = propertyKey;
81  	}
82  
83  }