Project

General

Profile

Download (2.6 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
 * @version 1.0
22
 */
23
public class HibernateProxyHelper {
24
	@SuppressWarnings("unused")
25
	private static final Logger logger = Logger.getLogger(HibernateProxyHelper.class);
26

    
27

    
28
	// ************************** Hibernate proxies *******************/
29
	 public static <T> T deproxy(Object object, Class<T> clazz) throws ClassCastException {
30
	     if (object instanceof HibernateProxy) {
31
	         return clazz.cast(((HibernateProxy) object).getHibernateLazyInitializer().getImplementation());
32
	     } else {
33
	         return clazz.cast(object);
34
	     }
35
	 }
36

    
37
	 /**
38
	  * Unwrap the target instance from the proxy.
39
	  */
40
	 public static <T> T deproxy(T entity){
41
	     if (entity == null){
42
	         return null;
43
	     }
44
	     if(entity instanceof HibernateProxy) {
45
	         Hibernate.initialize(entity);
46
	         entity = (T) ((HibernateProxy) entity).getHibernateLazyInitializer().getImplementation();
47
	     }
48
	     return entity;
49
	}
50

    
51

    
52
	public static boolean isInstanceOf(Object object, Class clazz) throws ClassCastException {
53
	     if (clazz == null || object == null){
54
	    	 return false;
55
	     }
56
		 if (object instanceof HibernateProxy) {
57
	    	 Object impl =  ((HibernateProxy) object).getHibernateLazyInitializer().getImplementation();
58
	         Class<?> implClass = impl.getClass();
59
	         return clazz.isAssignableFrom(implClass);
60
	     } else {
61
	         return clazz.isAssignableFrom(object.getClass());
62
	     }
63
	 }
64

    
65
	public static Serializable getIdentifierOf(Object object) {
66
        if (object instanceof HibernateProxy) {
67
            return  ((HibernateProxy) object).getHibernateLazyInitializer().getIdentifier();
68
        } else {
69
            throw new ClassCastException("Cannot cast the given Object to a known Hibernate proxy.");
70
        }
71
    }
72

    
73
	/**
74
	 * Get the class of an instance or the underlying class
75
	 * of a proxy (without initializing the proxy!). It is
76
	 * almost always better to use the entity name!
77
	 *
78
	 * delegates calls to {@link org.hibernate.proxy.HibernateProxyHelper}
79
	 */
80
	public static Class getClassWithoutInitializingProxy(Object object) {
81
		return org.hibernate.proxy.HibernateProxyHelper.getClassWithoutInitializingProxy(object);
82
	}
83
}
(5-5/11)