001: /*
002: * $Id: ModelFieldTypeReader.java,v 1.1 2003/08/16 22:05:48 ajzeneski Exp $
003: *
004: * Copyright (c) 2001, 2002 The Open For Business Project - www.ofbiz.org
005: *
006: * Permission is hereby granted, free of charge, to any person obtaining a
007: * copy of this software and associated documentation files (the "Software"),
008: * to deal in the Software without restriction, including without limitation
009: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
010: * and/or sell copies of the Software, and to permit persons to whom the
011: * Software is furnished to do so, subject to the following conditions:
012: *
013: * The above copyright notice and this permission notice shall be included
014: * in all copies or substantial portions of the Software.
015: *
016: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
017: * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
018: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
019: * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
020: * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
021: * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
022: * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
023: */
024: package org.ofbiz.entity.model;
025:
026: import java.util.Collection;
027: import java.util.HashMap;
028: import java.util.Map;
029:
030: import org.ofbiz.base.config.GenericConfigException;
031: import org.ofbiz.base.config.MainResourceHandler;
032: import org.ofbiz.base.config.ResourceHandler;
033: import org.ofbiz.entity.config.EntityConfigUtil;
034: import org.ofbiz.base.util.Debug;
035: import org.ofbiz.base.util.UtilCache;
036: import org.ofbiz.base.util.UtilTimer;
037: import org.ofbiz.base.util.UtilXml;
038: import org.w3c.dom.Document;
039: import org.w3c.dom.Element;
040: import org.w3c.dom.Node;
041:
042: /**
043: * Generic Entity - Field Type Definition Reader
044: *
045: * @author <a href="mailto:jonesde@ofbiz.org">David E. Jones</a>
046: * @version $Revision: 1.1 $
047: * @since 2.0
048: */
049: public class ModelFieldTypeReader {
050:
051: public static final String module = ModelFieldTypeReader.class
052: .getName();
053: public static UtilCache readers = new UtilCache(
054: "entity.ModelFieldTypeReader", 0, 0);
055:
056: public Map fieldTypeCache = null;
057:
058: public int numEntities = 0;
059: public int numFields = 0;
060: public int numRelations = 0;
061:
062: public String modelName;
063: public ResourceHandler fieldTypeResourceHandler;
064: public String entityFileName;
065:
066: public static ModelFieldTypeReader getModelFieldTypeReader(
067: String helperName) {
068: EntityConfigUtil.DatasourceInfo datasourceInfo = EntityConfigUtil
069: .getDatasourceInfo(helperName);
070: if (datasourceInfo == null) {
071: throw new IllegalArgumentException(
072: "Could not find a datasource/helper with the name "
073: + helperName);
074: }
075:
076: String tempModelName = datasourceInfo.fieldTypeName;
077: ModelFieldTypeReader reader = (ModelFieldTypeReader) readers
078: .get(tempModelName);
079:
080: if (reader == null) // don't want to block here
081: {
082: synchronized (ModelFieldTypeReader.class) {
083: // must check if null again as one of the blocked threads can still enter
084: reader = (ModelFieldTypeReader) readers
085: .get(tempModelName);
086: if (reader == null) {
087: reader = new ModelFieldTypeReader(tempModelName);
088: readers.put(tempModelName, reader);
089: }
090: }
091: }
092: return reader;
093: }
094:
095: public ModelFieldTypeReader(String modelName) {
096: this .modelName = modelName;
097: EntityConfigUtil.FieldTypeInfo fieldTypeInfo = EntityConfigUtil
098: .getFieldTypeInfo(modelName);
099:
100: if (fieldTypeInfo == null) {
101: throw new IllegalStateException(
102: "Could not find a field-type definition with name \""
103: + modelName + "\"");
104: }
105: fieldTypeResourceHandler = new MainResourceHandler(
106: EntityConfigUtil.ENTITY_ENGINE_XML_FILENAME,
107: fieldTypeInfo.resourceElement);
108:
109: // preload caches...
110: getFieldTypeCache();
111: }
112:
113: public Map getFieldTypeCache() {
114: if (fieldTypeCache == null) // don't want to block here
115: {
116: synchronized (ModelFieldTypeReader.class) {
117: // must check if null again as one of the blocked threads can still enter
118: if (fieldTypeCache == null) // now it's safe
119: {
120: fieldTypeCache = new HashMap();
121:
122: UtilTimer utilTimer = new UtilTimer();
123: // utilTimer.timerString("Before getDocument");
124:
125: Document document = null;
126:
127: try {
128: document = fieldTypeResourceHandler
129: .getDocument();
130: } catch (GenericConfigException e) {
131: Debug
132: .logError(
133: e,
134: "Error loading field type file",
135: module);
136: }
137: if (document == null) {
138: fieldTypeCache = null;
139: return null;
140: }
141:
142: // utilTimer.timerString("Before getDocumentElement");
143: Element docElement = document.getDocumentElement();
144:
145: if (docElement == null) {
146: fieldTypeCache = null;
147: return null;
148: }
149: docElement.normalize();
150:
151: Node curChild = docElement.getFirstChild();
152:
153: int i = 0;
154:
155: if (curChild != null) {
156: utilTimer
157: .timerString("Before start of field type loop");
158: do {
159: if (curChild.getNodeType() == Node.ELEMENT_NODE
160: && "field-type-def".equals(curChild
161: .getNodeName())) {
162: i++;
163: // utilTimer.timerString("Start loop -- " + i + " --");
164: Element curFieldType = (Element) curChild;
165: String fieldTypeName = UtilXml
166: .checkEmpty(curFieldType
167: .getAttribute("type"),
168: "[No type name]");
169: // utilTimer.timerString(" After fieldTypeName -- " + i + " --");
170: ModelFieldType fieldType = createModelFieldType(
171: curFieldType, docElement, null);
172:
173: // utilTimer.timerString(" After createModelFieldType -- " + i + " --");
174: if (fieldType != null) {
175: fieldTypeCache.put(fieldTypeName,
176: fieldType);
177: // utilTimer.timerString(" After fieldTypeCache.put -- " + i + " --");
178: if (Debug.verboseOn())
179: Debug
180: .logVerbose(
181: "-- getModelFieldType: #"
182: + i
183: + " Created fieldType: "
184: + fieldTypeName,
185: module);
186: } else {
187: Debug
188: .logWarning(
189: "-- -- ENTITYGEN ERROR:getModelFieldType: Could not create fieldType for fieldTypeName: "
190: + fieldTypeName,
191: module);
192: }
193:
194: }
195: } while ((curChild = curChild.getNextSibling()) != null);
196: } else
197: Debug.logWarning("No child nodes found.",
198: module);
199: utilTimer
200: .timerString("FINISHED - Total Field Types: "
201: + i + " FINISHED");
202: }
203: }
204: }
205: return fieldTypeCache;
206: }
207:
208: /** Creates a Collection with all of the ModelFieldType names
209: * @return A Collection of ModelFieldType names
210: */
211: public Collection getFieldTypeNames() {
212: Map ftc = getFieldTypeCache();
213:
214: return ftc.keySet();
215: }
216:
217: /** Creates a Collection with all of the ModelFieldTypes
218: * @return A Collection of ModelFieldTypes
219: */
220: public Collection getFieldTypes() {
221: Map ftc = getFieldTypeCache();
222:
223: return ftc.values();
224: }
225:
226: /** Gets an FieldType object based on a definition from the specified XML FieldType descriptor file.
227: * @param fieldTypeName The fieldTypeName of the FieldType definition to use.
228: * @return An FieldType object describing the specified fieldType of the specified descriptor file.
229: */
230: public ModelFieldType getModelFieldType(String fieldTypeName) {
231: Map ftc = getFieldTypeCache();
232:
233: if (ftc != null)
234: return (ModelFieldType) ftc.get(fieldTypeName);
235: else
236: return null;
237: }
238:
239: ModelFieldType createModelFieldType(Element fieldTypeElement,
240: Element docElement, UtilTimer utilTimer) {
241: if (fieldTypeElement == null)
242: return null;
243:
244: ModelFieldType field = new ModelFieldType(fieldTypeElement);
245:
246: return field;
247: }
248: }
|