001: /*
002: * VirtualHost.java
003: *
004: *
005: * Copyright (c) 2003 Rimfaxe ApS (www.rimfaxe.com).
006: * All rights reserved.
007: *
008: * This package is written by Lars Andersen <lars@rimfaxe.com>
009: * and licensed by Rimfaxe ApS.
010: *
011: * Redistribution and use in source and binary forms, with or without
012: * modification, are permitted provided that the following conditions
013: * are met:
014: *
015: * 1. Redistributions of source code must retain the above copyright
016: * notice, this list of conditions and the following disclaimer.
017: *
018: * 2. Redistributions in binary form must reproduce the above copyright
019: * notice, this list of conditions and the following disclaimer in
020: * the documentation and/or other materials provided with the
021: * distribution.
022: *
023: * 3. The end-user documentation included with the redistribution, if
024: * any, must include the following acknowlegement:
025: * "This product includes software developed by Rimfaxe ApS
026: (www.rimfaxe.com)"
027: * Alternately, this acknowlegement may appear in the software itself,
028: * if and wherever such third-party acknowlegements normally appear.
029: *
030: * 4. The names "Rimfaxe", "Rimfaxe Software", "Lars Andersen" and
031: * "Rimfaxe WebServer" must not be used to endorse or promote products
032: * derived from this software without prior written permission. For written
033: * permission, please contact info@rimfaxe.com
034: *
035: * 5. Products derived from this software may not be called "Rimfaxe"
036: * nor may "Rimfaxe" appear in their names without prior written
037: * permission of the Rimfaxe ApS.
038: *
039: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
040: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
041: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
042: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
043: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
044: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
045: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
046: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
047: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
048: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
049: * SUCH DAMAGE.
050: *
051: */
052:
053: package com.rimfaxe.webserver;
054:
055: import java.util.*;
056:
057: import org.w3c.dom.Attr;
058: import org.w3c.dom.Document;
059: import org.w3c.dom.NamedNodeMap;
060: import org.w3c.dom.Node;
061: import org.w3c.dom.NodeList;
062:
063: import com.rimfaxe.util.*;
064:
065: /**
066: *
067: * @author Lars Andersen
068: */
069: public class VirtualHost {
070: String name = "";
071:
072: boolean admin = false;
073: boolean isdefault = false;
074: boolean debug = false;
075: boolean trace = false;
076:
077: RimfaxeVector connectors = new RimfaxeVector();
078: RimfaxeVector webcontexts = new RimfaxeVector();
079:
080: com.rimfaxe.webserver.servletapi.session.SessionStore sessionstore;
081:
082: Configuration conf = null;
083:
084: /** Creates a new instance of VirtualHost */
085: public VirtualHost(Node node) {
086: name = node.getAttributes().getNamedItem("name").getNodeValue()
087: .trim();
088: traverse(node);
089:
090: sessionstore = new com.rimfaxe.webserver.servletapi.session.SessionStore(
091: this );
092:
093: }
094:
095: public com.rimfaxe.webserver.servletapi.RimfaxeServletContext getServletContext(String str)
096: {
097: com.rimfaxe.webserver.servletapi.RimfaxeServletContext best = null;
098: int longestmatch = -1;
099: Enumeration enum = webcontexts.elements();
100: while (enum.hasMoreElements())
101: {
102: WebContext ctx = (WebContext) enum.nextElement();
103: int m = ctx.matchURI(str);
104: if (m>longestmatch)
105: {
106: best = ctx.getServletContext();
107: longestmatch = m;
108: }
109: }
110: return best;
111: }
112:
113: public void initialize()
114: {
115: com.rimfaxe.webserver.compiler.GCJcompiler gcj = new com.rimfaxe.webserver.compiler.GCJcompiler();
116: Enumeration enum = webcontexts.elements();
117: while (enum.hasMoreElements())
118: {
119: WebContext webcontext = (WebContext) enum.nextElement();
120:
121: gcj.compileWebApp(this ,webcontext);
122:
123: webcontext.initialize();
124: }
125: }
126:
127: public boolean acceptHost(String str)
128: {
129: Enumeration enum = connectors.elements();
130: while (enum.hasMoreElements())
131: {
132: Connector c = (Connector) enum.nextElement();
133: if (c.acceptHost(str)) return true;
134: }
135:
136: return false;
137: }
138:
139: public RimfaxeVector getConnectors() {
140: return connectors;
141: }
142:
143: public com.rimfaxe.webserver.servletapi.session.SessionStore getSessionStore() {
144: return sessionstore;
145: }
146:
147: public String getName() {
148: return name;
149: }
150:
151: public boolean getDebug() {
152: return debug;
153: }
154:
155: public WebContext getWebContext(String str)
156: {
157: WebContext best = null;
158: int longestmatch = -1;
159: Enumeration enum = webcontexts.elements();
160: while (enum.hasMoreElements())
161: {
162: WebContext ctx = (WebContext) enum.nextElement();
163: int m = ctx.matchURI(str);
164: if (m>longestmatch)
165: {
166: best = ctx;
167: longestmatch = m;
168: }
169: }
170: return best;
171: }
172:
173: public boolean allowAdmin() {
174: return this .admin;
175: }
176:
177: public boolean isDefault() {
178: return this .isdefault;
179: }
180:
181: private String traverse(Node node) {
182:
183: StringBuffer str = new StringBuffer();
184:
185: if (node == null) {
186: return "";
187: }
188: int type = node.getNodeType();
189: switch (type) {
190:
191: case Node.DOCUMENT_NODE: {
192: traverse(((Document) node).getDocumentElement());
193: break;
194: }
195:
196: case Node.ELEMENT_NODE: {
197: //System.out.println("node = "+node.getNodeName());
198:
199: if (node.getNodeName().equalsIgnoreCase("connector")) {
200: connectors.addElement(new Connector(node));
201: } else if (node.getNodeName()
202: .equalsIgnoreCase("webcontext")) {
203: webcontexts.addElement(new WebContext(this , node));
204: } else if (node.getNodeName().equalsIgnoreCase("debug")) {
205: NodeList children = node.getChildNodes();
206: if (children != null) {
207: int len = children.getLength();
208: for (int i = 0; i < len; i++) {
209: String val = traverse(children.item(i));
210: if (val.trim().equalsIgnoreCase("true"))
211: debug = true;
212: else
213: debug = false;
214: }
215: }
216: } else if (node.getNodeName().equalsIgnoreCase("trace")) {
217: NodeList children = node.getChildNodes();
218: if (children != null) {
219: int len = children.getLength();
220: for (int i = 0; i < len; i++) {
221: String val = traverse(children.item(i));
222: if (val.trim().equalsIgnoreCase("true"))
223: trace = true;
224: else
225: trace = false;
226: }
227: }
228: } else if (node.getNodeName().equalsIgnoreCase("admin")) {
229: NodeList children = node.getChildNodes();
230: if (children != null) {
231: int len = children.getLength();
232: for (int i = 0; i < len; i++) {
233: String val = traverse(children.item(i));
234: if (val.trim().equalsIgnoreCase("true"))
235: admin = true;
236: else
237: admin = false;
238: }
239: }
240: } else {
241: NodeList children = node.getChildNodes();
242: if (children != null) {
243: int len = children.getLength();
244: for (int i = 0; i < len; i++) {
245: String val = traverse(children.item(i));
246: }
247: }
248: }
249: break;
250: }
251:
252: case Node.TEXT_NODE: {
253: //if (node instanceof TextImpl)
254: //{
255: str.append(node.getNodeValue());
256: //}
257: break;
258: }
259: }
260: return str.toString();
261: }
262:
263: public String toXML() {
264: return toXML(false);
265: }
266:
267: public String toXML(boolean snapshot)
268: {
269: StringBuffer buf = new StringBuffer();
270:
271: buf.append(" <virtual>\n\n");
272:
273: Enumeration enum = connectors.elements();
274: while (enum.hasMoreElements())
275: {
276: Connector c = (Connector) enum.nextElement();
277: buf.append( c.toXML() );
278: }
279:
280: buf.append("\n");
281: buf.append(" <default> "+isdefault+ " </default>\n");
282: buf.append(" <admin> "+admin+" </admin>\n\n");
283:
284: buf.append(" <debug> "+debug+ "</debug>\n");
285: buf.append(" <trace> "+trace+" </trace>\n\n");
286:
287: enum = webcontexts.elements();
288: while (enum.hasMoreElements())
289: {
290: WebContext c = (WebContext) enum.nextElement();
291: buf.append( c.toXML() );
292: }
293:
294: buf.append(" </virtual>\n\n");
295:
296:
297: return ""+buf;
298: } }
|