KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > entity > model > ModelFieldTypeReader


1 /*
2  * $Id: ModelFieldTypeReader.java 5720 2005-09-13 03:10:59Z jonesde $
3  *
4  * Copyright (c) 2001-2005 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */

24 package org.ofbiz.entity.model;
25
26 import java.io.Serializable JavaDoc;
27 import java.util.Collection JavaDoc;
28 import java.util.HashMap JavaDoc;
29 import java.util.Map JavaDoc;
30
31 import org.ofbiz.base.config.GenericConfigException;
32 import org.ofbiz.base.config.MainResourceHandler;
33 import org.ofbiz.base.config.ResourceHandler;
34 import org.ofbiz.base.util.Debug;
35 import org.ofbiz.base.util.UtilTimer;
36 import org.ofbiz.base.util.UtilXml;
37 import org.ofbiz.base.util.cache.UtilCache;
38 import org.ofbiz.entity.config.DatasourceInfo;
39 import org.ofbiz.entity.config.EntityConfigUtil;
40 import org.ofbiz.entity.config.FieldTypeInfo;
41 import org.w3c.dom.Document JavaDoc;
42 import org.w3c.dom.Element JavaDoc;
43 import org.w3c.dom.Node JavaDoc;
44
45 /**
46  * Generic Entity - Field Type Definition Reader
47  *
48  * @author <a HREF="mailto:jonesde@ofbiz.org">David E. Jones</a>
49  * @version $Rev: 5720 $
50  * @since 2.0
51  */

52 public class ModelFieldTypeReader implements Serializable JavaDoc {
53
54     public static final String JavaDoc module = ModelFieldTypeReader.class.getName();
55     public static UtilCache readers = new UtilCache("entity.ModelFieldTypeReader", 0, 0);
56
57     public Map JavaDoc fieldTypeCache = null;
58
59     public int numEntities = 0;
60     public int numFields = 0;
61     public int numRelations = 0;
62
63     public String JavaDoc modelName;
64     public ResourceHandler fieldTypeResourceHandler;
65     public String JavaDoc entityFileName;
66
67     public static ModelFieldTypeReader getModelFieldTypeReader(String JavaDoc helperName) {
68         DatasourceInfo datasourceInfo = EntityConfigUtil.getDatasourceInfo(helperName);
69         if (datasourceInfo == null) {
70             throw new IllegalArgumentException JavaDoc("Could not find a datasource/helper with the name " + helperName);
71         }
72
73         String JavaDoc tempModelName = datasourceInfo.fieldTypeName;
74         ModelFieldTypeReader reader = (ModelFieldTypeReader) readers.get(tempModelName);
75
76         if (reader == null) // don't want to block here
77
{
78             synchronized (ModelFieldTypeReader.class) {
79                 // must check if null again as one of the blocked threads can still enter
80
reader = (ModelFieldTypeReader) readers.get(tempModelName);
81                 if (reader == null) {
82                     reader = new ModelFieldTypeReader(tempModelName);
83                     readers.put(tempModelName, reader);
84                 }
85             }
86         }
87         return reader;
88     }
89
90     public ModelFieldTypeReader(String JavaDoc modelName) {
91         this.modelName = modelName;
92         FieldTypeInfo fieldTypeInfo = EntityConfigUtil.getFieldTypeInfo(modelName);
93
94         if (fieldTypeInfo == null) {
95             throw new IllegalStateException JavaDoc("Could not find a field-type definition with name \"" + modelName + "\"");
96         }
97         fieldTypeResourceHandler = new MainResourceHandler(EntityConfigUtil.ENTITY_ENGINE_XML_FILENAME, fieldTypeInfo.resourceElement);
98
99         // preload caches...
100
getFieldTypeCache();
101     }
102
103     public Map JavaDoc getFieldTypeCache() {
104         if (fieldTypeCache == null) // don't want to block here
105
{
106             synchronized (ModelFieldTypeReader.class) {
107                 // must check if null again as one of the blocked threads can still enter
108
if (fieldTypeCache == null) // now it's safe
109
{
110                     fieldTypeCache = new HashMap JavaDoc();
111
112                     UtilTimer utilTimer = new UtilTimer();
113                     // utilTimer.timerString("Before getDocument");
114

115                     Document JavaDoc document = null;
116
117                     try {
118                         document = fieldTypeResourceHandler.getDocument();
119                     } catch (GenericConfigException e) {
120                         Debug.logError(e, "Error loading field type file", module);
121                     }
122                     if (document == null) {
123                         fieldTypeCache = null;
124                         return null;
125                     }
126
127                     // utilTimer.timerString("Before getDocumentElement");
128
Element JavaDoc docElement = document.getDocumentElement();
129
130                     if (docElement == null) {
131                         fieldTypeCache = null;
132                         return null;
133                     }
134                     docElement.normalize();
135
136                     Node JavaDoc curChild = docElement.getFirstChild();
137
138                     int i = 0;
139
140                     if (curChild != null) {
141                         utilTimer.timerString("Before start of field type loop");
142                         do {
143                             if (curChild.getNodeType() == Node.ELEMENT_NODE && "field-type-def".equals(curChild.getNodeName())) {
144                                 i++;
145                                 // utilTimer.timerString("Start loop -- " + i + " --");
146
Element JavaDoc curFieldType = (Element JavaDoc) curChild;
147                                 String JavaDoc fieldTypeName = UtilXml.checkEmpty(curFieldType.getAttribute("type"), "[No type name]");
148                                 // utilTimer.timerString(" After fieldTypeName -- " + i + " --");
149
ModelFieldType fieldType = createModelFieldType(curFieldType, docElement, null);
150
151                                 // utilTimer.timerString(" After createModelFieldType -- " + i + " --");
152
if (fieldType != null) {
153                                     fieldTypeCache.put(fieldTypeName, fieldType);
154                                     // utilTimer.timerString(" After fieldTypeCache.put -- " + i + " --");
155
if (Debug.verboseOn()) Debug.logVerbose("-- getModelFieldType: #" + i + " Created fieldType: " + fieldTypeName, module);
156                                 } else {
157                                     Debug.logWarning("-- -- ENTITYGEN ERROR:getModelFieldType: Could not create fieldType for fieldTypeName: " + fieldTypeName, module);
158                                 }
159
160                             }
161                         } while ((curChild = curChild.getNextSibling()) != null);
162                     } else
163                         Debug.logWarning("No child nodes found.", module);
164                     utilTimer.timerString("FINISHED - Total Field Types: " + i + " FINISHED");
165                 }
166             }
167         }
168         return fieldTypeCache;
169     }
170
171     /** Creates a Collection with all of the ModelFieldType names
172      * @return A Collection of ModelFieldType names
173      */

174     public Collection JavaDoc getFieldTypeNames() {
175         Map JavaDoc ftc = getFieldTypeCache();
176
177         return ftc.keySet();
178     }
179
180     /** Creates a Collection with all of the ModelFieldTypes
181      * @return A Collection of ModelFieldTypes
182      */

183     public Collection JavaDoc getFieldTypes() {
184         Map JavaDoc ftc = getFieldTypeCache();
185
186         return ftc.values();
187     }
188
189     /** Gets an FieldType object based on a definition from the specified XML FieldType descriptor file.
190      * @param fieldTypeName The fieldTypeName of the FieldType definition to use.
191      * @return An FieldType object describing the specified fieldType of the specified descriptor file.
192      */

193     public ModelFieldType getModelFieldType(String JavaDoc fieldTypeName) {
194         Map JavaDoc ftc = getFieldTypeCache();
195
196         if (ftc != null)
197             return (ModelFieldType) ftc.get(fieldTypeName);
198         else
199             return null;
200     }
201
202     ModelFieldType createModelFieldType(Element JavaDoc fieldTypeElement, Element JavaDoc docElement, UtilTimer utilTimer) {
203         if (fieldTypeElement == null) return null;
204
205         ModelFieldType field = new ModelFieldType(fieldTypeElement);
206
207         return field;
208     }
209 }
210
Popular Tags