📄 genericdelegator.java
字号:
/*
* $Id: GenericDelegator.java,v 1.15 2004/01/20 17:10:49 ajzeneski Exp $
*
* Copyright (c) 2001, 2002 The Open For Business Project - www.ofbiz.org
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
* OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
* THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
package org.ofbiz.entity;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URL;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import javax.xml.parsers.ParserConfigurationException;
import org.ofbiz.base.util.Debug;
import org.ofbiz.base.util.UtilCache;
import org.ofbiz.base.util.UtilMisc;
import org.ofbiz.base.util.UtilXml;
import org.ofbiz.entity.condition.*;
import org.ofbiz.entity.config.EntityConfigUtil;
import org.ofbiz.entity.datasource.GenericHelper;
import org.ofbiz.entity.datasource.GenericHelperFactory;
import org.ofbiz.entity.eca.EntityEcaHandler;
import org.ofbiz.entity.model.DynamicViewEntity;
import org.ofbiz.entity.model.ModelEntity;
import org.ofbiz.entity.model.ModelField;
import org.ofbiz.entity.model.ModelFieldType;
import org.ofbiz.entity.model.ModelFieldTypeReader;
import org.ofbiz.entity.model.ModelGroupReader;
import org.ofbiz.entity.model.ModelKeyMap;
import org.ofbiz.entity.model.ModelReader;
import org.ofbiz.entity.model.ModelRelation;
import org.ofbiz.entity.model.ModelViewEntity;
import org.ofbiz.entity.serialize.SerializeException;
import org.ofbiz.entity.serialize.XmlSerializer;
import org.ofbiz.entity.transaction.TransactionUtil;
import org.ofbiz.entity.util.DistributedCacheClear;
import org.ofbiz.entity.util.EntityFindOptions;
import org.ofbiz.entity.util.EntityListIterator;
import org.ofbiz.entity.util.EntityUtil;
import org.ofbiz.entity.util.SequenceUtil;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.xml.sax.SAXException;
/**
* Generic Data Source Delegator Class
*
* @author <a href="mailto:jonesde@ofbiz.org">David E. Jones</a>
* @author <a href="mailto:chris_maurer@altavista.com">Chris Maurer</a>
* @author <a href="mailto:jaz@ofbiz.org">Andy Zeneski</a
* @version $Revision: 1.15 $
* @since 1.0
*/
public class GenericDelegator implements DelegatorInterface {
public static final String module = GenericDelegator.class.getName();
/** the delegatorCache will now be a HashMap, allowing reload of definitions,
* but the delegator will always be the same object for the given name */
protected static Map delegatorCache = new HashMap();
protected String delegatorName;
protected EntityConfigUtil.DelegatorInfo delegatorInfo = null;
/** set this to true for better performance; set to false to be able to reload definitions at runtime throught the cache manager */
public static final boolean keepLocalReaders = true;
protected ModelReader modelReader = null;
protected ModelGroupReader modelGroupReader = null;
protected UtilCache primaryKeyCache = null;
protected UtilCache allCache = null;
protected UtilCache andCache = null;
// keeps a list of field key sets used in the by and cache, a Set (of Sets of fieldNames) for each entityName
protected Map andCacheFieldSets = new HashMap();
protected DistributedCacheClear distributedCacheClear = null;
protected EntityEcaHandler entityEcaHandler = null;
public static final String ECA_HANDLER_CLASS_NAME = "org.ofbiz.entityext.eca.DelegatorEcaHandler";
protected SequenceUtil sequencer = null;
public static GenericDelegator getGenericDelegator(String delegatorName) {
GenericDelegator delegator = (GenericDelegator) delegatorCache.get(delegatorName);
if (delegator == null) {
synchronized (GenericDelegator.class) {
// must check if null again as one of the blocked threads can still enter
delegator = (GenericDelegator) delegatorCache.get(delegatorName);
if (delegator == null) {
try {
delegator = new GenericDelegator(delegatorName);
} catch (GenericEntityException e) {
Debug.logError(e, "Error creating delegator", module);
}
if (delegator != null) {
delegatorCache.put(delegatorName, delegator);
}
}
}
}
return delegator;
}
/** Only allow creation through the factory method */
protected GenericDelegator() {}
/** Only allow creation through the factory method */
protected GenericDelegator(String delegatorName) throws GenericEntityException {
if (Debug.infoOn()) Debug.logInfo("Creating new Delegator with name \"" + delegatorName + "\".", module);
this.delegatorName = delegatorName;
if (keepLocalReaders) {
modelReader = ModelReader.getModelReader(delegatorName);
modelGroupReader = ModelGroupReader.getModelGroupReader(delegatorName);
}
primaryKeyCache = new UtilCache("entity.FindByPrimaryKey." + delegatorName, 0, 0, true);
allCache = new UtilCache("entity.FindAll." + delegatorName, 0, 0, true);
andCache = new UtilCache("entity.FindByAnd." + delegatorName, 0, 0, true);
// initialize helpers by group
Iterator groups = UtilMisc.toIterator(getModelGroupReader().getGroupNames());
while (groups != null && groups.hasNext()) {
String groupName = (String) groups.next();
String helperName = this.getGroupHelperName(groupName);
if (Debug.infoOn()) Debug.logInfo("Delegator \"" + delegatorName + "\" initializing helper \"" +
helperName + "\" for entity group \"" + groupName + "\".", module);
TreeSet helpersDone = new TreeSet();
if (helperName != null && helperName.length() > 0) {
// make sure each helper is only loaded once
if (helpersDone.contains(helperName)) {
if (Debug.infoOn()) Debug.logInfo("Helper \"" + helperName + "\" already initialized, not re-initializing.", module);
continue;
}
helpersDone.add(helperName);
// pre-load field type defs, the return value is ignored
ModelFieldTypeReader.getModelFieldTypeReader(helperName);
// get the helper and if configured, do the datasource check
GenericHelper helper = GenericHelperFactory.getHelper(helperName);
EntityConfigUtil.DatasourceInfo datasourceInfo = EntityConfigUtil.getDatasourceInfo(helperName);
if (datasourceInfo.checkOnStart) {
if (Debug.infoOn()) Debug.logInfo("Doing database check as requested in entityengine.xml with addMissing=" + datasourceInfo.addMissingOnStart, module);
try {
helper.checkDataSource(this.getModelEntityMapByGroup(groupName), null, datasourceInfo.addMissingOnStart);
} catch (GenericEntityException e) {
Debug.logWarning(e.getMessage(), module);
}
}
}
}
//time to do some tricks with manual class loading that resolves circular dependencies, like calling services...
ClassLoader loader = Thread.currentThread().getContextClassLoader();
// if useDistributedCacheClear is false do nothing since the
// distributedCacheClear member field with a null value will cause the
// dcc code to do nothing
if (getDelegatorInfo().useDistributedCacheClear) {
// initialize the distributedCacheClear mechanism
String distributedCacheClearClassName = getDelegatorInfo().distributedCacheClearClassName;
try {
Class dccClass = loader.loadClass(distributedCacheClearClassName);
this.distributedCacheClear = (DistributedCacheClear) dccClass.newInstance();
this.distributedCacheClear.setDelegator(this, getDelegatorInfo().distributedCacheClearUserLoginId);
} catch (ClassNotFoundException e) {
Debug.logWarning(e, "DistributedCacheClear class with name " + distributedCacheClearClassName + " was not found, distributed cache clearing will be disabled", module);
} catch (InstantiationException e) {
Debug.logWarning(e, "DistributedCacheClear class with name " + distributedCacheClearClassName + " could not be instantiated, distributed cache clearing will be disabled", module);
} catch (IllegalAccessException e) {
Debug.logWarning(e, "DistributedCacheClear class with name " + distributedCacheClearClassName + " could not be accessed (illegal), distributed cache clearing will be disabled", module);
} catch (ClassCastException e) {
Debug.logWarning(e, "DistributedCacheClear class with name " + distributedCacheClearClassName + " does not implement the DistributedCacheClear interface, distributed cache clearing will be disabled", module);
}
}
// setup the Entity ECA Handler
try {
Class eecahClass = loader.loadClass(ECA_HANDLER_CLASS_NAME);
this.entityEcaHandler = (EntityEcaHandler) eecahClass.newInstance();
this.entityEcaHandler.setDelegator(this);
} catch (ClassNotFoundException e) {
Debug.logWarning(e, "EntityEcaHandler class with name " + ECA_HANDLER_CLASS_NAME + " was not found, Entity ECA Rules will be disabled", module);
} catch (InstantiationException e) {
Debug.logWarning(e, "EntityEcaHandler class with name " + ECA_HANDLER_CLASS_NAME + " could not be instantiated, Entity ECA Rules will be disabled", module);
} catch (IllegalAccessException e) {
Debug.logWarning(e, "EntityEcaHandler class with name " + ECA_HANDLER_CLASS_NAME + " could not be accessed (illegal), Entity ECA Rules will be disabled", module);
} catch (ClassCastException e) {
Debug.logWarning(e, "EntityEcaHandler class with name " + ECA_HANDLER_CLASS_NAME + " does not implement the EntityEcaHandler interface, Entity ECA Rules will be disabled", module);
}
}
/** Gets the name of the server configuration that corresponds to this delegator
* @return server configuration name
*/
public String getDelegatorName() {
return this.delegatorName;
}
protected EntityConfigUtil.DelegatorInfo getDelegatorInfo() {
if (delegatorInfo == null) {
delegatorInfo = EntityConfigUtil.getDelegatorInfo(this.delegatorName);
}
return delegatorInfo;
}
/** Gets the instance of ModelReader that corresponds to this delegator
*@return ModelReader that corresponds to this delegator
*/
public ModelReader getModelReader() {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -