📄 mapper.java
字号:
i = (b + a) / 2;
int result = name.compareTo(map[i].name);
if (result > 0) {
a = i;
} else if (result == 0) {
return i;
} else {
b = i;
}
if ((b - a) == 1) {
int result2 = name.compareTo(map[b].name);
if (result2 < 0) {
return a;
} else {
return b;
}
}
}
}
/**
* Compare given char chunk with String.
* Return -1, 0 or +1 if inferior, equal, or superior to the String.
*/
private static final int compare(CharChunk name, int start, int end,
String compareTo) {
int result = 0;
char[] c = name.getBuffer();
int len = compareTo.length();
if ((end - start) < len) {
len = end - start;
}
for (int i = 0; (i < len) && (result == 0); i++) {
if (c[i + start] > compareTo.charAt(i)) {
result = 1;
} else if (c[i + start] < compareTo.charAt(i)) {
result = -1;
}
}
if (result == 0) {
if (compareTo.length() > (end - start)) {
result = -1;
} else if (compareTo.length() < (end - start)) {
result = 1;
}
}
return result;
}
/**
* Compare given char chunk with String ignoring case.
* Return -1, 0 or +1 if inferior, equal, or superior to the String.
*/
private static final int compareIgnoreCase(CharChunk name, int start, int end,
String compareTo) {
int result = 0;
char[] c = name.getBuffer();
int len = compareTo.length();
if ((end - start) < len) {
len = end - start;
}
for (int i = 0; (i < len) && (result == 0); i++) {
if (Ascii.toLower(c[i + start]) > Ascii.toLower(compareTo.charAt(i))) {
result = 1;
} else if (Ascii.toLower(c[i + start]) < Ascii.toLower(compareTo.charAt(i))) {
result = -1;
}
}
if (result == 0) {
if (compareTo.length() > (end - start)) {
result = -1;
} else if (compareTo.length() < (end - start)) {
result = 1;
}
}
return result;
}
/**
* Find the position of the last slash in the given char chunk.
*/
private static final int lastSlash(CharChunk name) {
char[] c = name.getBuffer();
int end = name.getEnd();
int start = name.getStart();
int pos = end;
while (pos > start) {
if (c[--pos] == '/') {
break;
}
}
return (pos);
}
/**
* Find the position of the nth slash, in the given char chunk.
*/
private static final int nthSlash(CharChunk name, int n) {
char[] c = name.getBuffer();
int end = name.getEnd();
int start = name.getStart();
int pos = start;
int count = 0;
while (pos < end) {
if ((c[pos++] == '/') && ((++count) == n)) {
pos--;
break;
}
}
return (pos);
}
/**
* Return the slash count in a given string.
*/
private static final int slashCount(String name) {
int pos = -1;
int count = 0;
while ((pos = name.indexOf('/', pos + 1)) != -1) {
count++;
}
return count;
}
/**
* Insert into the right place in a sorted MapElement array, and prevent
* duplicates.
*/
private static final boolean insertMap
(MapElement[] oldMap, MapElement[] newMap, MapElement newElement) {
int pos = find(oldMap, newElement.name);
if ((pos != -1) && (newElement.name.equals(oldMap[pos].name))) {
return false;
}
System.arraycopy(oldMap, 0, newMap, 0, pos + 1);
newMap[pos + 1] = newElement;
System.arraycopy
(oldMap, pos + 1, newMap, pos + 2, oldMap.length - pos - 1);
return true;
}
/**
* Insert into the right place in a sorted MapElement array.
*/
private static final boolean removeMap
(MapElement[] oldMap, MapElement[] newMap, String name) {
int pos = find(oldMap, name);
if ((pos != -1) && (name.equals(oldMap[pos].name))) {
System.arraycopy(oldMap, 0, newMap, 0, pos);
System.arraycopy(oldMap, pos + 1, newMap, pos,
oldMap.length - pos - 1);
return true;
}
return false;
}
// ------------------------------------------------- MapElement Inner Class
protected static abstract class MapElement {
public String name = null;
public Object object = null;
}
// ------------------------------------------------------- Host Inner Class
protected static final class Host
extends MapElement {
public ContextList contextList = null;
}
// ------------------------------------------------ ContextList Inner Class
protected static final class ContextList {
public Context[] contexts = new Context[0];
public int nesting = 0;
}
// ---------------------------------------------------- Context Inner Class
protected static final class Context
extends MapElement {
public String path = null;
public String[] welcomeResources = new String[0];
public javax.naming.Context resources = null;
public Wrapper defaultWrapper = null;
public Wrapper[] exactWrappers = new Wrapper[0];
public Wrapper[] wildcardWrappers = new Wrapper[0];
public Wrapper[] extensionWrappers = new Wrapper[0];
public int nesting = 0;
}
// ---------------------------------------------------- Wrapper Inner Class
protected static class Wrapper
extends MapElement {
public String path = null;
public boolean jspWildCard = false;
}
// -------------------------------------------------------- Testing Methods
// FIXME: Externalize this
/*
public static void main(String args[]) {
try {
Mapper mapper = new Mapper();
System.out.println("Start");
mapper.addHost("sjbjdvwsbvhrb", new String[0], "blah1");
mapper.addHost("sjbjdvwsbvhr/", new String[0], "blah1");
mapper.addHost("wekhfewuifweuibf", new String[0], "blah2");
mapper.addHost("ylwrehirkuewh", new String[0], "blah3");
mapper.addHost("iohgeoihro", new String[0], "blah4");
mapper.addHost("fwehoihoihwfeo", new String[0], "blah5");
mapper.addHost("owefojiwefoi", new String[0], "blah6");
mapper.addHost("iowejoiejfoiew", new String[0], "blah7");
mapper.addHost("iowejoiejfoiew", new String[0], "blah17");
mapper.addHost("ohewoihfewoih", new String[0], "blah8");
mapper.addHost("fewohfoweoih", new String[0], "blah9");
mapper.addHost("ttthtiuhwoih", new String[0], "blah10");
mapper.addHost("lkwefjwojweffewoih", new String[0], "blah11");
mapper.addHost("zzzuyopjvewpovewjhfewoih", new String[0], "blah12");
mapper.addHost("xxxxgqwiwoih", new String[0], "blah13");
mapper.addHost("qwigqwiwoih", new String[0], "blah14");
System.out.println("Map:");
for (int i = 0; i < mapper.hosts.length; i++) {
System.out.println(mapper.hosts[i].name);
}
mapper.setDefaultHostName("ylwrehirkuewh");
String[] welcomes = new String[2];
welcomes[0] = "boo/baba";
welcomes[1] = "bobou";
mapper.addContext("iowejoiejfoiew", "", "context0", new String[0], null);
mapper.addContext("iowejoiejfoiew", "/foo", "context1", new String[0], null);
mapper.addContext("iowejoiejfoiew", "/foo/bar", "context2", welcomes, null);
mapper.addContext("iowejoiejfoiew", "/foo/bar/bla", "context3", new String[0], null);
mapper.addWrapper("iowejoiejfoiew", "/foo/bar", "/fo/*", "wrapper0");
mapper.addWrapper("iowejoiejfoiew", "/foo/bar", "/", "wrapper1");
mapper.addWrapper("iowejoiejfoiew", "/foo/bar", "/blh", "wrapper2");
mapper.addWrapper("iowejoiejfoiew", "/foo/bar", "*.jsp", "wrapper3");
mapper.addWrapper("iowejoiejfoiew", "/foo/bar", "/blah/bou/*", "wrapper4");
mapper.addWrapper("iowejoiejfoiew", "/foo/bar", "/blah/bobou/*", "wrapper5");
mapper.addWrapper("iowejoiejfoiew", "/foo/bar", "*.htm", "wrapper6");
MappingData mappingData = new MappingData();
MessageBytes host = MessageBytes.newInstance();
host.setString("iowejoiejfoiew");
MessageBytes uri = MessageBytes.newInstance();
uri.setString("/foo/bar/blah/bobou/foo");
uri.toChars();
uri.getCharChunk().setLimit(-1);
mapper.map(host, uri, mappingData);
System.out.println("MD Host:" + mappingData.host);
System.out.println("MD Context:" + mappingData.context);
System.out.println("MD Wrapper:" + mappingData.wrapper);
System.out.println("contextPath:" + mappingData.contextPath);
System.out.println("wrapperPath:" + mappingData.wrapperPath);
System.out.println("pathInfo:" + mappingData.pathInfo);
System.out.println("redirectPath:" + mappingData.redirectPath);
mappingData.recycle();
mapper.map(host, uri, mappingData);
System.out.println("MD Host:" + mappingData.host);
System.out.println("MD Context:" + mappingData.context);
System.out.println("MD Wrapper:" + mappingData.wrapper);
System.out.println("contextPath:" + mappingData.contextPath);
System.out.println("wrapperPath:" + mappingData.wrapperPath);
System.out.println("pathInfo:" + mappingData.pathInfo);
System.out.println("redirectPath:" + mappingData.redirectPath);
for (int i = 0; i < 1000000; i++) {
mappingData.recycle();
mapper.map(host, uri, mappingData);
}
long time = System.currentTimeMillis();
for (int i = 0; i < 1000000; i++) {
mappingData.recycle();
mapper.map(host, uri, mappingData);
}
System.out.println("Elapsed:" + (System.currentTimeMillis() - time));
System.out.println("MD Host:" + mappingData.host);
System.out.println("MD Context:" + mappingData.context);
System.out.println("MD Wrapper:" + mappingData.wrapper);
System.out.println("contextPath:" + mappingData.contextPath);
System.out.println("wrapperPath:" + mappingData.wrapperPath);
System.out.println("requestPath:" + mappingData.requestPath);
System.out.println("pathInfo:" + mappingData.pathInfo);
System.out.println("redirectPath:" + mappingData.redirectPath);
} catch (Exception e) {
e.printStackTrace();
}
}
*/
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -