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.ArrayList;
022: import java.util.Collection;
023: import java.util.HashMap;
024: import java.util.Iterator;
025: import java.util.LinkedList;
026: import java.util.List;
027: import java.util.Map;
028: import java.util.Set;
029: import java.util.TreeSet;
030:
031: import org.ofbiz.base.component.ComponentConfig;
032: import org.ofbiz.base.config.GenericConfigException;
033: import org.ofbiz.base.config.MainResourceHandler;
034: import org.ofbiz.base.config.ResourceHandler;
035: import org.ofbiz.entity.GenericEntityConfException;
036: import org.ofbiz.entity.config.DelegatorInfo;
037: import org.ofbiz.entity.config.EntityConfigUtil;
038: import org.ofbiz.entity.config.EntityGroupReaderInfo;
039: import org.ofbiz.base.util.Debug;
040: import org.ofbiz.base.util.cache.UtilCache;
041: import org.ofbiz.base.util.UtilTimer;
042: import org.ofbiz.base.util.UtilXml;
043: import org.w3c.dom.Document;
044: import org.w3c.dom.Element;
045: import org.w3c.dom.Node;
046:
047: /**
048: * Generic Entity - Entity Group Definition Reader
049: *
050: */
051: public class ModelGroupReader implements Serializable {
052:
053: public static final String module = ModelGroupReader.class
054: .getName();
055: public static UtilCache readers = new UtilCache(
056: "entity.ModelGroupReader", 0, 0);
057:
058: private Map groupCache = null;
059: private Set groupNames = null;
060:
061: public String modelName;
062: public List entityGroupResourceHandlers = new LinkedList();
063:
064: public static ModelGroupReader getModelGroupReader(
065: String delegatorName) throws GenericEntityConfException {
066: DelegatorInfo delegatorInfo = EntityConfigUtil
067: .getDelegatorInfo(delegatorName);
068:
069: if (delegatorInfo == null) {
070: throw new GenericEntityConfException(
071: "Could not find a delegator with the name "
072: + delegatorName);
073: }
074:
075: String tempModelName = delegatorInfo.entityGroupReader;
076: ModelGroupReader reader = (ModelGroupReader) readers
077: .get(tempModelName);
078:
079: if (reader == null) { // don't want to block here
080: synchronized (ModelGroupReader.class) {
081: // must check if null again as one of the blocked threads can still enter
082: reader = (ModelGroupReader) readers.get(tempModelName);
083: if (reader == null) {
084: reader = new ModelGroupReader(tempModelName);
085: readers.put(tempModelName, reader);
086: }
087: }
088: }
089: return reader;
090: }
091:
092: public ModelGroupReader(String modelName)
093: throws GenericEntityConfException {
094: this .modelName = modelName;
095: EntityGroupReaderInfo entityGroupReaderInfo = EntityConfigUtil
096: .getEntityGroupReaderInfo(modelName);
097:
098: if (entityGroupReaderInfo == null) {
099: throw new GenericEntityConfException(
100: "Cound not find an entity-group-reader with the name "
101: + modelName);
102: }
103: Iterator resourceElementIter = entityGroupReaderInfo.resourceElements
104: .iterator();
105: while (resourceElementIter.hasNext()) {
106: Element resourceElement = (Element) resourceElementIter
107: .next();
108: this .entityGroupResourceHandlers
109: .add(new MainResourceHandler(
110: EntityConfigUtil.ENTITY_ENGINE_XML_FILENAME,
111: resourceElement));
112: }
113:
114: // get all of the component resource group stuff, ie specified in each ofbiz-component.xml file
115: List componentResourceInfos = ComponentConfig
116: .getAllEntityResourceInfos("group");
117: Iterator componentResourceInfoIter = componentResourceInfos
118: .iterator();
119: while (componentResourceInfoIter.hasNext()) {
120: ComponentConfig.EntityResourceInfo componentResourceInfo = (ComponentConfig.EntityResourceInfo) componentResourceInfoIter
121: .next();
122: if (modelName.equals(componentResourceInfo.readerName)) {
123: this .entityGroupResourceHandlers
124: .add(componentResourceInfo
125: .createResourceHandler());
126: }
127: }
128:
129: // preload caches...
130: getGroupCache();
131: }
132:
133: public Map getGroupCache() {
134: if (this .groupCache == null) // don't want to block here
135: {
136: synchronized (ModelGroupReader.class) {
137: // must check if null again as one of the blocked threads can still enter
138: if (this .groupCache == null) {
139: // now it's safe
140: this .groupCache = new HashMap();
141: this .groupNames = new TreeSet();
142:
143: UtilTimer utilTimer = new UtilTimer();
144: // utilTimer.timerString("[ModelGroupReader.getGroupCache] Before getDocument");
145:
146: int i = 0;
147: Iterator entityGroupResourceHandlerIter = this .entityGroupResourceHandlers
148: .iterator();
149: while (entityGroupResourceHandlerIter.hasNext()) {
150: ResourceHandler entityGroupResourceHandler = (ResourceHandler) entityGroupResourceHandlerIter
151: .next();
152: Document document = null;
153:
154: try {
155: document = entityGroupResourceHandler
156: .getDocument();
157: } catch (GenericConfigException e) {
158: Debug.logError(e,
159: "Error loading entity group model",
160: module);
161: }
162: if (document == null) {
163: this .groupCache = null;
164: return null;
165: }
166:
167: // utilTimer.timerString("[ModelGroupReader.getGroupCache] Before getDocumentElement");
168: Element docElement = document
169: .getDocumentElement();
170: if (docElement == null) {
171: continue;
172: }
173: docElement.normalize();
174:
175: Node curChild = docElement.getFirstChild();
176: if (curChild != null) {
177: utilTimer
178: .timerString("[ModelGroupReader.getGroupCache] Before start of entity loop");
179: do {
180: if (curChild.getNodeType() == Node.ELEMENT_NODE
181: && "entity-group"
182: .equals(curChild
183: .getNodeName())) {
184: Element curEntity = (Element) curChild;
185: String entityName = UtilXml
186: .checkEmpty(curEntity
187: .getAttribute("entity"));
188: String groupName = UtilXml
189: .checkEmpty(curEntity
190: .getAttribute("group"));
191:
192: if (groupName == null
193: || entityName == null)
194: continue;
195: this .groupNames.add(groupName);
196: this .groupCache.put(entityName,
197: groupName);
198: // utilTimer.timerString(" After entityEntityName -- " + i + " --");
199: i++;
200: }
201: } while ((curChild = curChild
202: .getNextSibling()) != null);
203: } else {
204: Debug
205: .logWarning(
206: "[ModelGroupReader.getGroupCache] No child nodes found.",
207: module);
208: }
209: }
210: utilTimer
211: .timerString("[ModelGroupReader.getGroupCache] FINISHED - Total Entity-Groups: "
212: + i + " FINISHED");
213: }
214: }
215: }
216: return this .groupCache;
217: }
218:
219: /** Gets a group name based on a definition from the specified XML Entity Group descriptor file.
220: * @param entityName The entityName of the Entity Group definition to use.
221: * @return A group name
222: */
223: public String getEntityGroupName(String entityName) {
224: Map gc = getGroupCache();
225:
226: if (gc != null)
227: return (String) gc.get(entityName);
228: else
229: return null;
230: }
231:
232: /** Creates a Collection with all of the groupNames defined in the specified XML Entity Group Descriptor file.
233: * @return A Collection of groupNames Strings
234: */
235: public Collection getGroupNames() {
236: getGroupCache();
237: if (this .groupNames == null)
238: return null;
239: return new ArrayList(this .groupNames);
240: }
241:
242: /** Creates a Collection with names of all of the entities for a given group
243: * @param groupName
244: * @return A Collection of entityName Strings
245: */
246: public Collection getEntityNamesByGroup(String groupName) {
247: Map gc = getGroupCache();
248: Collection enames = new LinkedList();
249:
250: if (groupName == null || groupName.length() <= 0)
251: return enames;
252: if (gc == null || gc.size() < 0)
253: return enames;
254: Set gcEntries = gc.entrySet();
255: Iterator gcIter = gcEntries.iterator();
256:
257: while (gcIter.hasNext()) {
258: Map.Entry entry = (Map.Entry) gcIter.next();
259:
260: if (groupName.equals(entry.getValue()))
261: enames.add(entry.getKey());
262: }
263: return enames;
264: }
265: }
|