DomainEntityLazyLoader.java from Texai at Krugle
Show DomainEntityLazyLoader.java syntax highlighted
/*
* DomainEntityLazyLoader.java
*
* Created on March 2, 2007, 1:30 PM
*
* Description:
*
* Copyright (C) 2007 Stephen L. Reed.
*
* This program is free software; you can redistribute it and/or modify it under the terms
* of the GNU General Public License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program;
* if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
package org.texai.kb.persistence.lazy;
import java.lang.reflect.Field;
import net.sf.cglib.proxy.LazyLoader;
import org.apache.log4j.Logger;
import org.texai.kb.ejb.session.DomainEntityLoaderLocal;
import org.texai.kb.persistence.DomainProperty;
/**
*
* @author reed
*/
public final class DomainEntityLazyLoader implements LazyLoader {
/** the logger */
private static Logger LOGGER = Logger.getLogger(DomainEntityLazyLoader.class);
/** the domain entity loader */
private final DomainEntityLoaderLocal domainEntityLoader;
/** the domain instance */
private final Object domainEntity;
/** the domain instance field */
private final Field field;
/** the domain property */
private final DomainProperty domainProperty;
/** the loaded object */
private Object loadedObject;
/** the indicator that the lazy object is currently being loaded */
private boolean isLoading = false;
/** the indicator that domain entities can be loaded from the cache */
private final boolean isLoadableFromDomainEntityCache;
/**
* Creates a new instance of DomainEntityLazyLoader.
*
* @param domainEntityLoader the domain entity loader
* @param domainInstance the domain instance
* @param field the domain instance field
* @param domainProperty the domain property
* @param isLoadableFromDomainEntityCache the indicator that domain entities can be loaded from the cache
*/
public DomainEntityLazyLoader(
final DomainEntityLoaderLocal domainEntityLoader,
final Object domainInstance,
final Field field,
final DomainProperty domainProperty,
final boolean isLoadableFromDomainEntityCache) {
super();
//Preconditions
assert domainEntityLoader != null : "domainEntityLoader must not be null";
assert domainInstance != null : "domainInstance must not be null";
assert field != null : "field must not be null";
assert domainProperty != null : "domainProperty must not be null";
this.domainEntityLoader = domainEntityLoader;
this.domainEntity = domainInstance;
this.field = field;
this.domainProperty = domainProperty;
this.isLoadableFromDomainEntityCache = isLoadableFromDomainEntityCache;
}
/** Gets the domain entity loader.
*
* @return the domain entity loader
*/
public DomainEntityLoaderLocal getDomainEntityLoader() {
return domainEntityLoader;
}
/** Gets the domain instance.
*
* @return the domain instance
*/
public Object getDomainInstance() {
return domainEntity;
}
/** Gets the domain instance field.
*
* @return the domain instance field
*/
public Field getField() {
return field;
}
/** Gets the domain property.
*
* @return the domain property
*/
public DomainProperty getDomainProperty() {
return domainProperty;
}
/** Returns the object to which the original method invocation should be dispatched.
* Also replaces the lazy object with the loaded object on the domain entity so that subsequent
* invocations will directly access the object.
*
* @return the object to which the original method invocation should be dispatched
*/
public Object loadObject() throws Exception {
if (!isLoading && loadedObject == null) {
isLoading = true;
loadedObject = domainEntityLoader.loadDomainEntityField(
domainEntity,
field,
domainProperty,
isLoadableFromDomainEntityCache);
isLoading = false;
LOGGER.debug("dynamically loaded " + loadedObject + " for field " + field);
}
return loadedObject;
}
/** Returns whether this lazy object is in the process of loading.
*
* @return whether this lazy object is in the process of loading
*/
public boolean isLoading() {
return isLoading();
}
/** Returns whether this lazy object is loaded.
*
* @return whether this lazy object is loaded
*/
public boolean isLoaded() {
return !isLoading && loadedObject != null;
}
}
See more files for this project here