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