Project

General

Profile

Download (2.95 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2007 EDIT
3
* European Distributed Institute of Taxonomy
4
* http://www.e-taxonomy.eu
5
*
6
* The contents of this file are subject to the Mozilla Public License Version 1.1
7
* See LICENSE.TXT at the top of this package for the full license terms.
8
*/
9

    
10
package eu.etaxonomy.cdm.hibernate;
11

    
12
import java.io.Serializable;
13

    
14
import org.apache.log4j.Logger;
15
import org.hibernate.Hibernate;
16
import org.hibernate.proxy.HibernateProxy;
17

    
18
/**
19
 * @author a.mueller
20
 * @created 03.03.2009
21
 */
22
public class HibernateProxyHelper {
23
	@SuppressWarnings("unused")
24
	private static final Logger logger = Logger.getLogger(HibernateProxyHelper.class);
25

    
26

    
27
	// ************************** Hibernate proxies *******************/
28
	/**
29
	 * Deproxy and cast the given object to the given class.
30
	 * If clazz is <code>null</code>. If object is not an instance of HibernateProxy no
31
	 * deproxy is performed.
32
	 *
33
	 * @param object
34
	 * @param clazz
35
	 * @return the casted and deproxied object
36
	 * @throws ClassCastException
37
	 */
38
	public static <T> T deproxy(Object object, Class<T> clazz) throws ClassCastException {
39
	     if (object instanceof HibernateProxy) {
40
	         object = ((HibernateProxy) object).getHibernateLazyInitializer().getImplementation();
41
	         return clazz.cast(object);
42
	     }
43
	     if (clazz == null){
44
	         return (T)object;
45
	     }else {
46
	         return clazz.cast(object);
47
	     }
48
	 }
49

    
50
	 /**
51
	  * Unwrap the target instance from the proxy.
52
	  */
53
	 public static <T> T deproxy(T entity){
54
	     if (entity == null){
55
	         return null;
56
	     }
57
	     if(entity instanceof HibernateProxy) {
58
	         Hibernate.initialize(entity);
59
	         entity = (T) ((HibernateProxy) entity).getHibernateLazyInitializer().getImplementation();
60
	     }
61
	     return entity;
62
	}
63

    
64

    
65
	public static boolean isInstanceOf(Object object, Class clazz) throws ClassCastException {
66
	     if (clazz == null || object == null){
67
	    	 return false;
68
	     }
69
		 if (object instanceof HibernateProxy) {
70
	    	 Object impl =  ((HibernateProxy) object).getHibernateLazyInitializer().getImplementation();
71
	         Class<?> implClass = impl.getClass();
72
	         return clazz.isAssignableFrom(implClass);
73
	     } else {
74
	         return clazz.isAssignableFrom(object.getClass());
75
	     }
76
	 }
77

    
78
	public static Serializable getIdentifierOf(Object object) {
79
        if (object instanceof HibernateProxy) {
80
            return  ((HibernateProxy) object).getHibernateLazyInitializer().getIdentifier();
81
        } else {
82
            throw new ClassCastException("Cannot cast the given Object to a known Hibernate proxy.");
83
        }
84
    }
85

    
86
	/**
87
	 * Get the class of an instance or the underlying class
88
	 * of a proxy (without initializing the proxy!). It is
89
	 * almost always better to use the entity name!
90
	 *
91
	 * delegates calls to {@link org.hibernate.proxy.HibernateProxyHelper}
92
	 */
93
	public static Class getClassWithoutInitializingProxy(Object object) {
94
		return org.hibernate.proxy.HibernateProxyHelper.getClassWithoutInitializingProxy(object);
95
	}
96
}
(5-5/11)