1 25 package org.ofbiz.service.mail; 26 27 import java.util.Iterator ; 28 import java.util.List ; 29 import java.util.Set ; 30 import java.util.TreeSet ; 31 import java.util.Collection ; 32 33 import org.ofbiz.base.component.ComponentConfig; 34 import org.ofbiz.base.config.GenericConfigException; 35 import org.ofbiz.base.config.MainResourceHandler; 36 import org.ofbiz.base.config.ResourceHandler; 37 import org.ofbiz.base.util.Debug; 38 import org.ofbiz.base.util.UtilXml; 39 import org.ofbiz.base.util.cache.UtilCache; 40 import org.ofbiz.service.config.ServiceConfigUtil; 41 import org.ofbiz.service.LocalDispatcher; 42 import org.ofbiz.service.GenericServiceException; 43 import org.ofbiz.entity.GenericValue; 44 45 import org.w3c.dom.Element ; 46 47 53 public class ServiceMcaUtil { 54 55 public static final String module = ServiceMcaUtil.class.getName(); 56 public static UtilCache mcaCache = new UtilCache("service.ServiceMCAs", 0, 0, false); 57 58 public static void reloadConfig() { 59 mcaCache.clear(); 60 readConfig(); 61 } 62 63 public static void readConfig() { 64 Element rootElement = null; 65 try { 66 rootElement = ServiceConfigUtil.getXmlRootElement(); 67 } catch (GenericConfigException e) { 68 Debug.logError(e, "Error getting Service Engine XML root element", module); 69 return; 70 } 71 72 List serviceMcasElements = UtilXml.childElementList(rootElement, "service-mcas"); 73 Iterator secasIter = serviceMcasElements.iterator(); 74 while (secasIter.hasNext()) { 75 Element serviceMcasElement = (Element ) secasIter.next(); 76 ResourceHandler handler = new MainResourceHandler(ServiceConfigUtil.SERVICE_ENGINE_XML_FILENAME, serviceMcasElement); 77 addMcaDefinitions(handler); 78 } 79 80 List componentResourceInfos = ComponentConfig.getAllServiceResourceInfos("mca"); 82 Iterator componentResourceInfoIter = componentResourceInfos.iterator(); 83 while (componentResourceInfoIter.hasNext()) { 84 ComponentConfig.ServiceResourceInfo componentResourceInfo = (ComponentConfig.ServiceResourceInfo) componentResourceInfoIter.next(); 85 addMcaDefinitions(componentResourceInfo.createResourceHandler()); 86 } 87 } 88 89 public static void addMcaDefinitions(ResourceHandler handler) { 90 Element rootElement = null; 91 try { 92 rootElement = handler.getDocument().getDocumentElement(); 93 } catch (GenericConfigException e) { 94 Debug.logError(e, module); 95 return; 96 } 97 98 List ecaList = UtilXml.childElementList(rootElement, "mca"); 99 Iterator ecaIt = ecaList.iterator(); 100 int numDefs = 0; 101 102 while (ecaIt.hasNext()) { 103 Element e = (Element ) ecaIt.next(); 104 String ruleName = e.getAttribute("mail-rule-name"); 105 mcaCache.put(ruleName, new ServiceMcaRule(e)); 106 numDefs++; 107 } 108 109 if (Debug.importantOn()) { 110 String resourceLocation = handler.getLocation(); 111 try { 112 resourceLocation = handler.getURL().toExternalForm(); 113 } catch (GenericConfigException e) { 114 Debug.logError(e, "Could not get resource URL", module); 115 } 116 Debug.logImportant("Loaded " + numDefs + " Service MCA definitions from " + resourceLocation, module); 117 } 118 } 119 120 public static List getServiceMcaRules() { 121 if (mcaCache.size() == 0) { 122 readConfig(); 123 } 124 return mcaCache.values(); 125 } 126 127 public static void evalRules(LocalDispatcher dispatcher, MimeMessageWrapper wrapper, GenericValue userLogin) throws GenericServiceException { 128 List rules = getServiceMcaRules(); 129 Set actionsRun = new TreeSet (); 130 if (rules != null) { 131 Iterator i = rules.iterator(); 132 while (i.hasNext()) { 133 ServiceMcaRule rule = (ServiceMcaRule) i.next(); 134 rule.eval(dispatcher, wrapper, actionsRun, userLogin); 135 } 136 } 137 } 138 } 139 | Popular Tags |