Modified: tomcat/sandbox/tomcat-lite/java/org/apache/tomcat/lite/TomcatLite.java URL: http://svn.apache.org/viewvc/tomcat/sandbox/tomcat-lite/java/org/apache/tomcat/lite/TomcatLite.java?rev=640688&r1=640687&r2=640688&view=diff ============================================================================== --- tomcat/sandbox/tomcat-lite/java/org/apache/tomcat/lite/TomcatLite.java (original) +++ tomcat/sandbox/tomcat-lite/java/org/apache/tomcat/lite/TomcatLite.java Mon Mar 24 21:02:18 2008 @@ -2,7 +2,6 @@ */ package org.apache.tomcat.lite; -import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; @@ -14,16 +13,20 @@ import java.util.List; import javax.servlet.Filter; -import javax.servlet.RequestDispatcher; import javax.servlet.Servlet; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.UnavailableException; import javax.servlet.http.HttpServletResponse; -import org.apache.tomcat.lite.http11.Http11Connection; -import org.apache.tomcat.lite.util.MappingData; -import org.apache.tomcat.servlets.config.ServletData; +import org.apache.coyote.Adapter; +import org.apache.coyote.ProtocolHandler; +import org.apache.coyote.Request; +import org.apache.coyote.Response; +import org.apache.coyote.adapters.CoyoteServer; +import org.apache.coyote.adapters.MapperAdapter; +import org.apache.tomcat.util.http.mapper.MappingData; +import org.apache.tomcat.util.net.SocketStatus; /** * Simpler, lower footprint serlvet engine. @@ -32,21 +35,23 @@ * * @author Costin Manolache */ -public class TomcatLite { +public class TomcatLite implements Adapter { private static TomcatLite facade = new TomcatLite(); - Filter hostMapper; + private String serverDirName; + private File workDir; + private int port = 8003; // all contexts - hostMapper knows about hostnames and how they are mapped. - private ArrayList/*<ServletContextImpl>*/ contexts = new ArrayList(); + // this shouldn't be needed if we want to delegate ctx management + private ArrayList<ServletContextImpl> contexts = new ArrayList(); - //private Repository engineRepo; - URLClassLoader contextParentLoader; - // The / context - private ServletContextImpl rootContext; - private ServletContextImpl engineContext; + // Discovered or default Host/Context mapper + Filter hostMapper; + + ProtocolHandler connector; private TomcatLite() { } @@ -96,7 +101,29 @@ long t1 = System.currentTimeMillis(); System.err.println("Engine.start() " + (t1-t0)); } - + + public void startConnector() { + connector = CoyoteServer.getDefaultConnector(port); + connector.setAdapter(this); + try { + connector.init(); + connector.start(); + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + /** + * Add a context - used for IntrospectionUtils. + * + * ContextPath:ContextBaseDir + */ + public void setContext(String c) throws ServletException { + String[] pathDir = c.split(":", 2); + addServletContext("", pathDir[1], pathDir[0]); + } + public void stop() { Iterator i1 = contexts.iterator(); while (i1.hasNext()) { @@ -107,6 +134,13 @@ e.printStackTrace(); } } + if (connector != null) { + try { + connector.destroy(); + } catch (Exception e) { + e.printStackTrace(); + } + } } // -------------- Context add/remove -------------- @@ -114,6 +148,10 @@ public static String[] DEFAULT_WELCOME = { "index.html" }; /** + * Add a context. + * + * web.xml will be read as part of init, and the initialization will be + * part of start or lazy. * * @param hostname - "" * if default host, or string to be matched with Host header @@ -128,26 +166,18 @@ { ServletContextImpl ctx = new ServletContextImpl(this); ctx.setContextPath(path); + ctx.setBasePath(basePath); - contexts.add(ctx); - - boolean isRoot = false; - if (hostname == null || hostname.length() == 0) { - if ("/".equals(path)) { - rootContext = ctx; - isRoot = true; - } + if (hostMapper == null) { + hostMapper = new WebappContextMapper(); } - // TODO: read web.xml or equivalent - - // TODO: defaults + ((WebappContextMapper) hostMapper).addHost(hostname, null); + ((WebappContextMapper) hostMapper).addContext(hostname, + ctx); - if (isRoot && hostMapper == null) { - // Don't add it just yet - only if we have 2 contexts - return ctx; - } + contexts.add(ctx); return ctx; } @@ -162,16 +192,6 @@ } - public void service(Http11Connection connection) throws IOException, Exception { - ServletRequestImpl req = new ServletRequestImpl(); - ServletResponseImpl res = new ServletResponseImpl(); - req.setConnection(connection); - req.setResponse(res); - res.setRequest(req); - - service(req, res); - } - /** * Required for ServletContext.getContext(uri); * @throws ServletException @@ -179,23 +199,20 @@ */ public ServletContextImpl getContext(ServletContextImpl impl, String uri) throws IOException, ServletException { - if (hostMapper == null) { - return rootContext; - } - // Create a request - needs to be simplified ServletRequestImpl req = createMessage(impl, impl.contextPath, uri); hostMapper.doFilter(req, null, null); return req.getContext(); } - + + public void service(ServletRequestImpl req, ServletResponseImpl res) throws Exception, IOException { // parse the session id from URI req.parseSessionId(); try { - req.decodeRequest(); + MapperAdapter.decodeRequest(req.getCoyoteRequest()); } catch(IOException ioe) { res.setStatus(400); return; @@ -203,46 +220,39 @@ MappingData mapRes = req.getMappingData(); try { - if (hostMapper != null) { - // TODO: make hostMapper configurable, implement interface, - // simple to set on ROOT context - hostMapper.doFilter(req, null, null); - } else { - // That's all mapContext() need to do. Host is not actually - // used outside of hostMapper - mapRes.context = rootContext; - mapRes.contextPath.setString("/"); - } - + // TODO: make hostMapper configurable, implement interface, + // simple to set on ROOT context + hostMapper.doFilter(req, null, null); - ServletContextImpl ctx = (ServletContextImpl)mapRes.context; - if( ctx == null ) { - // TODO: 404 - res.setStatus(404); - return; - } - req.setContext(ctx); - // bind class loader - Thread.currentThread().setContextClassLoader(ctx.getClassLoader()); + ServletContextImpl ctx = (ServletContextImpl)mapRes.context; + if( ctx == null ) { + // TODO: 404 + res.setStatus(404); + return; + } + req.setContext(ctx); - WebappServletMapper mapper = ctx.getMapper(); - mapper.map(req.getDecodedRequestURIMB(), mapRes); + // bind class loader + Thread.currentThread().setContextClassLoader(ctx.getClassLoader()); - req.parseSessionCookiesId(); - - ServletConfigImpl h=(ServletConfigImpl)mapRes.wrapper; - if (h != null) { - req.setWrapper((ServletConfigImpl)mapRes.wrapper); - serviceServlet(ctx, req, res, h, mapRes ); - // send the response... - - //res.flushBuffer(); - - // Recycle the wrapper request and response - //req.recycle(); - //res.recycle(); - } + WebappServletMapper mapper = ctx.getMapper(); + mapper.map(req.getCoyoteRequest().decodedURI(), mapRes); + + req.parseSessionCookiesId(); + + ServletConfigImpl h=(ServletConfigImpl)mapRes.wrapper; + if (h != null) { + req.setWrapper((ServletConfigImpl)mapRes.wrapper); + serviceServlet(ctx, req, res, h, mapRes ); + // send the response... + + //res.flushBuffer(); + + // Recycle the wrapper request and response + //req.recycle(); + //res.recycle(); + } } finally { if(mapRes != null ) mapRes.recycle(); @@ -332,78 +342,24 @@ void notifyRemove(Object o) { } - File rootDir; - File workDir; - - public void setRootDir(String rootDir) { - this.rootDir = new File(rootDir); + public void setServerDir(String dir) { + this.serverDirName = dir; } - public File getRoot() { - if (rootDir == null) { - rootDir = new File("."); - } - return rootDir; + public void setPort(int port) { + this.port = port; } public File getWork() { if (workDir == null) { - workDir = new File(getRoot(), "tomcat-work"); - if (workDir.exists()) { - workDir.mkdirs(); - } + File rootDirFile = new File(serverDirName); + workDir = new File(rootDirFile, "tomcat-work"); + if (workDir.exists()) { + workDir.mkdirs(); + } } return workDir; } - - - String deployServletName = "org.apache.tomcat.servlets.deploy.InitServlet"; - ServletConfigImpl deployServlet = null; - - public static String A_ENGINE = "__x_tomcatlite"; - - public ServletConfigImpl getDeployServlet(ServletContextImpl deployCtx) - throws ServletException { - if (deployServlet == null) { - deployServlet = new ServletConfigImpl(deployCtx, - new ServletData("engineInit", deployServletName)); - deployCtx.addServletConfig(deployServlet); - deployCtx.init(); - } - return deployServlet; - } - - private static String SERVER_CFG = "tomcat-lite-web.xml"; - - /** - * Load engine config file - a web.xml file from jar or classpath. - * @throws IOException - * - */ - private void engineExtraConfig(ServletContextImpl engineCtx) - throws ServletException, IOException { - File webXmlFile = new File( engineCtx.getBasePath() + "/WEB-INF/" + - SERVER_CFG); - if (webXmlFile.exists()) { - engineCtx.readWebXml(new FileInputStream(webXmlFile)); - return; - } - - InputStream defWebXml = - TomcatLite.class.getClassLoader().getResourceAsStream(SERVER_CFG); - if (defWebXml == null) { - defWebXml = - getClass().getClassLoader().getResourceAsStream("org/apache/tomcat/lite/" + - SERVER_CFG); - } - if (defWebXml == null) { - System.err.println("Default engine web.xml not found"); - } else { - engineCtx.readWebXml(defWebXml); - } - engineCtx.init(); - } - /** * Init using cached tomcatLite.ser @@ -412,51 +368,39 @@ * @throws IOException */ public void init() throws ServletException, IOException { - init("webapps/ROOT", "/", null); + if (contexts.size() == 0) { + setContext("/:./webapps/ROOT"); + } + Iterator i1 = contexts.iterator(); + while (i1.hasNext()) { + ServletContextImpl ctx = (ServletContextImpl) i1.next(); + try { + ctx.init(); + } catch (Throwable e) { + e.printStackTrace(); + } + } } - public void init(String rootDir, String path, Servlet deployServlet) + /** + * Initialize an webapp and add it to the server. + * - load web.xml + * - call + * + * @param rootDir + * @param path + * @param deployServlet + */ + public void init(String rootDir, String path) throws ServletException, IOException { long t0 = System.currentTimeMillis(); - // Use the default - single webapp in rootDir. - File rootDirF = new File(rootDir); - rootDirF = rootDirF.getCanonicalFile(); ServletContextImpl ctx = (ServletContextImpl)addServletContext(null, rootDir, path); - engineContext = ctx; ctx.init(); - - // Special 'init' servlet - can add more webapps, context, - // filters, etc. Optional - if not found, only one webapp - // will be used. - ServletConfigImpl initSC = - engineContext.getServletConfig("engineInit"); - // getDeployServlet(engineContext); - if (initSC != null) { - // Create a request - needs to be simplified - ServletRequestImpl req = createMessage(engineContext, "/", "/"); - RequestDispatcher rd = - engineContext.getNamedDispatcher("engineInit"); - rd.forward(req, req.getResponse()); - } - - // Special case: load engine config from resource - if (rootContext == null) { - rootContext = (ServletContextImpl) - addServletContext(null, rootDir, "/"); - rootContext.init(); // defaults, read web.xml if any - } - - if (engineContext == null) { - engineContext = rootContext; - } - // Load exta config file for the engine context ( root or - // single context ) - engineExtraConfig(engineContext); long t1 = System.currentTimeMillis(); @@ -468,15 +412,17 @@ private ServletRequestImpl createMessage(ServletContextImpl deployCtx, String ctxPath, String reqPath) { - Http11Connection con = new Http11Connection(this, null, null); - con.setOutputStream(new ByteArrayOutputStream()); +// Http11Connection con = new Http11Connection(this); +// con.setOutputStream(new ByteArrayOutputStream()); ServletRequestImpl req = new ServletRequestImpl(); ServletResponseImpl res = new ServletResponseImpl(); + req.setCoyoteRequest(new Request()); + res.resB = new Response(); req.setResponse(res); res.setRequest(req); - req.setConnection(con); +// req.setConnection(con); req.setContextPath(ctxPath); req.setContext(deployCtx); @@ -506,7 +452,51 @@ } + /** + * Set a global filter that will be used for context mapping. + * + * The filter will get a request, with requestUri and hostname set. + * + * It needs to compute the context path and set it as an attribute. + * + * Advanced features may include on-demand loading of webapps, large scale + * virtual hosting, etc. + */ public void setContextMapper(Filter hostMapper2) { this.hostMapper = hostMapper2; + } + + // ---- Coyote --- + + public boolean event(Request req, Response res, SocketStatus status) + throws Exception { + return false; + } + + public static final int ADAPTER_NOTES = 1; + + public void service(Request req, Response res) throws Exception { + // find the facades + ServletRequestImpl sreq = (ServletRequestImpl) req.getNote(ADAPTER_NOTES); + ServletResponseImpl sres = (ServletResponseImpl) res.getNote(ADAPTER_NOTES); + if (sreq == null) { + sreq = new ServletRequestImpl(); + sres = new ServletResponseImpl(); + sreq.setCoyoteRequest(req); + sres.resB = res; + sreq.setResponse(sres); + sres.setRequest(sreq); + + req.setNote(ADAPTER_NOTES, sreq); + res.setNote(ADAPTER_NOTES, sres); + + } + + service(sreq, sres); + sres.outputBuffer.flush(); + res.finish(); + + sreq.recycle(); + sres.recycle(); } }
Modified: tomcat/sandbox/tomcat-lite/java/org/apache/tomcat/lite/TomcatLiteMain.java URL: http://svn.apache.org/viewvc/tomcat/sandbox/tomcat-lite/java/org/apache/tomcat/lite/TomcatLiteMain.java?rev=640688&r1=640687&r2=640688&view=diff ============================================================================== --- tomcat/sandbox/tomcat-lite/java/org/apache/tomcat/lite/TomcatLiteMain.java (original) +++ tomcat/sandbox/tomcat-lite/java/org/apache/tomcat/lite/TomcatLiteMain.java Mon Mar 24 21:02:18 2008 @@ -5,47 +5,31 @@ import org.apache.tomcat.util.IntrospectionUtils; /** - * Simpler, lower footprint serlvet engine. + * Launch tomcat lite. * - * + * With no arguments, it'll load only the root context / from + * ./webapps/ROOT + * + * Most configuration can be done using web.xml files, few settings + * can be set by flags. * * @author Costin Manolache */ public class TomcatLiteMain { - String rootDir = "./webapps/ROOT"; - String ctxPath = "/"; - - public String getPath() { - return ctxPath; - } - - public void setPath(String ctxPath) { - this.ctxPath = ctxPath; - } - - public String getDir() { - return rootDir; - } - - public void setDir(String rootDir) { - this.rootDir = rootDir; - } - - public void execute() throws Exception { - TomcatLite lite = TomcatLite.getServletImpl(); - - lite.init(rootDir, ctxPath, null); - lite.start(); - - } public static void main(String args[]) throws Exception { - - TomcatLiteMain facade = new TomcatLiteMain(); - IntrospectionUtils.processArgs(facade, args); + TomcatLite lite = TomcatLite.getServletImpl(); + // TODO: collect all args into a properties file, + // have them applied to web.xml params. + // format: webapp.servletname.param=value - facade.execute(); - } + // TODO: use properties file. + // TODO: integrate this with JMX + IntrospectionUtils.processArgs(lite, args); + lite.init(); + lite.start(); + lite.startConnector(); + } } Copied: tomcat/sandbox/tomcat-lite/java/org/apache/tomcat/lite/WebappContextMapper.java (from r639949, tomcat/sandbox/tomcat-lite/java/org/apache/tomcat/servlets/deploy/WebappContextMapper.java) URL: http://svn.apache.org/viewvc/tomcat/sandbox/tomcat-lite/java/org/apache/tomcat/lite/WebappContextMapper.java?p2=tomcat/sandbox/tomcat-lite/java/org/apache/tomcat/lite/WebappContextMapper.java&p1=tomcat/sandbox/tomcat-lite/java/org/apache/tomcat/servlets/deploy/WebappContextMapper.java&r1=639949&r2=640688&rev=640688&view=diff ============================================================================== --- tomcat/sandbox/tomcat-lite/java/org/apache/tomcat/servlets/deploy/WebappContextMapper.java (original) +++ tomcat/sandbox/tomcat-lite/java/org/apache/tomcat/lite/WebappContextMapper.java Mon Mar 24 21:02:18 2008 @@ -1,329 +1,129 @@ -package org.apache.tomcat.servlets.deploy; +package org.apache.tomcat.lite; -import java.io.File; import java.io.IOException; -import java.io.Serializable; -import java.util.ArrayList; import java.util.HashMap; -import java.util.Iterator; import java.util.Map; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; +import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; -import javax.servlet.http.HttpServletRequest; -import org.apache.tomcat.lite.ServletContextImpl; -import org.apache.tomcat.lite.ServletRequestImpl; -import org.apache.tomcat.lite.WebappServletMapper; -import org.apache.tomcat.lite.WebappServletMapper.ContextMapElement; -import org.apache.tomcat.lite.util.MappingData; +import org.apache.tomcat.servlets.addon.UserHostMapper; import org.apache.tomcat.util.buf.CharChunk; import org.apache.tomcat.util.buf.MessageBytes; +import org.apache.tomcat.util.http.mapper.MappingData; /** * This handles host and context mapping. * - * All context-specific mapping is done in WebappFilerMapper and - * WebappServletMapper. + * The default implementation for tomcat lite is very limitted - + * no support for virtual hosts, no support for contexts deeper than + * 1 level. The intention is to override this with more advanced mappers. + * + * With 'ConfigurableHosts' interface it is possible for a smart + * mapper to load/unload virtual hosts at runtime, maybe from a + * database. It should be possible to use databases to store huge number + * of hosts or webapps. * - * You can extend and override the mapper in MapperAdapter. */ -public class WebappContextMapper implements Filter { - - /** Data we store for each host - */ - static class HostMappingInfo - { - public WebappContextMapper.HostData hostData; - // <String path, ServletContextImp> - private HashMap children = new HashMap(); - // Used by the mapper - ContextMapElement[] contexts = new ContextMapElement[0]; - int nesting = 0; - - public String toString() { - StringBuffer sb = new StringBuffer(); - - sb.append("HostMappingInfo { nesting: " + nesting + - "\n hostName" + hostData.hostName); - for( int i=0; i < contexts.length; i++) { - sb.append(contexts[i].toString()); - } - sb.append("\n}"); - return sb.toString(); - } - - } - - public static class HostData implements Serializable { - /** Must be lowercase */ - public String hostName; - public ArrayList/*<String>*/ aliases = new ArrayList(); - } - - /** String lowercase(hostname) -> Host object - * All aliases are included. - */ - Map/*<String, HostMappingInfo>*/ hostMap = new HashMap(); - - // could it be multiple hosts ? - HashMap/*<ServletContextImpl, HostMappingInfo>*/ contextsParents = - new HashMap(); - - // If no host or alias matches - HostMappingInfo defaultHost = new HostMappingInfo(); - - public WebappContextMapper() { - defaultHost.hostData= new WebappContextMapper.HostData(); - defaultHost.hostData.hostName = ""; - hostMap.put("", defaultHost); - } - - /** - * Add a new host to the mapper. - * - * @param name Virtual host name - * @param host Host object - */ - public void addHost(String name, String[] aliases) { - HostMappingInfo host = new HostMappingInfo(); - hostMap.put(name.toLowerCase(), host); - for (int i = 0; i < aliases.length; i++) { - hostMap.put(aliases[i].toLowerCase(), host); - } - } - - /** - * Remove a host from the mapper. - * - * @param name Virtual host name - */ - public void removeHost(String name) { - HostMappingInfo host = findHost(name); - if (host == null) { - return; - } - Iterator hostIt = hostMap.entrySet().iterator(); - while( hostIt.hasNext()) { - Map.Entry entry = (Map.Entry)hostIt.next(); - if(entry.getValue() == host) { - hostIt.remove(); - } - } - } - - /** - * Add a new Context to an existing Host. - * - * @param hostName Virtual host name this context belongs to - * @param contextPath Context path - * @param context Context object - * @param welcomeResources Welcome files defined for this context - * @param resources Static resources of the context - */ - public void addContext(String hostName, ServletContextImpl context, - String[] welcomeResources, File resources) - throws ServletException - { - HostMappingInfo host = findHost(hostName); - - String path = context.getContextPath(); - host.children.put(path, context); - contextsParents.put(context, host); - - int slashCount = WebappServletMapper.slashCount(path); - if (slashCount > host.nesting) { - host.nesting = slashCount; - } - ContextMapElement[] contexts = host.contexts; - ContextMapElement[] newContexts = new ContextMapElement[contexts.length + 1]; - ContextMapElement newContext = context.getMapper().contextMapElement; - newContext.name = path; - newContext.object = context; - newContext.welcomeResources = welcomeResources; - newContext.resources = resources; - if (WebappServletMapper.insertMap(contexts, newContexts, newContext)) { - host.contexts = newContexts; - } - - } - - - /** - * Remove a context from an existing host. - * - * @param hostName Virtual host name this context belongs to - * @param path Context path - */ - public void removeContext(String hostName, String path) - throws ServletException { - HostMappingInfo host = findHost(hostName); - if (host == null) throw new ServletException("Host not found"); - if (host.hostData.hostName.equals(hostName)) { - synchronized (host) { - ContextMapElement[] contexts = host.contexts; - if( contexts.length == 0 ){ - return; - } - ContextMapElement[] newContexts = new ContextMapElement[contexts.length - 1]; - if (WebappServletMapper.removeMap(contexts, newContexts, path)) { - host.contexts = newContexts; - // Recalculate nesting - host.nesting = 0; - for (int i = 0; i < newContexts.length; i++) { - int slashCount = WebappServletMapper.slashCount(newContexts[i].name); - if (slashCount > host.nesting) { - host.nesting = slashCount; - } - } - } - } - } - } +public class WebappContextMapper implements Filter, UserHostMapper { + ServletContext rootContext; + Map<MessageBytes, ServletContext> contexts = new HashMap(); - // -------------------------------------------------------- Private Methods + public WebappContextMapper() { + } - /** Find a the host. - * Override for corner cases ( wildcards, huge number of hosts, - * special patterns, dynamic behavior ). - * - */ - public HostMappingInfo findHost(String hostName) { - if (hostName == null || "".equals(hostName)) return defaultHost; - HostMappingInfo host = null; - if(hostMap.size() > 0 ) { - // don't bother if we only have default host - host = (HostMappingInfo)hostMap.get(hostName.toLowerCase()); - } - if (host == null) host = defaultHost; - return host; - } + public void addHost(String name, String[] aliases) { + } + + /** + * Add a new Context to an existing Host. + * + * @param hostName Virtual host name this context belongs to + * @param contextPath Context path + * @param context Context object + * @param welcomeResources Welcome files defined for this context + * @param resources Static resources of the context + */ + public void addContext(String hostName, + ServletContext context) + throws ServletException + { + String path = context.getContextPath(); + if (path.lastIndexOf("/") > 0) { + throw new ServletException("Base context mapper supports only one level"); + } + if ("/".equals(path)) { + rootContext = context; + } + MessageBytes mb = new MessageBytes(); + mb.setChars(path.toCharArray(), 0, path.length()); + contexts.put(mb, context); + } + + + /** + * Remove a context from an existing host. + * + * @param hostName Virtual host name this context belongs to + * @param path Context path + */ + public void removeContext(String hostName, String path) + throws ServletException { + if ("/".equals(path)) { + rootContext = null; + } + contexts.remove(path); + } + + /** + * Map the specified URI. + */ + private void mapContext(ServletRequestImpl req) + throws IOException, ServletException { + MessageBytes uriMB = req.getCoyoteRequest().decodedURI(); + MappingData mappingData = req.getMappingData(); + uriMB.toChars(); + CharChunk uri = uriMB.getCharChunk(); + + + if (uri.length() < 2 || contexts.size() == 0) { + mappingData.context = rootContext; + mappingData.contextPath.setString(rootContext.getContextPath()); + return; + } + + int nextSlash = uri.indexOf('/', 1); + if (nextSlash == -1) { + nextSlash = uri.length(); + } + mappingData.contextPath.setChars(uri.getChars(), 0, nextSlash); + ServletContext servletContext = contexts.get(mappingData.contextPath); + + if (servletContext != null) { + mappingData.context = servletContext; + } else { + mappingData.context = rootContext; + mappingData.contextPath.setString(rootContext.getContextPath()); + } + } + + public void init(FilterConfig filterConfig) throws ServletException { + } + + public void doFilter(ServletRequest request, + ServletResponse response, + FilterChain chain) + throws IOException, ServletException { + ServletRequestImpl req = (ServletRequestImpl)request; + mapContext(req); + } - /** - * Map the specified URI. - */ - public void mapContext(ServletRequestImpl req) - throws IOException, ServletException { - String host = req.getHeader("Host"); - MessageBytes uriMB = req.getDecodedRequestURIMB(); - MappingData mappingData = req.getMappingData(); - - if (host == null) { - host = defaultHost.hostData.hostName; - } - uriMB.toChars(); - CharChunk uri = uriMB.getCharChunk(); - - //uri.setLimit(-1); - - // Virtual host mapping - if (mappingData.host == null) { - mappingData.host = findHost(host.toString()); - } - if (mappingData.host == null) { - throw new ServletException("Host not found"); - } - - ContextMapElement[] contexts = - ((HostMappingInfo)mappingData.host).contexts; - ContextMapElement context = null; - int nesting = ((HostMappingInfo)mappingData.host).nesting; - - // Context mapping - if (mappingData.context == null) { - int pos = WebappServletMapper.find(contexts, uri); - if (pos == -1) { - mappingData.context = contexts[0].object; - mappingData.contextPath.setString(contexts[0].name); - return; - } - - int lastSlash = -1; - int uriEnd = uri.getEnd(); - int length = -1; - boolean found = false; - while (pos >= 0) { - if (uri.startsWith(contexts[pos].name)) { - length = contexts[pos].name.length(); - if (uri.getLength() == length) { - found = true; - break; - } else if (uri.startsWithIgnoreCase("/", length)) { - found = true; - break; - } - } - if (lastSlash == -1) { - lastSlash = WebappServletMapper.nthSlash(uri, nesting + 1); - } else { - lastSlash = WebappServletMapper.lastSlash(uri); - } - uri.setEnd(lastSlash); - pos = WebappServletMapper.find(contexts, uri); - } - uri.setEnd(uriEnd); - - if (!found) { - mappingData.context = contexts[0].object; - mappingData.contextPath.setString(contexts[0].name); - } else { - context = contexts[pos]; - mappingData.context = context.object; - mappingData.contextPath.setString(context.name); - } - } - } - - - public void init(FilterConfig filterConfig) throws ServletException { - } - - public void doFilter(ServletRequest request, - ServletResponse response, - FilterChain chain) - throws IOException, ServletException { - ServletRequestImpl req = (ServletRequestImpl)request; - mapContext(req); - } - - public void destroy() { - } - - public ServletContextImpl getContext(ServletContextImpl impl, String uri) { - HostMappingInfo host = (HostMappingInfo) contextsParents.get(impl); - if (host == null) host = defaultHost; - return getContext(host, uri); - } - - ServletContextImpl getContext(HostMappingInfo host, String uri) { - ServletContextImpl child = null; - try { - - String mapuri = uri; - while (true) { - child = (ServletContextImpl) host.children.get(mapuri); - if (child != null) - break; - int slash = mapuri.lastIndexOf('/'); - if (slash < 0) - break; - mapuri = mapuri.substring(0, slash); - } - } catch (Throwable t) { - return (null); - } - return child; - } - - public void removeContext(ServletContextImpl ctx) { - // TODO Auto-generated method stub - - } - - + public void destroy() { + } } Modified: tomcat/sandbox/tomcat-lite/java/org/apache/tomcat/lite/WebappFilterMapper.java URL: http://svn.apache.org/viewvc/tomcat/sandbox/tomcat-lite/java/org/apache/tomcat/lite/WebappFilterMapper.java?rev=640688&r1=640687&r2=640688&view=diff ============================================================================== --- tomcat/sandbox/tomcat-lite/java/org/apache/tomcat/lite/WebappFilterMapper.java (original) +++ tomcat/sandbox/tomcat-lite/java/org/apache/tomcat/lite/WebappFilterMapper.java Mon Mar 24 21:02:18 2008 @@ -31,7 +31,7 @@ import javax.servlet.ServletResponse; import javax.servlet.http.HttpServletRequest; -import org.apache.tomcat.util.http.RequestUtil; +import org.apache.tomcat.servlets.util.RequestUtil; /** * First filter after the context and servlet are mapped. It will add Modified: tomcat/sandbox/tomcat-lite/java/org/apache/tomcat/lite/WebappServletMapper.java URL: http://svn.apache.org/viewvc/tomcat/sandbox/tomcat-lite/java/org/apache/tomcat/lite/WebappServletMapper.java?rev=640688&r1=640687&r2=640688&view=diff ============================================================================== --- tomcat/sandbox/tomcat-lite/java/org/apache/tomcat/lite/WebappServletMapper.java (original) +++ tomcat/sandbox/tomcat-lite/java/org/apache/tomcat/lite/WebappServletMapper.java Mon Mar 24 21:02:18 2008 @@ -17,11 +17,19 @@ package org.apache.tomcat.lite; import java.io.File; +import java.io.IOException; + +import javax.servlet.Filter; +import javax.servlet.FilterChain; +import javax.servlet.FilterConfig; +import javax.servlet.ServletException; +import javax.servlet.ServletRequest; +import javax.servlet.ServletResponse; -import org.apache.tomcat.lite.util.MappingData; import org.apache.tomcat.util.buf.Ascii; import org.apache.tomcat.util.buf.CharChunk; import org.apache.tomcat.util.buf.MessageBytes; +import org.apache.tomcat.util.http.mapper.MappingData; /** * Mapper, which implements the servlet API mapping rules (which are derived @@ -32,8 +40,10 @@ * * For corner cases ( very large number of rules, dynamic rules, etc ) you * can override the mapper for a context with a class extending this. + * + * TODO: remove, use coyote-level mapper or user-space */ -public class WebappServletMapper { +public class WebappServletMapper implements Filter { /** * Context associated with this wrapper, used for wrapper mapping. @@ -851,6 +861,20 @@ public static class WrapperMapElement extends MapElement { public boolean jspWildCard = false; + } + + + public void destroy() { + } + + + public void doFilter(ServletRequest request, ServletResponse response, + FilterChain chain) throws IOException, + ServletException { + } + + + public void init(FilterConfig filterConfig) throws ServletException { } } --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]