1 24 package org.ofbiz.service.group; 25 26 import java.util.HashMap ; 27 import java.util.Iterator ; 28 import java.util.LinkedList ; 29 import java.util.List ; 30 import java.util.Map ; 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 ; 39 40 47 public class GroupModel { 48 49 public static final String module = GroupModel.class.getName(); 50 51 private String groupName, sendMode; 52 private List services; 53 private int lastServiceRan; 54 55 59 public GroupModel(Element group) { 60 this.lastServiceRan = -1; 61 this.services = new LinkedList (); 62 List serviceList = UtilXml.childElementList(group, "service"); 63 Iterator i = serviceList.iterator(); 64 while (i.hasNext()) { 65 Element service = (Element ) 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 79 public GroupModel(String groupName, String sendMode, List services) { 80 this.lastServiceRan = -1; 81 this.groupName = groupName; 82 this.sendMode = sendMode; 83 this.services = services; 84 } 85 86 90 public String getGroupName() { 91 return this.groupName; 92 } 93 94 98 public String getSendMode() { 99 return this.sendMode; 100 } 101 102 106 public List getServices() { 107 return this.services; 108 } 109 110 118 public Map run(ServiceDispatcher dispatcher, String localName, Map 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 (); 130 } else { 131 throw new GenericServiceException("This mode is not currently supported"); 132 } 133 } 134 135 138 public String toString() { 139 StringBuffer str = new StringBuffer (); 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 runAll(ServiceDispatcher dispatcher, String localName, Map context) throws GenericServiceException { 149 Map runContext = new HashMap (context); 150 Map result = new HashMap (); 151 Iterator 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 thisResult = model.invoke(dispatcher, localName, runContext); 156 if (Debug.verboseOn()) Debug.logVerbose("Result: " + thisResult, module); 157 158 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 runIndex(ServiceDispatcher dispatcher, String localName, Map context, int index) throws GenericServiceException { 174 GroupServiceModel model = (GroupServiceModel) services.get(index); 175 return model.invoke(dispatcher, localName, context); 176 } 177 178 private Map runOne(ServiceDispatcher dispatcher, String localName, Map context) throws GenericServiceException { 179 Map result = null; 180 Iterator 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 |