⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 lazyinitializer.java

📁 用Java实现的23个常用设计模式源代码
💻 JAVA
字号:
//$Id: LazyInitializer.java,v 1.10.2.4 2003/09/11 06:25:08 oneovthafew Exp $package net.sf.hibernate.proxy;import java.io.Serializable;import java.lang.reflect.Method;import org.apache.commons.logging.LogFactory;import net.sf.hibernate.HibernateException;import net.sf.hibernate.LazyInitializationException;import net.sf.hibernate.engine.Key;import net.sf.hibernate.engine.SessionImplementor;import net.sf.hibernate.util.ReflectHelper;/** * @author Gavin King */public abstract class LazyInitializer {		protected static final Object INVOKE_IMPLEMENTATION = new Object();		protected Object target = null;	protected Serializable id;	protected SessionImplementor session;	protected Class persistentClass;	protected Method getIdentifierMethod;	protected Method setIdentifierMethod;	protected boolean overridesEquals;	private Object replacement;		protected LazyInitializer(Class persistentClass, Serializable id, Method getIdentifierMethod, Method setIdentifierMethod, SessionImplementor session) {		this.id = id;		this.session = session;		this.persistentClass = persistentClass;		this.getIdentifierMethod = getIdentifierMethod;		this.setIdentifierMethod = setIdentifierMethod;		overridesEquals = ReflectHelper.overridesEquals(persistentClass);	}		public void initialize() throws HibernateException {		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, Object proxy) 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 (replacement==null) replacement = serializableProxy();					return replacement;				}				else {					return target;				}				/*if (replacement==null) replacement = serializableProxy();				replacement.setSnapshot(snapshot);				replacement.setTarget( (Serializable) target );				return replacement;*/							}			else if ( !overridesEquals && "hashCode".equals(methodName) ) {				return new Integer( System.identityHashCode(proxy) );			}			else if ( method.equals(getIdentifierMethod) ) {				return id;			}			else if ( "finalize".equals( method.getName() ) ) {				return null;			}					}		else if ( params==1 ) {						if ( !overridesEquals && "equals".equals(methodName) ) {				return new Boolean( args[0]==proxy );			}			else if ( method.equals(setIdentifierMethod) ) {				initialize();				id = (Serializable) args[0];				return INVOKE_IMPLEMENTATION;			}					}				// otherwise:		return INVOKE_IMPLEMENTATION;			}		public final Serializable getIdentifier() {		return id;	}		public final void setIdentifier(Serializable id) {		this.id = 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() {		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() )		) );	}}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -