Skip to content

Commit e1f0eb6

Browse files
committed
created an interface that helps with dealing with proxied objects that has a default implementation and one for hibernate
1 parent 37032dc commit e1f0eb6

File tree

3 files changed

+229
-0
lines changed

3 files changed

+229
-0
lines changed
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
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+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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+
package org.codehaus.groovy.grails.support.proxy;
17+
18+
/**
19+
* Trivial default implementation that always returns true and the object
20+
*
21+
* @author Graeme Rocher
22+
* @since 1.2.2
23+
*
24+
*/
25+
public class DefaultProxyHandler implements ProxyHandler {
26+
27+
public boolean isInitialized(Object o) {
28+
return true;
29+
}
30+
31+
public boolean isInitialized(Object obj, String associationName) {
32+
return true;
33+
}
34+
35+
public Object unwrapIfProxy(Object instance) {
36+
return instance;
37+
}
38+
39+
public boolean isProxy(Object o) {
40+
return false;
41+
}
42+
43+
public void initialize(Object o) {
44+
// do nothing
45+
}
46+
47+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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.support.proxy;
18+
19+
/**
20+
* Interface that defines logic for handling proxied instances
21+
*
22+
* @author Graeme Rocher
23+
* @since 1.2.2
24+
*
25+
*/
26+
public interface ProxyHandler {
27+
28+
/**
29+
* Returns true if the specified object is a proxy
30+
* @param o The object in question
31+
* @return True if it is a proxy
32+
*/
33+
public boolean isProxy(Object o);
34+
/**
35+
* Returns the unwrapped proxy instance or the original object if not proxied
36+
*
37+
* @param instance The instance to unwrap
38+
* @return The unwrapped instance
39+
*/
40+
public Object unwrapIfProxy(Object instance);
41+
42+
/**
43+
* Returns whether a lazy proxied instance has been initialized
44+
*
45+
* @param o The instance to test
46+
* @return True if it has been initialized false otherwise
47+
*/
48+
public boolean isInitialized(Object o);
49+
50+
/**
51+
* Initializes an existing uninitialized proxy instance
52+
* @param o The proxy instance
53+
*/
54+
public void initialize(Object o);
55+
56+
/**
57+
* Tests whether an association of the given object has been initialized or not
58+
* @param obj The object to check
59+
* @param associationName The association
60+
* @return True if has been init
61+
*/
62+
boolean isInitialized(Object obj, String associationName);
63+
}

0 commit comments

Comments
 (0)