]> git.argeo.org Git - lgpl/argeo-commons.git/blob - server/runtime/org.argeo.server.jackrabbit/src/main/java/org/argeo/jackrabbit/remote/ScopedSessionProvider.java
Improve remoting
[lgpl/argeo-commons.git] / server / runtime / org.argeo.server.jackrabbit / src / main / java / org / argeo / jackrabbit / remote / ScopedSessionProvider.java
1 /*
2 * Copyright (C) 2007-2012 Mathieu Baudier
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16 package org.argeo.jackrabbit.remote;
17
18 import java.io.Serializable;
19 import java.util.List;
20
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;
28
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;
34
35 /**
36 * Session provider assuming a single workspace and a short life cycle,
37 * typically a Spring bean of scope (web) 'session'.
38 */
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);
43
44 private transient HttpSession httpSession = null;
45 private transient Session jcrSession = null;
46
47 private transient String currentRepositoryName = null;
48 private transient String currentWorkspaceName = null;
49
50 public Session getSession(HttpServletRequest request, Repository rep,
51 String workspace) throws LoginException, ServletException,
52 RepositoryException {
53
54 // HTTP session
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();
61
62 if (currentWorkspaceName == null)
63 currentWorkspaceName = workspace;
64
65 // TODO optimize
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);
73 jcrSession = null;
74 if (log.isDebugEnabled())
75 log.debug(getHttpSessionId()
76 + " Changed repository or workspace, logging out of "
77 + currentWorkspaceName);
78 }
79
80 // JCR session
81 if (jcrSession == null)
82 try {
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();
87 return jcrSession;
88 } catch (RepositoryException e) {
89 throw new ArgeoException("Cannot open session to workspace "
90 + workspace, e);
91 }
92 else
93 return jcrSession;
94 }
95
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 + "'");
104 return session;
105 }
106
107 public void releaseSession(Session session) {
108 if (log.isTraceEnabled())
109 log.trace(getHttpSessionId() + " Releasing JCR session " + session);
110 }
111
112 protected final String getHttpSessionId() {
113 return httpSession != null ? httpSession.getId() : "<null>";
114 }
115
116 public void init() {
117 }
118
119 public void destroy() {
120 JcrUtils.logoutQuietly(jcrSession);
121 jcrSession = null;
122 if (log.isDebugEnabled())
123 log.debug(getHttpSessionId()
124 + " Cleaned up provider for web session ");
125 httpSession = null;
126 }
127 }