incubator-graffito-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From clomb...@apache.org
Subject svn commit: r426267 - /incubator/graffito/trunk/jcr/spring/src/java/org/apache/portals/graffito/jcr/spring/JackrabbitSessionFactory.java
Date Thu, 27 Jul 2006 22:02:47 GMT
Author: clombart
Date: Thu Jul 27 15:02:47 2006
New Revision: 426267

URL: http://svn.apache.org/viewvc?rev=426267&view=rev
Log:
Apply patch provided by Jukka for issue GRFT-94

Modified:
    incubator/graffito/trunk/jcr/spring/src/java/org/apache/portals/graffito/jcr/spring/JackrabbitSessionFactory.java

Modified: incubator/graffito/trunk/jcr/spring/src/java/org/apache/portals/graffito/jcr/spring/JackrabbitSessionFactory.java
URL: http://svn.apache.org/viewvc/incubator/graffito/trunk/jcr/spring/src/java/org/apache/portals/graffito/jcr/spring/JackrabbitSessionFactory.java?rev=426267&r1=426266&r2=426267&view=diff
==============================================================================
--- incubator/graffito/trunk/jcr/spring/src/java/org/apache/portals/graffito/jcr/spring/JackrabbitSessionFactory.java
(original)
+++ incubator/graffito/trunk/jcr/spring/src/java/org/apache/portals/graffito/jcr/spring/JackrabbitSessionFactory.java
Thu Jul 27 15:02:47 2006
@@ -16,8 +16,9 @@
  */
 package org.apache.portals.graffito.jcr.spring;
 
+import javax.jcr.NamespaceException;
+import javax.jcr.NamespaceRegistry;
 import javax.jcr.RepositoryException;
-import javax.jcr.Session;
 
 import org.apache.portals.graffito.jcr.repository.RepositoryUtil;
 import org.springmodules.jcr.JcrSessionFactory;
@@ -38,34 +39,45 @@
 	 * @param session
 	 * @throws RepositoryException
 	 */
-	protected void registerNamespaces() throws RepositoryException {
+    protected void registerNamespaces() throws RepositoryException {
+        NamespaceRegistry registry =
+            getSession().getWorkspace().getNamespaceRegistry();
 
-		Session session = getSession();
-        try
-        {
-           String[] jcrNamespaces = session.getWorkspace().getNamespaceRegistry().getPrefixes();
-           boolean createGraffitoNamespace = true;
-           for (int i = 0; i < jcrNamespaces.length; i++)
-           {
-               if (jcrNamespaces[i].equals(RepositoryUtil.GRAFFITO_NAMESPACE_PREFIX))
-               {
-                   createGraffitoNamespace = false;
-                  
-               }
-           }
-            
-           if (createGraffitoNamespace)
-           {
-               session.getWorkspace().getNamespaceRegistry().registerNamespace(RepositoryUtil.GRAFFITO_NAMESPACE_PREFIX,RepositoryUtil.GRAFFITO_NAMESPACE);
              
-           }
-           
-       }
-       catch (Exception e)
-       {
-           throw new RepositoryException(e.getMessage());
-       }
-       
-       super.registerNamespaces();
-	}
+        // Keep trying until the Graffito namespace has been registered
+        int n = 0;
+        String prefix = null;
+        while (prefix == null) {
+            try {
+                // Is the Graffito namespace registered?
+                prefix = registry.getPrefix(RepositoryUtil.GRAFFITO_NAMESPACE);
+            } catch (NamespaceException e1) {
+                // No, try to register it with the default prefix
+                prefix = RepositoryUtil.GRAFFITO_NAMESPACE_PREFIX;
+                // ... and a sequence number if the first attempt failed 
+                if (n++ > 0) {
+                    prefix = prefix + n;
+                }
+                try {
+                    // Is this prefix registered to the Graffito namespace?
+                    if (!RepositoryUtil.GRAFFITO_NAMESPACE.equals(
+                            registry.getURI(prefix))) {
+                        // No, but it *is* registered. Try the next prefix...
+                        prefix = null;
+                    }
+                } catch (NamespaceException e2) {
+                    try {
+                        // No, and it's not registered. Try registering it:
+                        registry.registerNamespace(
+                                prefix, RepositoryUtil.GRAFFITO_NAMESPACE);
+                    } catch (NamespaceException e3) {
+                        // Registration failed. Try the next prefix...
+                        prefix = null;
+                    }
+                }
+            }
+        }
+
+        super.registerNamespaces();
+    }
 
 }



Mime
View raw message