001/** 002 * GRANITE DATA SERVICES 003 * Copyright (C) 2006-2013 GRANITE DATA SERVICES S.A.S. 004 * 005 * This file is part of the Granite Data Services Platform. 006 * 007 * Granite Data Services is free software; you can redistribute it and/or 008 * modify it under the terms of the GNU Lesser General Public 009 * License as published by the Free Software Foundation; either 010 * version 2.1 of the License, or (at your option) any later version. 011 * 012 * Granite Data Services is distributed in the hope that it will be useful, 013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser 015 * General Public License for more details. 016 * 017 * You should have received a copy of the GNU Lesser General Public 018 * License along with this library; if not, write to the Free Software 019 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 020 * USA, or see <http://www.gnu.org/licenses/>. 021 */ 022package org.granite.hibernate; 023 024import java.io.Serializable; 025 026import javax.persistence.Entity; 027 028import org.granite.logging.Logger; 029import org.granite.messaging.amf.io.util.DefaultClassGetter; 030import org.granite.util.TypeUtil; 031import org.hibernate.Hibernate; 032import org.hibernate.proxy.HibernateProxy; 033import org.hibernate.proxy.LazyInitializer; 034 035/** 036 * @author Franck WOLFF 037 */ 038public class HibernateClassGetter extends DefaultClassGetter { 039 040 private final static Logger log = Logger.getLogger(HibernateClassGetter.class); 041 042 @Override 043 public Class<?> getClass(Object o) { 044 045 if (o instanceof HibernateProxy) { 046 HibernateProxy proxy = (HibernateProxy)o; 047 LazyInitializer initializer = proxy.getHibernateLazyInitializer(); 048 049 String className = ( 050 initializer.isUninitialized() ? 051 initializer.getEntityName() : 052 initializer.getImplementation().getClass().getName() 053 ); 054 055 if (className != null && className.length() > 0) { 056 try { 057 return TypeUtil.forName(className); 058 } catch (Exception e) { 059 log.warn(e, "Could not get class with initializer: %s for: %s", initializer.getClass().getName(), className); 060 } 061 } 062 // fallback... 063 return initializer.getPersistentClass(); 064 } 065 066 return super.getClass(o); 067 } 068 069 @Override 070 public boolean isEntity(Object o) { 071 return o.getClass().isAnnotationPresent(Entity.class); 072 } 073 074 @Override 075 public Serializable getIdentifier(Object o) { 076 if (o instanceof HibernateProxy) 077 return ((HibernateProxy)o).getHibernateLazyInitializer().getIdentifier(); 078 return null; 079 } 080 081 @Override 082 public boolean isInitialized(Object owner, String propertyName, Object propertyValue) { 083 return Hibernate.isInitialized(propertyValue); 084 } 085 086 @Override 087 public void initialize(Object owner, String propertyName, Object propertyValue) { 088 Hibernate.initialize(propertyValue); 089 } 090}