Project

General

Profile

Download (1.71 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 org.apache.log4j.Logger;
13
import org.hibernate.proxy.HibernateProxy;
14

    
15
/**
16
 * @author a.mueller
17
 * @created 03.03.2009
18
 * @version 1.0
19
 */
20
public class HibernateProxyHelper {
21
	@SuppressWarnings("unused")
22
	private static final Logger logger = Logger.getLogger(HibernateProxyHelper.class);
23
	
24
	
25
	// ************************** Hibernate proxies *******************/
26
	/* (non-Javadoc)
27
	 * @see eu.etaxonomy.cdm.model.common.IProxyHelper#deproxy(java.lang.Object, java.lang.Class)
28
	 */
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
	 /* (non-Javadoc)
38
	 * @see eu.etaxonomy.cdm.model.common.IProxyHelper#isInstanceOf(java.lang.Object, java.lang.Class)
39
	 */
40
	public static boolean isInstanceOf(Object object, Class clazz) throws ClassCastException {
41
	     if (clazz == null){
42
	    	 return false;
43
	     }
44
		 if (object instanceof HibernateProxy) {
45
	    	 Object impl =  ((HibernateProxy) object).getHibernateLazyInitializer().getImplementation();
46
	         Class implClass = impl.getClass();
47
	         return clazz.isAssignableFrom(implClass);
48
	     } else {
49
	         return clazz.isAssignableFrom(object.getClass());
50
	     }
51
	 }
52
}
    (1-1/1)