1 16 17 package org.springframework.orm.toplink; 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 oracle.toplink.exceptions.TopLinkException; 25 import oracle.toplink.sessions.Session; 26 import oracle.toplink.sessions.UnitOfWork; 27 import org.apache.commons.logging.Log; 28 import org.apache.commons.logging.LogFactory; 29 30 41 public abstract class AbstractSessionFactory implements SessionFactory { 42 43 44 protected final Log logger = LogFactory.getLog(getClass()); 45 46 47 51 public Session createSession() throws TopLinkException { 52 logger.debug("Creating TopLink client Session"); 53 return createClientSession(); 54 } 55 56 61 public Session createManagedClientSession() throws TopLinkException { 62 logger.debug("Creating managed TopLink client Session"); 63 return (Session) Proxy.newProxyInstance( 64 getClass().getClassLoader(), new Class [] {Session.class}, 65 new ManagedClientInvocationHandler(createClientSession())); 66 } 67 68 75 public Session createTransactionAwareSession() throws TopLinkException { 76 logger.debug("Creating transaction-aware TopLink Session"); 77 return createTransactionAwareSession(this); 78 } 79 80 91 public Session createTransactionAwareSession(SessionFactory sessionFactory) throws TopLinkException { 92 return (Session) Proxy.newProxyInstance( 93 getClass().getClassLoader(), new Class [] {Session.class}, 94 new TransactionAwareInvocationHandler(sessionFactory, getMasterSession())); 95 } 96 97 98 103 protected abstract Session getMasterSession(); 104 105 111 protected abstract Session createClientSession() throws TopLinkException; 112 113 114 119 private static class ManagedClientInvocationHandler implements InvocationHandler { 120 121 private final Session target; 122 123 private final UnitOfWork uow; 124 125 public ManagedClientInvocationHandler(Session target) { 126 this.target = target; 127 this.uow = this.target.acquireUnitOfWork(); 128 } 129 130 public Object invoke(Object proxy, Method method, Object [] args) throws Throwable { 131 if (method.getName().equals("getActiveSession")) { 132 return this.target; 133 } 134 else if (method.getName().equals("getActiveUnitOfWork")) { 135 return this.uow; 136 } 137 else if (method.getName().equals("release")) { 138 this.uow.release(); 139 this.target.release(); 140 } 141 else if (method.getName().equals("equals")) { 142 return (proxy == args[0] ? Boolean.TRUE : Boolean.FALSE); 144 } 145 else if (method.getName().equals("hashCode")) { 146 return new Integer (hashCode()); 148 } 149 150 try { 152 return method.invoke(this.target, args); 153 } 154 catch (InvocationTargetException ex) { 155 throw ex.getTargetException(); 156 } 157 } 158 } 159 160 161 165 private static class TransactionAwareInvocationHandler implements InvocationHandler { 166 167 private final SessionFactory sessionFactory; 168 169 private final Session target; 170 171 public TransactionAwareInvocationHandler(SessionFactory sessionFactory, Session target) { 172 this.sessionFactory = sessionFactory; 173 this.target = target; 174 } 175 176 public Object invoke(Object proxy, Method method, Object [] args) throws Throwable { 177 179 if (method.getName().equals("getActiveSession")) { 180 try { 182 return SessionFactoryUtils.doGetSession(this.sessionFactory, false); 183 } 184 catch (IllegalStateException ex) { 185 return this.target; 187 } 188 } 189 else if (method.getName().equals("getActiveUnitOfWork")) { 190 try { 192 return SessionFactoryUtils.doGetSession(this.sessionFactory, false).getActiveUnitOfWork(); 193 } 194 catch (IllegalStateException ex) { 195 return null; 197 } 198 } 199 else if (method.getName().equals("equals")) { 200 return (proxy == args[0] ? Boolean.TRUE : Boolean.FALSE); 202 } 203 else if (method.getName().equals("hashCode")) { 204 return new Integer (hashCode()); 206 } 207 208 try { 210 return method.invoke(this.target, args); 211 } 212 catch (InvocationTargetException ex) { 213 throw ex.getTargetException(); 214 } 215 } 216 } 217 218 } 219 | Popular Tags |