001: /*
002: * $Id: GroupModel.java,v 1.4 2003/11/15 20:10:45 jonesde Exp $
003: *
004: * Copyright (c) 2001, 2002 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: package org.ofbiz.service.group;
025:
026: import java.util.HashMap;
027: import java.util.Iterator;
028: import java.util.LinkedList;
029: import java.util.List;
030: import java.util.Map;
031:
032: import org.ofbiz.base.util.Debug;
033: import org.ofbiz.base.util.UtilXml;
034: import org.ofbiz.service.GenericServiceException;
035: import org.ofbiz.service.ModelService;
036: import org.ofbiz.service.ServiceDispatcher;
037: import org.w3c.dom.Element;
038:
039: /**
040: * GroupModel.java
041: *
042: * @author <a href="mailto:jaz@ofbiz.org">Andy Zeneski</a>
043: * @version $Revision: 1.4 $
044: * @since 2.0
045: */
046: public class GroupModel {
047:
048: public static final String module = GroupModel.class.getName();
049:
050: private String groupName, sendMode;
051: private List services;
052: private int lastServiceRan;
053:
054: /**
055: * Constructor using DOM Element
056: * @param group DOM element for the group
057: */
058: public GroupModel(Element group) {
059: this .lastServiceRan = -1;
060: this .services = new LinkedList();
061: List serviceList = UtilXml.childElementList(group, "service");
062: Iterator i = serviceList.iterator();
063: while (i.hasNext()) {
064: Element service = (Element) i.next();
065: services.add(new GroupServiceModel(service));
066: }
067: this .groupName = group.getAttribute("name");
068: this .sendMode = group.getAttribute("send-mode");
069: if (Debug.verboseOn())
070: Debug.logVerbose("Created Service Group Model --> " + this ,
071: module);
072: }
073:
074: /**
075: * Basic Constructor
076: * @param groupName Name of the group
077: * @param sendMode Mode used (see DTD)
078: * @param services List of GroupServiceModel objects
079: */
080: public GroupModel(String groupName, String sendMode, List services) {
081: this .lastServiceRan = -1;
082: this .groupName = groupName;
083: this .sendMode = sendMode;
084: this .services = services;
085: }
086:
087: /**
088: * Getter for group name
089: * @return String
090: */
091: public String getGroupName() {
092: return this .groupName;
093: }
094:
095: /**
096: * Getter for send mode
097: * @return String
098: */
099: public String getSendMode() {
100: return this .sendMode;
101: }
102:
103: /**
104: * Returns a list of services in this group
105: * @return List
106: */
107: public List getServices() {
108: return this .services;
109: }
110:
111: /**
112: * Invokes the group of services in order defined
113: * @param dispatcher ServiceDispatcher used for invocation
114: * @param localName Name of the LocalDispatcher (namespace)
115: * @param context Full parameter context (combined for all services)
116: * @return Map Result Map
117: * @throws GenericServiceException
118: */
119: public Map run(ServiceDispatcher dispatcher, String localName,
120: Map context) throws GenericServiceException {
121: if (this .getSendMode().equals("all")) {
122: return runAll(dispatcher, localName, context);
123: } else if (this .getSendMode().equals("round-robin")) {
124: return runIndex(dispatcher, localName, context,
125: (++lastServiceRan % services.size()));
126: } else if (this .getSendMode().equals("random")) {
127: int randomIndex = (int) (Math.random() * (double) (services
128: .size()));
129: return runIndex(dispatcher, localName, context, randomIndex);
130: } else if (this .getSendMode().equals("first-available")) {
131: return runOne(dispatcher, localName, context);
132: } else if (this .getSendMode().equals("none")) {
133: return new HashMap();
134: } else {
135: throw new GenericServiceException(
136: "This mode is not currently supported");
137: }
138: }
139:
140: /**
141: * @see java.lang.Object#toString()
142: */
143: public String toString() {
144: StringBuffer str = new StringBuffer();
145: str.append(getGroupName());
146: str.append("::");
147: str.append(getSendMode());
148: str.append("::");
149: str.append(getServices());
150: return str.toString();
151: }
152:
153: private Map runAll(ServiceDispatcher dispatcher, String localName,
154: Map context) throws GenericServiceException {
155: Map runContext = new HashMap(context);
156: Map result = new HashMap();
157: Iterator i = services.iterator();
158: while (i.hasNext()) {
159: GroupServiceModel model = (GroupServiceModel) i.next();
160: if (Debug.verboseOn())
161: Debug
162: .logVerbose("Using Context: " + runContext,
163: module);
164: Map this Result = model.invoke(dispatcher, localName,
165: runContext);
166: if (Debug.verboseOn())
167: Debug.logVerbose("Result: " + this Result, module);
168:
169: // make sure we didn't fail
170: if (((String) this Result.get(ModelService.RESPONSE_MESSAGE))
171: .equals(ModelService.RESPOND_ERROR)) {
172: Debug.logError("Grouped service [" + model.getName()
173: + "] failed.", module);
174: return this Result;
175: }
176:
177: result.putAll(this Result);
178: if (model.resultToContext()) {
179: runContext.putAll(this Result);
180: Debug.logVerbose("Added result(s) to context.", module);
181: }
182: }
183: return result;
184: }
185:
186: private Map runIndex(ServiceDispatcher dispatcher,
187: String localName, Map context, int index)
188: throws GenericServiceException {
189: GroupServiceModel model = (GroupServiceModel) services
190: .get(index);
191: return model.invoke(dispatcher, localName, context);
192: }
193:
194: private Map runOne(ServiceDispatcher dispatcher, String localName,
195: Map context) throws GenericServiceException {
196: Map result = null;
197: Iterator i = services.iterator();
198: while (i.hasNext() && result != null) {
199: GroupServiceModel model = (GroupServiceModel) i.next();
200: try {
201: result = model.invoke(dispatcher, localName, context);
202: } catch (GenericServiceException e) {
203: Debug
204: .logError("Service: " + model + " failed.",
205: module);
206: }
207: }
208: if (result == null) {
209: throw new GenericServiceException(
210: "All services failed to run; none available.");
211: }
212: return result;
213: }
214: }
|