📄 mapper.java
字号:
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.tomcat.util.http.mapper;
import javax.naming.NamingException;
import javax.naming.directory.DirContext;
import org.apache.tomcat.util.buf.CharChunk;
import org.apache.tomcat.util.buf.MessageBytes;
import org.apache.tomcat.util.buf.Ascii;
import java.util.List;
import java.util.ArrayList;
/**
* Mapper, which implements the servlet API mapping rules (which are derived
* from the HTTP rules).
*
* @author Remy Maucherat
*/
public final class Mapper {
private static org.apache.juli.logging.Log logger =
org.apache.juli.logging.LogFactory.getLog(Mapper.class);
// ----------------------------------------------------- Instance Variables
/**
* Array containing the virtual hosts definitions.
*/
protected Host[] hosts = new Host[0];
/**
* Default host name.
*/
protected String defaultHostName = null;
/**
* Context associated with this wrapper, used for wrapper mapping.
*/
protected Context context = new Context();
// --------------------------------------------------------- Public Methods
/**
* Get default host.
*
* @return Default host name
*/
public String getDefaultHostName() {
return defaultHostName;
}
/**
* Set default host.
*
* @param defaultHostName Default host name
*/
public void setDefaultHostName(String defaultHostName) {
this.defaultHostName = defaultHostName;
}
/**
* Add a new host to the mapper.
*
* @param name Virtual host name
* @param host Host object
*/
public synchronized void addHost(String name, String[] aliases,
Object host) {
Host[] newHosts = new Host[hosts.length + 1];
Host newHost = new Host();
ContextList contextList = new ContextList();
newHost.name = name;
newHost.contextList = contextList;
newHost.object = host;
if (insertMap(hosts, newHosts, newHost)) {
hosts = newHosts;
}
for (int i = 0; i < aliases.length; i++) {
newHosts = new Host[hosts.length + 1];
newHost = new Host();
newHost.name = aliases[i];
newHost.contextList = contextList;
newHost.object = host;
if (insertMap(hosts, newHosts, newHost)) {
hosts = newHosts;
}
}
}
/**
* Remove a host from the mapper.
*
* @param name Virtual host name
*/
public synchronized void removeHost(String name) {
// Find and remove the old host
int pos = find(hosts, name);
if (pos < 0) {
return;
}
Object host = hosts[pos].object;
Host[] newHosts = new Host[hosts.length - 1];
if (removeMap(hosts, newHosts, name)) {
hosts = newHosts;
}
// Remove all aliases (they will map to the same host object)
for (int i = 0; i < newHosts.length; i++) {
if (newHosts[i].object == host) {
Host[] newHosts2 = new Host[hosts.length - 1];
if (removeMap(hosts, newHosts2, newHosts[i].name)) {
hosts = newHosts2;
}
}
}
}
public String[] getHosts() {
String hostN[] = new String[hosts.length];
for( int i = 0; i < hosts.length; i++ ) {
hostN[i] = hosts[i].name;
}
return hostN;
}
/**
* Set context, used for wrapper mapping (request dispatcher).
*
* @param welcomeResources Welcome files defined for this context
* @param resources Static resources of the context
*/
public void setContext(String path, String[] welcomeResources,
javax.naming.Context resources) {
context.name = path;
context.welcomeResources = welcomeResources;
context.resources = resources;
}
/**
* Add a new Context to an existing Host.
*
* @param hostName Virtual host name this context belongs to
* @param path 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, String path, Object context,
String[] welcomeResources, javax.naming.Context resources) {
Host[] hosts = this.hosts;
int pos = find(hosts, hostName);
if( pos <0 ) {
addHost(hostName, new String[0], "");
hosts = this.hosts;
pos = find(hosts, hostName);
}
if (pos < 0) {
logger.error("No host found: " + hostName);
}
Host host = hosts[pos];
if (host.name.equals(hostName)) {
int slashCount = slashCount(path);
synchronized (host) {
Context[] contexts = host.contextList.contexts;
// Update nesting
if (slashCount > host.contextList.nesting) {
host.contextList.nesting = slashCount;
}
Context[] newContexts = new Context[contexts.length + 1];
Context newContext = new Context();
newContext.name = path;
newContext.object = context;
newContext.welcomeResources = welcomeResources;
newContext.resources = resources;
if (insertMap(contexts, newContexts, newContext)) {
host.contextList.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) {
Host[] hosts = this.hosts;
int pos = find(hosts, hostName);
if (pos < 0) {
return;
}
Host host = hosts[pos];
if (host.name.equals(hostName)) {
synchronized (host) {
Context[] contexts = host.contextList.contexts;
if( contexts.length == 0 ){
return;
}
Context[] newContexts = new Context[contexts.length - 1];
if (removeMap(contexts, newContexts, path)) {
host.contextList.contexts = newContexts;
// Recalculate nesting
host.contextList.nesting = 0;
for (int i = 0; i < newContexts.length; i++) {
int slashCount = slashCount(newContexts[i].name);
if (slashCount > host.contextList.nesting) {
host.contextList.nesting = slashCount;
}
}
}
}
}
}
/**
* Return all contexts, in //HOST/PATH form
*
* @return The context names
*/
public String[] getContextNames() {
List list=new ArrayList();
for( int i=0; i<hosts.length; i++ ) {
for( int j=0; j<hosts[i].contextList.contexts.length; j++ ) {
String cname=hosts[i].contextList.contexts[j].name;
list.add("//" + hosts[i].name +
(cname.startsWith("/") ? cname : "/"));
}
}
String res[] = new String[list.size()];
return (String[])list.toArray(res);
}
/**
* Add a new Wrapper to an existing Context.
*
* @param hostName Virtual host name this wrapper belongs to
* @param contextPath Context path this wrapper belongs to
* @param path Wrapper mapping
* @param wrapper Wrapper object
*/
public void addWrapper(String hostName, String contextPath, String path,
Object wrapper) {
addWrapper(hostName, contextPath, path, wrapper, false);
}
public void addWrapper(String hostName, String contextPath, String path,
Object wrapper, boolean jspWildCard) {
Host[] hosts = this.hosts;
int pos = find(hosts, hostName);
if (pos < 0) {
return;
}
Host host = hosts[pos];
if (host.name.equals(hostName)) {
Context[] contexts = host.contextList.contexts;
int pos2 = find(contexts, contextPath);
if( pos2<0 ) {
logger.error("No context found: " + contextPath );
return;
}
Context context = contexts[pos2];
if (context.name.equals(contextPath)) {
addWrapper(context, path, wrapper, jspWildCard);
}
}
}
/**
* Add a wrapper to the context associated with this wrapper.
*
* @param path Wrapper mapping
* @param wrapper The Wrapper object
*/
public void addWrapper(String path, Object wrapper) {
addWrapper(context, path, wrapper);
}
public void addWrapper(String path, Object wrapper, boolean jspWildCard) {
addWrapper(context, path, wrapper, jspWildCard);
}
protected void addWrapper(Context context, String path, Object wrapper) {
addWrapper(context, path, wrapper, false);
}
/**
* Adds a wrapper to the given context.
*
* @param context The context to which to add the wrapper
* @param path Wrapper mapping
* @param wrapper The Wrapper object
* @param jspWildCard true if the wrapper corresponds to the JspServlet
* and the mapping path contains a wildcard; false otherwise
*/
protected void addWrapper(Context context, String path, Object wrapper,
boolean jspWildCard) {
synchronized (context) {
Wrapper newWrapper = new Wrapper();
newWrapper.object = wrapper;
newWrapper.jspWildCard = jspWildCard;
if (path.endsWith("/*")) {
// Wildcard wrapper
newWrapper.name = path.substring(0, path.length() - 2);
Wrapper[] oldWrappers = context.wildcardWrappers;
Wrapper[] newWrappers =
new Wrapper[oldWrappers.length + 1];
if (insertMap(oldWrappers, newWrappers, newWrapper)) {
context.wildcardWrappers = newWrappers;
int slashCount = slashCount(newWrapper.name);
if (slashCount > context.nesting) {
context.nesting = slashCount;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -