📄 modelreader.java
字号:
/*
* $Id: ModelReader.java,v 1.5 2003/12/04 20:54:53 jonesde 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.model;
import java.util.Collection;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import org.ofbiz.base.component.ComponentConfig;
import org.ofbiz.base.config.GenericConfigException;
import org.ofbiz.base.config.MainResourceHandler;
import org.ofbiz.base.config.ResourceHandler;
import org.ofbiz.entity.GenericEntityConfException;
import org.ofbiz.entity.GenericEntityException;
import org.ofbiz.entity.GenericModelException;
import org.ofbiz.entity.config.EntityConfigUtil;
import org.ofbiz.base.util.Debug;
import org.ofbiz.base.util.UtilCache;
import org.ofbiz.base.util.UtilTimer;
import org.ofbiz.base.util.UtilXml;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
/**
* Generic Entity - Entity Definition Reader
*
* @author <a href="mailto:jonesde@ofbiz.org">David E. Jones</a>
* @author <a href="mailto:jaz@ofbiz.org">Andy Zeneski</a>
* @version $Revision: 1.5 $
* @since 2.0
*/
public class ModelReader {
public static final String module = ModelReader.class.getName();
public static UtilCache readers = new UtilCache("entity.ModelReader", 0, 0);
protected Map entityCache = null;
protected int numEntities = 0;
protected int numViewEntities = 0;
protected int numFields = 0;
protected int numRelations = 0;
protected String modelName;
/** collection of filenames for entity definitions */
protected Collection entityResourceHandlers;
/** contains a collection of entity names for each ResourceHandler, populated as they are loaded */
protected Map resourceHandlerEntities;
/** for each entity contains a map to the ResourceHandler that the entity came from */
protected Map entityResourceHandlerMap;
public static ModelReader getModelReader(String delegatorName) throws GenericEntityException {
EntityConfigUtil.DelegatorInfo delegatorInfo = EntityConfigUtil.getDelegatorInfo(delegatorName);
if (delegatorInfo == null) {
throw new GenericEntityConfException("Could not find a delegator with the name " + delegatorName);
}
String tempModelName = delegatorInfo.entityModelReader;
ModelReader reader = (ModelReader) readers.get(tempModelName);
if (reader == null) { // don't want to block here
synchronized (ModelReader.class) {
// must check if null again as one of the blocked threads can still enter
reader = (ModelReader) readers.get(tempModelName);
if (reader == null) {
reader = new ModelReader(tempModelName);
// preload caches...
reader.getEntityCache();
readers.put(tempModelName, reader);
}
}
}
return reader;
}
public ModelReader(String modelName) throws GenericEntityException {
this.modelName = modelName;
entityResourceHandlers = new LinkedList();
resourceHandlerEntities = new HashMap();
entityResourceHandlerMap = new HashMap();
EntityConfigUtil.EntityModelReaderInfo entityModelReaderInfo = EntityConfigUtil.getEntityModelReaderInfo(modelName);
if (entityModelReaderInfo == null) {
throw new GenericEntityConfException("Cound not find an entity-model-reader with the name " + modelName);
}
// get all of the main resource model stuff, ie specified in the entityengine.xml file
List resourceElements = entityModelReaderInfo.resourceElements;
Iterator resIter = resourceElements.iterator();
while (resIter.hasNext()) {
Element resourceElement = (Element) resIter.next();
ResourceHandler handler = new MainResourceHandler(EntityConfigUtil.ENTITY_ENGINE_XML_FILENAME, resourceElement);
entityResourceHandlers.add(handler);
}
// get all of the component resource model stuff, ie specified in each ofbiz-component.xml file
List componentResourceInfos = ComponentConfig.getAllEntityResourceInfos("model");
Iterator componentResourceInfoIter = componentResourceInfos.iterator();
while (componentResourceInfoIter.hasNext()) {
ComponentConfig.EntityResourceInfo componentResourceInfo = (ComponentConfig.EntityResourceInfo) componentResourceInfoIter.next();
if (modelName.equals(componentResourceInfo.readerName)) {
entityResourceHandlers.add(componentResourceInfo.createResourceHandler());
}
}
}
public Map getEntityCache() throws GenericEntityException {
if (entityCache == null) { // don't want to block here
synchronized (ModelReader.class) {
// must check if null again as one of the blocked threads can still enter
if (entityCache == null) { // now it's safe
numEntities = 0;
numViewEntities = 0;
numFields = 0;
numRelations = 0;
entityCache = new HashMap();
List tempViewEntityList = new LinkedList();
UtilTimer utilTimer = new UtilTimer();
Iterator rhIter = entityResourceHandlers.iterator();
while (rhIter.hasNext()) {
ResourceHandler entityResourceHandler = (ResourceHandler) rhIter.next();
// utilTimer.timerString("Before getDocument in file " + entityFileName);
Document document = null;
try {
document = entityResourceHandler.getDocument();
} catch (GenericConfigException e) {
throw new GenericEntityConfException("Error getting document from resource handler", e);
}
if (document == null) {
throw new GenericEntityConfException("Could not get document for " + entityResourceHandler.toString());
}
Hashtable docElementValues = null;
docElementValues = new Hashtable();
// utilTimer.timerString("Before getDocumentElement in " + entityResourceHandler.toString());
Element docElement = document.getDocumentElement();
if (docElement == null) {
entityCache = null;
return null;
}
docElement.normalize();
Node curChild = docElement.getFirstChild();
int i = 0;
if (curChild != null) {
utilTimer.timerString("Before start of entity loop in " + entityResourceHandler.toString());
do {
boolean isEntity = "entity".equals(curChild.getNodeName());
boolean isViewEntity = "view-entity".equals(curChild.getNodeName());
if ((isEntity || isViewEntity) && curChild.getNodeType() == Node.ELEMENT_NODE) {
i++;
Element curEntity = (Element) curChild;
String entityName = UtilXml.checkEmpty(curEntity.getAttribute("entity-name"));
// add entityName to appropriate resourceHandlerEntities collection
Collection resourceHandlerEntityNames = (Collection) resourceHandlerEntities.get(entityResourceHandler);
if (resourceHandlerEntityNames == null) {
resourceHandlerEntityNames = new LinkedList();
resourceHandlerEntities.put(entityResourceHandler, resourceHandlerEntityNames);
}
resourceHandlerEntityNames.add(entityName);
// check to see if entity with same name has already been read
if (entityCache.containsKey(entityName)) {
Debug.logWarning("WARNING: Entity " + entityName +
" is defined more than once, most recent will over-write " +
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -