[dna-commits] DNA SVN: r576 - in trunk: dna-graph/src/main/java/org/jboss/dna/graph/properties and 2 other directories.

dna-commits at lists.jboss.org dna-commits at lists.jboss.org
Thu Oct 23 18:34:44 EDT 2008


Author: rhauch
Date: 2008-10-23 18:34:44 -0400 (Thu, 23 Oct 2008)
New Revision: 576

Added:
   trunk/dna-graph/src/main/java/org/jboss/dna/graph/properties/basic/BasicNamespace.java
Modified:
   trunk/dna-graph/src/main/java/org/jboss/dna/graph/connectors/BasicExecutionContextFactory.java
   trunk/dna-graph/src/main/java/org/jboss/dna/graph/properties/NamespaceRegistry.java
   trunk/dna-graph/src/main/java/org/jboss/dna/graph/properties/basic/BasicNamespaceRegistry.java
   trunk/dna-repository/src/main/java/org/jboss/dna/repository/util/JcrNamespaceRegistry.java
Log:
DNA-230 - Enhance NamespaceRegistry with additional functionality
http://jira.jboss.com/jira/browse/DNA-230

Added a method to the NamespaceRegistry interface to obtain a snapshot of the namespaces (prefix and namespace URI) in the registry.  Also changed an BasicExecutionContextFactory to use this method.

Modified: trunk/dna-graph/src/main/java/org/jboss/dna/graph/connectors/BasicExecutionContextFactory.java
===================================================================
--- trunk/dna-graph/src/main/java/org/jboss/dna/graph/connectors/BasicExecutionContextFactory.java	2008-10-23 18:57:40 UTC (rev 575)
+++ trunk/dna-graph/src/main/java/org/jboss/dna/graph/connectors/BasicExecutionContextFactory.java	2008-10-23 22:34:44 UTC (rev 576)
@@ -158,9 +158,8 @@
     }
 
     protected synchronized void initialize( ExecutionContext context ) {
-        for (String uri : this.defaultNamespaces.getRegisteredNamespaceUris()) {
-            String prefix = this.defaultNamespaces.getPrefixForNamespaceUri(uri, false);
-            context.getNamespaceRegistry().register(prefix, uri);
+        for (NamespaceRegistry.Namespace namespace : this.defaultNamespaces.getNamespaces()) {
+            context.getNamespaceRegistry().register(namespace.getPrefix(), namespace.getNamespaceUri());
         }
     }
 }

Modified: trunk/dna-graph/src/main/java/org/jboss/dna/graph/properties/NamespaceRegistry.java
===================================================================
--- trunk/dna-graph/src/main/java/org/jboss/dna/graph/properties/NamespaceRegistry.java	2008-10-23 18:57:40 UTC (rev 575)
+++ trunk/dna-graph/src/main/java/org/jboss/dna/graph/properties/NamespaceRegistry.java	2008-10-23 22:34:44 UTC (rev 576)
@@ -22,72 +22,88 @@
 package org.jboss.dna.graph.properties;
 
 import java.util.Set;
+import net.jcip.annotations.Immutable;
 import net.jcip.annotations.ThreadSafe;
 
 /**
  * Registry of namespaces, which are used to provide isolated and independent domains for {@link Name names}.
- *
+ * 
  * @author Randall Hauch
  */
 @ThreadSafe
 public interface NamespaceRegistry {
 
-	/**
-	 * Return the namespace URI that is currently mapped to the empty prefix, or null if there is no current default namespace.
-	 *
-	 * @return the namespace URI that represents the default namespace, or null if there is no default namespace
-	 */
-	String getDefaultNamespaceUri();
+    /**
+     * Return the namespace URI that is currently mapped to the empty prefix, or null if there is no current default namespace.
+     * 
+     * @return the namespace URI that represents the default namespace, or null if there is no default namespace
+     */
+    String getDefaultNamespaceUri();
 
-	/**
-	 * Get the namespace URI for the supplied prefix.
-	 *
-	 * @param prefix the namespace prefix
-	 * @return the namespace URI for the supplied prefix, or null if there is no namespace currently registered to use that prefix
-	 * @throws IllegalArgumentException if the prefix is null
-	 */
-	String getNamespaceForPrefix( String prefix );
+    /**
+     * Get the namespace URI for the supplied prefix.
+     * 
+     * @param prefix the namespace prefix
+     * @return the namespace URI for the supplied prefix, or null if there is no namespace currently registered to use that prefix
+     * @throws IllegalArgumentException if the prefix is null
+     */
+    String getNamespaceForPrefix( String prefix );
 
-	/**
-	 * Return the prefix used for the supplied namespace URI.
-	 *
-	 * @param namespaceUri the namespace URI
-	 * @param generateIfMissing true if the namespace URI has not already been registered and the method should auto-register the
-	 *        namespace with a generated prefix, or false if the method should never auto-register the namespace
-	 * @return the prefix currently being used for the namespace, or <code>null</code> if the namespace has not been registered
-	 *         and <code>generateIfMissing</code> is <code>false</code>
-	 * @throws IllegalArgumentException if the namespace URI is null
-	 * @see #isRegisteredNamespaceUri(String)
-	 */
-	String getPrefixForNamespaceUri( String namespaceUri,
-	                                 boolean generateIfMissing );
+    /**
+     * Return the prefix used for the supplied namespace URI.
+     * 
+     * @param namespaceUri the namespace URI
+     * @param generateIfMissing true if the namespace URI has not already been registered and the method should auto-register the
+     *        namespace with a generated prefix, or false if the method should never auto-register the namespace
+     * @return the prefix currently being used for the namespace, or <code>null</code> if the namespace has not been registered
+     *         and <code>generateIfMissing</code> is <code>false</code>
+     * @throws IllegalArgumentException if the namespace URI is null
+     * @see #isRegisteredNamespaceUri(String)
+     */
+    String getPrefixForNamespaceUri( String namespaceUri,
+                                     boolean generateIfMissing );
 
-	/**
-	 * Return whether there is a registered prefix for the supplied namespace URI.
-	 *
-	 * @param namespaceUri the namespace URI
-	 * @return true if the supplied namespace has been registered with a prefix, or false otherwise
-	 * @throws IllegalArgumentException if the namespace URI is null
-	 */
-	boolean isRegisteredNamespaceUri( String namespaceUri );
+    /**
+     * Return whether there is a registered prefix for the supplied namespace URI.
+     * 
+     * @param namespaceUri the namespace URI
+     * @return true if the supplied namespace has been registered with a prefix, or false otherwise
+     * @throws IllegalArgumentException if the namespace URI is null
+     */
+    boolean isRegisteredNamespaceUri( String namespaceUri );
 
-	/**
-	 * Register a new namespace using the supplied prefix, returning the namespace URI previously registered under that prefix.
-	 *
-	 * @param prefix the prefix for the namespace, or null if a namesapce prefix should be generated automatically
-	 * @param namespaceUri the namespace URI
-	 * @return the namespace URI that was previously registered with the supplied prefix, or null if the prefix was not previously
-	 *         bound to a namespace URI
-	 * @throws IllegalArgumentException if the namespace URI is null
-	 */
-	String register( String prefix,
-	                 String namespaceUri );
+    /**
+     * Register a new namespace using the supplied prefix, returning the namespace URI previously registered under that prefix.
+     * 
+     * @param prefix the prefix for the namespace, or null if a namesapce prefix should be generated automatically
+     * @param namespaceUri the namespace URI
+     * @return the namespace URI that was previously registered with the supplied prefix, or null if the prefix was not previously
+     *         bound to a namespace URI
+     * @throws IllegalArgumentException if the namespace URI is null
+     */
+    String register( String prefix,
+                     String namespaceUri );
 
-	/**
-	 * Obtain the set of namespaces that are registered.
-	 *
-	 * @return the set of
-	 */
-	Set<String> getRegisteredNamespaceUris();
+    /**
+     * Obtain the set of namespaces that are registered.
+     * 
+     * @return the set of
+     */
+    Set<String> getRegisteredNamespaceUris();
 
+    /**
+     * Obtain a snapshot of all of the {@link Namespace namespaces} registered at the time this method is called. The resulting
+     * set is immutable, and will <i>not</i> reflect changes made to the registry.
+     * 
+     * @return an immutable set of {@link Namespace} objects reflecting a snapshot of the registry; never null
+     */
+    Set<Namespace> getNamespaces();
+
+    @Immutable
+    interface Namespace {
+        String getPrefix();
+
+        String getNamespaceUri();
+    }
+
 }

Added: trunk/dna-graph/src/main/java/org/jboss/dna/graph/properties/basic/BasicNamespace.java
===================================================================
--- trunk/dna-graph/src/main/java/org/jboss/dna/graph/properties/basic/BasicNamespace.java	                        (rev 0)
+++ trunk/dna-graph/src/main/java/org/jboss/dna/graph/properties/basic/BasicNamespace.java	2008-10-23 22:34:44 UTC (rev 576)
@@ -0,0 +1,104 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2008, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors. 
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.dna.graph.properties.basic;
+
+import net.jcip.annotations.Immutable;
+import org.jboss.dna.graph.properties.NamespaceRegistry;
+import org.jboss.dna.graph.properties.NamespaceRegistry.Namespace;
+
+/**
+ * @author Randall Hauch
+ */
+ at Immutable
+public class BasicNamespace implements NamespaceRegistry.Namespace {
+    private final String prefix;
+    private final String namespaceUri;
+
+    /**
+     * Create a namespace instance.
+     * 
+     * @param prefix the namespace prefix; may not be null (this is not checked)
+     * @param namespaceUri the namespace URI; may not be null (this is not checked)
+     */
+    public BasicNamespace( String prefix,
+                           String namespaceUri ) {
+        assert prefix != null;
+        assert namespaceUri != null;
+        this.prefix = prefix;
+        this.namespaceUri = namespaceUri;
+    }
+
+    /**
+     * {@inheritDoc}
+     * 
+     * @see org.jboss.dna.graph.properties.NamespaceRegistry.Namespace#getNamespaceUri()
+     */
+    public String getNamespaceUri() {
+        return namespaceUri;
+    }
+
+    /**
+     * {@inheritDoc}
+     * 
+     * @see org.jboss.dna.graph.properties.NamespaceRegistry.Namespace#getPrefix()
+     */
+    public String getPrefix() {
+        return prefix;
+    }
+
+    /**
+     * {@inheritDoc}
+     * 
+     * @see java.lang.Object#hashCode()
+     */
+    @Override
+    public int hashCode() {
+        return namespaceUri.hashCode();
+    }
+
+    /**
+     * {@inheritDoc}
+     * 
+     * @see java.lang.Object#equals(java.lang.Object)
+     */
+    @Override
+    public boolean equals( Object obj ) {
+        if (obj == this) return true;
+        if (obj instanceof Namespace) {
+            Namespace that = (Namespace)obj;
+            if (!this.namespaceUri.equals(that.getNamespaceUri())) return false;
+            // if (!this.prefix.equals(that.getPrefix())) return false;
+            return true;
+        }
+        return false;
+    }
+
+    /**
+     * {@inheritDoc}
+     * 
+     * @see java.lang.Object#toString()
+     */
+    @Override
+    public String toString() {
+        return prefix + ":" + namespaceUri;
+    }
+}


Property changes on: trunk/dna-graph/src/main/java/org/jboss/dna/graph/properties/basic/BasicNamespace.java
___________________________________________________________________
Name: svn:mime-type
   + text/plain

Modified: trunk/dna-graph/src/main/java/org/jboss/dna/graph/properties/basic/BasicNamespaceRegistry.java
===================================================================
--- trunk/dna-graph/src/main/java/org/jboss/dna/graph/properties/basic/BasicNamespaceRegistry.java	2008-10-23 18:57:40 UTC (rev 575)
+++ trunk/dna-graph/src/main/java/org/jboss/dna/graph/properties/basic/BasicNamespaceRegistry.java	2008-10-23 22:34:44 UTC (rev 576)
@@ -209,6 +209,25 @@
         return Collections.unmodifiableSet(result);
     }
 
+    /**
+     * {@inheritDoc}
+     * 
+     * @see org.jboss.dna.graph.properties.NamespaceRegistry#getNamespaces()
+     */
+    public Set<Namespace> getNamespaces() {
+        Set<Namespace> result = new HashSet<Namespace>();
+        Lock lock = this.registryLock.readLock();
+        try {
+            lock.lock();
+            for (Map.Entry<String, String> entry : this.namespacesByPrefix.entrySet()) {
+                result.add(new BasicNamespace(entry.getKey(), entry.getValue()));
+            }
+        } finally {
+            lock.unlock();
+        }
+        return Collections.unmodifiableSet(result);
+    }
+
     @GuardedBy( "registryLock" )
     protected String generatePrefix() {
         DecimalFormat formatter = new DecimalFormat(this.generatedPrefixTemplate);

Modified: trunk/dna-repository/src/main/java/org/jboss/dna/repository/util/JcrNamespaceRegistry.java
===================================================================
--- trunk/dna-repository/src/main/java/org/jboss/dna/repository/util/JcrNamespaceRegistry.java	2008-10-23 18:57:40 UTC (rev 575)
+++ trunk/dna-repository/src/main/java/org/jboss/dna/repository/util/JcrNamespaceRegistry.java	2008-10-23 22:34:44 UTC (rev 576)
@@ -29,6 +29,7 @@
 import org.jboss.dna.common.util.CheckArg;
 import org.jboss.dna.graph.properties.NamespaceException;
 import org.jboss.dna.graph.properties.NamespaceRegistry;
+import org.jboss.dna.graph.properties.basic.BasicNamespace;
 
 /**
  * @author Randall Hauch
@@ -38,7 +39,8 @@
     private final String repositoryWorkspaceName;
     private final SessionFactory sessionFactory;
 
-    public JcrNamespaceRegistry( SessionFactory sessionFactory, String repositoryWorkspaceName ) {
+    public JcrNamespaceRegistry( SessionFactory sessionFactory,
+                                 String repositoryWorkspaceName ) {
         CheckArg.isNotNull(sessionFactory, "sessionFactory");
         CheckArg.isNotNull(repositoryWorkspaceName, "repositoryWorkspaceName");
         this.repositoryWorkspaceName = repositoryWorkspaceName;
@@ -82,7 +84,8 @@
     /**
      * {@inheritDoc}
      */
-    public String getPrefixForNamespaceUri( String namespaceUri, boolean generateIfMissing ) {
+    public String getPrefixForNamespaceUri( String namespaceUri,
+                                            boolean generateIfMissing ) {
         Session session = null;
         try {
             session = this.sessionFactory.createSession(this.repositoryWorkspaceName);
@@ -119,7 +122,8 @@
     /**
      * {@inheritDoc}
      */
-    public String register( String prefix, String namespaceUri ) {
+    public String register( String prefix,
+                            String namespaceUri ) {
         String previousNamespaceUriForPrefix = null;
         Session session = null;
         try {
@@ -159,4 +163,29 @@
         }
     }
 
+    /**
+     * {@inheritDoc}
+     * 
+     * @see org.jboss.dna.graph.properties.NamespaceRegistry#getNamespaces()
+     */
+    public Set<Namespace> getNamespaces() {
+        Session session = null;
+        try {
+            session = this.sessionFactory.createSession(this.repositoryWorkspaceName);
+            javax.jcr.NamespaceRegistry registry = session.getWorkspace().getNamespaceRegistry();
+            Set<Namespace> result = new HashSet<Namespace>();
+            for (String uri : registry.getURIs()) {
+                String prefix = registry.getPrefix(uri);
+                result.add(new BasicNamespace(prefix, uri));
+            }
+            return Collections.unmodifiableSet(result);
+        } catch (RepositoryException e) {
+            throw new NamespaceException(e);
+        } finally {
+            if (session != null) {
+                session.logout();
+            }
+        }
+    }
+
 }




More information about the dna-commits mailing list