1 package org
.argeo
.jackrabbit
.remote
;
3 import java
.util
.Enumeration
;
4 import java
.util
.Properties
;
6 import javax
.jcr
.Repository
;
7 import javax
.servlet
.ServletConfig
;
8 import javax
.servlet
.ServletContext
;
9 import javax
.servlet
.ServletException
;
10 import javax
.servlet
.http
.HttpServlet
;
11 import javax
.servlet
.http
.HttpServletRequest
;
13 import org
.apache
.commons
.logging
.Log
;
14 import org
.apache
.commons
.logging
.LogFactory
;
15 import org
.argeo
.ArgeoException
;
16 import org
.argeo
.jcr
.RepositoryRegister
;
17 import org
.springframework
.beans
.BeansException
;
18 import org
.springframework
.context
.ApplicationContext
;
19 import org
.springframework
.context
.ApplicationContextAware
;
20 import org
.springframework
.context
.ConfigurableApplicationContext
;
21 import org
.springframework
.web
.context
.ServletContextAware
;
22 import org
.springframework
.web
.servlet
.HandlerExecutionChain
;
23 import org
.springframework
.web
.servlet
.HandlerMapping
;
25 public abstract class MultipleRepositoryHandlerMapping
implements
26 HandlerMapping
, ApplicationContextAware
, ServletContextAware
{
27 private final static Log log
= LogFactory
28 .getLog(MultipleRepositoryHandlerMapping
.class);
30 private ConfigurableApplicationContext applicationContext
;
31 private ServletContext servletContext
;
33 private RepositoryRegister repositoryRegister
;
35 /** Actually creates the servlet to be registered. */
36 protected abstract HttpServlet
createServlet(Repository repository
,
37 String pathPrefix
) throws ServletException
;
39 public HandlerExecutionChain
getHandler(HttpServletRequest request
)
41 if (log
.isTraceEnabled()) {
42 log
.trace("getContextPath=" + request
.getContextPath());
43 log
.trace("getServletPath=" + request
.getServletPath());
44 log
.trace("getPathInfo=" + request
.getPathInfo());
47 String repositoryName
= extractRepositoryName(request
);
48 String pathPrefix
= request
.getServletPath() + '/' + repositoryName
;
49 String beanName
= pathPrefix
;
51 if (!applicationContext
.containsBean(beanName
)) {
52 Repository repository
= repositoryRegister
.getRepositories().get(
54 HttpServlet servlet
= createServlet(repository
, pathPrefix
);
55 applicationContext
.getBeanFactory().registerSingleton(beanName
,
57 // TODO: unregister it as well
59 HttpServlet remotingServlet
= (HttpServlet
) applicationContext
61 return new HandlerExecutionChain(remotingServlet
);
64 /** The repository name is the first part of the path info */
65 protected String
extractRepositoryName(HttpServletRequest request
) {
66 String pathInfo
= request
.getPathInfo();
67 // TODO: optimize by checking character by character
68 String
[] tokens
= pathInfo
.split("/");
69 StringBuffer currName
= new StringBuffer("");
70 tokens
: for (String token
: tokens
) {
73 currName
.append(token
);
74 if (repositoryRegister
.getRepositories().containsKey(
76 return currName
.toString();
79 throw new ArgeoException("No repository can be found for request "
83 public void setApplicationContext(ApplicationContext applicationContext
)
84 throws BeansException
{
85 this.applicationContext
= (ConfigurableApplicationContext
) applicationContext
;
88 public void setServletContext(ServletContext servletContext
) {
89 this.servletContext
= servletContext
;
92 public void setRepositoryRegister(RepositoryRegister repositoryRegister
) {
93 this.repositoryRegister
= repositoryRegister
;
96 protected class DelegatingServletConfig
implements ServletConfig
{
98 private Properties initParameters
;
100 public DelegatingServletConfig(String name
, Properties initParameters
) {
103 this.initParameters
= initParameters
;
106 public String
getServletName() {
110 public ServletContext
getServletContext() {
111 return servletContext
;
114 public String
getInitParameter(String paramName
) {
115 return initParameters
.getProperty(paramName
);
118 @SuppressWarnings("rawtypes")
119 public Enumeration
getInitParameterNames() {
120 return initParameters
.keys();