001: /* ====================================================================
002: The Jicarilla Software License
003:
004: Copyright (c) 2003 Leo Simons.
005: All rights reserved.
006:
007: Permission is hereby granted, free of charge, to any person obtaining
008: a copy of this software and associated documentation files (the
009: "Software"), to deal in the Software without restriction, including
010: without limitation the rights to use, copy, modify, merge, publish,
011: distribute, sublicense, and/or sell copies of the Software, and to
012: permit persons to whom the Software is furnished to do so, subject to
013: the following conditions:
014:
015: The above copyright notice and this permission notice shall be
016: included in all copies or substantial portions of the Software.
017:
018: THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
019: EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
020: MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
021: IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
022: CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
023: TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
024: SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
025: ==================================================================== */
026: package org.jicarilla.webserver;
027:
028: import bsh.EvalError;
029: import bsh.Interpreter;
030: import org.apache.commons.logging.Log;
031: import org.jicarilla.lang.LifecycleUtil;
032: import org.jicarilla.net.SocketServer;
033: import org.picocontainer.PicoContainer;
034:
035: import java.io.FileNotFoundException;
036: import java.io.IOException;
037:
038: /**
039: * this is a basic CLI wrapper that calls the
040: * BeanShell startup script.
041: *
042: * @author <a href="lsimons at jicarilla dot org">Leo Simons</a>
043: * @version $Id: Main.java,v 1.1 2004/03/31 12:11:00 lsimons Exp $
044: */
045: public class Main {
046: public final static int SHUTDOWN_DELAY_IN_MILLISECONDS = 1000;
047:
048: public static void main(final String[] args) {
049: Log log = null;
050: SocketServer server = null;
051: PicoContainer container = null;
052:
053: final Interpreter i = new Interpreter();
054: try {
055: i.set("args", args);
056: } catch (EvalError e) {
057: printError(
058: "The server experienced a strange internal error: ",
059: "Please contact your support department.", e, true);
060: }
061:
062: try {
063: i.source("startup.bsh");
064: } catch (FileNotFoundException fnfe) {
065: printError(
066: "Cannot find the configuration file: ",
067: "Please try putting startup.bsh in the correct location.",
068: fnfe, true);
069: } catch (IOException ioe) {
070: printError(
071: "Error reading the configuration file: ",
072: "Please try again. If the problem persists, contact your administrator.",
073: ioe, true);
074: } catch (EvalError ee) {
075: final int line = ee.getErrorLineNumber();
076: printError("Error in configuration file on line " + line
077: + ": ", "", ee, true);
078: }
079: try {
080:
081: log = (Log) i.get("log");
082: server = (SocketServer) i.get("server");
083: container = (PicoContainer) i.get("container");
084: }
085:
086: catch (Exception e) {
087: printError(
088: "Error parsing the configuration file: ",
089: "Please examine the stack trace to find the cause of the problem and fix it.",
090: e, false);
091:
092: if (log != null) {
093: log.error("An exception occured during startup", e);
094: log.error("Exiting on error...");
095: }
096:
097: try {
098: LifecycleUtil.dispose(server);
099: LifecycleUtil.dispose(container);
100: Thread.sleep(SHUTDOWN_DELAY_IN_MILLISECONDS);
101: } catch (Throwable t) {
102: printError("Error attempting a graceful shutdown: ",
103: "This is probably not a big problem.", e, true);
104: }
105: }
106: }
107:
108: protected static void printError(final String prefix,
109: final String postfix, final Throwable t,
110: final boolean recurse) {
111: System.err.println(prefix + t.getMessage());
112: if (recurse) {
113: System.err.println("A stack trace follows:");
114: System.err
115: .println("-----------------------------------------------------");
116: printRecursiveMessages(t.getCause(), 4);
117: if (t instanceof EvalError) {
118: t.printStackTrace();
119: System.err.println(((EvalError) t)
120: .getScriptStackTrace());
121: }
122: System.err
123: .println("-----------------------------------------------------");
124: }
125: System.err.println(postfix);
126: }
127:
128: protected static void printRecursiveMessages(final Throwable t,
129: final int indent) {
130: if (t == null)
131: return;
132:
133: for (int i = 0; i < indent; i++)
134: System.err.print(' ');
135:
136: System.err.print(friendlyClassName(t) + ": " + t.getMessage());
137: System.err.println();
138:
139: printRecursiveMessages(t.getCause(), indent + 4);
140: }
141:
142: protected static String friendlyClassName(final Object o) {
143: final String fqn = o.getClass().getName();
144: final String last = fqn.substring(fqn.lastIndexOf('.'));
145: return last;
146: }
147: }
|