Author: ssmaeklu
Date: 2009-01-22 11:31:40 +0100 (Thu, 22 Jan 2009)
New Revision: 7161

Modified:
   
branches/2.17/war/src/main/java/no/sesat/search/http/urlrewrite/UrlRewriterContainerFactory.java
Log:
Improved locking. Fixes problems with rewrite URLs not being found.

Modified: 
branches/2.17/war/src/main/java/no/sesat/search/http/urlrewrite/UrlRewriterContainerFactory.java
===================================================================
--- 
branches/2.17/war/src/main/java/no/sesat/search/http/urlrewrite/UrlRewriterContainerFactory.java
    2009-01-20 22:47:36 UTC (rev 7160)
+++ 
branches/2.17/war/src/main/java/no/sesat/search/http/urlrewrite/UrlRewriterContainerFactory.java
    2009-01-22 10:31:40 UTC (rev 7161)
@@ -1,4 +1,4 @@
-/* Copyright (2006-2007) Schibsted Søk AS
+/* Copyright (2006-2009) Schibsted Søk AS
  * This file is part of SESAT.
  *
  *   SESAT is free software: you can redistribute it and/or modify
@@ -13,7 +13,6 @@
  *
  *   You should have received a copy of the GNU Affero General Public License
  *   along with SESAT.  If not, see <http://www.gnu.org/licenses/>.
-
  */
 /*
  * UrlRewriterContainerFactory.java
@@ -97,25 +96,34 @@
 
         final Site site = cxt.getSite();
         assert null != site;
-        
-        UrlRewriterContainerFactory instance;
-        try{
+
+        try {
             INSTANCES_LOCK.readLock().lock();
-            instance = INSTANCES.get(site);
-        }finally{
+            if (!INSTANCES.containsKey(site)) {
+                // It is not possible to upgrade a read lock...
+                INSTANCES_LOCK.readLock().unlock();
+                INSTANCES_LOCK.writeLock().lock();
+                try {
+                    // ...so check the condition again.
+                    if (!INSTANCES.containsKey(site)) {
+                        INSTANCES.put(site, new 
UrlRewriterContainerFactory(cxt));
+                    }
+                } catch (ParserConfigurationException e) {
+                    LOG.error(ERR_DOC_BUILDER_CREATION, e);
+                } finally {
+                    // Downgrade to a read lock.
+                    INSTANCES_LOCK.readLock().lock();
+                    INSTANCES_LOCK.writeLock().unlock();
+                }
+            }
+            return INSTANCES.get(site);
+        } finally {
+            // Finally release the read lock.
             INSTANCES_LOCK.readLock().unlock();
         }
-
-        if (instance == null) {
-            try {
-                instance = new UrlRewriterContainerFactory(cxt);
-            } catch (ParserConfigurationException ex) {
-                LOG.error(ERR_DOC_BUILDER_CREATION,ex);
-            }
-        }
-        return instance;
     }
 
+
     /** Remove the factory in use for the skin defined within the context. **/
     public boolean remove(final Site site){
 
@@ -131,29 +139,16 @@
 
     /** Creates a new instance of ViewFactory */
     private UrlRewriterContainerFactory(final Context cxt) throws 
ParserConfigurationException {
+        context = cxt;
 
-        LOG.trace("UrlRewriterContainerFactory(cxt)");
-        try{
-            INSTANCES_LOCK.writeLock().lock();
+        // configuration files
+        final DocumentBuilderFactory factory = 
DocumentBuilderFactory.newInstance();
+        factory.setValidating(false);
+        final DocumentBuilder builder = factory.newDocumentBuilder();
+        loader = context.newDocumentLoader(cxt, URLREWRITE_XMLFILE, builder);
 
-            context = cxt;
-
-            // configuration files
-            final DocumentBuilderFactory factory = 
DocumentBuilderFactory.newInstance();
-            factory.setValidating(false);
-            final DocumentBuilder builder = factory.newDocumentBuilder();
-            loader = context.newDocumentLoader(cxt, URLREWRITE_XMLFILE, 
builder);
-
-            // start initialisation
-            urlRewriterContainer = initUrlRewriterContainer();
-
-            // update the store of factories
-            INSTANCES.put(context.getSite(), this);
-
-        }finally{
-            INSTANCES_LOCK.writeLock().unlock();
-        }
-
+        // start initialisation
+        urlRewriterContainer = initUrlRewriterContainer();
     }
 
     // Public --------------------------------------------------------

_______________________________________________
Kernel-commits mailing list
[email protected]
http://sesat.no/mailman/listinfo/kernel-commits

Reply via email to