001: //** Copyright Statement ***************************************************
002: //The Salmon Open Framework for Internet Applications (SOFIA)
003: // Copyright (C) 1999 - 2002, Salmon LLC
004: //
005: // This program is free software; you can redistribute it and/or
006: // modify it under the terms of the GNU General Public License version 2
007: // as published by the Free Software Foundation;
008: //
009: // This program is distributed in the hope that it will be useful,
010: // but WITHOUT ANY WARRANTY; without even the implied warranty of
011: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: // GNU General Public License for more details.
013: //
014: // You should have received a copy of the GNU General Public License
015: // along with this program; if not, write to the Free Software
016: // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
017: //
018: // For more information please visit http://www.salmonllc.com
019: //** End Copyright Statement ***************************************************
020: // ====================================================================
021: //
022: // The Apache Software License, Version 1.1
023: //
024: // Copyright (c) 1999 The Apache Software Foundation. All rights
025: // reserved.
026: //
027: // Redistribution and use in source and binary forms, with or without
028: // modification, are permitted provided that the following conditions
029: // are met:
030: //
031: // 1. Redistributions of source code must retain the above copyright
032: // notice, this list of conditions and the following disclaimer.
033: //
034: // 2. Redistributions in binary form must reproduce the above copyright
035: // notice, this list of conditions and the following disclaimer in
036: // the documentation and/or other materials provided with the
037: // distribution.
038: //
039: // 3. The end-user documentation included with the redistribution, if
040: // any, must include the following acknowlegement:
041: // "This product includes software developed by the
042: // Apache Software Foundation (http://www.apache.org/)."
043: // Alternately, this acknowlegement may appear in the software itself,
044: // if and wherever such third-party acknowlegements normally appear.
045: //
046: // 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
047: // Foundation" must not be used to endorse or promote products derived
048: // from this software without prior written permission. For written
049: // permission, please contact apache@apache.org.
050: //
051: // 5. Products derived from this software may not be called "Apache"
052: // nor may "Apache" appear in their names without prior written
053: // permission of the Apache Group.
054: //
055: // THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
056: // WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
057: // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
058: // DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
059: // ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
060: // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
061: // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
062: // USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
063: // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
064: // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
065: // OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
066: // SUCH DAMAGE.
067: // ====================================================================
068: //
069: // This software consists of voluntary contributions made by many
070: // individuals on behalf of the Apache Software Foundation. For more
071: // information on the Apache Software Foundation, please see
072: // <http://www.apache.org/>.
073:
074: package com.salmonllc.ideTools;
075:
076: import org.apache.catalina.startup.ClassLoaderFactory;
077:
078: import java.io.File; //import org.apache.catalina.startup.*;
079: import java.lang.reflect.Method;
080:
081: /**
082: * Boostrap loader for Catalina. This application constructs a class loader
083: * for use in loading the Catalina internal classes (by accumulating all of the
084: * JAR files found in the "server" directory under "catalina.home"), and
085: * starts the regular execution of the container. The purpose of this
086: * roundabout approach is to keep the Catalina internal classes (and any
087: * other classes they depend on, such as an XML parser) out of the system
088: * class path and therefore not visible to application level classes.
089: *
090: * @author Craig R. McClanahan
091: * @version $Revision: 2 $ $Date: 10/31/02 11:37a $
092: */
093:
094: public final class Tomcat41Bootstrap {
095:
096: // ------------------------------------------------------- Static Variables
097:
098: /**
099: * Debugging detail level for processing the startup.
100: */
101: private static int debug = 0;
102: private boolean _restart = false;
103: private String _extraCom = null;
104: private String _extraArgs = null;
105:
106: public static int START = 1;
107: public static int STOP = 2;
108: public static int RESTART = 3;
109:
110: // ----------------------------------------------------------- Main Program
111:
112: /**
113: * Create a new bootstrap object, extraCom and extraArgs are so the browser can start after the server starts
114: */
115: private Tomcat41Bootstrap(boolean restart, String extraCom,
116: String extraArgs) {
117: _restart = restart;
118: _extraCom = extraCom;
119: _extraArgs = extraArgs;
120: }
121:
122: /**
123: * Start, stop or restart the local tomcat server. Optionally launch the browser as well.
124: * @param process constants START, STOP or RESTART
125: * @param browserPath - The full path to the browser executable
126: * @param webPage - The URL of the web page to run
127: */
128: public static void execute(int process, String browserPath,
129: String webPage) {
130: Tomcat41Bootstrap tb = new Tomcat41Bootstrap(
131: process == RESTART, browserPath, webPage);
132: if (process == START)
133: executeCommand("start", tb);
134: else if (process == STOP || process == RESTART)
135: executeCommand("stop", tb);
136:
137: }
138:
139: /**
140: * The main program for the bootstrap.
141: *
142: * @param args Command line arguments to be processed
143: */
144: public static void executeCommand(String args[],
145: Tomcat41Bootstrap tb) {
146:
147: // Set the debug flag appropriately
148: for (int i = 0; i < args.length; i++) {
149: if ("-debug".equals(args[i]))
150: debug = 1;
151: }
152:
153: // Configure catalina.base from catalina.home if not yet set
154: if (System.getProperty("catalina.base") == null)
155: System.setProperty("catalina.base", getCatalinaHome());
156:
157: // Construct the class loaders we will need
158: ClassLoader commonLoader = null;
159: ClassLoader catalinaLoader = null;
160: ClassLoader sharedLoader = null;
161: try {
162:
163: File unpacked[] = new File[1];
164: File packed[] = new File[1];
165: File packed2[] = new File[2];
166: ClassLoaderFactory.setDebug(debug);
167:
168: unpacked[0] = new File(getCatalinaHome(), "common"
169: + File.separator + "classes");
170: packed2[0] = new File(getCatalinaHome(), "common"
171: + File.separator + "endorsed");
172: packed2[1] = new File(getCatalinaHome(), "common"
173: + File.separator + "lib");
174: commonLoader = ClassLoaderFactory.createClassLoader(
175: unpacked, packed2, null);
176:
177: unpacked[0] = new File(getCatalinaHome(), "server"
178: + File.separator + "classes");
179: packed[0] = new File(getCatalinaHome(), "server"
180: + File.separator + "lib");
181: catalinaLoader = ClassLoaderFactory.createClassLoader(
182: unpacked, packed, commonLoader);
183:
184: unpacked[0] = new File(getCatalinaBase(), "shared"
185: + File.separator + "classes");
186: packed[0] = new File(getCatalinaBase(), "shared"
187: + File.separator + "lib");
188: sharedLoader = ClassLoaderFactory.createClassLoader(
189: unpacked, packed, commonLoader);
190: } catch (Throwable t) {
191:
192: log("Class loader creation threw exception", t);
193: System.exit(1);
194:
195: }
196:
197: Thread.currentThread().setContextClassLoader(catalinaLoader);
198:
199: // Load our startup class and call its process() method
200: try {
201:
202: //SecurityClassLoad.securityClassLoad(catalinaLoader);
203:
204: // Instantiate a startup class instance
205: if (debug >= 1)
206: log("Loading startup class");
207: Class startupClass = catalinaLoader
208: .loadClass("com.salmonllc.ideTools.Tomcat41Engine");
209: Object startupInstance = startupClass.newInstance();
210:
211: // Set the shared extensions class loader
212: if (debug >= 1)
213: log("Setting startup class properties");
214: String methodName = "setParentClassLoader";
215: Class paramTypes[] = new Class[1];
216: paramTypes[0] = Class.forName("java.lang.ClassLoader");
217: Object paramValues[] = new Object[1];
218: paramValues[0] = sharedLoader;
219: Method method = startupInstance.getClass().getMethod(
220: methodName, paramTypes);
221: method.invoke(startupInstance, paramValues);
222:
223: // Call the process() method
224: if (debug >= 1)
225: log("Calling startup class process() method");
226: methodName = "process";
227: paramTypes = new Class[2];
228: paramTypes[0] = args.getClass();
229: paramTypes[1] = tb.getClass();
230: paramValues = new Object[2];
231: paramValues[0] = args;
232: paramValues[1] = tb;
233: method = startupInstance.getClass().getMethod(methodName,
234: paramTypes);
235: method.invoke(startupInstance, paramValues);
236:
237: } catch (Exception e) {
238: System.out.println("Exception during startup processing");
239: e.printStackTrace(System.out);
240: System.exit(2);
241: }
242:
243: }
244:
245: /**
246: * Get the value of the catalina.home environment variable.
247: */
248: private static String getCatalinaHome() {
249: return System.getProperty("catalina.home", System
250: .getProperty("user.dir"));
251: }
252:
253: /**
254: * Get the value of the catalina.base environment variable.
255: */
256: private static String getCatalinaBase() {
257: return System.getProperty("catalina.base", getCatalinaHome());
258: }
259:
260: /**
261: * Log a debugging detail message.
262: *
263: * @param message The message to be logged
264: */
265: private static void log(String message) {
266:
267: System.out.print("Bootstrap: ");
268: System.out.println(message);
269:
270: }
271:
272: /**
273: * Log a debugging detail message with an exception.
274: *
275: * @param message The message to be logged
276: * @param exception The exception to be logged
277: */
278: private static void log(String message, Throwable exception) {
279:
280: log(message);
281: exception.printStackTrace(System.out);
282:
283: }
284:
285: private static void executeCommand(String args, Tomcat41Bootstrap tb) {
286: String argsArr[] = new String[1];
287: argsArr[0] = args;
288: executeCommand(argsArr, tb);
289: }
290:
291: String getBrowserExe() {
292: return _extraCom;
293: }
294:
295: String getBrowserURL() {
296: return _extraArgs;
297: }
298:
299: /**
300: * This method gets fired when a process is executed
301: */
302: void notifyComplete() {
303: if (_restart) {
304: _restart = false;
305: executeCommand("start", this);
306: }
307: }
308:
309: }
|