svn commit: r1343643 - /ofbiz/trunk/specialpurpose/workflow/src/org/ofbiz/workflow/WfFactory.java

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

svn commit: r1343643 - /ofbiz/trunk/specialpurpose/workflow/src/org/ofbiz/workflow/WfFactory.java

jacopoc
Author: jacopoc
Date: Tue May 29 10:17:42 2012
New Revision: 1343643

URL: http://svn.apache.org/viewvc?rev=1343643&view=rev
Log:
Improved code that manages the cache:
 * removed unnecessary synchronization
 * replaced non atomic containsKey+put+get with get + putIfAbsentAndGet
 * protected the UtilCache object (static field) by making it private and final

Modified:
    ofbiz/trunk/specialpurpose/workflow/src/org/ofbiz/workflow/WfFactory.java

Modified: ofbiz/trunk/specialpurpose/workflow/src/org/ofbiz/workflow/WfFactory.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/specialpurpose/workflow/src/org/ofbiz/workflow/WfFactory.java?rev=1343643&r1=1343642&r2=1343643&view=diff
==============================================================================
--- ofbiz/trunk/specialpurpose/workflow/src/org/ofbiz/workflow/WfFactory.java (original)
+++ ofbiz/trunk/specialpurpose/workflow/src/org/ofbiz/workflow/WfFactory.java Tue May 29 10:17:42 2012
@@ -41,8 +41,8 @@ public class WfFactory {
 
     public static final String module = WfFactory.class.getName();
 
-    protected static UtilCache<String, WfProcessMgrImpl> wfProcessMgrCache = UtilCache.createUtilCache("workflow.processmgr");
-    protected static UtilCache<DispatchContext, WorkflowClient> wfClientCache = UtilCache.createUtilCache("workflow.client");
+    private static final UtilCache<String, WfProcessMgrImpl> wfProcessMgrCache = UtilCache.createUtilCache("workflow.processmgr");
+    private static final UtilCache<DispatchContext, WorkflowClient> wfClientCache = UtilCache.createUtilCache("workflow.client");
 
     /**
      * Creates a new {@link WfActivity} instance.
@@ -137,14 +137,11 @@ public class WfFactory {
         if (pid == null) throw new WfException("Workflow process id cannot be null");
 
         String key = delegator.getDelegatorName() + ":" + pkg + ":" + pkver + ":" + pid + ":" + pver;
-        if (!wfProcessMgrCache.containsKey(key)) {
-            synchronized (WfFactory.class) {
-                if (!wfProcessMgrCache.containsKey(key)) {
-                    wfProcessMgrCache.put(key, new WfProcessMgrImpl(delegator, pkg, pkver, pid, pver));
-                }
-            }
+        WfProcessMgrImpl wfProcessMgr = wfProcessMgrCache.get(key);
+        if (wfProcessMgr == null) {
+            wfProcessMgr = wfProcessMgrCache.putIfAbsentAndGet(key, new WfProcessMgrImpl(delegator, pkg, pkver, pid, pver));
         }
-        return wfProcessMgrCache.get(key);
+        return wfProcessMgr;
     }
 
     /**
@@ -194,13 +191,11 @@ public class WfFactory {
     }
 
     public static WorkflowClient getClient(DispatchContext dctx) {
-        if (!wfClientCache.containsKey(dctx)) {
-            synchronized (WfFactory.class) {
-                if (!wfClientCache.containsKey(dctx))
-                wfClientCache.put(dctx, new WorkflowClient(dctx));
-            }
+        WorkflowClient client = wfClientCache.get(dctx);
+        if (client == null) {
+            client = wfClientCache.putIfAbsentAndGet(dctx, new WorkflowClient(dctx));
         }
-        return wfClientCache.get(dctx);
+        return client;
     }
 
 }