001: /*
002: * Web.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.webapp;
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.webserver.servletapi.RimfaxeFilterChain;
064: import com.rimfaxe.util.*;
065:
066: /**
067: *
068: * @author Lars Andersen
069: */
070: public class Web {
071: HashMap mimetypes = new HashMap();
072: HashMap servlets = new HashMap();
073: HashMap filters = new HashMap();
074:
075: Hashtable taglibs = new Hashtable();
076:
077: ArrayList servletmappings = new ArrayList();
078: ArrayList staticmappings = new ArrayList();
079: ArrayList filtermappings = new ArrayList();
080:
081: SessionConfig sessionconfig = null;
082: WelcomeFileList welcomefilelist = null;
083:
084: String rootdir = "";
085:
086: long last_modified = System.currentTimeMillis();
087: String filename = "config/default.xml";
088:
089: /** Creates a new instance of Web */
090: public Web() {
091: rootdir = "";
092: parseXML();
093: }
094:
095: /** Creates a new instance of Web */
096: public Web(String filename) {
097: this .filename = filename;
098: rootdir = filename.substring(0, filename.length() - 16);
099: parseXML();
100: }
101:
102: public void parseXML() {
103: com.rimfaxe.util.RimfaxeFile file = new com.rimfaxe.util.RimfaxeFile(
104: filename);
105: String xml = file.read();
106:
107: java.io.File f = new java.io.File(filename);
108: last_modified = f.lastModified();
109:
110: if (file.getError() == 0) {
111: com.rimfaxe.xml.ParserInterface parser = com.rimfaxe.xml.ParserFactory
112: .getInstance().checkoutParser();
113: parser.parse(xml, false);
114: Document document = parser.getDocument();
115: traverse(document);
116: }
117: }
118:
119: /** Creates a new instance of Web */
120: //public Web(String xml, boolean validate)
121: //{
122: // com.rimfaxe.xml.ParserInterface parser = com.rimfaxe.xml.ParserFactory.getInstance().checkoutParser();
123: // parser.parse(xml,false);
124: // Document document = parser.getDocument();
125: // traverse(document);
126: //}
127: public void reload()
128: {
129: java.io.File f = new java.io.File(filename);
130: long lm = f.lastModified();
131:
132: if (lm>last_modified)
133: {
134: System.out.println("-- web.xml has been changed - reload");
135: parseXML();
136: }
137:
138:
139: Enumeration enum = taglibs.elements();
140: while (enum.hasMoreElements())
141: {
142: TagLibraryInfoImpl tl = (TagLibraryInfoImpl) enum.nextElement();
143: tl.reload();
144: }
145: }
146:
147: public SessionConfig getSessionConfig() {
148: return sessionconfig;
149: }
150:
151: public Hashtable getTagLibraries() {
152: return taglibs;
153: }
154:
155: public TagLibraryInfoImpl getTagLibrary(String uri) {
156: TagLibraryInfoImpl tl = (TagLibraryInfoImpl) taglibs.get(uri);
157: return tl;
158: }
159:
160: public String[] getWelcomeFileList() {
161: if (this .welcomefilelist == null)
162: return null;
163:
164: return this .welcomefilelist.getArray();
165: }
166:
167: public String getMimeType(String ext) {
168: MimeMapping t = (MimeMapping) mimetypes.get(ext);
169: if (t == null)
170: return null;
171: return t.getMimeType();
172: }
173:
174: public ArrayList getServletMappings() {
175: return servletmappings;
176: }
177:
178: public ArrayList getStaticMappings() {
179: return staticmappings;
180: }
181:
182: public ArrayList getFilterMappings() {
183: return filtermappings;
184: }
185:
186: public Servlet getServlet(String name) {
187: Servlet s = (Servlet) servlets.get(name.toLowerCase());
188: return s;
189: }
190:
191: public Filter getFilter(String name) {
192:
193: Filter f = (Filter) filters.get(name.toLowerCase());
194: return f;
195: }
196:
197: public Enumeration getServletNames() {
198:
199: Vector vec = new Vector();
200: Object[] a = servlets.values().toArray();
201: for (int i = 0; i < a.length; i++) {
202: Servlet sdef = (Servlet) a[i];
203: vec.addElement(sdef.getKey());
204: }
205: return vec.elements();
206: }
207:
208: private String traverse(Node node) {
209:
210: StringBuffer str = new StringBuffer();
211:
212: if (node == null) {
213: return "";
214: }
215: int type = node.getNodeType();
216: switch (type) {
217:
218: case Node.DOCUMENT_NODE: {
219: traverse(((Document) node).getDocumentElement());
220: break;
221: }
222:
223: case Node.ELEMENT_NODE: {
224: if (node.getNodeName().equalsIgnoreCase("servlet")) {
225: Servlet s = new Servlet(node);
226: servlets.put(s.getKey().toLowerCase(), s);
227: } else if (node.getNodeName().equalsIgnoreCase("filter")) {
228: Filter f = new Filter(node);
229: filters.put(f.getKey().toLowerCase(), f);
230: } else if (node.getNodeName().equalsIgnoreCase("taglib")) {
231: TagLibraryInfoImpl tl = new TagLibraryInfoImpl(node,
232: rootdir);
233: taglibs.put(tl.getKey().toLowerCase(), tl);
234: } else if (node.getNodeName().equalsIgnoreCase(
235: "static-mapping")) {
236:
237: StaticMapping sm = new StaticMapping(node);
238: staticmappings.add(sm);
239: } else if (node.getNodeName().equalsIgnoreCase(
240: "servlet-mapping")) {
241:
242: ServletMapping sm = new ServletMapping(node);
243: servletmappings.add(sm);
244: } else if (node.getNodeName().equalsIgnoreCase(
245: "filter-mapping")) {
246:
247: FilterMapping fm = new FilterMapping(node);
248: filtermappings.add(fm);
249: } else if (node.getNodeName().equalsIgnoreCase(
250: "session-config")) {
251:
252: SessionConfig sc = new SessionConfig(node);
253: sessionconfig = sc;
254: } else if (node.getNodeName().equalsIgnoreCase(
255: "mime-mapping")) {
256: MimeMapping mm = new MimeMapping(node);
257: mimetypes.put(mm.getKey(), mm);
258: } else if (node.getNodeName().equalsIgnoreCase(
259: "welcome-file-list")) {
260:
261: WelcomeFileList wfl = new WelcomeFileList(node);
262: welcomefilelist = wfl;
263: } else {
264: NodeList children = node.getChildNodes();
265: if (children != null) {
266: int len = children.getLength();
267: for (int i = 0; i < len; i++) {
268: String val = traverse(children.item(i));
269: }
270: }
271: }
272: break;
273: }
274:
275: case Node.TEXT_NODE: {
276: //if (node instanceof TextImpl)
277: //{
278: str.append(node.getNodeValue());
279: //}
280: break;
281: }
282: }
283: return str.toString();
284: }
285:
286: public String toXML() {
287: StringBuffer buf = new StringBuffer();
288:
289: buf
290: .append("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n\n");
291:
292: buf
293: .append("<!DOCTYPE web-app \n PUBLIC \"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN\" \n \"http://java.sun.com/dtd/web-app_2_3.dtd\"> \n\n");
294: buf.append("<web-app> \n\n");
295:
296: buf.append("\n");
297:
298: Object[] objarray = servlets.values().toArray();
299: for (int i = 0; i < objarray.length; i++) {
300: Servlet s = (Servlet) objarray[i];
301: buf.append(s.toXML() + "\n");
302: }
303:
304: buf.append("\n");
305:
306: objarray = filters.values().toArray();
307: for (int i = 0; i < objarray.length; i++) {
308: Filter f = (Filter) objarray[i];
309: buf.append(f.toXML() + "\n");
310: }
311:
312: buf.append("\n");
313:
314: objarray = filtermappings.toArray();
315: for (int i = 0; i < objarray.length; i++) {
316: FilterMapping fm = (FilterMapping) objarray[i];
317: buf.append(fm.toXML() + "\n");
318: }
319:
320: buf.append("\n");
321:
322: objarray = staticmappings.toArray();
323: for (int i = 0; i < objarray.length; i++) {
324: StaticMapping sm = (StaticMapping) objarray[i];
325: buf.append(sm.toXML() + "\n");
326: }
327:
328: buf.append("\n");
329:
330: objarray = servletmappings.toArray();
331: for (int i = 0; i < objarray.length; i++) {
332: ServletMapping sm = (ServletMapping) objarray[i];
333: buf.append(sm.toXML() + "\n");
334: }
335:
336: if (sessionconfig != null)
337: buf.append("\n" + sessionconfig.toXML() + "\n");
338:
339: objarray = mimetypes.values().toArray();
340: for (int i = 0; i < objarray.length; i++) {
341: MimeMapping mm = (MimeMapping) objarray[i];
342: buf.append(mm.toXML());
343: }
344:
345: if (welcomefilelist != null)
346: buf.append("\n" + welcomefilelist.toXML() + "\n");
347:
348: buf.append("</web-app>\n\n");
349:
350: return "" + buf;
351: }
352:
353: }
|