001: /*
002: * $Id: EntityEcaUtil.java,v 1.3 2003/08/17 08:46:58 jonesde Exp $
003: *
004: * Copyright (c) 2002-2003 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: */
025: package org.ofbiz.entityext.eca;
026:
027: import java.util.HashMap;
028: import java.util.Iterator;
029: import java.util.LinkedList;
030: import java.util.List;
031: import java.util.Map;
032:
033: import org.ofbiz.base.component.ComponentConfig;
034: import org.ofbiz.base.config.GenericConfigException;
035: import org.ofbiz.base.config.MainResourceHandler;
036: import org.ofbiz.base.config.ResourceHandler;
037: import org.ofbiz.base.util.Debug;
038: import org.ofbiz.base.util.UtilCache;
039: import org.ofbiz.base.util.UtilXml;
040: import org.ofbiz.entity.config.EntityConfigUtil;
041: import org.w3c.dom.Element;
042:
043: /**
044: * EntityEcaUtil
045: *
046: * @author <a href="mailto:jonesde@ofbiz.org">David E. Jones</a>
047: * @author <a href="mailto:jaz@ofbiz.org">Andy Zeneski</a>
048: * @version $Revision: 1.3 $
049: * @since 2.1
050: */
051: public class EntityEcaUtil {
052:
053: public static final String module = EntityEcaUtil.class.getName();
054:
055: public static UtilCache entityEcaReaders = new UtilCache(
056: "entity.EcaReaders", 0, 0, false);
057:
058: public static Map getEntityEcaCache(String entityEcaReaderName) {
059: Map ecaCache = (Map) entityEcaReaders.get(entityEcaReaderName);
060: if (ecaCache == null) {
061: synchronized (EntityEcaUtil.class) {
062: ecaCache = (Map) entityEcaReaders
063: .get(entityEcaReaderName);
064: if (ecaCache == null) {
065: ecaCache = new HashMap();
066: readConfig(entityEcaReaderName, ecaCache);
067: entityEcaReaders.put(entityEcaReaderName, ecaCache);
068: }
069: }
070: }
071: return ecaCache;
072: }
073:
074: public static String getEntityEcaReaderName(String delegatorName) {
075: EntityConfigUtil.DelegatorInfo delegatorInfo = EntityConfigUtil
076: .getDelegatorInfo(delegatorName);
077: if (delegatorInfo == null) {
078: Debug.logError(
079: "BAD ERROR: Could not find delegator config with name: "
080: + delegatorName, module);
081: return null;
082: }
083: return delegatorInfo.entityEcaReader;
084: }
085:
086: protected static void readConfig(String entityEcaReaderName,
087: Map ecaCache) {
088: EntityConfigUtil.EntityEcaReaderInfo entityEcaReaderInfo = EntityConfigUtil
089: .getEntityEcaReaderInfo(entityEcaReaderName);
090: if (entityEcaReaderInfo == null) {
091: Debug.logError(
092: "BAD ERROR: Could not find entity-eca-reader config with name: "
093: + entityEcaReaderName, module);
094: return;
095: }
096:
097: Iterator eecaResourceIter = entityEcaReaderInfo.resourceElements
098: .iterator();
099: while (eecaResourceIter.hasNext()) {
100: Element eecaResourceElement = (Element) eecaResourceIter
101: .next();
102: ResourceHandler handler = new MainResourceHandler(
103: EntityConfigUtil.ENTITY_ENGINE_XML_FILENAME,
104: eecaResourceElement);
105: addEcaDefinitions(handler, ecaCache);
106: }
107:
108: // get all of the component resource eca stuff, ie specified in each ofbiz-component.xml file
109: List componentResourceInfos = ComponentConfig
110: .getAllEntityResourceInfos("eca");
111: Iterator componentResourceInfoIter = componentResourceInfos
112: .iterator();
113: while (componentResourceInfoIter.hasNext()) {
114: ComponentConfig.EntityResourceInfo componentResourceInfo = (ComponentConfig.EntityResourceInfo) componentResourceInfoIter
115: .next();
116: if (entityEcaReaderName
117: .equals(componentResourceInfo.readerName)) {
118: addEcaDefinitions(componentResourceInfo
119: .createResourceHandler(), ecaCache);
120: }
121: }
122: }
123:
124: protected static void addEcaDefinitions(ResourceHandler handler,
125: Map ecaCache) {
126: Element rootElement = null;
127: try {
128: rootElement = handler.getDocument().getDocumentElement();
129: } catch (GenericConfigException e) {
130: Debug.logError(e, module);
131: return;
132: }
133:
134: List ecaList = UtilXml.childElementList(rootElement, "eca");
135: Iterator ecaIt = ecaList.iterator();
136: int numDefs = 0;
137: while (ecaIt.hasNext()) {
138: Element e = (Element) ecaIt.next();
139: String entityName = e.getAttribute("entity");
140: String eventName = e.getAttribute("event");
141: Map eventMap = (Map) ecaCache.get(entityName);
142: List rules = null;
143: if (eventMap == null) {
144: eventMap = new HashMap();
145: rules = new LinkedList();
146: ecaCache.put(entityName, eventMap);
147: eventMap.put(eventName, rules);
148: } else {
149: rules = (List) eventMap.get(eventName);
150: if (rules == null) {
151: rules = new LinkedList();
152: eventMap.put(eventName, rules);
153: }
154: }
155: rules.add(new EntityEcaRule(e));
156: numDefs++;
157: }
158: Debug.logImportant("Loaded " + numDefs
159: + " Entity ECA definitions from "
160: + handler.getLocation() + " in loader "
161: + handler.getLoaderName(), module);
162: }
163: }
|