| 1 | |
package org.webslinger.servlet; |
| 2 | |
|
| 3 | |
import java.io.File; |
| 4 | |
import java.io.FileInputStream; |
| 5 | |
import java.io.IOException; |
| 6 | |
import java.net.URL; |
| 7 | |
import java.util.Collection; |
| 8 | |
import java.util.ArrayList; |
| 9 | |
import java.util.HashMap; |
| 10 | |
import java.util.HashSet; |
| 11 | |
import java.util.Iterator; |
| 12 | |
import java.util.LinkedHashSet; |
| 13 | |
import java.util.List; |
| 14 | |
import java.util.Set; |
| 15 | |
|
| 16 | |
import javax.management.JMException; |
| 17 | |
import javax.naming.NamingException; |
| 18 | |
import javax.servlet.ServletConfig; |
| 19 | |
import javax.servlet.ServletContext; |
| 20 | |
import javax.servlet.ServletException; |
| 21 | |
import javax.servlet.http.HttpServletRequest; |
| 22 | |
import javax.servlet.http.HttpServletResponse; |
| 23 | |
import javax.xml.parsers.ParserConfigurationException; |
| 24 | |
import org.w3c.dom.Document; |
| 25 | |
import org.w3c.dom.Element; |
| 26 | |
import org.xml.sax.SAXException; |
| 27 | |
|
| 28 | |
import org.webslinger.AbstractMappingWebslingerServletContextFactory; |
| 29 | |
import org.webslinger.WebslingerServletContext; |
| 30 | |
import org.webslinger.collections.ArrayUtil; |
| 31 | |
import org.webslinger.container.WebslingerContainer; |
| 32 | |
import org.webslinger.servlet.WebslingerServlet; |
| 33 | |
import org.webslinger.util.GeneratedResult; |
| 34 | |
import org.webslinger.util.TTLCachedObject; |
| 35 | |
import org.webslinger.util.TTLObject; |
| 36 | |
import org.webslinger.xml.XmlUtil; |
| 37 | |
|
| 38 | 0 | public class XmlContextMapper extends AbstractMappingWebslingerServletContextFactory { |
| 39 | |
protected File mappingFile; |
| 40 | 0 | protected final HashSet<String> suffixes = new HashSet<String>(); |
| 41 | |
|
| 42 | |
protected ServletContext servletContext; |
| 43 | 0 | protected final HashMap<String, String> hostToServer = new HashMap<String, String>(); |
| 44 | 0 | protected final HashMap<String, Layout> servers = new HashMap<String, Layout>(); |
| 45 | |
|
| 46 | |
public void init(ServletConfig config) throws ServletException, IOException { |
| 47 | 0 | servletContext = config.getServletContext(); |
| 48 | |
try { |
| 49 | 0 | Document doc = XmlUtil.read(servletContext.getResourceAsStream("/WEB-INF/mapping.xml")); |
| 50 | 0 | for (Element e: XmlUtil.getElementsNamed(doc.getDocumentElement(), "suffix")) { |
| 51 | 0 | suffixes.add(e.getAttribute("value")); |
| 52 | |
} |
| 53 | 0 | for (Element e: XmlUtil.getElementsNamed(doc.getDocumentElement(), "server")) { |
| 54 | 0 | LinkedHashSet<String> bases = new LinkedHashSet<String>(); |
| 55 | 0 | for (Element e2: XmlUtil.getElementsNamed(e, "base")) { |
| 56 | 0 | bases.add(e2.getAttribute("target")); |
| 57 | |
} |
| 58 | 0 | servers.put(e.getAttribute("id"), newMappingLayout(e.getAttribute("id"), e.getAttribute("target"), bases.toArray(new String[bases.size()]))); |
| 59 | 0 | } |
| 60 | 0 | for (Element e: XmlUtil.getElementsNamed(doc.getDocumentElement(), "host")) { |
| 61 | 0 | hostToServer.put(e.getAttribute("name"), e.getAttribute("id")); |
| 62 | |
} |
| 63 | 0 | System.err.println("suffixes=" + suffixes); |
| 64 | 0 | System.err.println("servers=" + servers); |
| 65 | 0 | System.err.println("hostToServer=" + hostToServer); |
| 66 | 0 | } catch (Exception e) { |
| 67 | 0 | throw new ServletException(e.getMessage(), e); |
| 68 | 0 | } |
| 69 | 0 | super.init(config, config.getInitParameter("module-base")); |
| 70 | 0 | } |
| 71 | |
|
| 72 | |
protected Layout[] getStartLayouts() throws ServletException, IOException { |
| 73 | 0 | return new Layout[0]; |
| 74 | |
} |
| 75 | |
|
| 76 | |
public void destroy() { |
| 77 | 0 | super.destroy(); |
| 78 | 0 | } |
| 79 | |
|
| 80 | |
|
| 81 | |
|
| 82 | |
|
| 83 | |
|
| 84 | |
|
| 85 | |
|
| 86 | |
protected Set<String> getSuffixes() { |
| 87 | 0 | return suffixes; |
| 88 | |
} |
| 89 | |
|
| 90 | |
protected MappingLayout newMappingLayout(String id, String target, String[] bases) { |
| 91 | 0 | return new MappingLayout(id, target, bases); |
| 92 | |
} |
| 93 | |
|
| 94 | |
protected Layout lookupLayout(String hostName, String contextPath) { |
| 95 | 0 | String id = hostToServer.get(hostName); |
| 96 | 0 | if (id == null) return null; |
| 97 | 0 | return servers.get(id); |
| 98 | |
} |
| 99 | |
|
| 100 | 0 | protected class MappingLayout implements Layout { |
| 101 | |
private final String id; |
| 102 | |
private final String target; |
| 103 | |
private final String[] bases; |
| 104 | |
private int hashCode; |
| 105 | |
|
| 106 | 0 | protected MappingLayout(String id, String target, String[] bases) { |
| 107 | 0 | this.id = id; |
| 108 | 0 | this.target = target; |
| 109 | 0 | this.bases = bases; |
| 110 | 0 | hashCode = target.hashCode() ^ ArrayUtil.hashCodeHelper(bases); |
| 111 | 0 | } |
| 112 | |
|
| 113 | |
public String getId() { |
| 114 | 0 | return id; |
| 115 | |
} |
| 116 | |
|
| 117 | |
public String getTarget() { |
| 118 | 0 | return target; |
| 119 | |
} |
| 120 | |
|
| 121 | |
public String[] getBases() { |
| 122 | 0 | return bases; |
| 123 | |
} |
| 124 | |
|
| 125 | |
public int hashCode() { |
| 126 | 0 | return hashCode; |
| 127 | |
} |
| 128 | |
|
| 129 | |
public boolean equals(Object o) { |
| 130 | 0 | if (!(o instanceof MappingLayout)) return false; |
| 131 | 0 | MappingLayout other = (MappingLayout) o; |
| 132 | 0 | if (!target.equals(other.target)) return false; |
| 133 | 0 | return ArrayUtil.equalsHelper(bases, other.bases); |
| 134 | |
} |
| 135 | |
} |
| 136 | |
} |