Author: jacopoc
Date: Tue May 29 13:46:49 2012
New Revision: 1343724

URL: http://svn.apache.org/viewvc?rev=1343724&view=rev
Log:
Improved code that manages the cache:
* protected the UtilCache object (static field) by making it private and final
* removed unnecessary synchronization


Modified:
    ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/event/BsfEventHandler.java

Modified: 
ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/event/BsfEventHandler.java
URL: 
http://svn.apache.org/viewvc/ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/event/BsfEventHandler.java?rev=1343724&r1=1343723&r2=1343724&view=diff
==============================================================================
--- 
ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/event/BsfEventHandler.java 
(original)
+++ 
ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/event/BsfEventHandler.java 
Tue May 29 13:46:49 2012
@@ -43,7 +43,7 @@ import org.ofbiz.webapp.control.ConfigXM
 public class BsfEventHandler implements EventHandler {
 
     public static final String module = BsfEventHandler.class.getName();
-    public static UtilCache<String, String> eventCache = 
UtilCache.createUtilCache("webapp.BsfEvents");
+    private static final UtilCache<String, String> eventCache = 
UtilCache.createUtilCache("webapp.BsfEvents");
 
     /**
      * @see 
org.ofbiz.webapp.event.EventHandler#init(javax.servlet.ServletContext)
@@ -85,38 +85,30 @@ public class BsfEventHandler implements 
                 cacheName = event.invoke;
                 scriptString = eventCache.get(cacheName);
                 if (scriptString == null) {
-                    synchronized(eventCache) {
-                        if (scriptString == null) {
-                            if (Debug.verboseOn()) {
-                                Debug.logVerbose("Loading BSF Script at 
location: " + cacheName, module);
-                            }
-                            URL scriptUrl = 
FlexibleLocation.resolveLocation(cacheName);
-                            if (scriptUrl == null) {
-                                throw new EventHandlerException("BSF script 
not found at location [" + cacheName + "]");
-                            }
-                            scriptStream = scriptUrl.openStream();
-                            scriptString = IOUtils.getStringFromReader(new 
InputStreamReader(scriptStream));
-                            scriptStream.close();
-                            eventCache.put(cacheName, scriptString);
-                        }
+                    if (Debug.verboseOn()) {
+                        Debug.logVerbose("Loading BSF Script at location: " + 
cacheName, module);
                     }
+                    URL scriptUrl = 
FlexibleLocation.resolveLocation(cacheName);
+                    if (scriptUrl == null) {
+                        throw new EventHandlerException("BSF script not found 
at location [" + cacheName + "]");
+                    }
+                    scriptStream = scriptUrl.openStream();
+                    scriptString = IOUtils.getStringFromReader(new 
InputStreamReader(scriptStream));
+                    scriptStream.close();
+                    scriptString = eventCache.putIfAbsentAndGet(cacheName, 
scriptString);
                 }
             } else {
                 // we are a script in the webapp - load by resource
                 cacheName = context.getServletContextName() + ":" + event.path 
+ event.invoke;
                 scriptString = eventCache.get(cacheName);
                 if (scriptString == null) {
-                    synchronized(eventCache) {
-                        if (scriptString == null) {
-                            scriptStream = 
context.getResourceAsStream(event.path + event.invoke);
-                            if (scriptStream == null) {
-                                throw new EventHandlerException("Could not 
find BSF script file in webapp context: " + event.path + event.invoke);
-                            }
-                            scriptString = IOUtils.getStringFromReader(new 
InputStreamReader(scriptStream));
-                            scriptStream.close();
-                            eventCache.put(cacheName, scriptString);
-                        }
+                    scriptStream = context.getResourceAsStream(event.path + 
event.invoke);
+                    if (scriptStream == null) {
+                        throw new EventHandlerException("Could not find BSF 
script file in webapp context: " + event.path + event.invoke);
                     }
+                    scriptString = IOUtils.getStringFromReader(new 
InputStreamReader(scriptStream));
+                    scriptStream.close();
+                    scriptString = eventCache.putIfAbsentAndGet(cacheName, 
scriptString);
                 }
             }
 


Reply via email to