jackrabbit-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ju...@apache.org
Subject svn commit: r994951 - in /jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core: SessionImpl.java session/SessionContext.java session/SessionState.java
Date Wed, 08 Sep 2010 09:55:06 GMT
Author: jukka
Date: Wed Sep  8 09:55:05 2010
New Revision: 994951

URL: http://svn.apache.org/viewvc?rev=994951&view=rev
Log:
JCR-2741: Improved logging for session operations

Add a unique internal name for sessions.
Set this name as the "jcr.session" MDC variable during SessionOperations.

Modified:
    jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/SessionImpl.java
    jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/session/SessionContext.java
    jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/session/SessionState.java

Modified: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/SessionImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/SessionImpl.java?rev=994951&r1=994950&r2=994951&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/SessionImpl.java
(original)
+++ jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/SessionImpl.java
Wed Sep  8 09:55:05 2010
@@ -230,7 +230,7 @@ public class SessionImpl extends Abstrac
         this.repositoryContext = repositoryContext;
         this.subject = subject;
 
-        userId = retrieveUserId(subject, wspConfig.getName());
+        this.userId = retrieveUserId(subject, wspConfig.getName());
 
         namePathResolver = new DefaultNamePathResolver(this, this, true);
         context.setItemStateManager(createSessionItemStateManager());
@@ -1212,6 +1212,20 @@ public class SessionImpl extends Abstrac
         context.getItemStateManager().dump(ps);
     }
 
+    //--------------------------------------------------------------< Object >
+
+    /**
+     * Returns the unique internal name of this session. The returned name
+     * is especially useful for debugging and logging purposes.
+     *
+     * @see #sessionName
+     * @return session name
+     */
+    @Override
+    public String toString() {
+        return context.toString();
+    }
+
     /**
      * Finalize the session. If the application doesn't call Session.logout(),
      * the session is closed automatically; however a warning is written to the log file,

Modified: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/session/SessionContext.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/session/SessionContext.java?rev=994951&r1=994950&r2=994951&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/session/SessionContext.java
(original)
+++ jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/session/SessionContext.java
Wed Sep  8 09:55:05 2010
@@ -42,6 +42,7 @@ import org.apache.jackrabbit.spi.Path;
 import org.apache.jackrabbit.spi.commons.conversion.IllegalNameException;
 import org.apache.jackrabbit.spi.commons.conversion.MalformedPathException;
 import org.apache.jackrabbit.spi.commons.conversion.NamePathResolver;
+import org.apache.jackrabbit.test.api.util.Text;
 
 /**
  * Component context of a session. This class keeps track of the internal
@@ -50,6 +51,27 @@ import org.apache.jackrabbit.spi.commons
 public class SessionContext implements NamePathResolver {
 
     /**
+     * Session counter. Used to generate unique internal session names.
+     */
+    private static long counter = 0;
+
+    /**
+     * Creates a unique internal session name for a session with the
+     * given user.
+     *
+     * @param userId session user, or <code>null</code>
+     * @return session name
+     */
+    private static synchronized String createSessionName(String userId) {
+        if (userId != null) {
+            String user = Text.escapeIllegalJcrChars(userId);
+            return "session-" + user + "-" + counter++;
+        } else {
+            return "session-" + counter++;
+        }
+    }
+
+    /**
      * The repository context of this session.
      */
     private final RepositoryContext repositoryContext;
@@ -60,6 +82,12 @@ public class SessionContext implements N
     private final SessionImpl session;
 
     /**
+     * Unique internal name of this session. Returned by the
+     * {@link #toString()} method for use in logging and debugging.
+     */
+    private final String sessionName;
+
+    /**
      * The state of this session.
      */
     private final SessionState state;
@@ -119,6 +147,7 @@ public class SessionContext implements N
         assert session != null;
         this.repositoryContext = repositoryContext;
         this.session = session;
+        this.sessionName = createSessionName(session.getUserID());
         this.state = new SessionState(this);
         this.valueFactory =
             new ValueFactoryImpl(session, repositoryContext.getDataStore());
@@ -315,4 +344,18 @@ public class SessionContext implements N
         return session.getJCRPath(path);
     }
 
+    //--------------------------------------------------------------< Object >
+
+    /**
+     * Returns the unique internal name of this session. The returned name
+     * is especially useful for debugging and logging purposes.
+     *
+     * @see #sessionName
+     * @return session name
+     */
+    @Override
+    public String toString() {
+        return sessionName;
+    }
+
 }

Modified: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/session/SessionState.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/session/SessionState.java?rev=994951&r1=994950&r2=994951&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/session/SessionState.java
(original)
+++ jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/session/SessionState.java
Wed Sep  8 09:55:05 2010
@@ -24,6 +24,7 @@ import javax.jcr.Session;
 
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
+import org.slf4j.MDC;
 
 /**
  * Internal session state. This class keeps track of the lifecycle of
@@ -109,6 +110,21 @@ public class SessionState {
      */
     public <T> T perform(SessionOperation<T> operation)
             throws RepositoryException {
+        String previous = MDC.get("jcr.session");
+        MDC.put("jcr.session", context.toString());
+        try {
+            return internalPerform(operation);
+        } finally {
+            if (previous != null) {
+                MDC.put("jcr.session", previous);
+            } else {
+                MDC.remove("jcr.session");
+            }
+        }
+    }
+
+    private <T> T internalPerform(SessionOperation<T> operation)
+            throws RepositoryException {
         if (!lock.tryLock()) {
             log.debug("Attempt to perform {} while another thread is"
                     + " concurrently accessing the session. Blocking until"



Mime
View raw message