[dna-commits] DNA SVN: r530 - in trunk/dna-jcr: src/main/java/org/jboss/dna/jcr and 1 other directories.

dna-commits at lists.jboss.org dna-commits at lists.jboss.org
Wed Sep 17 18:56:21 EDT 2008


Author: jverhaeg at redhat.com
Date: 2008-09-17 18:56:21 -0400 (Wed, 17 Sep 2008)
New Revision: 530

Modified:
   trunk/dna-jcr/pom.xml
   trunk/dna-jcr/src/main/java/org/jboss/dna/jcr/JcrRepository.java
   trunk/dna-jcr/src/main/java/org/jboss/dna/jcr/JcrSession.java
   trunk/dna-jcr/src/test/java/org/jboss/dna/jcr/JcrSessionTest.java
Log:
Changed UUID map implementations in session to use ReferenceMap from Google collections since it provides for soft references, which is what we want, rather than weak references.

Modified: trunk/dna-jcr/pom.xml
===================================================================
--- trunk/dna-jcr/pom.xml	2008-09-17 20:13:39 UTC (rev 529)
+++ trunk/dna-jcr/pom.xml	2008-09-17 22:56:21 UTC (rev 530)
@@ -100,5 +100,10 @@
       <groupId>org.apache.jackrabbit</groupId>
       <artifactId>jackrabbit-jcr-tests</artifactId>
     </dependency>
+    <dependency>
+      <groupId>com.google.code.google-collections</groupId>
+      <artifactId>google-collect</artifactId>
+      <version>snapshot-20080530</version>
+    </dependency>
   </dependencies>
 </project>
\ No newline at end of file

Modified: trunk/dna-jcr/src/main/java/org/jboss/dna/jcr/JcrRepository.java
===================================================================
--- trunk/dna-jcr/src/main/java/org/jboss/dna/jcr/JcrRepository.java	2008-09-17 20:13:39 UTC (rev 529)
+++ trunk/dna-jcr/src/main/java/org/jboss/dna/jcr/JcrRepository.java	2008-09-17 22:56:21 UTC (rev 530)
@@ -21,14 +21,12 @@
  */
 package org.jboss.dna.jcr;
 
-import java.lang.ref.WeakReference;
 import java.lang.reflect.Method;
 import java.security.AccessControlContext;
 import java.util.Collections;
 import java.util.HashMap;
 import java.util.Map;
 import java.util.UUID;
-import java.util.WeakHashMap;
 import javax.jcr.Credentials;
 import javax.jcr.LoginException;
 import javax.jcr.Node;
@@ -42,6 +40,8 @@
 import org.jboss.dna.spi.ExecutionContext;
 import org.jboss.dna.spi.ExecutionContextFactory;
 import org.jboss.dna.spi.connector.RepositoryConnectionFactory;
+import com.google.common.base.ReferenceType;
+import com.google.common.collect.ReferenceMap;
 
 /**
  * Creates JCR {@link Session sessions} to an underlying repository (which may be a federated repository).
@@ -259,6 +259,6 @@
         if (workspaceName == null) workspaceName = JcrI18n.defaultWorkspaceName.text();
         // Create session
         return new JcrSession(this, execContext, workspaceName, connectionFactory.createConnection(workspaceName),
-                              new WeakHashMap<UUID, WeakReference<Node>>());
+                              new ReferenceMap<UUID, Node>(ReferenceType.STRONG, ReferenceType.SOFT));
     }
 }

Modified: trunk/dna-jcr/src/main/java/org/jboss/dna/jcr/JcrSession.java
===================================================================
--- trunk/dna-jcr/src/main/java/org/jboss/dna/jcr/JcrSession.java	2008-09-17 20:13:39 UTC (rev 529)
+++ trunk/dna-jcr/src/main/java/org/jboss/dna/jcr/JcrSession.java	2008-09-17 22:56:21 UTC (rev 530)
@@ -23,15 +23,12 @@
 
 import java.io.InputStream;
 import java.io.OutputStream;
-import java.lang.ref.WeakReference;
 import java.security.AccessControlException;
 import java.security.Principal;
 import java.util.Calendar;
 import java.util.HashSet;
-import java.util.Map;
 import java.util.Set;
 import java.util.UUID;
-import java.util.WeakHashMap;
 import javax.jcr.Credentials;
 import javax.jcr.Item;
 import javax.jcr.Node;
@@ -61,6 +58,8 @@
 import org.jboss.dna.spi.graph.commands.GraphCommand;
 import org.jboss.dna.spi.graph.commands.impl.BasicGetNodeCommand;
 import org.xml.sax.ContentHandler;
+import com.google.common.base.ReferenceType;
+import com.google.common.collect.ReferenceMap;
 
 /**
  * @author John Verhaeg
@@ -72,8 +71,8 @@
     private final Repository repository;
     private final ExecutionContext executionContext;
     private RepositoryConnection connection;
-    private final Map<UUID, WeakReference<Node>> nodesByUuid;
-    private final Map<String, WeakReference<Node>> nodesByJcrUuid;
+    private final ReferenceMap<UUID, Node> nodesByUuid;
+    private final ReferenceMap<String, Node> nodesByJcrUuid;
     private boolean isLive;
     private Workspace workspace;
     private JcrRootNode rootNode;
@@ -82,7 +81,7 @@
                 ExecutionContext executionContext,
                 String workspaceName,
                 RepositoryConnection connection,
-                Map<UUID, WeakReference<Node>> nodesByUuid ) throws RepositoryException {
+                ReferenceMap<UUID, Node> nodesByUuid ) throws RepositoryException {
         assert repository != null;
         assert executionContext != null;
         assert workspaceName != null;
@@ -92,7 +91,7 @@
         this.executionContext = executionContext;
         this.connection = connection;
         this.nodesByUuid = nodesByUuid;
-        this.nodesByJcrUuid = new WeakHashMap<String, WeakReference<Node>>();
+        this.nodesByJcrUuid = new ReferenceMap<String, Node>(ReferenceType.STRONG, ReferenceType.SOFT);
         this.isLive = true;
         // Following must be initialized after session's state is initialized
         this.workspace = new JcrWorkspace(this, workspaceName);
@@ -315,8 +314,7 @@
     }
 
     Node getNode( UUID uuid ) {
-        WeakReference<Node> ref = nodesByUuid.get(uuid);
-        return (ref == null ? null : ref.get());
+        return nodesByUuid.get(uuid);
     }
 
     /**
@@ -554,12 +552,12 @@
             if (dnaUuidProp == null || !referenceable) uuid = UUID.randomUUID();
             else {
                 uuid = uuidFactory.create(dnaUuidProp.getValues()).next();
-                nodesByJcrUuid.put(uuid.toString(), new WeakReference<Node>(node));
+                nodesByJcrUuid.put(uuid.toString(), node);
             }
         }
         node.setInternalUuid(uuid);
         // Setup node to be retrieved by DNA UUID
-        nodesByUuid.put(new UUID(uuid.getMostSignificantBits(), uuid.getLeastSignificantBits()), new WeakReference<Node>(node));
+        nodesByUuid.put(uuid, node);
     }
 
     /**

Modified: trunk/dna-jcr/src/test/java/org/jboss/dna/jcr/JcrSessionTest.java
===================================================================
--- trunk/dna-jcr/src/test/java/org/jboss/dna/jcr/JcrSessionTest.java	2008-09-17 20:13:39 UTC (rev 529)
+++ trunk/dna-jcr/src/test/java/org/jboss/dna/jcr/JcrSessionTest.java	2008-09-17 22:56:21 UTC (rev 530)
@@ -29,13 +29,10 @@
 import static org.junit.Assert.assertThat;
 import static org.mockito.Mockito.stub;
 import java.io.InputStream;
-import java.lang.ref.WeakReference;
 import java.security.AccessControlException;
 import java.security.Principal;
 import java.util.Calendar;
 import java.util.Collections;
-import java.util.HashMap;
-import java.util.Map;
 import java.util.UUID;
 import javax.jcr.Item;
 import javax.jcr.NamespaceException;
@@ -59,7 +56,8 @@
 import org.junit.Test;
 import org.mockito.Mockito;
 import org.mockito.MockitoAnnotations;
-import org.mockito.MockitoAnnotations.Mock;
+import com.google.common.base.ReferenceType;
+import com.google.common.collect.ReferenceMap;
 
 /**
  * @author jverhaeg
@@ -91,12 +89,12 @@
     }
 
     private Session session;
-    @Mock
-    private Map<UUID, WeakReference<Node>> nodesByUuid;
+    private ReferenceMap<UUID, Node> nodesByUuid;
 
     @Before
     public void before() throws Exception {
         MockitoAnnotations.initMocks(this);
+        nodesByUuid = new ReferenceMap<UUID, Node>(ReferenceType.STRONG, ReferenceType.SOFT);
         session = repository.login();
     }
 
@@ -225,7 +223,7 @@
 
     @Test
     public void shouldProvideRootNode() throws Exception {
-        Map<UUID, WeakReference<Node>> nodesByUuid = new HashMap<UUID, WeakReference<Node>>();
+        ReferenceMap<UUID, Node> nodesByUuid = new ReferenceMap<UUID, Node>(ReferenceType.STRONG, ReferenceType.SOFT);
         Session session = new JcrSession(repository, executionContext, WORKSPACE_NAME,
                                          connectionFactory.createConnection(WORKSPACE_NAME), nodesByUuid);
         assertThat(nodesByUuid.isEmpty(), is(true));
@@ -233,9 +231,7 @@
         assertThat(root, notNullValue());
         UUID uuid = ((JcrRootNode)root).getInternalUuid();
         assertThat(uuid, notNullValue());
-        WeakReference<Node> ref = nodesByUuid.get(uuid);
-        assertThat(ref, notNullValue());
-        assertThat(ref.get(), is(root));
+        assertThat(nodesByUuid.get(uuid), is(root));
     }
 
     @Test




More information about the dna-commits mailing list