jackrabbit-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ju...@apache.org
Subject svn commit: r1236821 - /jackrabbit/trunk/jackrabbit-jcr-server/src/main/java/org/apache/jackrabbit/server/remoting/davex/DavexServletService.java
Date Fri, 27 Jan 2012 18:55:23 GMT
Author: jukka
Date: Fri Jan 27 18:55:23 2012
New Revision: 1236821

URL: http://svn.apache.org/viewvc?rev=1236821&view=rev
Log:
JCR-3222: Allow servlet filters to specify custom session providers

Add support for custom session providers as OSGi services

Modified:
    jackrabbit/trunk/jackrabbit-jcr-server/src/main/java/org/apache/jackrabbit/server/remoting/davex/DavexServletService.java

Modified: jackrabbit/trunk/jackrabbit-jcr-server/src/main/java/org/apache/jackrabbit/server/remoting/davex/DavexServletService.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-jcr-server/src/main/java/org/apache/jackrabbit/server/remoting/davex/DavexServletService.java?rev=1236821&r1=1236820&r2=1236821&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-jcr-server/src/main/java/org/apache/jackrabbit/server/remoting/davex/DavexServletService.java
(original)
+++ jackrabbit/trunk/jackrabbit-jcr-server/src/main/java/org/apache/jackrabbit/server/remoting/davex/DavexServletService.java
Fri Jan 27 18:55:23 2012
@@ -18,17 +18,29 @@
  */
 package org.apache.jackrabbit.server.remoting.davex;
 
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.LinkedHashMap;
 import java.util.Map;
+import java.util.Set;
 
+import javax.jcr.LoginException;
 import javax.jcr.Repository;
+import javax.jcr.RepositoryException;
+import javax.jcr.Session;
 import javax.servlet.Servlet;
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServletRequest;
 
 import org.apache.felix.scr.annotations.Activate;
 import org.apache.felix.scr.annotations.Component;
 import org.apache.felix.scr.annotations.Properties;
 import org.apache.felix.scr.annotations.Property;
 import org.apache.felix.scr.annotations.Reference;
+import org.apache.felix.scr.annotations.ReferenceCardinality;
+import org.apache.felix.scr.annotations.ReferencePolicy;
 import org.apache.felix.scr.annotations.Service;
+import org.apache.jackrabbit.server.SessionProvider;
 import org.apache.jackrabbit.webdav.server.AbstractWebdavServlet;
 import org.apache.jackrabbit.webdav.util.CSRFUtil;
 
@@ -38,8 +50,15 @@ import org.apache.jackrabbit.webdav.util
     @Property(name = "service.description", value = "Apache Jackrabbit JcrRemoting Servlet"),
     @Property(name = JcrRemotingServlet.INIT_PARAM_AUTHENTICATE_HEADER, value = AbstractWebdavServlet.DEFAULT_AUTHENTICATE_HEADER),
     @Property(name = JcrRemotingServlet.INIT_PARAM_CSRF_PROTECTION, value = CSRFUtil.DISABLED),
-    @Property(name = JcrRemotingServlet.INIT_PARAM_MISSING_AUTH_MAPPING, value = "") })
-public class DavexServletService extends JcrRemotingServlet {
+    @Property(name = JcrRemotingServlet.INIT_PARAM_MISSING_AUTH_MAPPING, value = ""),
+    @Property(name = "contextId", value = "") })
+@Reference(
+        name = "providers", referenceInterface = SessionProvider.class,
+        policy = ReferencePolicy.DYNAMIC,
+        cardinality = ReferenceCardinality.OPTIONAL_MULTIPLE,
+        bind = "addSessionProvider", unbind = "removeSessionProvider")
+public class DavexServletService extends JcrRemotingServlet
+        implements SessionProvider {
 
     /** Serial version UID */
     private static final long serialVersionUID = -901601294536148635L;
@@ -54,6 +73,22 @@ public class DavexServletService extends
 
     private String alias;
 
+    /**
+     * Currently available custom session providers. They're used
+     * first before the default provider gets consulted. The associated
+     * set of sessions is used to forcibly release all sessions acquired
+     * from a provider when that provider is being removed.
+     */
+    private final Map<SessionProvider, Set<Session>> providers =
+            new LinkedHashMap<SessionProvider, Set<Session>>();
+
+    /**
+     * Currently active sessions. Used to link a session to the correct
+     * provider in the {@link #releaseSession(Session)} method.
+     */
+    private final Map<Session, SessionProvider> sessions =
+            new HashMap<Session, SessionProvider>();
+
     @Override
     protected Repository getRepository() {
         return repository;
@@ -78,4 +113,78 @@ public class DavexServletService extends
         }
     }
 
+    @Override
+    protected SessionProvider getSessionProvider() {
+        return this;
+    }
+
+    /**
+     * Adds a custom session provider service.
+     *
+     * @param provider session provider
+     */
+    public synchronized void addSessionProvider(SessionProvider provider) {
+        providers.put(provider, new HashSet<Session>());
+    }
+
+    /**
+     * Removes a custom session provider service. All active sessions
+     * acquired from that provider are forcibly released.
+     *
+     * @param provider session provider
+     */
+    public synchronized void removeSessionProvider(SessionProvider provider) {
+        Set<Session> sessions = providers.remove(provider);
+        if (sessions != null) {
+            for (Session session : sessions) {
+                releaseSession(session);
+            }
+        }
+    }
+
+    //-----------------------------------------------------< SessionProvider >
+
+    /**
+     * Asks each available session provider in order for a session and
+     * returns the first session given. The default provider is used
+     * if no custom provider service is available or can provide a requested
+     * session.
+     */
+    public synchronized Session getSession(
+            HttpServletRequest request, Repository repository, String workspace)
+            throws LoginException, ServletException, RepositoryException {
+        SessionProvider provider = null;
+        Session session = null;
+
+        for (Map.Entry<SessionProvider, Set<Session>> entry : providers.entrySet())
{
+            provider = entry.getKey();
+            session = provider.getSession(request, repository, workspace);
+            if (session != null) {
+                entry.getValue().add(session);
+                break;
+            }
+        }
+
+        if (session == null) {
+            provider = super.getSessionProvider();
+            session = provider.getSession(request, repository, workspace);
+        }
+
+        if (session != null) {
+            sessions.put(session, provider);
+        }
+
+        return session;
+    }
+
+    /**
+     * Releases the given session using the provider from which it was acquired.
+     */
+    public synchronized void releaseSession(Session session) {
+        SessionProvider provider = sessions.remove(session);
+        if (provider != null) {
+            provider.releaseSession(session);
+        }
+    }
+
 }



Mime
View raw message