1 25 package org.ofbiz.service; 26 27 import java.io.Serializable ; 28 import java.net.URL ; 29 import java.util.Collection ; 30 import java.util.Iterator ; 31 import java.util.List ; 32 import java.util.Map ; 33 import java.util.Set ; 34 import java.util.TreeSet ; 35 import javax.wsdl.WSDLException; 36 37 import javolution.util.FastMap; 38 39 import org.ofbiz.base.component.ComponentConfig; 40 import org.ofbiz.base.config.GenericConfigException; 41 import org.ofbiz.base.config.MainResourceHandler; 42 import org.ofbiz.base.config.ResourceHandler; 43 import org.ofbiz.base.util.Debug; 44 import org.ofbiz.base.util.UtilXml; 45 import org.ofbiz.base.util.cache.UtilCache; 46 import org.ofbiz.entity.GenericDelegator; 47 import org.ofbiz.security.Security; 48 import org.ofbiz.service.config.ServiceConfigUtil; 49 import org.ofbiz.service.eca.ServiceEcaUtil; 50 51 import org.w3c.dom.Document ; 52 import org.w3c.dom.Element ; 53 54 62 public class DispatchContext implements Serializable { 63 64 public static final String module = DispatchContext.class.getName(); 65 66 protected static final String GLOBAL_KEY = "global.services"; 67 protected static UtilCache modelService = new UtilCache("service.ModelServices", 0, 0, false); 68 69 protected transient LocalDispatcher dispatcher; 70 protected transient ClassLoader loader; 71 protected Collection readers; 72 protected Map attributes; 73 protected String name; 74 75 80 public DispatchContext(String name, Collection readers, ClassLoader loader, LocalDispatcher dispatcher) { 81 this.name = name; 82 this.readers = readers; 83 this.loader = loader; 84 this.dispatcher = dispatcher; 85 this.attributes = FastMap.newInstance(); 86 } 87 88 public void loadReaders() { 89 Map localService = addReaders(readers); 90 if (localService != null) { 91 modelService.put(name, localService); 92 } 93 94 Map globalService = addGlobal(); 95 if (globalService != null) { 96 modelService.put(GLOBAL_KEY, globalService); 97 } 98 } 99 100 105 public Object getAttribute(String name) { 106 if (attributes.containsKey(name)) 107 return attributes.get(name); 108 return null; 109 } 110 111 116 public void setAttribute(String name, Object object) { 117 attributes.put(name, object); 118 } 119 120 124 public ClassLoader getClassLoader() { 125 return this.loader; 126 } 127 128 132 public Collection getReaders() { 133 return readers; 134 } 135 136 140 public String getName() { 141 return name; 142 } 143 144 153 public Map makeValidContext(String serviceName, String mode, Map context) throws GenericServiceException { 154 ModelService model = this.getModelService(serviceName); 155 return makeValidContext(model, mode, context); 156 157 } 158 159 168 public Map makeValidContext(ModelService model, String mode, Map context) throws GenericServiceException { 169 Map newContext = null; 170 171 int modeInt = 0; 172 if (mode.equalsIgnoreCase("in")) { 173 modeInt = 1; 174 } else if (mode.equalsIgnoreCase("out")) { 175 modeInt = 2; 176 } 177 178 if (model == null) { 179 throw new GenericServiceException("Model service is null! Should never happen."); 180 } else { 181 switch (modeInt) { 182 case 2: 183 newContext = model.makeValid(context, ModelService.OUT_PARAM, true, null); 184 break; 185 case 1: 186 newContext = model.makeValid(context, ModelService.IN_PARAM, true, null); 187 break; 188 default: 189 throw new GenericServiceException("Invalid mode, should be either IN or OUT"); 190 } 191 return newContext; 192 } 193 } 194 195 200 public ModelService getModelService(String serviceName) throws GenericServiceException { 201 ModelService retVal = getLocalModelService(serviceName); 202 if (retVal == null) { 203 retVal = getGlobalModelService(serviceName); 204 } 205 206 if (retVal == null) { 207 throw new GenericServiceException("Cannot locate service by name (" + serviceName + ")"); 208 } 209 210 return retVal; 211 } 212 213 private ModelService getLocalModelService(String serviceName) throws GenericServiceException { 214 Map serviceMap = (Map ) modelService.get(name); 215 if (serviceMap == null) { 216 synchronized (this) { 217 serviceMap = (Map ) modelService.get(name); 218 if (serviceMap == null) { 219 serviceMap = addReaders(readers); 220 if (serviceMap != null) { 221 modelService.put(name, serviceMap); 222 ServiceEcaUtil.reloadConfig(); 223 } 224 } 225 } 226 } 227 228 ModelService retVal = null; 229 if (serviceMap != null) { 230 retVal = (ModelService) serviceMap.get(serviceName); 231 if (retVal != null && !retVal.inheritedParameters()) { 232 retVal.interfaceUpdate(this); 233 } 234 } 235 236 return retVal; 237 } 238 239 private ModelService getGlobalModelService(String serviceName) throws GenericServiceException { 240 Map serviceMap = (Map ) modelService.get(GLOBAL_KEY); 241 if (serviceMap == null) { 242 synchronized (this) { 243 serviceMap = (Map ) modelService.get(GLOBAL_KEY); 244 if (serviceMap == null) { 245 serviceMap = addGlobal(); 246 if (serviceMap != null) { 247 modelService.put(GLOBAL_KEY, serviceMap); 248 ServiceEcaUtil.reloadConfig(); 249 } 250 } 251 } 252 } 253 254 ModelService retVal = null; 255 if (serviceMap != null) { 256 retVal = (ModelService) serviceMap.get(serviceName); 257 if (retVal != null && !retVal.inheritedParameters()) { 258 retVal.interfaceUpdate(this); 259 } 260 } 261 262 return retVal; 263 } 264 265 269 public LocalDispatcher getDispatcher() { 270 return this.dispatcher; 271 } 272 273 277 public void setDispatcher(LocalDispatcher dispatcher) { 278 this.dispatcher = dispatcher; 279 } 280 281 285 public GenericDelegator getDelegator() { 286 return dispatcher.getDelegator(); 287 } 288 289 293 public Security getSecurity() { 294 return dispatcher.getSecurity(); 295 } 296 297 private Map addReaders(Collection readerURLs) { 298 Map serviceMap = FastMap.newInstance(); 299 300 if (readerURLs == null) 301 return null; 302 Iterator urlIter = readerURLs.iterator(); 303 304 while (urlIter.hasNext()) { 305 URL readerURL = (URL ) urlIter.next(); 306 307 serviceMap.putAll(addReader(readerURL)); 308 } 309 return serviceMap; 310 } 311 312 private Map addReader(URL readerURL) { 313 if (readerURL == null) { 314 Debug.logError("Cannot add reader with a null reader URL", module); 315 return null; 316 } 317 318 ModelServiceReader reader = ModelServiceReader.getModelServiceReader(readerURL, this); 319 320 if (reader == null) { 321 Debug.logError("Could not load the reader for the reader URL " + readerURL, module); 322 return null; 323 } 324 325 Map serviceMap = reader.getModelServices(); 326 327 return serviceMap; 328 } 329 330 private Map addReader(ResourceHandler handler) { 331 ModelServiceReader reader = ModelServiceReader.getModelServiceReader(handler, this); 332 333 if (reader == null) { 334 Debug.logError("Could not load the reader for " + handler, module); 335 return null; 336 } 337 338 Map serviceMap = reader.getModelServices(); 339 340 return serviceMap; 341 } 342 343 private Map addGlobal() { 344 Map globalMap = FastMap.newInstance(); 345 346 Element rootElement = null; 347 348 try { 349 rootElement = ServiceConfigUtil.getXmlRootElement(); 350 } catch (GenericConfigException e) { 351 Debug.logError(e, "Error getting Service Engine XML root element", module); 352 return null; 353 } 354 355 List globalServicesElements = UtilXml.childElementList(rootElement, "global-services"); 356 Iterator gseIter = globalServicesElements.iterator(); 357 while (gseIter.hasNext()) { 358 Element globalServicesElement = (Element ) gseIter.next(); 359 ResourceHandler handler = new MainResourceHandler( 360 ServiceConfigUtil.SERVICE_ENGINE_XML_FILENAME, globalServicesElement); 361 362 Map servicesMap = addReader(handler); 363 if (servicesMap != null) { 364 globalMap.putAll(servicesMap); 365 } 366 } 367 368 List componentResourceInfos = ComponentConfig.getAllServiceResourceInfos("model"); 370 Iterator componentResourceInfoIter = componentResourceInfos.iterator(); 371 while (componentResourceInfoIter.hasNext()) { 372 ComponentConfig.ServiceResourceInfo componentResourceInfo = (ComponentConfig.ServiceResourceInfo) componentResourceInfoIter.next(); 373 Map servicesMap = addReader(componentResourceInfo.createResourceHandler()); 374 if (servicesMap != null) { 375 globalMap.putAll(servicesMap); 376 } 377 } 378 379 return globalMap; 380 } 381 382 public Set getAllServiceNames() { 383 Set serviceNames = new TreeSet (); 384 385 Map globalServices = (Map ) modelService.get(GLOBAL_KEY); 386 Map localServices = (Map ) modelService.get(name); 387 if (globalServices != null) { 388 serviceNames.addAll(globalServices.keySet()); 389 } 390 if (localServices != null) { 391 serviceNames.addAll(localServices.keySet()); 392 } 393 return serviceNames; 394 } 395 396 public Document getWSDL(String serviceName, String locationURI) throws GenericServiceException, WSDLException { 397 ModelService model = this.getModelService(serviceName); 398 return model.toWSDL(locationURI); 399 } 400 } | Popular Tags |