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

dna-commits at lists.jboss.org dna-commits at lists.jboss.org
Thu Nov 26 15:37:08 EST 2009


Author: rhauch
Date: 2009-11-26 15:37:08 -0500 (Thu, 26 Nov 2009)
New Revision: 1359

Added:
   trunk/dna-graph/src/test/java/org/jboss/dna/graph/ExecutionContextTest.java
Modified:
   trunk/dna-graph/src/main/java/org/jboss/dna/graph/ExecutionContext.java
   trunk/dna-graph/src/main/java/org/jboss/dna/graph/observe/Changes.java
   trunk/dna-graph/src/main/java/org/jboss/dna/graph/request/processor/RequestProcessor.java
Log:
DNA-548 The ExecutionContext serves as the universal mechanism by which the DNA code knows the context in which operations are performed, and is available to the connectors that process the requests, the session that creates the requests, and the Observation system that post-processes the (complete) requests. The JcrRepository.login(...) method always creates a new ExecutionContext via the ExecutionContext.with(...), which means that every JcrSession implementation has a unique ExecutionContext. Therefore, if we add a unique identifier to the ExecutionContext, that identifier would correspond to the Session instance.

This commit adds this identifier to the ExecutionContext, and adds a 'contextId' to the Changes class.  Then, the RequestProcessor was changed to set the context ID for the Changes object that it creates.

Modified: trunk/dna-graph/src/main/java/org/jboss/dna/graph/ExecutionContext.java
===================================================================
--- trunk/dna-graph/src/main/java/org/jboss/dna/graph/ExecutionContext.java	2009-11-26 00:36:08 UTC (rev 1358)
+++ trunk/dna-graph/src/main/java/org/jboss/dna/graph/ExecutionContext.java	2009-11-26 20:37:08 UTC (rev 1359)
@@ -25,6 +25,7 @@
 
 import java.security.AccessControlContext;
 import java.security.AccessController;
+import java.util.UUID;
 import javax.security.auth.login.LoginException;
 import net.jcip.annotations.Immutable;
 import org.jboss.dna.common.component.ClassLoaderFactory;
@@ -65,6 +66,8 @@
     private final NamespaceRegistry namespaceRegistry;
     private final MimeTypeDetector mimeTypeDetector;
     private final SecurityContext securityContext;
+    /** The unique ID string, which is always generated so that it can be final and not volatile. */
+    private final String id = UUID.randomUUID().toString();
 
     /**
      * Create an instance of an execution context that uses the {@link AccessController#getContext() current JAAS calling context}
@@ -235,6 +238,15 @@
     }
 
     /**
+     * Get the unique identifier for this context.
+     * 
+     * @return the unique identifier string; never null and never empty
+     */
+    public String getId() {
+        return id;
+    }
+
+    /**
      * Create a new execution context that mirrors this context but that uses the supplied namespace registry. The resulting
      * context's {@link #getValueFactories() value factories} and {@link #getPropertyFactory() property factory} all make use of
      * the new namespace registry.
@@ -312,7 +324,11 @@
      */
     @Override
     public String toString() {
-        return "Execution context for " + getSecurityContext() == null ? "null" : getSecurityContext().getUserName();
+        StringBuilder sb = new StringBuilder("Execution context for ");
+        if (getSecurityContext() == null) sb.append("null");
+        else sb.append(getSecurityContext().getUserName());
+        sb.append(" (").append(id).append(')');
+        return sb.toString();
     }
 
     /**

Modified: trunk/dna-graph/src/main/java/org/jboss/dna/graph/observe/Changes.java
===================================================================
--- trunk/dna-graph/src/main/java/org/jboss/dna/graph/observe/Changes.java	2009-11-26 00:36:08 UTC (rev 1358)
+++ trunk/dna-graph/src/main/java/org/jboss/dna/graph/observe/Changes.java	2009-11-26 20:37:08 UTC (rev 1359)
@@ -27,6 +27,7 @@
 import java.util.Collections;
 import java.util.List;
 import net.jcip.annotations.Immutable;
+import org.jboss.dna.graph.ExecutionContext;
 import org.jboss.dna.graph.SecurityContext;
 import org.jboss.dna.graph.property.DateTime;
 import org.jboss.dna.graph.request.ChangeRequest;
@@ -40,19 +41,14 @@
     private static final long serialVersionUID = 1L;
 
     protected final String processId;
+    protected final String contextId;
     protected final String userName;
     protected final String sourceName;
     protected final DateTime timestamp;
     protected final List<ChangeRequest> changeRequests;
 
-    public Changes( String userName,
-                    String sourceName,
-                    DateTime timestamp,
-                    List<ChangeRequest> requests ) {
-        this("", userName, sourceName, timestamp, requests);
-    }
-
     public Changes( String processId,
+                    String contextId,
                     String userName,
                     String sourceName,
                     DateTime timestamp,
@@ -64,6 +60,7 @@
         this.timestamp = timestamp;
         this.changeRequests = Collections.unmodifiableList(requests);
         this.processId = processId != null ? processId : "";
+        this.contextId = contextId != null ? contextId : "";
         assert this.userName != null;
         assert this.sourceName != null;
         assert this.timestamp != null;
@@ -77,11 +74,13 @@
         this.timestamp = changes.timestamp;
         this.changeRequests = changes.changeRequests;
         this.processId = changes.processId;
+        this.contextId = changes.contextId;
         assert this.userName != null;
         assert this.sourceName != null;
         assert this.timestamp != null;
         assert this.changeRequests != null;
         assert this.processId != null;
+        assert this.contextId != null;
     }
 
     /**
@@ -122,6 +121,16 @@
     }
 
     /**
+     * Get the {@link ExecutionContext#getId() identifier} of the {@link ExecutionContext} where these changes originated. This
+     * identifier may be useful in preventing feedbacks.
+     * 
+     * @return the context identifier; never null
+     */
+    public String getContextId() {
+        return processId;
+    }
+
+    /**
      * Get the list of changes.
      * 
      * @return the immutable list of change requests; never null and never empty

Modified: trunk/dna-graph/src/main/java/org/jboss/dna/graph/request/processor/RequestProcessor.java
===================================================================
--- trunk/dna-graph/src/main/java/org/jboss/dna/graph/request/processor/RequestProcessor.java	2009-11-26 00:36:08 UTC (rev 1358)
+++ trunk/dna-graph/src/main/java/org/jboss/dna/graph/request/processor/RequestProcessor.java	2009-11-26 20:37:08 UTC (rev 1359)
@@ -848,7 +848,9 @@
         if (observer != null && !this.changes.isEmpty()) {
             String userName = context.getSecurityContext() != null ? context.getSecurityContext().getUserName() : null;
             if (userName == null) userName = "";
-            Changes changes = new Changes(userName, getSourceName(), getNowInUtc(), this.changes);
+            String contextId = context.getId();
+            String processId = null;
+            Changes changes = new Changes(processId, contextId, userName, getSourceName(), getNowInUtc(), this.changes);
             observer.notify(changes);
         }
     }

Added: trunk/dna-graph/src/test/java/org/jboss/dna/graph/ExecutionContextTest.java
===================================================================
--- trunk/dna-graph/src/test/java/org/jboss/dna/graph/ExecutionContextTest.java	                        (rev 0)
+++ trunk/dna-graph/src/test/java/org/jboss/dna/graph/ExecutionContextTest.java	2009-11-26 20:37:08 UTC (rev 1359)
@@ -0,0 +1,77 @@
+/*
+ * JBoss DNA (http://www.jboss.org/dna)
+ * See the COPYRIGHT.txt file distributed with this work for information
+ * regarding copyright ownership.  Some portions may be licensed
+ * to Red Hat, Inc. under one or more contributor license agreements.
+ * See the AUTHORS.txt file in the distribution for a full listing of 
+ * individual contributors.
+ *
+ * JBoss DNA is free software. Unless otherwise indicated, all code in JBoss DNA
+ * is licensed to you 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.
+ * 
+ * JBoss DNA 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;
+
+import static org.hamcrest.core.Is.is;
+import static org.hamcrest.core.IsNot.not;
+import static org.hamcrest.core.IsNull.notNullValue;
+import static org.junit.Assert.assertThat;
+import static org.mockito.Mockito.mock;
+import java.util.HashSet;
+import java.util.Set;
+import org.jboss.dna.common.component.ClassLoaderFactory;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+public class ExecutionContextTest {
+
+    private ExecutionContext context;
+
+    @Before
+    public void beforeEach() {
+        context = new ExecutionContext();
+    }
+
+    @After
+    public void afterEach() {
+        context = null;
+    }
+
+    @Test
+    public void shouldHaveIdentifierThatIsNotNull() {
+        assertThat(context.getId(), is(notNullValue()));
+    }
+
+    @Test
+    public void shouldHaveIdentifierThatIsNotBlank() {
+        assertThat(context.getId().length(), is(not(0)));
+        assertThat(context.getId().trim().length(), is(not(0)));
+    }
+
+    @Test
+    public void shouldHaveIdentifierThatIsUnique() {
+        // Can't really test this, but we certainly can test that there are no duplicates in many contexts ...
+        Set<String> ids = new HashSet<String>();
+        for (int i = 0; i != 50; ++i) {
+            assertThat(ids.add(new ExecutionContext().getId()), is(true));
+        }
+    }
+
+    @Test
+    public void shouldCreateSubcontextsWithDifferentIdentifiers() {
+        ExecutionContext newContext = context.with(mock(ClassLoaderFactory.class));
+        assertThat(newContext.getId(), is(not(context.getId())));
+    }
+}


Property changes on: trunk/dna-graph/src/test/java/org/jboss/dna/graph/ExecutionContextTest.java
___________________________________________________________________
Name: svn:keywords
   + Id Revision
Name: svn:eol-style
   + LF



More information about the dna-commits mailing list