KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > service > group > GroupModel


1 /*
2  * $Id: GroupModel.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 2001, 2002 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 package org.ofbiz.service.group;
25
26 import java.util.HashMap JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.LinkedList JavaDoc;
29 import java.util.List JavaDoc;
30 import java.util.Map JavaDoc;
31
32 import org.ofbiz.base.util.Debug;
33 import org.ofbiz.base.util.UtilXml;
34 import org.ofbiz.service.GenericServiceException;
35 import org.ofbiz.service.ServiceDispatcher;
36 import org.ofbiz.service.ServiceUtil;
37
38 import org.w3c.dom.Element JavaDoc;
39
40 /**
41  * GroupModel.java
42  *
43  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
44  * @version $Rev: 5462 $
45  * @since 2.0
46  */

47 public class GroupModel {
48     
49     public static final String JavaDoc module = GroupModel.class.getName();
50     
51     private String JavaDoc groupName, sendMode;
52     private List JavaDoc services;
53     private int lastServiceRan;
54     
55     /**
56      * Constructor using DOM Element
57      * @param group DOM element for the group
58      */

59     public GroupModel(Element JavaDoc group) {
60         this.lastServiceRan = -1;
61         this.services = new LinkedList JavaDoc();
62         List JavaDoc serviceList = UtilXml.childElementList(group, "service");
63         Iterator JavaDoc i = serviceList.iterator();
64         while (i.hasNext()) {
65             Element JavaDoc service = (Element JavaDoc) i.next();
66             services.add(new GroupServiceModel(service));
67         }
68         this.groupName = group.getAttribute("name");
69         this.sendMode = group.getAttribute("send-mode");
70         if (Debug.verboseOn()) Debug.logVerbose("Created Service Group Model --> " + this, module);
71     }
72     
73     /**
74      * Basic Constructor
75      * @param groupName Name of the group
76      * @param sendMode Mode used (see DTD)
77      * @param services List of GroupServiceModel objects
78      */

79     public GroupModel(String JavaDoc groupName, String JavaDoc sendMode, List JavaDoc services) {
80         this.lastServiceRan = -1;
81         this.groupName = groupName;
82         this.sendMode = sendMode;
83         this.services = services;
84     }
85     
86     /**
87      * Getter for group name
88      * @return String
89      */

90     public String JavaDoc getGroupName() {
91         return this.groupName;
92     }
93     
94     /**
95      * Getter for send mode
96      * @return String
97      */

98     public String JavaDoc getSendMode() {
99         return this.sendMode;
100     }
101     
102     /**
103      * Returns a list of services in this group
104      * @return List
105      */

106     public List JavaDoc getServices() {
107         return this.services;
108     }
109     
110     /**
111      * Invokes the group of services in order defined
112      * @param dispatcher ServiceDispatcher used for invocation
113      * @param localName Name of the LocalDispatcher (namespace)
114      * @param context Full parameter context (combined for all services)
115      * @return Map Result Map
116      * @throws GenericServiceException
117      */

118     public Map JavaDoc run(ServiceDispatcher dispatcher, String JavaDoc localName, Map JavaDoc context) throws GenericServiceException {
119         if (this.getSendMode().equals("all")) {
120             return runAll(dispatcher, localName, context);
121         } else if (this.getSendMode().equals("round-robin")) {
122             return runIndex(dispatcher, localName, context, (++lastServiceRan % services.size()));
123         } else if (this.getSendMode().equals("random")) {
124             int randomIndex = (int) (Math.random() * (double) (services.size()));
125             return runIndex(dispatcher, localName, context, randomIndex);
126         } else if (this.getSendMode().equals("first-available")) {
127             return runOne(dispatcher, localName, context);
128         } else if (this.getSendMode().equals("none")) {
129             return new HashMap JavaDoc();
130         } else {
131             throw new GenericServiceException("This mode is not currently supported");
132         }
133     }
134     
135     /**
136      * @see java.lang.Object#toString()
137      */

138     public String JavaDoc toString() {
139         StringBuffer JavaDoc str = new StringBuffer JavaDoc();
140         str.append(getGroupName());
141         str.append("::");
142         str.append(getSendMode());
143         str.append("::");
144         str.append(getServices());
145         return str.toString();
146     }
147     
148     private Map JavaDoc runAll(ServiceDispatcher dispatcher, String JavaDoc localName, Map JavaDoc context) throws GenericServiceException {
149         Map JavaDoc runContext = new HashMap JavaDoc(context);
150         Map JavaDoc result = new HashMap JavaDoc();
151         Iterator JavaDoc i = services.iterator();
152         while (i.hasNext()) {
153             GroupServiceModel model = (GroupServiceModel) i.next();
154             if (Debug.verboseOn()) Debug.logVerbose("Using Context: " + runContext, module);
155             Map JavaDoc thisResult = model.invoke(dispatcher, localName, runContext);
156             if (Debug.verboseOn()) Debug.logVerbose("Result: " + thisResult, module);
157             
158             // make sure we didn't fail
159
if (ServiceUtil.isError(thisResult)) {
160                 Debug.logError("Grouped service [" + model.getName() + "] failed.", module);
161                 return thisResult;
162             }
163             
164             result.putAll(thisResult);
165             if (model.resultToContext()) {
166                 runContext.putAll(thisResult);
167                 Debug.logVerbose("Added result(s) to context.", module);
168             }
169         }
170         return result;
171     }
172     
173     private Map JavaDoc runIndex(ServiceDispatcher dispatcher, String JavaDoc localName, Map JavaDoc context, int index) throws GenericServiceException {
174         GroupServiceModel model = (GroupServiceModel) services.get(index);
175         return model.invoke(dispatcher, localName, context);
176     }
177     
178     private Map JavaDoc runOne(ServiceDispatcher dispatcher, String JavaDoc localName, Map JavaDoc context) throws GenericServiceException {
179         Map JavaDoc result = null;
180         Iterator JavaDoc i = services.iterator();
181         while (i.hasNext() && result != null) {
182             GroupServiceModel model = (GroupServiceModel) i.next();
183             try {
184                 result = model.invoke(dispatcher, localName, context);
185             } catch (GenericServiceException e) {
186                 Debug.logError("Service: " + model + " failed.", module);
187             }
188         }
189         if (result == null) {
190             throw new GenericServiceException("All services failed to run; none available.");
191         }
192         return result;
193     }
194 }
195
Popular Tags