001: /*
002: * $Header: /home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/startup/CatalinaService.java,v 1.7 2002/07/09 10:46:16 jfclere Exp $
003: * $Revision: 1.7 $
004: * $Date: 2002/07/09 10:46:16 $
005: *
006: * ====================================================================
007: *
008: * The Apache Software License, Version 1.1
009: *
010: * Copyright (c) 1999 The Apache Software Foundation. All rights
011: * reserved.
012: *
013: * Redistribution and use in source and binary forms, with or without
014: * modification, are permitted provided that the following conditions
015: * are met:
016: *
017: * 1. Redistributions of source code must retain the above copyright
018: * notice, this list of conditions and the following disclaimer.
019: *
020: * 2. Redistributions in binary form must reproduce the above copyright
021: * notice, this list of conditions and the following disclaimer in
022: * the documentation and/or other materials provided with the
023: * distribution.
024: *
025: * 3. The end-user documentation included with the redistribution, if
026: * any, must include the following acknowlegement:
027: * "This product includes software developed by the
028: * Apache Software Foundation (http://www.apache.org/)."
029: * Alternately, this acknowlegement may appear in the software itself,
030: * if and wherever such third-party acknowlegements normally appear.
031: *
032: * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
033: * Foundation" must not be used to endorse or promote products derived
034: * from this software without prior written permission. For written
035: * permission, please contact apache@apache.org.
036: *
037: * 5. Products derived from this software may not be called "Apache"
038: * nor may "Apache" appear in their names without prior written
039: * permission of the Apache Group.
040: *
041: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
042: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
043: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
044: * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
045: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
046: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
047: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
048: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
049: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
050: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
051: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
052: * SUCH DAMAGE.
053: * ====================================================================
054: *
055: * This software consists of voluntary contributions made by many
056: * individuals on behalf of the Apache Software Foundation. For more
057: * information on the Apache Software Foundation, please see
058: * <http://www.apache.org/>.
059: *
060: * [Additional notices, if required by prior licensing conditions]
061: *
062: */
063:
064: package org.apache.catalina.startup;
065:
066: import java.io.File;
067: import java.io.IOException;
068: import java.io.OutputStream;
069: import java.lang.reflect.InvocationTargetException;
070: import java.lang.reflect.Constructor;
071: import java.net.Socket;
072: import java.security.Security;
073: import java.util.Stack;
074: import org.apache.catalina.Container;
075: import org.apache.catalina.Lifecycle;
076: import org.apache.catalina.LifecycleException;
077: import org.apache.catalina.LifecycleListener;
078: import org.apache.catalina.Server;
079: import org.apache.catalina.Loader;
080: import org.apache.commons.digester.Digester;
081:
082: /**
083: * Startup/Shutdown shell program for Catalina. The following command line
084: * options are recognized:
085: * <ul>
086: * <li><b>-config {pathname}</b> - Set the pathname of the configuration file
087: * to be processed. If a relative path is specified, it will be
088: * interpreted as relative to the directory pathname specified by the
089: * "catalina.base" system property. [conf/server.xml]
090: * <li><b>-help</b> - Display usage information.
091: * <li><b>-stop</b> - Stop the currently running instance of Catalina.
092: * </ul>
093: * Special version of the Catalina bootstrap, designed to be invoked with JNI,
094: * and designed to allow easier wrapping by system level components, which
095: * would otherwise be confused by the asychronous startup and shutdown Catalina
096: * uses. This class should be used to run Catalina as a system service under
097: * Windows NT and clones.
098: *
099: * @author Craig R. McClanahan
100: * @author Remy Maucherat
101: * @version $Revision: 1.7 $ $Date: 2002/07/09 10:46:16 $
102: */
103:
104: public class CatalinaService extends Catalina {
105:
106: // ----------------------------------------------------- Instance Variables
107:
108: // ------------------------------------------------------ Protected Methods
109:
110: /**
111: * Process the specified command line arguments, and return
112: * <code>true</code> if we should continue processing; otherwise
113: * return <code>false</code>.
114: *
115: * @param args Command line arguments to process
116: */
117: protected boolean arguments(String args[]) {
118:
119: boolean isConfig = false;
120:
121: if (args.length < 1) {
122: usage();
123: return (false);
124: }
125:
126: for (int i = 0; i < args.length; i++) {
127: if (isConfig) {
128: configFile = args[i];
129: isConfig = false;
130: } else if (args[i].equals("-config")) {
131: isConfig = true;
132: } else if (args[i].equals("-debug")) {
133: debug = true;
134: } else if (args[i].equals("-nonaming")) {
135: useNaming = false;
136: } else if (args[i].equals("-help")) {
137: usage();
138: return (false);
139: } else if (args[i].equals("start")) {
140: starting = true;
141: stopping = false;
142: } else if (args[i].equals("stop")) {
143: starting = false;
144: stopping = true;
145: } else {
146: usage();
147: return (false);
148: }
149: }
150:
151: return (true);
152:
153: }
154:
155: /**
156: * Execute the processing that has been configured from the command line.
157: */
158: protected void execute() throws Exception {
159:
160: if (starting) {
161: load();
162: start();
163: } else if (stopping) {
164: stop();
165: }
166:
167: }
168:
169: /**
170: * Start a new server instance.
171: */
172: public void load() {
173:
174: // Create and execute our Digester
175: Digester digester = createStartDigester();
176: File file = configFile();
177: try {
178: digester.push(this );
179: digester.parse(file);
180: } catch (Exception e) {
181: System.out.println("Catalina.start: " + e);
182: e.printStackTrace(System.out);
183: System.exit(1);
184: }
185:
186: // Setting additional variables
187: if (!useNaming) {
188: System.setProperty("catalina.useNaming", "false");
189: } else {
190: System.setProperty("catalina.useNaming", "true");
191: String value = "org.apache.naming";
192: String oldValue = System
193: .getProperty(javax.naming.Context.URL_PKG_PREFIXES);
194: if (oldValue != null) {
195: value = value + ":" + oldValue;
196: }
197: System.setProperty(javax.naming.Context.URL_PKG_PREFIXES,
198: value);
199: System.setProperty(
200: javax.naming.Context.INITIAL_CONTEXT_FACTORY,
201: "org.apache.naming.java.javaURLContextFactory");
202: }
203:
204: // If a SecurityManager is being used, set properties for
205: // checkPackageAccess() and checkPackageDefinition
206: if (System.getSecurityManager() != null) {
207: String access = Security.getProperty("package.access");
208: if (access != null && access.length() > 0)
209: access += ",";
210: else
211: access = "sun.,";
212: Security.setProperty("package.access", access
213: + "org.apache.catalina.,org.apache.jasper.");
214: String definition = Security
215: .getProperty("package.definition");
216: if (definition != null && definition.length() > 0)
217: definition += ",";
218: else
219: definition = "sun.,";
220: Security
221: .setProperty("package.definition",
222: // FIX ME package "javax." was removed to prevent HotSpot
223: // fatal internal errors
224: definition
225: + "java.,org.apache.catalina.,org.apache.jasper.");
226: }
227:
228: // Start the new server
229: if (server instanceof Lifecycle) {
230: try {
231: server.initialize();
232: } catch (LifecycleException e) {
233: System.out.println("Catalina.start: " + e);
234: e.printStackTrace(System.out);
235: if (e.getThrowable() != null) {
236: System.out.println("----- Root Cause -----");
237: e.getThrowable().printStackTrace(System.out);
238: }
239: }
240: }
241:
242: }
243:
244: /*
245: * Load using arguments
246: */
247: public void load(String args[]) {
248:
249: setCatalinaHome();
250: setCatalinaBase();
251: try {
252: if (arguments(args))
253: load();
254: } catch (Exception e) {
255: e.printStackTrace(System.out);
256: }
257: }
258:
259: /**
260: * Start a new server instance.
261: */
262: public void start() {
263:
264: // Start the new server
265: if (server instanceof Lifecycle) {
266: try {
267: ((Lifecycle) server).start();
268: } catch (LifecycleException e) {
269: System.out.println("Catalina.start: " + e);
270: e.printStackTrace(System.out);
271: if (e.getThrowable() != null) {
272: System.out.println("----- Root Cause -----");
273: e.getThrowable().printStackTrace(System.out);
274: }
275: }
276: }
277:
278: }
279:
280: /**
281: * Stop an existing server instance.
282: */
283: public void stop() {
284:
285: // Shut down the server
286: if (server instanceof Lifecycle) {
287: try {
288: ((Lifecycle) server).stop();
289: } catch (LifecycleException e) {
290: System.out.println("Catalina.stop: " + e);
291: e.printStackTrace(System.out);
292: if (e.getThrowable() != null) {
293: System.out.println("----- Root Cause -----");
294: e.getThrowable().printStackTrace(System.out);
295: }
296: }
297: }
298:
299: }
300:
301: }
|