001: /*******************************************************************************
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: *******************************************************************************/package org.ofbiz.entity.model;
019:
020: import java.io.Serializable;
021: import java.util.Collection;
022: import java.util.HashMap;
023: import java.util.Map;
024:
025: import org.ofbiz.base.config.GenericConfigException;
026: import org.ofbiz.base.config.MainResourceHandler;
027: import org.ofbiz.base.config.ResourceHandler;
028: import org.ofbiz.base.util.Debug;
029: import org.ofbiz.base.util.UtilTimer;
030: import org.ofbiz.base.util.UtilXml;
031: import org.ofbiz.base.util.cache.UtilCache;
032: import org.ofbiz.entity.config.DatasourceInfo;
033: import org.ofbiz.entity.config.EntityConfigUtil;
034: import org.ofbiz.entity.config.FieldTypeInfo;
035: import org.w3c.dom.Document;
036: import org.w3c.dom.Element;
037: import org.w3c.dom.Node;
038:
039: /**
040: * Generic Entity - Field Type Definition Reader
041: *
042: */
043: public class ModelFieldTypeReader implements Serializable {
044:
045: public static final String module = ModelFieldTypeReader.class
046: .getName();
047: public static UtilCache readers = new UtilCache(
048: "entity.ModelFieldTypeReader", 0, 0);
049:
050: public Map fieldTypeCache = null;
051:
052: public int numEntities = 0;
053: public int numFields = 0;
054: public int numRelations = 0;
055:
056: public String modelName;
057: public ResourceHandler fieldTypeResourceHandler;
058: public String entityFileName;
059:
060: public static ModelFieldTypeReader getModelFieldTypeReader(
061: String helperName) {
062: DatasourceInfo datasourceInfo = EntityConfigUtil
063: .getDatasourceInfo(helperName);
064: if (datasourceInfo == null) {
065: throw new IllegalArgumentException(
066: "Could not find a datasource/helper with the name "
067: + helperName);
068: }
069:
070: String tempModelName = datasourceInfo.fieldTypeName;
071: ModelFieldTypeReader reader = (ModelFieldTypeReader) readers
072: .get(tempModelName);
073:
074: if (reader == null) // don't want to block here
075: {
076: synchronized (ModelFieldTypeReader.class) {
077: // must check if null again as one of the blocked threads can still enter
078: reader = (ModelFieldTypeReader) readers
079: .get(tempModelName);
080: if (reader == null) {
081: reader = new ModelFieldTypeReader(tempModelName);
082: readers.put(tempModelName, reader);
083: }
084: }
085: }
086: return reader;
087: }
088:
089: public ModelFieldTypeReader(String modelName) {
090: this .modelName = modelName;
091: FieldTypeInfo fieldTypeInfo = EntityConfigUtil
092: .getFieldTypeInfo(modelName);
093:
094: if (fieldTypeInfo == null) {
095: throw new IllegalStateException(
096: "Could not find a field-type definition with name \""
097: + modelName + "\"");
098: }
099: fieldTypeResourceHandler = new MainResourceHandler(
100: EntityConfigUtil.ENTITY_ENGINE_XML_FILENAME,
101: fieldTypeInfo.resourceElement);
102:
103: // preload caches...
104: getFieldTypeCache();
105: }
106:
107: public Map getFieldTypeCache() {
108: if (fieldTypeCache == null) // don't want to block here
109: {
110: synchronized (ModelFieldTypeReader.class) {
111: // must check if null again as one of the blocked threads can still enter
112: if (fieldTypeCache == null) // now it's safe
113: {
114: fieldTypeCache = new HashMap();
115:
116: UtilTimer utilTimer = new UtilTimer();
117: // utilTimer.timerString("Before getDocument");
118:
119: Document document = null;
120:
121: try {
122: document = fieldTypeResourceHandler
123: .getDocument();
124: } catch (GenericConfigException e) {
125: Debug
126: .logError(
127: e,
128: "Error loading field type file",
129: module);
130: }
131: if (document == null) {
132: fieldTypeCache = null;
133: return null;
134: }
135:
136: // utilTimer.timerString("Before getDocumentElement");
137: Element docElement = document.getDocumentElement();
138:
139: if (docElement == null) {
140: fieldTypeCache = null;
141: return null;
142: }
143: docElement.normalize();
144:
145: Node curChild = docElement.getFirstChild();
146:
147: int i = 0;
148:
149: if (curChild != null) {
150: utilTimer
151: .timerString("Before start of field type loop");
152: do {
153: if (curChild.getNodeType() == Node.ELEMENT_NODE
154: && "field-type-def".equals(curChild
155: .getNodeName())) {
156: i++;
157: // utilTimer.timerString("Start loop -- " + i + " --");
158: Element curFieldType = (Element) curChild;
159: String fieldTypeName = UtilXml
160: .checkEmpty(curFieldType
161: .getAttribute("type"),
162: "[No type name]");
163: // utilTimer.timerString(" After fieldTypeName -- " + i + " --");
164: ModelFieldType fieldType = createModelFieldType(
165: curFieldType, docElement, null);
166:
167: // utilTimer.timerString(" After createModelFieldType -- " + i + " --");
168: if (fieldType != null) {
169: fieldTypeCache.put(fieldTypeName,
170: fieldType);
171: // utilTimer.timerString(" After fieldTypeCache.put -- " + i + " --");
172: if (Debug.verboseOn())
173: Debug
174: .logVerbose(
175: "-- getModelFieldType: #"
176: + i
177: + " Created fieldType: "
178: + fieldTypeName,
179: module);
180: } else {
181: Debug
182: .logWarning(
183: "-- -- ENTITYGEN ERROR:getModelFieldType: Could not create fieldType for fieldTypeName: "
184: + fieldTypeName,
185: module);
186: }
187:
188: }
189: } while ((curChild = curChild.getNextSibling()) != null);
190: } else
191: Debug.logWarning("No child nodes found.",
192: module);
193: utilTimer
194: .timerString("FINISHED - Total Field Types: "
195: + i + " FINISHED");
196: }
197: }
198: }
199: return fieldTypeCache;
200: }
201:
202: /** Creates a Collection with all of the ModelFieldType names
203: * @return A Collection of ModelFieldType names
204: */
205: public Collection getFieldTypeNames() {
206: Map ftc = getFieldTypeCache();
207:
208: return ftc.keySet();
209: }
210:
211: /** Creates a Collection with all of the ModelFieldTypes
212: * @return A Collection of ModelFieldTypes
213: */
214: public Collection getFieldTypes() {
215: Map ftc = getFieldTypeCache();
216:
217: return ftc.values();
218: }
219:
220: /** Gets an FieldType object based on a definition from the specified XML FieldType descriptor file.
221: * @param fieldTypeName The fieldTypeName of the FieldType definition to use.
222: * @return An FieldType object describing the specified fieldType of the specified descriptor file.
223: */
224: public ModelFieldType getModelFieldType(String fieldTypeName) {
225: Map ftc = getFieldTypeCache();
226:
227: if (ftc != null)
228: return (ModelFieldType) ftc.get(fieldTypeName);
229: else
230: return null;
231: }
232:
233: ModelFieldType createModelFieldType(Element fieldTypeElement,
234: Element docElement, UtilTimer utilTimer) {
235: if (fieldTypeElement == null)
236: return null;
237:
238: ModelFieldType field = new ModelFieldType(fieldTypeElement);
239:
240: return field;
241: }
242: }
|