001: /*
002: * Configuration.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 Configuration {
070: Edition edition = new Edition();
071:
072: boolean debug = true;
073: boolean active = true;
074:
075: String webapp_mode = "rws";
076:
077: RimfaxeVector daemons = new RimfaxeVector();
078:
079: Vector virtuallist = new Vector();
080:
081: String admin;
082: String password;
083: String RWShome = "";
084:
085: String CryptoProvider = "RIMFAXE";
086:
087: RimfaxeVector sharedlibs = new RimfaxeVector();
088:
089: //com.rimfaxe.webserver.daemon.ServerHandlerManager serverhandlermanager = null;
090:
091: String dhfile = "dh1024.pem";
092: String certfile = "req.pem";
093: String sslpassphrase = "lamatop";
094:
095: com.rimfaxe.webserver.webapp.Web webapp_default = new com.rimfaxe.webserver.webapp.Web();
096:
097: String gcj_compiler = "/usr/local/bin/gcj";
098:
099: int cache_size = 32000;
100:
101: String timezone = "Europe/Copenhagen";
102:
103: String java_encoding = "ISO-8859-1";
104:
105: /** Creates a new instance of Configuration */
106: public Configuration() {
107: com.rimfaxe.util.RimfaxeFile file = new com.rimfaxe.util.RimfaxeFile(
108: "config/rimfaxe-webserver.xml");
109: String xml = file.read();
110:
111: com.rimfaxe.xml.ParserInterface parser = com.rimfaxe.xml.ParserFactory
112: .getInstance().checkoutParser();
113:
114: parser.parse(xml, false);
115: Document document = parser.getDocument();
116:
117: traverse(document);
118:
119: }
120:
121: /** Creates a new instance of Configuration */
122: public Configuration(String xml, boolean validate) {
123: com.rimfaxe.xml.ParserInterface parser = com.rimfaxe.xml.ParserFactory
124: .getInstance().checkoutParser();
125: parser.parse(xml, false);
126: Document document = parser.getDocument();
127:
128: traverse(document);
129: }
130:
131: public void initialize()
132: {
133: com.rimfaxe.webserver.compiler.GCJcompiler gcj = new com.rimfaxe.webserver.compiler.GCJcompiler();
134:
135: // Load shared libs
136: loadShared();
137:
138: Enumeration enum = virtuallist.elements();
139: while (enum.hasMoreElements())
140: {
141: VirtualHost vh = (VirtualHost) enum.nextElement();
142:
143: vh.initialize();
144: }
145:
146:
147: }
148:
149: public void loadShared() {
150: //System.out.println("Load shared");
151: java.io.File root = null;
152:
153: try {
154: //System.out.println("Load shared 1");
155: root = new java.io.File(this .getHome() + "/shared");
156:
157: if (!root.exists())
158: root.mkdir();
159: } catch (Exception e) {
160: }
161:
162: if (root == null)
163: return;
164:
165: String[] dirlist = root.list();
166:
167: //System.out.println("Load shared 2");
168:
169: if (dirlist == null)
170: return;
171:
172: //System.out.println("Load shared 3");
173:
174: for (int i = 0; i < dirlist.length; i++) {
175: String f = dirlist[i];
176:
177: java.io.File temp = new java.io.File(this .getHome()
178: + "/shared" + "/" + f);
179:
180: if (temp.isDirectory()) {
181: //skip
182: } else {
183: if ((f.endsWith(".so")) && (f.startsWith("lib"))) {
184: try {
185:
186: String libname = f.substring(3, f.length() - 3);
187: System.out.println("Shared lib [" + libname
188: + "]");
189: sharedlibs.addElement(libname);
190: } catch (Exception e) {
191: }
192: }
193: }
194: }
195: }
196:
197: public Enumeration getSharedLibs() {
198: return this .sharedlibs.elements();
199: }
200:
201: public com.rimfaxe.webserver.webapp.Web getWebApp() {
202: return webapp_default;
203: }
204:
205: public String getCertfile() {
206: return certfile;
207: }
208:
209: public String getDHfile() {
210: return dhfile;
211: }
212:
213: public String getSSLpassphrase() {
214: return sslpassphrase;
215: }
216:
217: public String getCryptoProvider() {
218: return CryptoProvider;
219: }
220:
221: public String getWebappMode() {
222: return webapp_mode;
223: }
224:
225: public String getAdminIP() {
226: return "127.0.0.1";
227: }
228:
229: public Integer getAdminPort() {
230: return new Integer(8111);
231: }
232:
233: public void save() {
234: com.rimfaxe.util.RimfaxeFile file = new com.rimfaxe.util.RimfaxeFile(
235: "config/rimfaxe-webserver.xml");
236: String xml = toXML();
237: file.rewrite(xml);
238: }
239:
240: public boolean isTrial() {
241: return edition.isTrial();
242: }
243:
244: public long getRunningTime() {
245: return edition.getRunningTime();
246: }
247:
248: public void shutdown() {
249: //serverhandlermanager.shutdown();
250: System.out.println("Shutdown finished");
251: System.exit(0);
252: }
253:
254: public VirtualHost getVirtualHost(String id)
255: {
256: Enumeration enum = virtuallist.elements();
257: while (enum.hasMoreElements())
258: {
259: VirtualHost vhost = (VirtualHost) enum.nextElement();
260: if (vhost.acceptHost(id)) return vhost;
261: }
262: return null;
263: }
264:
265: public String getSerial() {
266: return edition.getSerial();
267: }
268:
269: public String getEdition() {
270: return edition.getEdition();
271: }
272:
273: public String getVersion() {
274: return edition.getVersion();
275: }
276:
277: public String getBanner() {
278: return edition.getBanner();
279: }
280:
281: public String getOS() {
282: return edition.getOS();
283: }
284:
285: public String getAdmin() {
286: return admin;
287: }
288:
289: public String getPassword() {
290: return password;
291: }
292:
293: public String getHome() {
294: return RWShome;
295: }
296:
297: public void setHome(String dir) {
298: RWShome = dir;
299: }
300:
301: public void setAdmin(String admin) {
302: this .admin = admin;
303: }
304:
305: public void setPassword(String password) {
306: this .password = password;
307: }
308:
309: public void setCertfile(String c) {
310: this .certfile = c;
311: }
312:
313: public void setDHfile(String dh) {
314: this .dhfile = dh;
315: }
316:
317: public void setSSLpassphrase(String pass) {
318: this .sslpassphrase = pass;
319: }
320:
321: public Enumeration getVHostList() {
322: return virtuallist.elements();
323: }
324:
325: public Enumeration getVHostPortList()
326: {
327: RimfaxeVector ports = daemons;
328: Enumeration enum = virtuallist.elements();
329: while (enum.hasMoreElements())
330: {
331: VirtualHost vh = (VirtualHost) enum.nextElement();
332: Enumeration enum2 = vh.getConnectors().elements();
333: while (enum2.hasMoreElements())
334: {
335: Connector con = (Connector) enum2.nextElement();
336: ServerPort sp = new ServerPort(con.getIP(), new Integer(con.getPort()), con.getProtocol());
337: if (!ports.contains(sp))
338: {
339: ports.addElement(sp);
340: }
341: }
342:
343: }
344: daemons = ports;
345: return ports.elements();
346: }
347:
348: public RimfaxeVector getDaemons() {
349: return daemons;
350: }
351:
352: public String getGCJ() {
353: return this .gcj_compiler;
354: }
355:
356: public String getJavaEncoding() {
357: return this .java_encoding;
358: }
359:
360: public int getCacheSize() {
361: return this .cache_size;
362: }
363:
364: private String traverse(Node node) {
365:
366: StringBuffer str = new StringBuffer();
367:
368: if (node == null) {
369: return "";
370: }
371: int type = node.getNodeType();
372: switch (type) {
373:
374: case Node.DOCUMENT_NODE: {
375: traverse(((Document) node).getDocumentElement());
376: break;
377: }
378:
379: case Node.ELEMENT_NODE: {
380: if (node.getNodeName().equalsIgnoreCase("virtual")) {
381: //com.rimfaxe.util.Log.log("Virtual server found");
382: VirtualHost vh = new VirtualHost(node);
383:
384: virtuallist.addElement(vh);
385:
386: } else if (node.getNodeName().equalsIgnoreCase("admin")) {
387: NodeList children = node.getChildNodes();
388: if (children != null) {
389: int len = children.getLength();
390: for (int i = 0; i < len; i++) {
391: String val = traverse(children.item(i));
392: admin = val.trim().toLowerCase();
393: }
394: }
395: } else if (node.getNodeName().equalsIgnoreCase("password")) {
396: NodeList children = node.getChildNodes();
397: if (children != null) {
398: int len = children.getLength();
399: for (int i = 0; i < len; i++) {
400: String val = traverse(children.item(i));
401: password = val.trim().toLowerCase();
402: }
403: }
404: } else if (node.getNodeName().equalsIgnoreCase("gcj")) {
405: NodeList children = node.getChildNodes();
406: if (children != null) {
407: int len = children.getLength();
408: for (int i = 0; i < len; i++) {
409: String val = traverse(children.item(i));
410: gcj_compiler = val.trim();
411: }
412: }
413: } else if (node.getNodeName().equalsIgnoreCase(
414: "JavaEncoding")) {
415: NodeList children = node.getChildNodes();
416: if (children != null) {
417: int len = children.getLength();
418: for (int i = 0; i < len; i++) {
419: String val = traverse(children.item(i));
420: java_encoding = val.trim();
421: }
422: }
423: } else if (node.getNodeName().equalsIgnoreCase("timezone")) {
424: NodeList children = node.getChildNodes();
425: if (children != null) {
426: int len = children.getLength();
427: for (int i = 0; i < len; i++) {
428: String val = traverse(children.item(i));
429: timezone = val.trim();
430: try {
431: com.rimfaxe.util.Calendar
432: .setDefaultTimeZone(TimeZone
433: .getTimeZone(timezone));
434: } catch (Exception e) {
435: System.err.println("Invalid timezone : "
436: + timezone);
437: }
438: }
439: }
440: } else if (node.getNodeName().equalsIgnoreCase(
441: "webapp-mode")) {
442: NodeList children = node.getChildNodes();
443: if (children != null) {
444: int len = children.getLength();
445: for (int i = 0; i < len; i++) {
446: String val = traverse(children.item(i));
447:
448: if (val.trim().equalsIgnoreCase("rws"))
449: webapp_mode = "rws";
450: if (val.trim().equalsIgnoreCase("classic"))
451: webapp_mode = "classic";
452:
453: }
454: }
455: } else if (node.getNodeName()
456: .equalsIgnoreCase("cache-size")) {
457: NodeList children = node.getChildNodes();
458: if (children != null) {
459: int len = children.getLength();
460: for (int i = 0; i < len; i++) {
461: String val = traverse(children.item(i));
462: try {
463: Integer csize = new Integer(val.trim());
464: cache_size = csize.intValue();
465: } catch (Exception e) {
466: }
467: }
468: }
469: } else if (node.getNodeName().equalsIgnoreCase(
470: "sslpassphrase")) {
471: NodeList children = node.getChildNodes();
472: if (children != null) {
473: int len = children.getLength();
474: for (int i = 0; i < len; i++) {
475: String val = traverse(children.item(i));
476: sslpassphrase = val.trim().toLowerCase();
477: }
478: }
479: } else if (node.getNodeName().equalsIgnoreCase("certfile")) {
480: NodeList children = node.getChildNodes();
481: if (children != null) {
482: int len = children.getLength();
483: for (int i = 0; i < len; i++) {
484: String val = traverse(children.item(i));
485: certfile = val.trim().toLowerCase();
486: }
487: }
488: } else if (node.getNodeName().equalsIgnoreCase("dhfile")) {
489: NodeList children = node.getChildNodes();
490: if (children != null) {
491: int len = children.getLength();
492: for (int i = 0; i < len; i++) {
493: String val = traverse(children.item(i));
494: dhfile = val.trim().toLowerCase();
495: }
496: }
497: } else if (node.getNodeName().equalsIgnoreCase("home")) {
498: NodeList children = node.getChildNodes();
499: if (children != null) {
500: int len = children.getLength();
501: for (int i = 0; i < len; i++) {
502: String val = traverse(children.item(i));
503: RWShome = val.trim();
504: }
505: }
506: } else {
507: NodeList children = node.getChildNodes();
508: if (children != null) {
509: int len = children.getLength();
510: for (int i = 0; i < len; i++) {
511: String val = traverse(children.item(i));
512: }
513: }
514: }
515: break;
516: }
517:
518: case Node.TEXT_NODE: {
519: str.append(node.getNodeValue());
520: break;
521: }
522: }
523: return str.toString();
524: }
525:
526: public String toXML() {
527: return toXML(false);
528: }
529:
530: public String snapshot() {
531: return toXML(true);
532: }
533:
534: public String toXML(boolean snapshot)
535: {
536: StringBuffer buf = new StringBuffer();
537:
538: buf.append("<?xml version='1.0'?>\n\n<document>\n\n");
539:
540: buf.append(" <admin> "+admin+ " </admin>\n");
541: buf.append(" <password> "+password+" </password>\n\n");
542:
543: buf.append(" <home> "+RWShome+" </home>\n\n");
544: buf.append(" <gcj> "+gcj_compiler+" </gcj>\n\n");
545: buf.append(" <cache-size> "+cache_size+" </cache-size>\n\n");
546: buf.append(" <webapp-mode> "+webapp_mode+" </webapp-mode>\n\n");
547:
548: Enumeration enum = virtuallist.elements();
549: while (enum.hasMoreElements())
550: {
551: VirtualHost vh = (VirtualHost) enum.nextElement();
552: buf.append(""+vh.toXML(snapshot));
553: }
554:
555: buf.append("\n\n</document>");
556: return ""+buf;
557: }
558:
559: public String format(String in, int w) {
560: StringBuffer buf = new StringBuffer();
561:
562: int c = 0;
563:
564: while ((c + w) <= in.length()) {
565: String tmp = in.substring(c, c + w);
566: c += w;
567: buf.append(tmp + "\n");
568: }
569:
570: if (c < in.length())
571: buf.append(in.substring(c, in.length()));
572:
573: return "" + buf;
574: }
575: }
|