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: /////////////////////////
077: //$Archive: /SOFIA/SourceCode/com/salmonllc/ideTools/Tomcat40Bootstrap.java $
078: //$Author: Srufle $
079: //$Revision: 3 $
080: //$Modtime: 4/15/03 2:24p $
081: /////////////////////////
082: import java.io.File;
083: import java.lang.reflect.Method;
084:
085: /**
086: * Boostrap loader for Catalina. This was copied from Catalina 4.03 source and modified to run from the IDETool
087: */
088:
089: public final class Tomcat40Bootstrap {
090:
091: private static int _debug = 0;
092: private boolean _restart = false;
093: private String _extraCom = null;
094: private String _extraArgs = null;
095:
096: public static int START = 1;
097: public static int STOP = 2;
098: public static int RESTART = 3;
099:
100: /**
101: * Create a new bootstrap object, extraCom and extraArgs are so the browser can start after the server starts
102: */
103: private Tomcat40Bootstrap(boolean restart, String extraCom,
104: String extraArgs) {
105: _restart = restart;
106: _extraCom = extraCom;
107: _extraArgs = extraArgs;
108: }
109:
110: private static ClassLoader createClassLoader(File[] arg1,
111: File[] arg2, ClassLoader arg3) {
112: try {
113: Class fact = Class
114: .forName("org.apache.catalina.startup.ClassLoaderFactory");
115:
116: Class[] parmTypes = new Class[3];
117: parmTypes[0] = File[].class;
118: parmTypes[1] = File[].class;
119: parmTypes[2] = ClassLoader.class;
120:
121: Object[] parms = new Object[3];
122: parms[0] = arg1;
123: parms[1] = arg2;
124: parms[2] = arg3;
125:
126: Method m = fact.getMethod("createClassLoader", parmTypes);
127: Object ret = m.invoke(null, parms);
128: return (ClassLoader) ret;
129: } catch (Exception e) {
130: return null;
131: }
132: }
133:
134: /**
135: * Start, stop or restart the local tomcat server. Optionally launch the browser as well.
136: * @param process constants START, STOP or RESTART
137: * @param browserPath - The full path to the browser executable
138: * @param webPage - The URL of the web page to run
139: */
140: public static void execute(int process, String browserPath,
141: String webPage) {
142: Tomcat40Bootstrap tb = new Tomcat40Bootstrap(
143: process == RESTART, browserPath, webPage);
144: if (process == START)
145: executeCommand("start", tb);
146: else if (process == STOP || process == RESTART)
147: executeCommand("stop", tb);
148:
149: }
150:
151: /**
152: * The main program for the bootstrap.
153: */
154: private static void executeCommand(String command[],
155: Tomcat40Bootstrap tb) {
156:
157: // Set the debug flag appropriately
158: for (int i = 0; i < command.length; i++) {
159: if ("-debug".equals(command[i]))
160: _debug = 1;
161: }
162:
163: // Configure catalina.base from catalina.home if not yet set
164: if (System.getProperty("catalina.base") == null)
165: System.setProperty("catalina.base", getCatalinaHome());
166:
167: // Construct the class loaders we will need
168: ClassLoader commonLoader = null;
169: ClassLoader catalinaLoader = null;
170: ClassLoader sharedLoader = null;
171: try {
172:
173: File unpacked[] = new File[1];
174: File packed[] = new File[1];
175:
176: //ClassLoaderFactory.setDebug(_debug);
177:
178: unpacked[0] = new File(getCatalinaHome(), "common"
179: + File.separator + "classes");
180: packed[0] = new File(getCatalinaHome(), "common"
181: + File.separator + "lib");
182: commonLoader = createClassLoader(unpacked, packed, null);
183:
184: unpacked[0] = new File(getCatalinaHome(), "server"
185: + File.separator + "classes");
186: packed[0] = new File(getCatalinaHome(), "server"
187: + File.separator + "lib");
188: catalinaLoader = createClassLoader(unpacked, packed,
189: commonLoader);
190:
191: unpacked[0] = new File(getCatalinaHome(), "classes");
192: packed[0] = new File(getCatalinaHome(), "lib");
193: sharedLoader = createClassLoader(unpacked, packed,
194: commonLoader);
195:
196: } catch (Throwable t) {
197: log("Class loader creation threw exception", t);
198: System.exit(1);
199: }
200:
201: Thread.currentThread().setContextClassLoader(catalinaLoader);
202:
203: // Load our startup class and call its process() method
204: try {
205:
206: if (System.getSecurityManager() != null) {
207: // Pre load some classes required for SecurityManager
208: // so that defineClassInPackage does not throw a
209: // security exception.
210: String basePackage = "org.apache.catalina.";
211: catalinaLoader
212: .loadClass(basePackage
213: + "core.ApplicationContext$PrivilegedGetRequestDispatcher");
214: catalinaLoader
215: .loadClass(basePackage
216: + "core.ApplicationContext$PrivilegedGetResource");
217: catalinaLoader
218: .loadClass(basePackage
219: + "core.ApplicationContext$PrivilegedGetResourcePaths");
220: catalinaLoader
221: .loadClass(basePackage
222: + "core.ApplicationContext$PrivilegedLogMessage");
223: catalinaLoader
224: .loadClass(basePackage
225: + "core.ApplicationContext$PrivilegedLogException");
226: catalinaLoader
227: .loadClass(basePackage
228: + "core.ApplicationContext$PrivilegedLogThrowable");
229: catalinaLoader
230: .loadClass(basePackage
231: + "core.ApplicationDispatcher$PrivilegedForward");
232: catalinaLoader
233: .loadClass(basePackage
234: + "core.ApplicationDispatcher$PrivilegedInclude");
235: catalinaLoader
236: .loadClass(basePackage
237: + "connector.HttpRequestBase$PrivilegedGetSession");
238: catalinaLoader
239: .loadClass(basePackage
240: + "loader.WebappClassLoader$PrivilegedFindResource");
241: catalinaLoader.loadClass(basePackage
242: + "session.StandardSession");
243: catalinaLoader.loadClass(basePackage
244: + "util.CookieTools");
245: catalinaLoader.loadClass(basePackage + "util.URL");
246: catalinaLoader.loadClass(basePackage
247: + "util.Enumerator");
248: catalinaLoader.loadClass("javax.servlet.http.Cookie");
249: }
250:
251: // Instantiate a startup class instance
252: if (_debug >= 1)
253: log("Loading startup class");
254: Class startupClass = catalinaLoader
255: .loadClass("com.salmonllc.ideTools.Tomcat40Engine");
256: //("org.apache.catalina.startup.Catalina");
257: Object startupInstance = startupClass.newInstance();
258:
259: // Set the shared extensions class loader
260: if (_debug >= 1)
261: log("Setting startup class properties");
262:
263: String methodName = "setParentClassLoader";
264: Class paramTypes[] = new Class[1];
265: paramTypes[0] = Class.forName("java.lang.ClassLoader");
266: Object paramValues[] = new Object[1];
267: paramValues[0] = sharedLoader;
268: Method method = startupInstance.getClass().getMethod(
269: methodName, paramTypes);
270: method.invoke(startupInstance, paramValues);
271:
272: // Call the process() method
273: if (_debug >= 1)
274: log("Calling startup class process() method");
275: methodName = "process";
276: paramTypes = new Class[2];
277: paramTypes[0] = command.getClass();
278: paramTypes[1] = tb.getClass();
279: paramValues = new Object[2];
280: paramValues[0] = command;
281: paramValues[1] = tb;
282: method = startupInstance.getClass().getMethod(methodName,
283: paramTypes);
284: method.invoke(startupInstance, paramValues);
285:
286: } catch (Exception e) {
287: System.out.println("Exception during startup processing");
288: e.printStackTrace(System.out);
289: System.exit(2);
290: }
291:
292: }
293:
294: /**
295: * Make the command look like an argument array passed to main
296: */
297: private static void executeCommand(String args, Tomcat40Bootstrap tb) {
298: String argsArr[] = new String[1];
299: argsArr[0] = args;
300: executeCommand(argsArr, tb);
301: }
302:
303: String getBrowserExe() {
304: return _extraCom;
305: }
306:
307: String getBrowserURL() {
308: return _extraArgs;
309: }
310:
311: /**
312: * Get the value of the catalina.home environment variable.
313: */
314: private static String getCatalinaHome() {
315: return System.getProperty("catalina.home", System
316: .getProperty("user.dir"));
317: }
318:
319: /**
320: * Log a debugging detail message.
321: *
322: * @param message The message to be logged
323: */
324: private static void log(String message) {
325:
326: System.out.print("Bootstrap: ");
327: System.out.println(message);
328:
329: }
330:
331: /**
332: * Log a debugging detail message with an exception.
333: *
334: * @param message The message to be logged
335: * @param exception The exception to be logged
336: */
337: private static void log(String message, Throwable exception) {
338:
339: log(message);
340: exception.printStackTrace(System.out);
341:
342: }
343:
344: /**
345: * This method gets fired when a process is executed
346: */
347: void notifyComplete() {
348: if (_restart) {
349: _restart = false;
350: executeCommand("start", this);
351: }
352: }
353: }
|