1 16 17 package org.springframework.orm.jdo; 18 19 import java.lang.reflect.InvocationHandler ; 20 import java.lang.reflect.InvocationTargetException ; 21 import java.lang.reflect.Method ; 22 import java.lang.reflect.Proxy ; 23 24 import javax.jdo.PersistenceManager; 25 import javax.jdo.PersistenceManagerFactory; 26 27 import org.springframework.beans.factory.FactoryBean; 28 import org.springframework.util.ClassUtils; 29 30 70 public class TransactionAwarePersistenceManagerFactoryProxy implements FactoryBean { 71 72 private PersistenceManagerFactory target; 73 74 private boolean allowCreate = true; 75 76 private PersistenceManagerFactory proxy; 77 78 79 85 public void setTargetPersistenceManagerFactory(PersistenceManagerFactory target) { 86 this.target = target; 87 Class [] ifcs = ClassUtils.getAllInterfaces(target); 88 this.proxy = (PersistenceManagerFactory) Proxy.newProxyInstance( 89 getClass().getClassLoader(), ifcs, new TransactionAwareFactoryInvocationHandler()); 90 } 91 92 95 public PersistenceManagerFactory getTargetPersistenceManagerFactory() { 96 return this.target; 97 } 98 99 110 public void setAllowCreate(boolean allowCreate) { 111 this.allowCreate = allowCreate; 112 } 113 114 119 protected boolean isAllowCreate() { 120 return allowCreate; 121 } 122 123 124 public Object getObject() { 125 return this.proxy; 126 } 127 128 public Class getObjectType() { 129 return PersistenceManagerFactory.class; 130 } 131 132 public boolean isSingleton() { 133 return true; 134 } 135 136 137 142 private class TransactionAwareFactoryInvocationHandler implements InvocationHandler { 143 144 public Object invoke(Object proxy, Method method, Object [] args) throws Throwable { 145 PersistenceManagerFactory target = getTargetPersistenceManagerFactory(); 147 148 if (method.getName().equals("getPersistenceManager")) { 149 PersistenceManager pm = 150 PersistenceManagerFactoryUtils.doGetPersistenceManager(target, isAllowCreate()); 151 Class [] ifcs = ClassUtils.getAllInterfaces(pm); 152 return (PersistenceManager) Proxy.newProxyInstance( 153 getClass().getClassLoader(), ifcs, new TransactionAwareInvocationHandler(pm, target)); 154 } 155 else if (method.getName().equals("equals")) { 156 return (proxy == args[0] ? Boolean.TRUE : Boolean.FALSE); 158 } 159 else if (method.getName().equals("hashCode")) { 160 return new Integer (hashCode()); 162 } 163 164 try { 166 return method.invoke(target, args); 167 } 168 catch (InvocationTargetException ex) { 169 throw ex.getTargetException(); 170 } 171 } 172 } 173 174 175 179 private static class TransactionAwareInvocationHandler implements InvocationHandler { 180 181 private final PersistenceManager target; 182 183 private final PersistenceManagerFactory persistenceManagerFactory; 184 185 public TransactionAwareInvocationHandler(PersistenceManager target, PersistenceManagerFactory pmf) { 186 this.target = target; 187 this.persistenceManagerFactory = pmf; 188 } 189 190 public Object invoke(Object proxy, Method method, Object [] args) throws Throwable { 191 193 if (method.getName().equals("equals")) { 194 return (proxy == args[0] ? Boolean.TRUE : Boolean.FALSE); 196 } 197 else if (method.getName().equals("hashCode")) { 198 return new Integer (hashCode()); 200 } 201 else if (method.getName().equals("close")) { 202 if (this.persistenceManagerFactory != null) { 204 PersistenceManagerFactoryUtils.doReleasePersistenceManager( 205 this.target, this.persistenceManagerFactory); 206 } 207 return null; 208 } 209 210 try { 212 return method.invoke(this.target, args); 213 } 214 catch (InvocationTargetException ex) { 215 throw ex.getTargetException(); 216 } 217 } 218 } 219 220 } 221 | Popular Tags |