From: <one...@us...> - 2002-11-26 03:35:47
|
Update of /cvsroot/hibernate/Hibernate/cirrus/hibernate/proxy In directory sc8-pr-cvs1:/tmp/cvs-serv20166/cirrus/hibernate/proxy Modified Files: CGLIBLazyInitializer.java LazyInitializer.java SerializableProxy.java Log Message: fixed broken line-endings and added a test Index: CGLIBLazyInitializer.java =================================================================== RCS file: /cvsroot/hibernate/Hibernate/cirrus/hibernate/proxy/CGLIBLazyInitializer.java,v retrieving revision 1.5 retrieving revision 1.6 diff -C2 -d -r1.5 -r1.6 *** CGLIBLazyInitializer.java 7 Nov 2002 10:10:59 -0000 1.5 --- CGLIBLazyInitializer.java 26 Nov 2002 03:35:44 -0000 1.6 *************** *** 1,56 **** ! //$Id$ ! package cirrus.hibernate.proxy; ! ! import java.io.Serializable; ! import java.lang.reflect.Method; ! import org.apache.commons.logging.LogFactory; ! ! import cirrus.hibernate.HibernateException; ! import cirrus.hibernate.engine.SessionImplementor; ! import net.sf.cglib.proxy.Enhancer; ! import net.sf.cglib.proxy.MethodInterceptor; ! - /** - * - */ - public final class CGLIBLazyInitializer extends LazyInitializer implements MethodInterceptor { - - private Class[] interfaces; - - public static HibernateProxy getProxy(Class persistentClass, Class[] interfaces, Method getIdentifierMethod, Serializable id, SessionImplementor session) throws HibernateException { - try { - return (HibernateProxy) Enhancer.enhance( - persistentClass, - interfaces, - new CGLIBLazyInitializer(persistentClass, interfaces, id, getIdentifierMethod, session), - persistentClass.getClassLoader() - ); - } - catch (Throwable t) { - LogFactory.getLog(LazyInitializer.class).error("CGLIB Enhancement failed", t); - throw new HibernateException( "CGLIB Enhancement failed", t ); - } - } - - private CGLIBLazyInitializer(Class persistentClass, Class[] interfaces, Serializable id, Method getIdentifierMethod, SessionImplementor session) { - super(persistentClass, id, getIdentifierMethod, session); - this.interfaces = interfaces; - } - - public boolean invokeSuper(Object obj, Method method, Object args[]) throws Throwable { - return false; - } - - - public Object afterReturn(Object obj, Method method, Object args[], boolean invokedSuper, Object retValFromSuper, Throwable e) throws Throwable{ - return invoke(method, args); - } - - protected Object serializableProxy() { - return new SerializableProxy(persistentClass, interfaces, id, getIdentifierMethod); - } - } --- 1,56 ---- ! //$Id$ ! package cirrus.hibernate.proxy; ! import java.io.Serializable; ! import java.lang.reflect.Method; ! import org.apache.commons.logging.LogFactory; ! ! import cirrus.hibernate.HibernateException; ! import cirrus.hibernate.engine.SessionImplementor; ! ! import net.sf.cglib.proxy.Enhancer; ! import net.sf.cglib.proxy.MethodInterceptor; ! ! ! /** ! * ! */ ! public final class CGLIBLazyInitializer extends LazyInitializer implements MethodInterceptor { ! ! private Class[] interfaces; ! ! public static HibernateProxy getProxy(Class persistentClass, Class[] interfaces, Method getIdentifierMethod, Serializable id, SessionImplementor session) throws HibernateException { ! try { ! return (HibernateProxy) Enhancer.enhance( ! persistentClass, ! interfaces, ! new CGLIBLazyInitializer(persistentClass, interfaces, id, getIdentifierMethod, session), ! persistentClass.getClassLoader() ! ); ! } ! catch (Throwable t) { ! LogFactory.getLog(LazyInitializer.class).error("CGLIB Enhancement failed", t); ! throw new HibernateException( "CGLIB Enhancement failed", t ); ! } ! } ! ! private CGLIBLazyInitializer(Class persistentClass, Class[] interfaces, Serializable id, Method getIdentifierMethod, SessionImplementor session) { ! super(persistentClass, id, getIdentifierMethod, session); ! this.interfaces = interfaces; ! } ! ! public boolean invokeSuper(Object obj, Method method, Object args[]) throws Throwable { ! return false; ! } ! ! ! public Object afterReturn(Object obj, Method method, Object args[], boolean invokedSuper, Object retValFromSuper, Throwable e) throws Throwable{ ! return invoke(method, args); ! } ! ! protected Object serializableProxy() { ! return new SerializableProxy(persistentClass, interfaces, id, getIdentifierMethod); ! } } Index: LazyInitializer.java =================================================================== RCS file: /cvsroot/hibernate/Hibernate/cirrus/hibernate/proxy/LazyInitializer.java,v retrieving revision 1.10 retrieving revision 1.11 diff -C2 -d -r1.10 -r1.11 *** LazyInitializer.java 5 Nov 2002 06:46:06 -0000 1.10 --- LazyInitializer.java 26 Nov 2002 03:35:44 -0000 1.11 *************** *** 1,156 **** ! //$Id$ ! package cirrus.hibernate.proxy; ! ! import java.io.Serializable; ! import java.lang.reflect.Method; ! import java.sql.SQLException; ! ! import org.apache.commons.logging.LogFactory; ! ! import cirrus.hibernate.HibernateException; ! import cirrus.hibernate.LazyInitializationException; ! import cirrus.hibernate.engine.Key; ! import cirrus.hibernate.engine.SessionImplementor; ! import cirrus.hibernate.helpers.ReflectHelper; ! ! /** ! * ! */ ! public abstract class LazyInitializer { ! ! protected Object target = null; ! protected Serializable id; ! protected SessionImplementor session; ! protected Class persistentClass; ! protected Method getIdentifierMethod; ! protected boolean overridesEquals; ! ! protected LazyInitializer(Class persistentClass, Serializable id, Method getIdentifierMethod, SessionImplementor session) { ! this.id = id; ! this.session = session; ! this.persistentClass = persistentClass; ! this.getIdentifierMethod = getIdentifierMethod; ! overridesEquals = ReflectHelper.overridesEquals(persistentClass); ! } ! ! public void initialize() throws HibernateException, SQLException { ! if (target==null) { ! if ( session==null ) { ! throw new HibernateException("Could not initialize proxy - no Session"); ! } ! else if ( !session.isOpen() ) { ! throw new HibernateException("Could not initialize proxy - the owning Session was closed"); ! } ! else { ! target = session.immediateLoad(persistentClass, id); ! } ! } ! } ! ! private void initializeWrapExceptions() { ! try { ! initialize(); ! } ! catch (Exception e) { ! LogFactory.getLog(LazyInitializer.class).error("Exception initializing proxy", e); ! throw new LazyInitializationException(e); ! } ! } ! ! protected abstract Object serializableProxy(); ! ! protected final Object invoke(Method method, Object[] args) throws Throwable { ! ! String methodName = method.getName(); ! int params = method.getParameterTypes().length; ! ! if ( params==0 ) { ! ! if ( "writeReplace".equals(methodName) ) { ! ! if (target==null && session!=null ) target = session.getEntity( ! new Key( id, session.getFactory().getPersister(persistentClass) ) ! ); ! if (target==null) { ! /*if ( session==null || !session.isOpen() ) { ! return session.getFactory().getPersister(persistentClass).instantiate(id); //A little "empty" object ! } ! else {*/ ! return serializableProxy(); ! //} ! } ! else { ! return target; ! } ! ! } ! else if ( !overridesEquals && "hashCode".equals(methodName) ) { ! // kinda dodgy, since it redefines the hashcode of the proxied object. ! // but necessary if we are to keep proxies in HashSets without ! // forcing them to be initialized ! return new Integer( id.hashCode() ); ! } ! else if ( method.equals(getIdentifierMethod) ) { ! return id; ! } ! else if ( "finalize".equals( method.getName() ) ) { ! return null; ! } ! ! } ! else if ( params==1 && !overridesEquals && "equals".equals(methodName) ) { ! // less dodgy because Hibernate forces == to be same as identifier equals ! return new Boolean( id.equals( getIdentifierMethod.invoke( args[0], null ) ) ); ! } ! ! // otherwise: ! return method.invoke( getImplementation(), args ); ! ! } ! ! public final Serializable getIdentifier() { ! return id; ! } ! ! public final Class getPersistentClass() { ! return persistentClass; ! } ! ! public final boolean isUninitialized() { ! return target == null; ! } ! ! public final SessionImplementor getSession() { ! return session; ! } ! ! public final void setSession(SessionImplementor s) { ! if (s!=session) { ! if ( session!=null && session.isOpen() ) { ! //TODO: perhaps this should be some other RuntimeException... ! throw new LazyInitializationException("Illegally attempted to associate a proxy with two open Sessions"); ! } ! else { ! session = s; ! } ! } ! } ! ! /** ! * Return the underlying persistent object, initializing if necessary ! */ ! public final Object getImplementation() throws HibernateException, SQLException { ! initializeWrapExceptions(); ! return target; ! } ! ! /** ! * Return the underlying persistent object in the given <tt>Session</tt>, or null ! */ ! public final Object getImplementation(SessionImplementor s) throws HibernateException { ! return s.getEntity( new Key( ! getIdentifier(), ! s.getFactory().getPersister( getPersistentClass() ) ! ) ); ! } ! } --- 1,156 ---- ! //$Id$ ! package cirrus.hibernate.proxy; ! ! import java.io.Serializable; ! import java.lang.reflect.Method; ! import java.sql.SQLException; ! ! import org.apache.commons.logging.LogFactory; ! ! import cirrus.hibernate.HibernateException; ! import cirrus.hibernate.LazyInitializationException; ! import cirrus.hibernate.engine.Key; ! import cirrus.hibernate.engine.SessionImplementor; ! import cirrus.hibernate.helpers.ReflectHelper; ! ! /** ! * ! */ ! public abstract class LazyInitializer { ! ! protected Object target = null; ! protected Serializable id; ! protected SessionImplementor session; ! protected Class persistentClass; ! protected Method getIdentifierMethod; ! protected boolean overridesEquals; ! ! protected LazyInitializer(Class persistentClass, Serializable id, Method getIdentifierMethod, SessionImplementor session) { ! this.id = id; ! this.session = session; ! this.persistentClass = persistentClass; ! this.getIdentifierMethod = getIdentifierMethod; ! overridesEquals = ReflectHelper.overridesEquals(persistentClass); ! } ! ! public void initialize() throws HibernateException, SQLException { ! if (target==null) { ! if ( session==null ) { ! throw new HibernateException("Could not initialize proxy - no Session"); ! } ! else if ( !session.isOpen() ) { ! throw new HibernateException("Could not initialize proxy - the owning Session was closed"); ! } ! else { ! target = session.immediateLoad(persistentClass, id); ! } ! } ! } ! ! private void initializeWrapExceptions() { ! try { ! initialize(); ! } ! catch (Exception e) { ! LogFactory.getLog(LazyInitializer.class).error("Exception initializing proxy", e); ! throw new LazyInitializationException(e); ! } ! } ! ! protected abstract Object serializableProxy(); ! ! protected final Object invoke(Method method, Object[] args) throws Throwable { ! ! String methodName = method.getName(); ! int params = method.getParameterTypes().length; ! ! if ( params==0 ) { ! ! if ( "writeReplace".equals(methodName) ) { ! ! if (target==null && session!=null ) target = session.getEntity( ! new Key( id, session.getFactory().getPersister(persistentClass) ) ! ); ! if (target==null) { ! /*if ( session==null || !session.isOpen() ) { ! return session.getFactory().getPersister(persistentClass).instantiate(id); //A little "empty" object ! } ! else {*/ ! return serializableProxy(); ! //} ! } ! else { ! return target; ! } ! ! } ! else if ( !overridesEquals && "hashCode".equals(methodName) ) { ! // kinda dodgy, since it redefines the hashcode of the proxied object. ! // but necessary if we are to keep proxies in HashSets without ! // forcing them to be initialized ! return new Integer( id.hashCode() ); ! } ! else if ( method.equals(getIdentifierMethod) ) { ! return id; ! } ! else if ( "finalize".equals( method.getName() ) ) { ! return null; ! } ! ! } ! else if ( params==1 && !overridesEquals && "equals".equals(methodName) ) { ! // less dodgy because Hibernate forces == to be same as identifier equals ! return new Boolean( id.equals( getIdentifierMethod.invoke( args[0], null ) ) ); ! } ! ! // otherwise: ! return method.invoke( getImplementation(), args ); ! ! } ! ! public final Serializable getIdentifier() { ! return id; ! } ! ! public final Class getPersistentClass() { ! return persistentClass; ! } ! ! public final boolean isUninitialized() { ! return target == null; ! } ! ! public final SessionImplementor getSession() { ! return session; ! } ! ! public final void setSession(SessionImplementor s) { ! if (s!=session) { ! if ( session!=null && session.isOpen() ) { ! //TODO: perhaps this should be some other RuntimeException... ! throw new LazyInitializationException("Illegally attempted to associate a proxy with two open Sessions"); ! } ! else { ! session = s; ! } ! } ! } ! ! /** ! * Return the underlying persistent object, initializing if necessary ! */ ! public final Object getImplementation() throws HibernateException, SQLException { ! initializeWrapExceptions(); ! return target; ! } ! ! /** ! * Return the underlying persistent object in the given <tt>Session</tt>, or null ! */ ! public final Object getImplementation(SessionImplementor s) throws HibernateException { ! return s.getEntity( new Key( ! getIdentifier(), ! s.getFactory().getPersister( getPersistentClass() ) ! ) ); ! } ! } Index: SerializableProxy.java =================================================================== RCS file: /cvsroot/hibernate/Hibernate/cirrus/hibernate/proxy/SerializableProxy.java,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** SerializableProxy.java 29 Oct 2002 02:45:42 -0000 1.2 --- SerializableProxy.java 26 Nov 2002 03:35:44 -0000 1.3 *************** *** 1,46 **** ! //$Id$ ! package cirrus.hibernate.proxy; ! ! import java.io.Serializable; ! import java.lang.reflect.Method; ! ! import org.apache.commons.logging.LogFactory; ! ! import cirrus.hibernate.helpers.ReflectHelper; ! ! /** ! * Serializable placeholder for <tt>CGLIB</tt> proxies ! */ ! public final class SerializableProxy implements Serializable { ! ! public SerializableProxy() {} ! ! public SerializableProxy(Class persistentClass, Class[] interfaces, Serializable id, Method getIdentifierMethod) { ! this.persistentClass = persistentClass; ! this.interfaces = interfaces; ! this.id = id; ! this.getIdentifierMethod = (getIdentifierMethod==null) ? null : getIdentifierMethod.getName(); ! } ! ! private Class persistentClass; ! private Class[] interfaces; ! private Serializable id; ! private String getIdentifierMethod; ! ! Object readResolve() { ! try { ! return CGLIBLazyInitializer.getProxy( ! persistentClass, ! interfaces, ! (getIdentifierMethod==null) ? null : ReflectHelper.getMethod(persistentClass, getIdentifierMethod), ! id, ! null ! ); ! } ! catch (Exception e) { ! LogFactory.getLog(CGLIBLazyInitializer.class).error("Exception deserializing proxy", e); ! //TODO ! return null; ! } ! } ! } --- 1,46 ---- ! //$Id$ ! package cirrus.hibernate.proxy; ! ! import java.io.Serializable; ! import java.lang.reflect.Method; ! ! import org.apache.commons.logging.LogFactory; ! ! import cirrus.hibernate.helpers.ReflectHelper; ! ! /** ! * Serializable placeholder for <tt>CGLIB</tt> proxies ! */ ! public final class SerializableProxy implements Serializable { ! ! public SerializableProxy() {} ! ! public SerializableProxy(Class persistentClass, Class[] interfaces, Serializable id, Method getIdentifierMethod) { ! this.persistentClass = persistentClass; ! this.interfaces = interfaces; ! this.id = id; ! this.getIdentifierMethod = (getIdentifierMethod==null) ? null : getIdentifierMethod.getName(); ! } ! ! private Class persistentClass; ! private Class[] interfaces; ! private Serializable id; ! private String getIdentifierMethod; ! ! Object readResolve() { ! try { ! return CGLIBLazyInitializer.getProxy( ! persistentClass, ! interfaces, ! (getIdentifierMethod==null) ? null : ReflectHelper.getMethod(persistentClass, getIdentifierMethod), ! id, ! null ! ); ! } ! catch (Exception e) { ! LogFactory.getLog(CGLIBLazyInitializer.class).error("Exception deserializing proxy", e); ! //TODO ! return null; ! } ! } ! } |