1 25 package org.ofbiz.service.engine; 26 27 import java.util.Map ; 28 import java.util.List ; 29 import java.util.Iterator ; 30 31 import javolution.util.FastMap; 32 33 import org.ofbiz.service.ServiceDispatcher; 34 import org.ofbiz.service.ModelService; 35 import org.ofbiz.service.GenericServiceException; 36 import org.ofbiz.service.GenericServiceCallback; 37 import org.ofbiz.service.config.ServiceConfigUtil; 38 import org.ofbiz.base.config.GenericConfigException; 39 import org.ofbiz.base.util.Debug; 40 import org.ofbiz.base.util.UtilXml; 41 42 import org.w3c.dom.Element ; 43 44 51 public abstract class AbstractEngine implements GenericEngine { 52 53 public static final String module = AbstractEngine.class.getName(); 54 protected static Map locationMap = null; 55 56 protected ServiceDispatcher dispatcher = null; 57 58 protected AbstractEngine(ServiceDispatcher dispatcher) { 59 this.dispatcher = dispatcher; 60 initLocations(); 61 } 62 63 protected synchronized void initLocations() { 65 if (locationMap == null) { 66 locationMap = FastMap.newInstance(); 67 68 Element root = null; 69 try { 70 root = ServiceConfigUtil.getXmlRootElement(); 71 } catch (GenericConfigException e) { 72 Debug.logError(e, module); 73 } 74 75 if (root != null) { 76 List locationElements = UtilXml.childElementList(root, "service-location"); 77 if (locationElements != null) { 78 Iterator i = locationElements.iterator(); 79 while (i.hasNext()) { 80 Element e = (Element ) i.next(); 81 locationMap.put(e.getAttribute("name"), e.getAttribute("location")); 82 } 83 } 84 } 85 Debug.logInfo("Loaded Service Locations : " + locationMap, module); 86 } 87 } 88 89 protected String getLocation(ModelService model) { 91 if (locationMap.containsKey(model.location)) { 92 return (String ) locationMap.get(model.location); 93 } else { 94 return model.location; 95 } 96 } 97 98 101 public void sendCallbacks(ModelService model, Map context, Object cbObj, int mode) throws GenericServiceException { 102 List callbacks = dispatcher.getCallbacks(model.name); 103 if (callbacks != null) { 104 Iterator i = callbacks.iterator(); 105 while (i.hasNext()) { 106 GenericServiceCallback gsc = (GenericServiceCallback) i.next(); 107 if (gsc.isEnabled()) { 108 if (cbObj == null) { 109 gsc.receiveEvent(context); 110 } else if (cbObj instanceof Throwable ) { 111 gsc.receiveEvent(context, (Throwable ) cbObj); 112 } else if (cbObj instanceof Map ) { 113 gsc.receiveEvent(context, (Map ) cbObj); 114 } else { 115 throw new GenericServiceException("Callback object is not Throwable or Map"); 116 } 117 } else { 118 i.remove(); 119 } 120 } 121 } 122 } 123 } 124 | Popular Tags |