1 16 package org.directwebremoting.hibernate; 17 18 import java.beans.BeanInfo ; 19 import java.beans.IntrospectionException ; 20 import java.beans.Introspector ; 21 import java.beans.PropertyDescriptor ; 22 import java.lang.reflect.Method ; 23 import java.util.HashMap ; 24 import java.util.Map ; 25 26 import org.directwebremoting.convert.BeanConverter; 27 import org.directwebremoting.convert.PlainProperty; 28 import org.directwebremoting.extend.Converter; 29 import org.directwebremoting.extend.MarshallException; 30 import org.directwebremoting.util.Logger; 31 import org.hibernate.Hibernate; 32 import org.hibernate.engine.SessionImplementor; 33 import org.hibernate.proxy.HibernateProxy; 34 import org.hibernate.proxy.LazyInitializer; 35 36 40 public class H3BeanConverter extends BeanConverter implements Converter 41 { 42 45 public Map getPropertyMapFromObject(Object example, boolean readRequired, boolean writeRequired) throws MarshallException 46 { 47 Class clazz = getClass(example); 48 49 try 50 { 51 BeanInfo info = Introspector.getBeanInfo(clazz); 52 PropertyDescriptor [] descriptors = info.getPropertyDescriptors(); 53 54 Map properties = new HashMap (); 55 for (int i = 0; i < descriptors.length; i++) 56 { 57 PropertyDescriptor descriptor = descriptors[i]; 58 String name = descriptor.getName(); 59 60 if (name.equals("class")) 62 { 63 continue; 64 } 65 66 if (name.equals("hibernateLazyInitializer")) 68 { 69 continue; 70 } 71 72 if (!isAllowedByIncludeExcludeRules(name)) 74 { 75 continue; 76 } 77 78 if (readRequired && descriptor.getReadMethod() == null) 79 { 80 continue; 81 } 82 83 if (writeRequired && descriptor.getWriteMethod() == null) 84 { 85 continue; 86 } 87 88 if (!assumeSession) 89 { 90 String propertyName = descriptor.getName(); 93 Method method = findGetter(example, propertyName); 94 95 if (method == null) 96 { 97 log.warn("Failed to find property: " + propertyName); 98 99 properties.put(name, new PlainProperty(propertyName, null)); 100 continue; 101 } 102 103 if (!Hibernate.isPropertyInitialized(example, propertyName)) 104 { 105 properties.put(name, new PlainProperty(propertyName, null)); 106 continue; 107 } 108 109 Object retval = method.invoke(example, new Object [] {}); 111 if (!Hibernate.isInitialized(retval)) 112 { 113 properties.put(name, new PlainProperty(propertyName, null)); 114 continue; 115 } 116 } 117 118 properties.put(name, new H3PropertyDescriptorProperty(descriptor)); 119 } 120 121 return properties; 122 } 123 catch (Exception ex) 124 { 125 throw new MarshallException(clazz, ex); 126 } 127 } 128 129 134 public Class getClass(Object example) 135 { 136 if (example instanceof HibernateProxy) 137 { 138 HibernateProxy proxy = (HibernateProxy) example; 139 LazyInitializer initializer = proxy.getHibernateLazyInitializer(); 140 SessionImplementor implementor = initializer.getSession(); 141 142 if (initializer.isUninitialized()) 143 { 144 try 145 { 146 if (implementor.isClosed()) 148 { 149 return example.getClass(); 151 } 152 } 153 catch (NoSuchMethodError ex) 154 { 155 } 158 } 159 160 return initializer.getImplementation().getClass(); 161 } 162 else 163 { 164 return example.getClass(); 165 } 166 } 167 168 176 protected Method findGetter(Object data, String property) throws IntrospectionException 177 { 178 String key = data.getClass().getName() + ":" + property; 179 180 Method method = (Method ) methods.get(key); 181 if (method == null) 182 { 183 PropertyDescriptor [] props = Introspector.getBeanInfo(data.getClass()).getPropertyDescriptors(); 184 for (int i = 0; i < props.length; i++) 185 { 186 if (props[i].getName().equalsIgnoreCase(property)) 187 { 188 method = props[i].getReadMethod(); 189 } 190 } 191 192 methods.put(key, method); 193 } 194 195 return method; 196 } 197 198 201 public void setAssumeSession(boolean assumeSession) 202 { 203 this.assumeSession = assumeSession; 204 } 205 206 209 protected boolean assumeSession = false; 210 211 214 protected static final Map methods = new HashMap (); 215 216 219 private static final Logger log = Logger.getLogger(H3BeanConverter.class); 220 } 221 | Popular Tags |