01: /*
02: * Copyright 2005 Joe Walker
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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.directwebremoting.hibernate;
17:
18: import java.beans.PropertyDescriptor;
19:
20: import javax.servlet.ServletContext;
21:
22: import org.directwebremoting.WebContextFactory;
23: import org.directwebremoting.extend.MarshallException;
24: import org.directwebremoting.extend.Property;
25: import org.directwebremoting.impl.PropertyDescriptorProperty;
26: import org.hibernate.Hibernate;
27: import org.hibernate.Session;
28: import org.hibernate.engine.SessionImplementor;
29: import org.hibernate.proxy.HibernateProxy;
30: import org.hibernate.proxy.LazyInitializer;
31:
32: /**
33: * A {@link Property} that catches hiberntate exceptions.
34: * This is useful for Hibernate 2 where lazy loading results in an exception
35: * and you are unable to detect and prevent this.
36: * @author Joe Walker [joe at getahead dot ltd dot uk]
37: */
38: public class H3PropertyDescriptorProperty extends
39: PropertyDescriptorProperty {
40: /**
41: * Simple constructor
42: * @param descriptor The PropertyDescriptor that we are proxying to
43: */
44: public H3PropertyDescriptorProperty(PropertyDescriptor descriptor) {
45: super (descriptor);
46: }
47:
48: /* (non-Javadoc)
49: * @see org.directwebremoting.impl.PropertyDescriptorProperty#getValue(java.lang.Object)
50: */
51: @Override
52: public Object getValue(Object bean) throws MarshallException {
53: if (!(bean instanceof HibernateProxy)) {
54: // This is not a hibernate dynamic proxy, just use it
55: return super .getValue(bean);
56: } else {
57: // If the property is already initialized, use it
58: boolean initialized = Hibernate.isPropertyInitialized(bean,
59: descriptor.getName());
60: if (initialized) {
61: // This might be a lazy-collection so we need to double check
62: Object reply = super .getValue(bean);
63: initialized = Hibernate.isInitialized(reply);
64: }
65:
66: if (initialized) {
67: return super .getValue(bean);
68: } else {
69: // If the session bound to the property is live, use it
70: HibernateProxy proxy = (HibernateProxy) bean;
71: LazyInitializer initializer = proxy
72: .getHibernateLazyInitializer();
73: SessionImplementor implementor = initializer
74: .getSession();
75: if (implementor.isOpen()) {
76: return super .getValue(bean);
77: }
78:
79: // So the property needs database access, and the session is closed
80: // We'll need to try get another session
81: ServletContext context = WebContextFactory.get()
82: .getServletContext();
83: Session session = H3SessionAjaxFilter
84: .getCurrentSession(context);
85:
86: if (session != null) {
87: session.update(bean);
88: return super.getValue(bean);
89: }
90:
91: return null;
92: }
93: }
94: }
95: }
|