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.HibernateException;
|
17
|
import org.hibernate.proxy.HibernateProxy;
|
18
|
|
19
|
/**
|
20
|
* @author a.mueller
|
21
|
* @since 03.03.2009
|
22
|
*/
|
23
|
public class HibernateProxyHelper {
|
24
|
@SuppressWarnings("unused")
|
25
|
private static final Logger logger = Logger.getLogger(HibernateProxyHelper.class);
|
26
|
|
27
|
|
28
|
// ************************** Hibernate proxies *******************/
|
29
|
/**
|
30
|
* Deproxy and cast the given object to the given class.
|
31
|
* If clazz is <code>null</code>. If object is not an instance of HibernateProxy no
|
32
|
* deproxy is performed.
|
33
|
*
|
34
|
* @param object
|
35
|
* @param clazz
|
36
|
* @return the casted and deproxied object
|
37
|
* @throws ClassCastException
|
38
|
*/
|
39
|
public static <T> T deproxy(Object object, Class<T> clazz) throws ClassCastException {
|
40
|
if (object instanceof HibernateProxy) {
|
41
|
object = ((HibernateProxy) object).getHibernateLazyInitializer().getImplementation();
|
42
|
return clazz.cast(object);
|
43
|
}
|
44
|
if (clazz == null){
|
45
|
return (T)object;
|
46
|
}else {
|
47
|
return clazz.cast(object);
|
48
|
}
|
49
|
}
|
50
|
|
51
|
/**
|
52
|
* Unwrap the target instance from the proxy.
|
53
|
*
|
54
|
* @throws org.hibernate.HibernateException
|
55
|
*/
|
56
|
public static <T> T deproxy(T entity){
|
57
|
if (entity == null){
|
58
|
return null;
|
59
|
}
|
60
|
if(entity instanceof HibernateProxy) {
|
61
|
Hibernate.initialize(entity);
|
62
|
entity = (T) ((HibernateProxy) entity).getHibernateLazyInitializer().getImplementation();
|
63
|
}
|
64
|
return entity;
|
65
|
}
|
66
|
|
67
|
/**
|
68
|
* Unwrap the target instance from the proxy if possible otrherwise return null.
|
69
|
*
|
70
|
* @param entity
|
71
|
* @return the deproxied entity or null in case it was not initialized.
|
72
|
*/
|
73
|
public static <T> T deproxyOrNull(T entity){
|
74
|
try{
|
75
|
return deproxy(entity);
|
76
|
} catch (HibernateException e) {
|
77
|
return null;
|
78
|
}
|
79
|
}
|
80
|
|
81
|
|
82
|
public static boolean isInstanceOf(Object object, Class clazz) throws ClassCastException {
|
83
|
if (clazz == null || object == null){
|
84
|
return false;
|
85
|
}
|
86
|
if (object instanceof HibernateProxy) {
|
87
|
Object impl = ((HibernateProxy) object).getHibernateLazyInitializer().getImplementation();
|
88
|
Class<?> implClass = impl.getClass();
|
89
|
return clazz.isAssignableFrom(implClass);
|
90
|
} else {
|
91
|
return clazz.isAssignableFrom(object.getClass());
|
92
|
}
|
93
|
}
|
94
|
|
95
|
public static Serializable getIdentifierOf(Object object) {
|
96
|
if (object instanceof HibernateProxy) {
|
97
|
return ((HibernateProxy) object).getHibernateLazyInitializer().getIdentifier();
|
98
|
} else {
|
99
|
throw new ClassCastException("Cannot cast the given Object to a known Hibernate proxy.");
|
100
|
}
|
101
|
}
|
102
|
|
103
|
/**
|
104
|
* Get the class of an instance or the underlying class
|
105
|
* of a proxy (without initializing the proxy!). It is
|
106
|
* almost always better to use the entity name!
|
107
|
*
|
108
|
* delegates calls to {@link org.hibernate.proxy.HibernateProxyHelper}
|
109
|
*/
|
110
|
public static Class getClassWithoutInitializingProxy(Object object) {
|
111
|
return org.hibernate.proxy.HibernateProxyHelper.getClassWithoutInitializingProxy(object);
|
112
|
}
|
113
|
}
|