Author: adrianc
Date: Thu Nov 19 18:16:35 2009 New Revision: 882236 URL: http://svn.apache.org/viewvc?rev=882236&view=rev Log: Major code cleanup in ModelFieldTypeReader.java. Eliminated unused fields, made public fields protected, simplified code. Modified: ofbiz/trunk/framework/entity/src/org/ofbiz/entity/model/ModelFieldTypeReader.java Modified: ofbiz/trunk/framework/entity/src/org/ofbiz/entity/model/ModelFieldTypeReader.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/entity/src/org/ofbiz/entity/model/ModelFieldTypeReader.java?rev=882236&r1=882235&r2=882236&view=diff ============================================================================== --- ofbiz/trunk/framework/entity/src/org/ofbiz/entity/model/ModelFieldTypeReader.java (original) +++ ofbiz/trunk/framework/entity/src/org/ofbiz/entity/model/ModelFieldTypeReader.java Thu Nov 19 18:16:35 2009 @@ -20,162 +20,103 @@ import java.io.Serializable; import java.util.Collection; -import java.util.HashMap; +import java.util.List; import java.util.Map; +import javolution.util.FastMap; + import org.ofbiz.base.config.GenericConfigException; import org.ofbiz.base.config.MainResourceHandler; import org.ofbiz.base.config.ResourceHandler; import org.ofbiz.base.util.Debug; import org.ofbiz.base.util.UtilTimer; +import org.ofbiz.base.util.UtilValidate; import org.ofbiz.base.util.UtilXml; import org.ofbiz.base.util.cache.UtilCache; import org.ofbiz.entity.config.DatasourceInfo; import org.ofbiz.entity.config.EntityConfigUtil; import org.ofbiz.entity.config.FieldTypeInfo; + import org.w3c.dom.Document; import org.w3c.dom.Element; -import org.w3c.dom.Node; /** - * Generic Entity - Field Type Definition Reader + * Field Type Definition Reader. * */ +@SuppressWarnings("serial") public class ModelFieldTypeReader implements Serializable { public static final String module = ModelFieldTypeReader.class.getName(); - public static UtilCache<String, ModelFieldTypeReader> readers = new UtilCache<String, ModelFieldTypeReader>("entity.ModelFieldTypeReader", 0, 0); - - public Map<String, ModelFieldType> fieldTypeCache = null; + protected static final UtilCache<String, ModelFieldTypeReader> readers = new UtilCache<String, ModelFieldTypeReader>("entity.ModelFieldTypeReader", 0, 0); - public int numEntities = 0; - public int numFields = 0; - public int numRelations = 0; - - public String modelName; - public ResourceHandler fieldTypeResourceHandler; - public String entityFileName; + protected static Map<String, ModelFieldType> createFieldTypeCache(Element docElement, String location) { + docElement.normalize(); + Map<String, ModelFieldType> fieldTypeMap = FastMap.newInstance(); + List<? extends Element> fieldTypeList = UtilXml.childElementList(docElement, "field-type-def"); + for (Element curFieldType: fieldTypeList) { + String fieldTypeName = curFieldType.getAttribute("type"); + if (UtilValidate.isEmpty(fieldTypeName)) { + Debug.logError("Invalid field-type element, type attribute is missing in file " + location, module); + } else { + ModelFieldType fieldType = new ModelFieldType(curFieldType); + fieldTypeMap.put(fieldTypeName, fieldType); + } + } + return fieldTypeMap; + } public static ModelFieldTypeReader getModelFieldTypeReader(String helperName) { DatasourceInfo datasourceInfo = EntityConfigUtil.getDatasourceInfo(helperName); if (datasourceInfo == null) { throw new IllegalArgumentException("Could not find a datasource/helper with the name " + helperName); } - String tempModelName = datasourceInfo.fieldTypeName; ModelFieldTypeReader reader = readers.get(tempModelName); - - if (reader == null) // don't want to block here + if (reader == null) { - synchronized (ModelFieldTypeReader.class) { - // must check if null again as one of the blocked threads can still enter - reader = readers.get(tempModelName); - if (reader == null) { - reader = new ModelFieldTypeReader(tempModelName); - readers.put(tempModelName, reader); + synchronized (readers) { + FieldTypeInfo fieldTypeInfo = EntityConfigUtil.getFieldTypeInfo(tempModelName); + if (fieldTypeInfo == null) { + throw new IllegalArgumentException("Could not find a field-type definition with name \"" + tempModelName + "\""); } + ResourceHandler fieldTypeResourceHandler = new MainResourceHandler(EntityConfigUtil.ENTITY_ENGINE_XML_FILENAME, fieldTypeInfo.resourceElement); + UtilTimer utilTimer = new UtilTimer(); + utilTimer.timerString("[ModelFieldTypeReader.getModelFieldTypeReader] Reading field types from " + fieldTypeResourceHandler.getLocation()); + Document document = null; + try { + document = fieldTypeResourceHandler.getDocument(); + } catch (GenericConfigException e) { + Debug.logError(e, module); + throw new IllegalStateException("Error loading field type file " + fieldTypeResourceHandler.getLocation()); + } + Map<String, ModelFieldType> fieldTypeMap = createFieldTypeCache(document.getDocumentElement(), fieldTypeResourceHandler.getLocation()); + reader = new ModelFieldTypeReader(fieldTypeMap); + readers.put(tempModelName, reader); + utilTimer.timerString("[ModelFieldTypeReader.getModelFieldTypeReader] Read " + fieldTypeMap.size() + " field types"); } } return reader; } - public ModelFieldTypeReader(String modelName) { - this.modelName = modelName; - FieldTypeInfo fieldTypeInfo = EntityConfigUtil.getFieldTypeInfo(modelName); - - if (fieldTypeInfo == null) { - throw new IllegalStateException("Could not find a field-type definition with name \"" + modelName + "\""); - } - fieldTypeResourceHandler = new MainResourceHandler(EntityConfigUtil.ENTITY_ENGINE_XML_FILENAME, fieldTypeInfo.resourceElement); - - // preload caches... - getFieldTypeCache(); - } + protected final Map<String, ModelFieldType> fieldTypeCache; - public Map<String, ModelFieldType> getFieldTypeCache() { - if (fieldTypeCache == null) // don't want to block here - { - synchronized (ModelFieldTypeReader.class) { - // must check if null again as one of the blocked threads can still enter - if (fieldTypeCache == null) // now it's safe - { - fieldTypeCache = new HashMap<String, ModelFieldType>(); - - UtilTimer utilTimer = new UtilTimer(); - // utilTimer.timerString("Before getDocument"); - - Document document = null; - - try { - document = fieldTypeResourceHandler.getDocument(); - } catch (GenericConfigException e) { - Debug.logError(e, "Error loading field type file", module); - } - if (document == null) { - fieldTypeCache = null; - return null; - } - - // utilTimer.timerString("Before getDocumentElement"); - Element docElement = document.getDocumentElement(); - - if (docElement == null) { - fieldTypeCache = null; - return null; - } - docElement.normalize(); - - Node curChild = docElement.getFirstChild(); - - int i = 0; - - if (curChild != null) { - utilTimer.timerString("Before start of field type loop"); - do { - if (curChild.getNodeType() == Node.ELEMENT_NODE && "field-type-def".equals(curChild.getNodeName())) { - i++; - // utilTimer.timerString("Start loop -- " + i + " --"); - Element curFieldType = (Element) curChild; - String fieldTypeName = UtilXml.checkEmpty(curFieldType.getAttribute("type"), "[No type name]").intern(); - // utilTimer.timerString(" After fieldTypeName -- " + i + " --"); - ModelFieldType fieldType = createModelFieldType(curFieldType, docElement, null); - - // utilTimer.timerString(" After createModelFieldType -- " + i + " --"); - if (fieldType != null) { - fieldTypeCache.put(fieldTypeName, fieldType); - // utilTimer.timerString(" After fieldTypeCache.put -- " + i + " --"); - if (Debug.verboseOn()) Debug.logVerbose("-- getModelFieldType: #" + i + " Created fieldType: " + fieldTypeName, module); - } else { - Debug.logWarning("-- -- ENTITYGEN ERROR:getModelFieldType: Could not create fieldType for fieldTypeName: " + fieldTypeName, module); - } - - } - } while ((curChild = curChild.getNextSibling()) != null); - } else - Debug.logWarning("No child nodes found.", module); - utilTimer.timerString("FINISHED - Total Field Types: " + i + " FINISHED"); - } - } - } - return fieldTypeCache; + public ModelFieldTypeReader(Map<String, ModelFieldType> fieldTypeMap) { + this.fieldTypeCache = fieldTypeMap; } /** Creates a Collection with all of the ModelFieldType names * @return A Collection of ModelFieldType names */ public Collection<String> getFieldTypeNames() { - Map<String, ModelFieldType> ftc = getFieldTypeCache(); - - return ftc.keySet(); + return this.fieldTypeCache.keySet(); } /** Creates a Collection with all of the ModelFieldTypes * @return A Collection of ModelFieldTypes */ public Collection<ModelFieldType> getFieldTypes() { - Map<String, ModelFieldType> ftc = getFieldTypeCache(); - - return ftc.values(); + return this.fieldTypeCache.values(); } /** Gets an FieldType object based on a definition from the specified XML FieldType descriptor file. @@ -183,19 +124,7 @@ * @return An FieldType object describing the specified fieldType of the specified descriptor file. */ public ModelFieldType getModelFieldType(String fieldTypeName) { - Map<String, ModelFieldType> ftc = getFieldTypeCache(); - - if (ftc != null) - return ftc.get(fieldTypeName); - else - return null; + return this.fieldTypeCache.get(fieldTypeName); } - ModelFieldType createModelFieldType(Element fieldTypeElement, Element docElement, UtilTimer utilTimer) { - if (fieldTypeElement == null) return null; - - ModelFieldType field = new ModelFieldType(fieldTypeElement); - - return field; - } } |
Free forum by Nabble | Edit this page |