jackrabbit-oak-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ju...@apache.org
Subject svn commit: r1513054 - in /jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr: SessionContext.java SessionImpl.java
Date Mon, 12 Aug 2013 07:05:05 GMT
Author: jukka
Date: Mon Aug 12 07:05:04 2013
New Revision: 1513054

URL: http://svn.apache.org/r1513054
Log:
OAK-953: Allow sub-classing of SessionImpl

Add factory methods for creating the session and workspace instances in SessionContext
Simplify lock manager access in SessionImpl

Modified:
    jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/SessionContext.java
    jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/SessionImpl.java

Modified: jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/SessionContext.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/SessionContext.java?rev=1513054&r1=1513053&r2=1513054&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/SessionContext.java
(original)
+++ jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/SessionContext.java
Mon Aug 12 07:05:04 2013
@@ -81,12 +81,15 @@ public class SessionContext implements N
 
     private final RepositoryImpl repository;
     private final Whiteboard whiteboard;
+    private final Map<String, Object> attributes;
     private final SessionDelegate delegate;
+
     private final SessionNamespaces namespaces;
     private final NamePathMapper namePathMapper;
     private final ValueFactory valueFactory;
-    private final SessionImpl session;
-    private final WorkspaceImpl workspace;
+
+    private SessionImpl session = null;
+    private WorkspaceImpl workspace = null;
 
     private AccessControlManager accessControlManager;
     private PermissionProvider permissionProvider;
@@ -100,7 +103,9 @@ public class SessionContext implements N
             Map<String, Object> attributes, @Nonnull final SessionDelegate delegate)
{
         this.repository = checkNotNull(repository);
         this.whiteboard = checkNotNull(whiteboard);
+        this.attributes = attributes;
         this.delegate = checkNotNull(delegate);
+
         this.namespaces = new SessionNamespaces(this);
         LocalNameMapper nameMapper = new LocalNameMapper() {
             @Override
@@ -117,19 +122,48 @@ public class SessionContext implements N
                 nameMapper, delegate.getIdManager());
         this.valueFactory = new ValueFactoryImpl(
                 delegate.getRoot().getBlobFactory(), namePathMapper);
+    }
 
-        this.session = new SessionImpl(this, attributes);
-        this.workspace = new WorkspaceImpl(this);
+    public final Map<String, Object> getAttributes() {
+        return attributes;
     }
 
-    public Session getSession() {
+    public final synchronized SessionImpl getSession() {
+        if (session == null) {
+            session = createSession();
+        }
         return session;
     }
 
-    public Workspace getWorkspace() {
+    public final synchronized WorkspaceImpl getWorkspace() {
+        if (workspace == null) {
+            workspace = createWorkspace();
+        }
         return workspace;
     }
 
+    /**
+     * Factory method for creating the {@link Session} instance for this
+     * context. Called by {@link #getSession()} when first accessed. Can be
+     * overridden by subclasses to customize the session implementation.
+     *
+     * @return session instance
+     */
+    protected SessionImpl createSession() {
+        return new SessionImpl(this);
+    }
+
+    /**
+     * Factory method for creating the {@link Workspace} instance for this
+     * context. Called by {@link #getWorkspace()} when first accessed. Can be
+     * overridden by subclasses to customize the workspace implementation.
+     *
+     * @return session instance
+     */
+    protected WorkspaceImpl createWorkspace() {
+        return new WorkspaceImpl(this);
+    }
+
     public LockManager getLockManager() {
         return workspace.getLockManager();
     }

Modified: jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/SessionImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/SessionImpl.java?rev=1513054&r1=1513053&r2=1513054&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/SessionImpl.java
(original)
+++ jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/SessionImpl.java
Mon Aug 12 07:05:04 2013
@@ -16,6 +16,7 @@
  */
 package org.apache.jackrabbit.oak.jcr;
 
+import static com.google.common.collect.Sets.newTreeSet;
 import static org.apache.jackrabbit.oak.commons.PathUtils.getParentPath;
 
 import java.io.IOException;
@@ -23,7 +24,6 @@ import java.io.InputStream;
 import java.io.OutputStream;
 import java.security.AccessControlException;
 import java.util.Collections;
-import java.util.Map;
 import java.util.Set;
 
 import javax.annotation.CheckForNull;
@@ -42,12 +42,10 @@ import javax.jcr.Session;
 import javax.jcr.UnsupportedRepositoryOperationException;
 import javax.jcr.ValueFactory;
 import javax.jcr.Workspace;
-import javax.jcr.lock.LockManager;
 import javax.jcr.nodetype.ConstraintViolationException;
 import javax.jcr.retention.RetentionManager;
 import javax.jcr.security.AccessControlManager;
 
-import com.google.common.collect.Sets;
 import org.apache.jackrabbit.api.JackrabbitSession;
 import org.apache.jackrabbit.api.security.principal.PrincipalManager;
 import org.apache.jackrabbit.api.security.user.UserManager;
@@ -80,12 +78,10 @@ public class SessionImpl implements Jack
 
     private final SessionContext sessionContext;
     private final SessionDelegate sd;
-    private final Map<String, Object> attributes;
 
-    SessionImpl(SessionContext sessionContext, Map<String, Object> attributes) {
+    SessionImpl(SessionContext sessionContext) {
         this.sessionContext = sessionContext;
         this.sd = sessionContext.getSessionDelegate();
-        this.attributes = attributes;
     }
 
     static void checkIndexOnName(SessionContext sessionContext, String path) throws RepositoryException
{
@@ -228,7 +224,7 @@ public class SessionImpl implements Jack
 
     @Override
     public String[] getAttributeNames() {
-        Set<String> names = Sets.newHashSet(attributes.keySet());
+        Set<String> names = newTreeSet(sessionContext.getAttributes().keySet());
         Collections.addAll(names, sd.getAuthInfo().getAttributeNames());
         return names.toArray(new String[names.size()]);
     }
@@ -236,11 +232,13 @@ public class SessionImpl implements Jack
     @Override
     public Object getAttribute(String name) {
         Object attribute = sd.getAuthInfo().getAttribute(name);
-        return attribute == null ? attributes.get(name) : attribute;
+        if (attribute == null) {
+            attribute = sessionContext.getAttributes().get(name);
+        }
+        return attribute;
     }
 
-    @Override
-    @Nonnull
+    @Override @Nonnull
     public Workspace getWorkspace() {
         return sessionContext.getWorkspace();
     }
@@ -536,46 +534,31 @@ public class SessionImpl implements Jack
         }
     }
 
-    @Nonnull
-    private LockManager getLockManager() {
-        return sessionContext.getLockManager();
-    }
-
-    /**
-     * @see javax.jcr.Session#addLockToken(String)
-     */
     @Override
     public void addLockToken(String lt) {
         try {
-            getLockManager().addLockToken(lt);
+            getWorkspace().getLockManager().addLockToken(lt);
         } catch (RepositoryException e) {
-            log.warn("Unable to add lock token '{}' to this session: {}", lt, e.getMessage());
+            log.warn("Unable to add lock token " + lt + " to session", e);
         }
     }
 
-    /**
-     * @see javax.jcr.Session#getLockTokens()
-     */
-    @Override
-    @Nonnull
+    @Override @Nonnull
     public String[] getLockTokens() {
         try {
-            return getLockManager().getLockTokens();
+            return getWorkspace().getLockManager().getLockTokens();
         } catch (RepositoryException e) {
-            log.warn("Unable to retrieve lock tokens for this session: {}", e.getMessage());
+            log.warn("Unable to retrieve lock tokens from session", e);
             return new String[0];
         }
     }
 
-    /**
-     * @see javax.jcr.Session#removeLockToken(String)
-     */
     @Override
     public void removeLockToken(String lt) {
         try {
-            getLockManager().addLockToken(lt);
+            getWorkspace().getLockManager().removeLockToken(lt);
         } catch (RepositoryException e) {
-            log.warn("Unable to add lock token '{}' to this session: {}", lt, e.getMessage());
+            log.warn("Unable to remove lock token " + lt + " from session", e);
         }
     }
 



Mime
View raw message