KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > service > mail > ServiceMcaUtil


1 /*
2  * $Id: ServiceMcaUtil.java 5462 2005-08-05 18:35:48Z 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  */

25 package org.ofbiz.service.mail;
26
27 import java.util.Iterator JavaDoc;
28 import java.util.List JavaDoc;
29 import java.util.Set JavaDoc;
30 import java.util.TreeSet JavaDoc;
31 import java.util.Collection JavaDoc;
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 JavaDoc;
46
47 /**
48  *
49  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
50  * @version $Rev: 5462 $
51  * @since 3.3
52  */

53 public class ServiceMcaUtil {
54
55     public static final String JavaDoc 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 JavaDoc 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 JavaDoc serviceMcasElements = UtilXml.childElementList(rootElement, "service-mcas");
73         Iterator JavaDoc secasIter = serviceMcasElements.iterator();
74         while (secasIter.hasNext()) {
75             Element JavaDoc serviceMcasElement = (Element JavaDoc) secasIter.next();
76             ResourceHandler handler = new MainResourceHandler(ServiceConfigUtil.SERVICE_ENGINE_XML_FILENAME, serviceMcasElement);
77             addMcaDefinitions(handler);
78         }
79
80         // get all of the component resource eca stuff, ie specified in each ofbiz-component.xml file
81
List JavaDoc componentResourceInfos = ComponentConfig.getAllServiceResourceInfos("mca");
82         Iterator JavaDoc 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 JavaDoc rootElement = null;
91         try {
92             rootElement = handler.getDocument().getDocumentElement();
93         } catch (GenericConfigException e) {
94             Debug.logError(e, module);
95             return;
96         }
97
98         List JavaDoc ecaList = UtilXml.childElementList(rootElement, "mca");
99         Iterator JavaDoc ecaIt = ecaList.iterator();
100         int numDefs = 0;
101
102         while (ecaIt.hasNext()) {
103             Element JavaDoc e = (Element JavaDoc) ecaIt.next();
104             String JavaDoc ruleName = e.getAttribute("mail-rule-name");
105             mcaCache.put(ruleName, new ServiceMcaRule(e));
106             numDefs++;
107         }
108
109         if (Debug.importantOn()) {
110             String JavaDoc 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 JavaDoc 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 JavaDoc rules = getServiceMcaRules();
129         Set JavaDoc actionsRun = new TreeSet JavaDoc();
130         if (rules != null) {
131             Iterator JavaDoc 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