2 * Copyright (C) 2007-2012 Mathieu Baudier
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org
.argeo
.jackrabbit
.remote
;
18 import java
.io
.Serializable
;
19 import java
.util
.List
;
21 import javax
.jcr
.LoginException
;
22 import javax
.jcr
.Repository
;
23 import javax
.jcr
.RepositoryException
;
24 import javax
.jcr
.Session
;
25 import javax
.servlet
.ServletException
;
26 import javax
.servlet
.http
.HttpServletRequest
;
27 import javax
.servlet
.http
.HttpSession
;
29 import org
.apache
.commons
.logging
.Log
;
30 import org
.apache
.commons
.logging
.LogFactory
;
31 import org
.apache
.jackrabbit
.server
.SessionProvider
;
32 import org
.argeo
.ArgeoException
;
33 import org
.argeo
.jcr
.JcrUtils
;
36 * Session provider assuming a single workspace and a short life cycle,
37 * typically a Spring bean of scope (web) 'session'.
39 public class ScopedSessionProvider
implements SessionProvider
, Serializable
{
40 private static final long serialVersionUID
= 6589775984177317058L;
41 private final static Log log
= LogFactory
42 .getLog(ScopedSessionProvider
.class);
44 private transient HttpSession httpSession
= null;
45 private transient Session jcrSession
= null;
47 private transient String currentRepositoryName
= null;
48 private transient String currentWorkspaceName
= null;
50 public Session
getSession(HttpServletRequest request
, Repository rep
,
51 String workspace
) throws LoginException
, ServletException
,
55 if (httpSession
!= null
56 && !httpSession
.getId().equals(request
.getSession().getId()))
57 throw new ArgeoException(
58 "Only session scope is supported in this mode");
59 if (httpSession
== null)
60 httpSession
= request
.getSession();
62 if (currentWorkspaceName
== null)
63 currentWorkspaceName
= workspace
;
66 String pathInfo
= request
.getPathInfo();
67 List
<String
> tokens
= JcrUtils
.tokenize(pathInfo
);
68 if (currentRepositoryName
== null)
69 currentRepositoryName
= tokens
.get(0);
70 else if (!currentRepositoryName
.equals(tokens
.get(0))
71 || !currentWorkspaceName
.equals(workspace
)) {
72 JcrUtils
.logoutQuietly(jcrSession
);
74 if (log
.isDebugEnabled())
75 log
.debug(getHttpSessionId()
76 + " Changed repository or workspace, logging out of "
77 + currentWorkspaceName
);
81 if (jcrSession
== null)
83 jcrSession
= login(rep
, workspace
);
84 currentRepositoryName
= tokens
.get(0);
85 // do not use workspace variable which may be null
86 currentWorkspaceName
= jcrSession
.getWorkspace().getName();
88 } catch (RepositoryException e
) {
89 throw new ArgeoException("Cannot open session to workspace "
96 protected Session
login(Repository repository
, String workspace
)
97 throws RepositoryException
{
98 Session session
= repository
.login(workspace
);
99 if (log
.isDebugEnabled())
100 log
.debug(getHttpSessionId() + " User '" + session
.getUserID()
101 + "' logged in workspace '"
102 + session
.getWorkspace().getName() + "' of repository '"
103 + currentRepositoryName
+ "'");
107 public void releaseSession(Session session
) {
108 if (log
.isTraceEnabled())
109 log
.trace(getHttpSessionId() + " Releasing JCR session " + session
);
112 protected final String
getHttpSessionId() {
113 return httpSession
!= null ? httpSession
.getId() : "<null>";
119 public void destroy() {
120 JcrUtils
.logoutQuietly(jcrSession
);
122 if (log
.isDebugEnabled())
123 log
.debug(getHttpSessionId()
124 + " Cleaned up provider for web session ");