|
| 1 | +/* |
| 2 | + * Copyright 2004-2008 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.codehaus.groovy.grails.orm.hibernate.proxy; |
| 18 | + |
| 19 | +import java.lang.reflect.InvocationTargetException; |
| 20 | + |
| 21 | +import org.apache.commons.beanutils.PropertyUtils; |
| 22 | +import org.codehaus.groovy.grails.support.proxy.ProxyHandler; |
| 23 | +import org.hibernate.Hibernate; |
| 24 | +import org.hibernate.collection.AbstractPersistentCollection; |
| 25 | +import org.hibernate.collection.PersistentCollection; |
| 26 | +import org.hibernate.proxy.HibernateProxy; |
| 27 | +import org.hibernate.proxy.LazyInitializer; |
| 28 | + |
| 29 | +/** |
| 30 | + * Implementation of the ProxyHandler interface for Hibernate |
| 31 | + * |
| 32 | + * @author Graeme Rocher |
| 33 | + * @since 1.2.2 |
| 34 | + */ |
| 35 | +public class HibernateProxyHandler implements ProxyHandler { |
| 36 | + |
| 37 | + public boolean isInitialized(Object o) { |
| 38 | + if(o instanceof HibernateProxy) { |
| 39 | + return !((HibernateProxy)o).getHibernateLazyInitializer().isUninitialized(); |
| 40 | + } |
| 41 | + else if(o instanceof PersistentCollection) { |
| 42 | + return ((PersistentCollection)o).wasInitialized(); |
| 43 | + } |
| 44 | + return true; |
| 45 | + } |
| 46 | + |
| 47 | + public boolean isInitialized(Object obj, String associationName) { |
| 48 | + try { |
| 49 | + Object proxy = PropertyUtils.getProperty(obj, associationName); |
| 50 | + return Hibernate.isInitialized(proxy); |
| 51 | + } |
| 52 | + catch (IllegalAccessException e) { |
| 53 | + return false; |
| 54 | + } |
| 55 | + catch (InvocationTargetException e) { |
| 56 | + return false; |
| 57 | + } |
| 58 | + catch (NoSuchMethodException e) { |
| 59 | + return false; |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + public Object unwrapIfProxy(Object instance) { |
| 64 | + if(instance instanceof HibernateProxy) { |
| 65 | + final HibernateProxy proxy = (HibernateProxy)instance; |
| 66 | + return unwrapProxy(proxy); |
| 67 | + } |
| 68 | + else if(instance instanceof AbstractPersistentCollection) { |
| 69 | + initialize(instance); |
| 70 | + return instance; |
| 71 | + } |
| 72 | + else { |
| 73 | + return instance; |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + public Object unwrapProxy(final HibernateProxy proxy) { |
| 78 | + final LazyInitializer lazyInitializer = proxy.getHibernateLazyInitializer(); |
| 79 | + if(lazyInitializer.isUninitialized()) { |
| 80 | + lazyInitializer.initialize(); |
| 81 | + } |
| 82 | + return lazyInitializer.getImplementation(); |
| 83 | + } |
| 84 | + |
| 85 | + public HibernateProxy getAssociationProxy(Object obj, String associationName) { |
| 86 | + try { |
| 87 | + Object proxy = PropertyUtils.getProperty(obj, associationName); |
| 88 | + if(proxy instanceof HibernateProxy) return (HibernateProxy) proxy; |
| 89 | + else return null; |
| 90 | + } |
| 91 | + catch (IllegalAccessException e) { |
| 92 | + return null; |
| 93 | + } |
| 94 | + catch (InvocationTargetException e) { |
| 95 | + return null; |
| 96 | + } |
| 97 | + catch (NoSuchMethodException e) { |
| 98 | + return null; |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + public boolean isProxy(Object o) { |
| 103 | + return (o instanceof HibernateProxy) || (o instanceof AbstractPersistentCollection); |
| 104 | + } |
| 105 | + |
| 106 | + public void initialize(Object o) { |
| 107 | + if(o instanceof HibernateProxy) { |
| 108 | + final LazyInitializer hibernateLazyInitializer = ((HibernateProxy)o).getHibernateLazyInitializer(); |
| 109 | + if(hibernateLazyInitializer.isUninitialized()) |
| 110 | + hibernateLazyInitializer.initialize(); |
| 111 | + } |
| 112 | + else if(o instanceof AbstractPersistentCollection) { |
| 113 | + final AbstractPersistentCollection col = (AbstractPersistentCollection)o; |
| 114 | + if(!col.wasInitialized()) |
| 115 | + col.forceInitialization(); |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | +} |
0 commit comments