Author: jonathan
Date: Sat Jul 19 00:29:39 2008
New Revision: 19619
URL: http://wso2.org/svn/browse/wso2?view=rev&revision=19619

Log:
MASHUP-990: Added simple (and only 99.99% foolproof) semaphore to prevent 
conflicting reads and writes of the database file.

Modified:
   trunk/mashup/java/modules/samples/feedCache/feedCache.js

Modified: trunk/mashup/java/modules/samples/feedCache/feedCache.js
URL: 
http://wso2.org/svn/browse/wso2/trunk/mashup/java/modules/samples/feedCache/feedCache.js?rev=19619&r1=19618&r2=19619&view=diff
==============================================================================
--- trunk/mashup/java/modules/samples/feedCache/feedCache.js    (original)
+++ trunk/mashup/java/modules/samples/feedCache/feedCache.js    Sat Jul 19 
00:29:39 2008
@@ -22,6 +22,7 @@
     // look for stuff to prefetch every 5 min
     system.setInterval(fetch, 5*60*1000);
 }
+this.scope = "application";
 
 cacheSettings.documentation = <div>Set the caching properties for a feed.
                                 <b>prefetch</b> determines whether the feed 
should be fetched regularly in the background (true), or only on demand (false).
@@ -30,27 +31,39 @@
 cacheSettings.inputTypes = {"feedUrl" : "xs:anyURI", "lifespan" : "number", 
"prefetch" : "boolean"};
 cacheSettings.outputType = "boolean";
 function cacheSettings(feedUrl, lifespan, prefetch) {
-    // read database.
-    var cachedb = readXML("db");
-    if (cachedb == null) cachedb = <cache/>;
+    // check for, then place a lock on the database.
+    while (session.get("semaphore") == "true") {
+        system.wait();
+    }
+    session.put("semaphore", "true");
+
+    try {
+        // read database
+        var cachedb = readXML("db");
+        if (cachedb == null) cachedb = <cache/>;
+
+        // if this url isn't being tracked in the cache, add it to the cache 
with default settings
+        if (cachedb.feed.(@url == feedUrl).length() == 0) {
+            cachedb.appendChild(<feed url={feedUrl} prefetch="false" 
lifespan={24*60*60*1000} timestamp="0" />);
+        }
 
-    // if this url isn't being tracked in the cache, add it to the cache with 
default settings
-    if (cachedb.feed.(@url == feedUrl).length() == 0) {
-        cachedb.appendChild(<feed url={feedUrl} prefetch="false" 
lifespan={24*60*60*1000} timestamp="0" />);
-    }
+        // find the settings for the requested feed
+        var feedInfo = cachedb.feed.(@url == feedUrl);
 
-    // find the settings for the requested feed
-    var feedInfo = cachedb.feed.(@url == feedUrl);
+        // record the prefetch and lifespam settings.  Only change the 
automatic refresh interval if it's shorter than someone else has asked for.
+        [EMAIL PROTECTED] = prefetch;
+        if (prefetch && parseInt([EMAIL PROTECTED]) > lifespan) {
+            [EMAIL PROTECTED] = lifespan;
+        }
 
-    // record the prefetch and lifespam settings.  Only change the automatic 
refresh interval if it's shorter than someone else has asked for.
-    [EMAIL PROTECTED] = prefetch;
-    if (prefetch && parseInt([EMAIL PROTECTED]) > lifespan) {
-        [EMAIL PROTECTED] = lifespan;
+        // save the database
+        writeXML("db", cachedb);
+        session.put("semaphore", "false");
+
+    } catch (e) {
+        session.put("semaphore", "false");
+        throw (e);
     }
-
-    // save the database
-    writeXML("db", cachedb);
-
     // force an immediate cache refresh.  This should execute the prefetch on 
the new feed item (and any others that are stale)
     system.setTimeout(fetch, 1);
 
@@ -63,36 +76,49 @@
 feedReference.outputType = "xs:anyURI";
 feedReference.safe = true;
 function feedReference(feedUrl) {
-    // read database.
-    var cachedb = readXML("db");
-    if (cachedb == null) cachedb = <cache/>;
+    // check for, then place a lock on the database.
+    while (session.get("semaphore") == "true") {
+        system.wait();
+    }
+    session.put("semaphore", "true");
+
+    try {
+        // read database.
+        var cachedb = readXML("db");
+        if (cachedb == null) cachedb = <cache/>;
+
+        // if this url isn't being tracked in the cache, add it to the cache 
with default settings
+        if (cachedb.feed.(@url == feedUrl).length() == 0) {
+            cachedb.appendChild(<feed url={feedUrl} prefetch="false" 
lifespan={24*60*60*1000} timestamp="0" />);
+        }
+
+        // find the settings for the requested feed
+        var feedInfo = cachedb.feed.(@url == feedUrl);
 
-    // if this url isn't being tracked in the cache, add it to the cache with 
default settings
-    if (cachedb.feed.(@url == feedUrl).length() == 0) {
-        cachedb.appendChild(<feed url={feedUrl} prefetch="false" 
lifespan={24*60*60*1000} timestamp="0" />);
-    }
-
-    // find the settings for the requested feed
-    var feedInfo = cachedb.feed.(@url == feedUrl);
-
-    var cacheURL = system.wwwURL + "/" + escapePathSegment(feedUrl) + ".xml";
-    var now = new Date().valueOf();
-    // if cache is stale, refresh it
-    if (parseInt([EMAIL PROTECTED]) + parseInt([EMAIL PROTECTED]) < now) {
-        // record the attempt.  Somebody might make use of this for diagnostic 
information.
-        [EMAIL PROTECTED] = now;
-        try {
-            var feed = system.getXML(feedUrl);
-            writeXML(feedUrl, feed);
-            [EMAIL PROTECTED] = now;
-        } catch (e) {
-            // if the feed is unfetchable, return the url the user gave us and 
let him deal with it
-            cacheURL = feedUrl;
+        var cacheURL = system.wwwURL + "/" + escapePathSegment(feedUrl) + 
".xml";
+        var now = new Date().valueOf();
+        // if cache is stale, refresh it
+        if (parseInt([EMAIL PROTECTED]) + parseInt([EMAIL PROTECTED]) < now) {
+            // record the attempt.  Somebody might make use of this for 
diagnostic information.
+            [EMAIL PROTECTED] = now;
+            try {
+                var feed = system.getXML(feedUrl);
+                writeXML(feedUrl, feed);
+                [EMAIL PROTECTED] = now;
+            } catch (e) {
+                // if the feed is unfetchable, return the url the user gave us 
and let him deal with it
+                cacheURL = feedUrl;
+            }
         }
-    }
 
-    // save the database
-    writeXML("db", cachedb);
+        // save the database
+        writeXML("db", cachedb);
+        session.put("semaphore", "false");
+
+    } catch (e) {
+        session.put("semaphore", "false");
+        throw (e);
+    }
 
     return cacheURL;
 }

_______________________________________________
Mashup-dev mailing list
[email protected]
http://wso2.org/cgi-bin/mailman/listinfo/mashup-dev

Reply via email to