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) 2009 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.services.remoting.jaxb.hibernate;
18  
19  import java.lang.reflect.Field;
20  import java.lang.reflect.Method;
21  
22  import javax.xml.bind.JAXBException;
23  
24  import org.slf4j.Logger;
25  import org.slf4j.LoggerFactory;
26  
27  import com.sun.xml.bind.AccessorFactory;
28  import com.sun.xml.bind.AccessorFactoryImpl;
29  import com.sun.xml.bind.v2.runtime.reflect.Accessor;
30  
31  /**
32   * A JAXB Accessor factory that uses {@link HibernateJAXBAccessor} if possible.
33   *
34   * @svnLink $Revision: 3945 $;$Date: 2009-10-16 09:31:52 +0200 (Fr, 16. Okt 2009) $;$Author: swismer $;$URL: https://el4j.svn.sourceforge.net/svnroot/el4j/branches/el4j_3_1/el4j/framework/modules/remoting_jaxws/src/main/java/ch/elca/el4j/services/remoting/jaxb/hibernate/HibernateJAXBAccessorFactory.java $
35   *
36   * @author clemenb (see https://forum.hibernate.org/viewtopic.php?f=1&t=998896)
37   */
38  public class HibernateJAXBAccessorFactory implements AccessorFactory {
39  
40  	/**
41  	 * The delegate accessor factory.
42  	 */
43  	private final AccessorFactory delegate;
44  
45  	/**
46  	 * The method to call for checking if value is uninitialized.
47  	 */
48  	private Method hibernateInitializationCheck;
49  
50  	/**
51  	 * Create a new AccessorFactory that never throws LazyInitializationException during (un)marshaling.
52  	 */
53  	public HibernateJAXBAccessorFactory() {
54  		this(AccessorFactoryImpl.getInstance());
55  	}
56  
57  	/**
58  	 * @param delegate    the delegate accessor factory
59  	 */
60  	@SuppressWarnings("unchecked")
61  	public HibernateJAXBAccessorFactory(AccessorFactory delegate) {
62  		this.delegate = delegate;
63  		try {
64  			Class hibernate = Class.forName("org.hibernate.Hibernate");
65  			hibernateInitializationCheck = hibernate.getMethod("isInitialized", Object.class);
66  			Logger logger = LoggerFactory.getLogger(HibernateJAXBAccessorFactory.class);
67  			logger.info("Detected Hibernate: Enabled "
68  				+ "hiding of uninitialized lazy objects and collections during XML marshalling.");
69  		} catch (ClassNotFoundException e) {
70  			hibernateInitializationCheck = null;
71  			Logger logger = LoggerFactory.getLogger(HibernateJAXBAccessorFactory.class);
72  			logger.info("Hibernate was not detected: Disabled "
73  				+ "hiding of uninitialized lazy objects and collections during XML marshalling.");
74  		} catch (Exception e) {
75  			hibernateInitializationCheck = null;
76  			Logger logger = LoggerFactory.getLogger(HibernateJAXBAccessorFactory.class);
77  			logger.warn("Detected Hibernate, but failed "
78  				+ "to enable hiding of uninitialized lazy objects and collections during XML marshalling.", e);
79  		}
80  	}
81  
82  	@SuppressWarnings("unchecked")
83  	@Override
84  	public Accessor createFieldAccessor(Class bean, Field field, boolean readOnly) throws JAXBException {
85  		Accessor accessor = delegate.createFieldAccessor(bean, field, readOnly);
86  
87  		if (hibernateInitializationCheck == null) {
88  			return accessor;
89  		} else {
90  			return new HibernateJAXBAccessor(accessor, hibernateInitializationCheck);
91  		}
92  	}
93  
94  	@SuppressWarnings("unchecked")
95  	@Override
96  	public Accessor createPropertyAccessor(Class bean, Method getter, Method setter) throws JAXBException {
97  		Accessor accessor = delegate.createPropertyAccessor(bean, getter, setter);
98  
99  		if (hibernateInitializationCheck == null) {
100 			return accessor;
101 		} else {
102 			return new HibernateJAXBAccessor(accessor, hibernateInitializationCheck);
103 		}
104 	}
105 }