📄 genericdelegator.java
字号:
/* * $Id: GenericDelegator.java 7003 2006-03-16 15:40:40Z jaz $ * * Copyright (c) 2001-2005 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.Iterator;import java.util.List;import java.util.Map;import java.util.Set;import java.util.TreeSet;import javax.xml.parsers.ParserConfigurationException;import javolution.util.FastList;import javolution.util.FastMap;import org.w3c.dom.Document;import org.w3c.dom.Element;import org.w3c.dom.Node;import org.xml.sax.SAXException;import org.ofbiz.base.util.Debug;import org.ofbiz.base.util.UtilFormatOut;import org.ofbiz.base.util.UtilMisc;import org.ofbiz.base.util.UtilValidate;import org.ofbiz.base.util.UtilXml;import org.ofbiz.base.util.cache.CacheLine;import org.ofbiz.base.util.cache.UtilCache;import org.ofbiz.entity.cache.Cache;import org.ofbiz.entity.condition.EntityCondition;import org.ofbiz.entity.condition.EntityConditionList;import org.ofbiz.entity.condition.EntityExpr;import org.ofbiz.entity.condition.EntityFieldMap;import org.ofbiz.entity.condition.EntityOperator;import org.ofbiz.entity.config.DatasourceInfo;import org.ofbiz.entity.config.DelegatorInfo;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.*;import org.ofbiz.entity.serialize.SerializeException;import org.ofbiz.entity.serialize.XmlSerializer;import org.ofbiz.entity.transaction.GenericTransactionException;import org.ofbiz.entity.transaction.TransactionUtil;import org.ofbiz.entity.util.DistributedCacheClear;import org.ofbiz.entity.util.EntityCrypto;import org.ofbiz.entity.util.EntityFindOptions;import org.ofbiz.entity.util.EntityListIterator;import org.ofbiz.entity.util.SequenceUtil;/** * 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 $Rev: 7003 $ * @since 1.0 */public class GenericDelegator implements DelegatorInterface { public static final String module = GenericDelegator.class.getName(); /** 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; /** This flag is only here for lower level technical testing, it shouldn't be user configurable (or at least I don't think so yet); when true all operations without a transaction will be wrapped in one; seems to be necessary for some (all?) XA aware connection pools, and should improve overall stability and consistency */ public static final boolean alwaysUseTransaction = true; /** 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 = FastMap.newInstance(); protected String delegatorName = null; protected DelegatorInfo delegatorInfo = null; protected Cache cache = 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 = FastMap.newInstance(); protected DistributedCacheClear distributedCacheClear = null; protected EntityEcaHandler entityEcaHandler = null; protected SequenceUtil sequencer = null; protected EntityCrypto crypto = null; public static GenericDelegator getGenericDelegator(String delegatorName) { if (delegatorName == null) { delegatorName = "default"; Debug.logWarning("Got a getGenericDelegator call with a null delegatorName, assuming default for the name.", module); } 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) { if (Debug.infoOn()) Debug.logInfo("Creating new delegator [" + delegatorName + "] (" + Thread.currentThread().getName() + ")", module); //Debug.logInfo(new Exception(), "Showing stack where new delegator is being created...", module); try { delegator = new GenericDelegator(delegatorName); } catch (GenericEntityException e) { Debug.logError(e, "Error creating delegator", module); } if (delegator != null) { delegatorCache.put(delegatorName, delegator); } else { Debug.logError("Could not create delegator with name " + delegatorName + ", constructor failed (got null value) not sure why/how.", module); } } } } 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); } cache = new Cache(delegatorName); // do the entity model check List warningList = FastList.newInstance(); Debug.logImportant("Doing entity definition check...", module); ModelEntityChecker.checkEntities(this, warningList); if (warningList.size() > 0) { Debug.logWarning("=-=-=-=-= Found " + warningList.size() + " warnings when checking the entity definitions:", module); Iterator warningIter = warningList.iterator(); while (warningIter.hasNext()) { String warning = (String) warningIter.next(); Debug.logWarning(warning, module); } } // 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); 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, e.getMessage(), module); } } } } // NOTE: doing some things before the ECAs and such to make sure it is in place just in case it is used in a service engine startup thing or something // put the delegator in the master Map by its name delegatorCache.put(delegatorName, this); // setup the crypto class this.crypto = new EntityCrypto(this); //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); } } else { Debug.logInfo("Distributed Cache Clear System disabled for delegator [" + delegatorName + "]", module); } // setup the Entity ECA Handler if (getDelegatorInfo().useEntityEca) { // initialize the entity eca handler String entityEcaHandlerClassName = getDelegatorInfo().entityEcaHandlerClassName; try { Class eecahClass = loader.loadClass(entityEcaHandlerClassName); this.entityEcaHandler = (EntityEcaHandler) eecahClass.newInstance(); this.entityEcaHandler.setDelegator(this); } catch (ClassNotFoundException e) { Debug.logWarning(e, "EntityEcaHandler class with name " + entityEcaHandlerClassName + " was not found, Entity ECA Rules will be disabled", module); } catch (InstantiationException e) { Debug.logWarning(e, "EntityEcaHandler class with name " + entityEcaHandlerClassName + " could not be instantiated, Entity ECA Rules will be disabled", module); } catch (IllegalAccessException e) { Debug.logWarning(e, "EntityEcaHandler class with name " + entityEcaHandlerClassName + " could not be accessed (illegal), Entity ECA Rules will be disabled", module); } catch (ClassCastException e) { Debug.logWarning(e, "EntityEcaHandler class with name " + entityEcaHandlerClassName + " does not implement the EntityEcaHandler interface, Entity ECA Rules will be disabled", module); } } else { Debug.logInfo("Entity ECA Handler disabled for delegator [" + delegatorName + "]", module); } } /** Gets the name of the server configuration that corresponds to this delegator * @return server configuration name */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -