View Javadoc
1   package org.oxerr.commons.ws.rs.bean;
2   
3   import java.lang.reflect.InvocationTargetException;
4   
5   import org.apache.commons.beanutils.BeanUtils;
6   import org.apache.commons.beanutils.BeanUtilsBean;
7   import org.apache.commons.beanutils.ContextClassLoaderLocal;
8   
9   public class NullAwareBeanUtilsBean extends BeanUtilsBean {
10  
11  	/**
12  	 * Contains <code>NullAwareBeanUtilsBean</code> instances indexed by context classloader.
13  	 */
14  	private static final ContextClassLoaderLocal<NullAwareBeanUtilsBean>
15  		BEANS_BY_CLASSLOADER = new ContextClassLoaderLocal<NullAwareBeanUtilsBean>() {
16  			// Creates the default instance used when the context classloader is unavailable
17  			@Override
18  			protected NullAwareBeanUtilsBean initialValue() {
19  				return new NullAwareBeanUtilsBean();
20  			}
21  		};
22  
23  	/**
24  	 * Gets the instance which provides the functionality for {@link BeanUtils}.
25  	 * This is a pseudo-singleton - an single instance is provided per (thread) context classloader.
26  	 * This mechanism provides isolation for web apps deployed in the same container.
27  	 *
28  	 * @return The (pseudo-singleton) BeanUtils bean instance
29  	 */
30  	public static NullAwareBeanUtilsBean getInstance() {
31  		return BEANS_BY_CLASSLOADER.get();
32  	}
33  
34  	@Override
35  	public void copyProperty(Object dest, String name, Object value)
36  			throws IllegalAccessException, InvocationTargetException {
37  		if (value == null) {
38  			return;
39  		}
40  
41  		super.copyProperty(dest, name, value);
42  	}
43  
44  }