svn commit: r1846783 - in /ofbiz/ofbiz-framework/trunk/framework/service/src/main/java/org/apache/ofbiz/service: ServiceDispatcher.java engine/AbstractEngine.java

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

svn commit: r1846783 - in /ofbiz/ofbiz-framework/trunk/framework/service/src/main/java/org/apache/ofbiz/service: ServiceDispatcher.java engine/AbstractEngine.java

jleroux@apache.org
Author: jleroux
Date: Sat Nov 17 08:57:19 2018
New Revision: 1846783

URL: http://svn.apache.org/viewvc?rev=1846783&view=rev
Log:
Improved: Simplification of the service callback handling
(OFBIZ-10649)

The service callback handling done in
  org.apache.ofbiz.service.engine.AbstractEngine
can be rewritten more nicely without explicit iterators.
Additionally it is not nice that the engine is responsible for removing
disabled callbacks. The ServiceDispatcher should be responsible for removing
them.

Thanks: Mathieu Lirzin

Modified:
    ofbiz/ofbiz-framework/trunk/framework/service/src/main/java/org/apache/ofbiz/service/ServiceDispatcher.java
    ofbiz/ofbiz-framework/trunk/framework/service/src/main/java/org/apache/ofbiz/service/engine/AbstractEngine.java

Modified: ofbiz/ofbiz-framework/trunk/framework/service/src/main/java/org/apache/ofbiz/service/ServiceDispatcher.java
URL: http://svn.apache.org/viewvc/ofbiz/ofbiz-framework/trunk/framework/service/src/main/java/org/apache/ofbiz/service/ServiceDispatcher.java?rev=1846783&r1=1846782&r2=1846783&view=diff
==============================================================================
--- ofbiz/ofbiz-framework/trunk/framework/service/src/main/java/org/apache/ofbiz/service/ServiceDispatcher.java (original)
+++ ofbiz/ofbiz-framework/trunk/framework/service/src/main/java/org/apache/ofbiz/service/ServiceDispatcher.java Sat Nov 17 08:57:19 2018
@@ -18,6 +18,7 @@
  *******************************************************************************/
 package org.apache.ofbiz.service;
 
+import java.util.Collections;
 import java.util.HashMap;
 import java.util.LinkedList;
 import java.util.List;
@@ -201,17 +202,28 @@ public class ServiceDispatcher {
         }
     }
 
+    /**
+     * Registers a callback by associating it to a service.
+     *
+     * @param serviceName the name of the service to associate the callback with
+     * @param cb the callback to register
+     */
     public synchronized void registerCallback(String serviceName, GenericServiceCallback cb) {
-        List<GenericServiceCallback> callBackList = callbacks.get(serviceName);
-        if (callBackList == null) {
-            callBackList = new LinkedList<>();
-        }
-        callBackList.add(cb);
-        callbacks.put(serviceName, callBackList);
+        callbacks.computeIfAbsent(serviceName, x -> new LinkedList<>()).add(cb);
     }
 
+    /**
+     * Provides a list of the enabled callbacks corresponding to a service.
+     *
+     * As a side effect, disabled callbacks are removed.
+     *
+     * @param serviceName the name of service whose callbacks should be called
+     * @return a list of callbacks corresponding to {@code serviceName}
+     */
     public List<GenericServiceCallback> getCallbacks(String serviceName) {
-        return callbacks.get(serviceName);
+        List<GenericServiceCallback> res = callbacks.getOrDefault(serviceName, Collections.emptyList());
+        res.removeIf(gsc -> !gsc.isEnabled());
+        return res;
     }
 
     /**

Modified: ofbiz/ofbiz-framework/trunk/framework/service/src/main/java/org/apache/ofbiz/service/engine/AbstractEngine.java
URL: http://svn.apache.org/viewvc/ofbiz/ofbiz-framework/trunk/framework/service/src/main/java/org/apache/ofbiz/service/engine/AbstractEngine.java?rev=1846783&r1=1846782&r2=1846783&view=diff
==============================================================================
--- ofbiz/ofbiz-framework/trunk/framework/service/src/main/java/org/apache/ofbiz/service/engine/AbstractEngine.java (original)
+++ ofbiz/ofbiz-framework/trunk/framework/service/src/main/java/org/apache/ofbiz/service/engine/AbstractEngine.java Sat Nov 17 08:57:19 2018
@@ -20,13 +20,11 @@ package org.apache.ofbiz.service.engine;
 
 import java.util.Collections;
 import java.util.HashMap;
-import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
 
 import org.apache.ofbiz.base.config.GenericConfigException;
 import org.apache.ofbiz.base.util.Debug;
-import org.apache.ofbiz.service.GenericServiceCallback;
 import org.apache.ofbiz.service.GenericServiceException;
 import org.apache.ofbiz.service.ModelService;
 import org.apache.ofbiz.service.ServiceDispatcher;
@@ -75,60 +73,27 @@ public abstract class AbstractEngine imp
         return model.location;
     }
 
-    /**
-     * @see org.apache.ofbiz.service.engine.GenericEngine#sendCallbacks(org.apache.ofbiz.service.ModelService, java.util.Map, int)
-     */
-    public void sendCallbacks(ModelService model, Map<String, Object> context, int mode) throws GenericServiceException {
-        if (!allowCallbacks(model, context, mode)) {
-            return;
-        }
-        List<GenericServiceCallback> callbacks = dispatcher.getCallbacks(model.name);
-        if (callbacks != null) {
-            Iterator<GenericServiceCallback> i = callbacks.iterator();
-            while (i.hasNext()) {
-                GenericServiceCallback gsc = i.next();
-                if (gsc.isEnabled()) {
-                    gsc.receiveEvent(context);
-                } else {
-                    i.remove();
-                }
-            }
+    @Override
+    public void sendCallbacks(ModelService model, Map<String, Object> context, int mode)
+            throws GenericServiceException {
+        if (allowCallbacks(model, context, mode)) {
+            dispatcher.getCallbacks(model.name).forEach(gsc -> gsc.receiveEvent(context));
         }
     }
 
-    public void sendCallbacks(ModelService model, Map<String, Object> context, Throwable t, int mode) throws GenericServiceException {
-        if (!allowCallbacks(model, context, mode)) {
-            return;
-        }
-        List<GenericServiceCallback> callbacks = dispatcher.getCallbacks(model.name);
-        if (callbacks != null) {
-            Iterator<GenericServiceCallback> i = callbacks.iterator();
-            while (i.hasNext()) {
-                GenericServiceCallback gsc = i.next();
-                if (gsc.isEnabled()) {
-                    gsc.receiveEvent(context,t);
-                } else {
-                    i.remove();
-                }
-            }
+    @Override
+    public void sendCallbacks(ModelService model, Map<String, Object> context, Throwable t, int mode)
+            throws GenericServiceException {
+        if (allowCallbacks(model, context, mode)) {
+            dispatcher.getCallbacks(model.name).forEach(gsc -> gsc.receiveEvent(context, t));
         }
     }
 
-    public void sendCallbacks(ModelService model, Map<String, Object> context, Map<String, Object> result, int mode) throws GenericServiceException {
-        if (!allowCallbacks(model, context, mode)) {
-            return;
-        }
-        List<GenericServiceCallback> callbacks = dispatcher.getCallbacks(model.name);
-        if (callbacks != null) {
-            Iterator<GenericServiceCallback> i = callbacks.iterator();
-            while (i.hasNext()) {
-                GenericServiceCallback gsc = i.next();
-                if (gsc.isEnabled()) {
-                    gsc.receiveEvent(context, result);
-                } else {
-                    i.remove();
-                }
-            }
+    @Override
+    public void sendCallbacks(ModelService model, Map<String, Object> context, Map<String, Object> result, int mode)
+            throws GenericServiceException {
+        if (allowCallbacks(model, context, mode)) {
+            dispatcher.getCallbacks(model.name).forEach(gsc -> gsc.receiveEvent(context, result));
         }
     }