1 package org
.argeo
.jcr
.mvc
;
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 HandlerExecutionChain hec
= new HandlerExecutionChain(remotingServlet
);
65 /** The repository name is the first part of the path info */
66 protected String
extractRepositoryName(HttpServletRequest request
) {
67 String pathInfo
= request
.getPathInfo();
68 // TODO: optimize by checking character by character
69 String
[] tokens
= pathInfo
.split("/");
70 StringBuffer currName
= new StringBuffer("");
71 tokens
: for (String token
: tokens
) {
74 currName
.append(token
);
75 if (repositoryRegister
.getRepositories().containsKey(
77 return currName
.toString();
80 throw new ArgeoException("No repository can be found for request "
84 public void setApplicationContext(ApplicationContext applicationContext
)
85 throws BeansException
{
86 this.applicationContext
= (ConfigurableApplicationContext
) applicationContext
;
89 public void setServletContext(ServletContext servletContext
) {
90 this.servletContext
= servletContext
;
93 public void setRepositoryRegister(RepositoryRegister repositoryRegister
) {
94 this.repositoryRegister
= repositoryRegister
;
97 protected class DelegatingServletConfig
implements ServletConfig
{
99 private Properties initParameters
;
101 public DelegatingServletConfig(String name
, Properties initParameters
) {
104 this.initParameters
= initParameters
;
107 public String
getServletName() {
111 public ServletContext
getServletContext() {
112 return servletContext
;
115 public String
getInitParameter(String paramName
) {
116 return initParameters
.getProperty(paramName
);
119 @SuppressWarnings("rawtypes")
120 public Enumeration
getInitParameterNames() {
121 return initParameters
.keys();