KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > directwebremoting > hibernate > H3PropertyDescriptorProperty


1 /*
2  * Copyright 2005 Joe Walker
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.directwebremoting.hibernate;
17
18 import java.beans.PropertyDescriptor JavaDoc;
19
20 import javax.servlet.ServletContext JavaDoc;
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 PropertyDescriptorProperty
39 {
40     /**
41      * Simple constructor
42      * @param descriptor The PropertyDescriptor that we are proxying to
43      */

44     public H3PropertyDescriptorProperty(PropertyDescriptor JavaDoc descriptor)
45     {
46         super(descriptor);
47     }
48
49     /* (non-Javadoc)
50      * @see org.directwebremoting.impl.PropertyDescriptorProperty#getValue(java.lang.Object)
51      */

52     public Object JavaDoc getValue(Object JavaDoc bean) throws MarshallException
53     {
54         if (!(bean instanceof HibernateProxy))
55         {
56             // This is not a hibernate dynamic proxy, just use it
57
return super.getValue(bean);
58         }
59         else
60         {
61             // If the property is already initialized, use it
62
boolean initialized = Hibernate.isPropertyInitialized(bean, descriptor.getName());
63             if (initialized)
64             {
65                 // This might be a lazy-collection so we need to double check
66
Object JavaDoc reply = super.getValue(bean);
67                 initialized = Hibernate.isInitialized(reply);
68             }
69
70             if (initialized)
71             {
72                 return super.getValue(bean);
73             }
74             else
75             {
76                 // If the session bound to the property is live, use it
77
HibernateProxy proxy = (HibernateProxy) bean;
78                 LazyInitializer initializer = proxy.getHibernateLazyInitializer();
79                 SessionImplementor implementor = initializer.getSession();
80                 if (implementor.isOpen())
81                 {
82                     return super.getValue(bean);
83                 }
84
85                 // So the property needs database access, and the session is closed
86
// We'll need to try get another session
87
ServletContext JavaDoc context = WebContextFactory.get().getServletContext();
88                 Session session = H3SessionAjaxFilter.getCurrentSession(context);
89
90                 if (session != null)
91                 {
92                     session.update(bean);
93                     return super.getValue(bean);
94                 }
95
96                 return null;
97             }
98         }
99     }
100 }
101
Popular Tags