0001: //** Copyright Statement ***************************************************
0002: //The Salmon Open Framework for Internet Applications (SOFIA)
0003: // Copyright (C) 1999 - 2002, Salmon LLC
0004: //
0005: // This program is free software; you can redistribute it and/or
0006: // modify it under the terms of the GNU General Public License version 2
0007: // as published by the Free Software Foundation;
0008: //
0009: // This program is distributed in the hope that it will be useful,
0010: // but WITHOUT ANY WARRANTY; without even the implied warranty of
0011: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
0012: // GNU General Public License for more details.
0013: //
0014: // You should have received a copy of the GNU General Public License
0015: // along with this program; if not, write to the Free Software
0016: // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
0017: //
0018: // For more information please visit http://www.salmonllc.com
0019: //** End Copyright Statement ***************************************************
0020: // ====================================================================
0021: //
0022: // The Apache Software License, Version 1.1
0023: //
0024: // Copyright (c) 1999 The Apache Software Foundation. All rights
0025: // reserved.
0026: //
0027: // Redistribution and use in source and binary forms, with or without
0028: // modification, are permitted provided that the following conditions
0029: // are met:
0030: //
0031: // 1. Redistributions of source code must retain the above copyright
0032: // notice, this list of conditions and the following disclaimer.
0033: //
0034: // 2. Redistributions in binary form must reproduce the above copyright
0035: // notice, this list of conditions and the following disclaimer in
0036: // the documentation and/or other materials provided with the
0037: // distribution.
0038: //
0039: // 3. The end-user documentation included with the redistribution, if
0040: // any, must include the following acknowlegement:
0041: // "This product includes software developed by the
0042: // Apache Software Foundation (http://www.apache.org/)."
0043: // Alternately, this acknowlegement may appear in the software itself,
0044: // if and wherever such third-party acknowlegements normally appear.
0045: //
0046: // 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
0047: // Foundation" must not be used to endorse or promote products derived
0048: // from this software without prior written permission. For written
0049: // permission, please contact apache@apache.org.
0050: //
0051: // 5. Products derived from this software may not be called "Apache"
0052: // nor may "Apache" appear in their names without prior written
0053: // permission of the Apache Group.
0054: //
0055: // THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
0056: // WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
0057: // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
0058: // DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
0059: // ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
0060: // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
0061: // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
0062: // USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
0063: // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
0064: // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
0065: // OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
0066: // SUCH DAMAGE.
0067: // ====================================================================
0068: //
0069: // This software consists of voluntary contributions made by many
0070: // individuals on behalf of the Apache Software Foundation. For more
0071: // information on the Apache Software Foundation, please see
0072: // <http://www.apache.org/>.
0073:
0074: package com.salmonllc.ideTools;
0075:
0076: import java.io.*; /////////////////////////
0077: //$Archive: /JADE/SourceCode/com/salmonllc/ideTools/Tomcat40Engine.java $
0078: //$Author: Dan $
0079: //$Revision: 2 $
0080: //$Modtime: 10/30/02 2:49p $
0081: /////////////////////////
0082: import java.lang.reflect.InvocationTargetException;
0083: import java.lang.reflect.Constructor;
0084: import java.net.Socket;
0085: import java.security.Security;
0086: import java.util.Stack;
0087:
0088: import org.apache.catalina.Container;
0089: import org.apache.catalina.Lifecycle;
0090: import org.apache.catalina.LifecycleException;
0091: import org.apache.catalina.LifecycleListener;
0092: import org.apache.catalina.Server;
0093: import org.apache.catalina.Loader;
0094: import org.apache.catalina.util.xml.SaxContext;
0095: import org.apache.catalina.util.xml.XmlAction;
0096: import org.apache.catalina.util.xml.XmlMapper;
0097: import org.xml.sax.AttributeList;
0098: import com.salmonllc.util.MessageLog;
0099:
0100: /**
0101: * Startup/Shutdown shell program for Catalina. This was copied from Tomcat 4.0.3 source and modified to work with the IDETool
0102: */
0103:
0104: public class Tomcat40Engine {
0105:
0106: private Process _browserProcess = null;
0107:
0108: class CopyParentClassLoaderAction extends XmlAction {
0109: public CopyParentClassLoaderAction() {
0110: }
0111:
0112: public void start(SaxContext context) throws Exception {
0113: if (context.getDebug() >= 1)
0114: context.log("Copying parent class loader");
0115: Stack stack = context.getObjectStack();
0116: Container child = (Container) stack.pop();
0117: Container parent = (Container) stack.peek();
0118: stack.push(child);
0119: child.setParentClassLoader(parent.getParentClassLoader());
0120: }
0121: }
0122:
0123: class CreateLoaderAction extends XmlAction {
0124: public CreateLoaderAction(String loaderClass) {
0125: this (loaderClass, null);
0126: }
0127:
0128: public CreateLoaderAction(String loaderClass,
0129: String attributeName) {
0130: this .loaderClass = loaderClass;
0131: this .attributeName = attributeName;
0132: }
0133:
0134: public void start(SaxContext context) throws Exception {
0135: Stack stack = context.getObjectStack();
0136: Container container = (Container) stack.peek();
0137: ClassLoader parentClassLoader = container
0138: .getParentClassLoader();
0139: String className = loaderClass;
0140: if (attributeName != null) {
0141: int top = context.getTagCount() - 1;
0142: AttributeList attributes = context
0143: .getAttributeList(top);
0144: if (attributes.getValue(attributeName) != null)
0145: className = attributes.getValue(attributeName);
0146: }
0147: Class clazz = Class.forName(className);
0148: Class paramTypes[] = { java.lang.ClassLoader.class };
0149: Object arguments[] = { parentClassLoader };
0150: Constructor constructor = clazz
0151: .getDeclaredConstructor(paramTypes);
0152: Loader loader = (Loader) constructor.newInstance(arguments);
0153: stack.push(loader);
0154: if (context.getDebug() >= 1)
0155: context.log("new " + loader.getClass().getName());
0156: }
0157:
0158: public void cleanup(SaxContext context) {
0159: Stack stack = context.getObjectStack();
0160: Object o = stack.pop();
0161: if (context.getDebug() >= 1)
0162: context.log("pop " + o.getClass().getName());
0163: }
0164:
0165: protected String loaderClass;
0166: protected String attributeName;
0167: }
0168:
0169: /**
0170: * Class that adds a LifecycleListener for the top class on the stack.
0171: */
0172: class LifecycleListenerAction extends XmlAction {
0173: public LifecycleListenerAction(String listenerClass) {
0174: this (listenerClass, null);
0175: }
0176:
0177: public LifecycleListenerAction(String listenerClass,
0178: String attributeName) {
0179: super ();
0180: this .listenerClass = listenerClass;
0181: this .attributeName = attributeName;
0182:
0183: }
0184:
0185: private String attributeName = null;
0186: private String listenerClass = null;
0187:
0188: public void start(SaxContext context) throws Exception {
0189: // Create a new listener object
0190: String className = listenerClass;
0191: if (attributeName != null) {
0192: int top = context.getTagCount() - 1;
0193: AttributeList attributes = context
0194: .getAttributeList(top);
0195: if (attributes.getValue(attributeName) != null)
0196: className = attributes.getValue(attributeName);
0197: }
0198: if (context.getDebug() >= 1)
0199: context.log("Add " + className + " listener");
0200: Class clazz = Class.forName(className);
0201: LifecycleListener listener = (LifecycleListener) clazz
0202: .newInstance();
0203:
0204: // Add it to the top object on the stack
0205: Stack stack = context.getObjectStack();
0206: Lifecycle top = (Lifecycle) stack.peek();
0207: top.addLifecycleListener(listener);
0208:
0209: }
0210: }
0211:
0212: class SetParentClassLoaderAction extends XmlAction {
0213: private ClassLoader parentCL = null;
0214:
0215: public SetParentClassLoaderAction(ClassLoader parentClassLoader) {
0216: this .parentCL = parentClassLoader;
0217: }
0218:
0219: public void start(SaxContext context) throws Exception {
0220: if (context.getDebug() >= 1)
0221: context.log("Setting parent class loader");
0222: Stack stack = context.getObjectStack();
0223: Container top = (Container) stack.peek();
0224: top.setParentClassLoader(parentCL);
0225: }
0226: }
0227:
0228: // ----------------------------------------------------- Instance Variables
0229:
0230: /**
0231: * Pathname to the server configuration file.
0232: */
0233: protected String configFile = "conf/server.xml";
0234:
0235: /**
0236: * Set the debugging detail level on our XmlMapper.
0237: */
0238: protected boolean debug = false;
0239:
0240: /**
0241: * The shared extensions class loader for this server.
0242: */
0243: protected ClassLoader parentClassLoader = ClassLoader
0244: .getSystemClassLoader();
0245:
0246: /**
0247: * The server component we are starting or stopping
0248: */
0249: protected Server server = null;
0250:
0251: /**
0252: * Are we starting a new server?
0253: */
0254: protected boolean starting = false;
0255:
0256: /**
0257: * Are we stopping an existing server?
0258: */
0259: protected boolean stopping = false;
0260:
0261: /**
0262: * Are we using naming ?
0263: */
0264: protected boolean useNaming = true;
0265:
0266: /**
0267: * The bootstrap loader to notify about stuff
0268: */
0269: private Tomcat40Bootstrap _bootstrap;
0270:
0271: // ------------------------------------------------------ Protected Methods
0272:
0273: /**
0274: * Process the specified command line arguments, and return
0275: * <code>true</code> if we should continue processing; otherwise
0276: * return <code>false</code>.
0277: *
0278: * @param args Command line arguments to process
0279: */
0280: protected boolean arguments(String args[]) {
0281:
0282: boolean isConfig = false;
0283:
0284: if (args.length < 1) {
0285: usage();
0286: return (false);
0287: }
0288:
0289: for (int i = 0; i < args.length; i++) {
0290: if (isConfig) {
0291: configFile = args[i];
0292: isConfig = false;
0293: } else if (args[i].equals("-config")) {
0294: isConfig = true;
0295: } else if (args[i].equals("-debug")) {
0296: debug = true;
0297: } else if (args[i].equals("-nonaming")) {
0298: useNaming = false;
0299: } else if (args[i].equals("-help")) {
0300: usage();
0301: return (false);
0302: } else if (args[i].equals("start")) {
0303: starting = true;
0304: } else if (args[i].equals("stop")) {
0305: stopping = true;
0306: } else {
0307: usage();
0308: return (false);
0309: }
0310: }
0311:
0312: return (true);
0313:
0314: }
0315:
0316: /**
0317: * Return a File object representing our configuration file.
0318: */
0319: protected File configFile() {
0320:
0321: File file = new File(configFile);
0322: if (!file.isAbsolute())
0323: file = new File(System.getProperty("catalina.base"),
0324: configFile);
0325: return (file);
0326:
0327: }
0328:
0329: /**
0330: * Create the mapper rules for a Context which are common to both
0331: * a Context and a DefaultContext, based on the specified prefix.
0332: *
0333: * @param prefix Prefix to rule selectors to be created
0334: * @param mapper The mapper we are updating
0335: */
0336: protected void createContextCommon(String prefix, XmlMapper mapper) {
0337:
0338: mapper.addRule(prefix + "/Ejb", mapper
0339: .objectCreate("org.apache.catalina.deploy.ContextEjb"));
0340: mapper.addRule(prefix + "/Ejb", mapper.setProperties());
0341: mapper.addRule(prefix + "/Ejb", mapper.addChild("addEjb",
0342: "org.apache.catalina.deploy.ContextEjb"));
0343:
0344: mapper
0345: .addRule(
0346: prefix + "/Environment",
0347: mapper
0348: .objectCreate("org.apache.catalina.deploy.ContextEnvironment"));
0349: mapper.addRule(prefix + "/Environment", mapper.setProperties());
0350: mapper.addRule(prefix + "/Environment", mapper.addChild(
0351: "addEnvironment",
0352: "org.apache.catalina.deploy.ContextEnvironment"));
0353:
0354: mapper.addRule(prefix + "/InstanceListener", mapper
0355: .methodSetter("addInstanceListener", 0));
0356:
0357: mapper.addRule(prefix + "/Listener", mapper.objectCreate(null,
0358: "className"));
0359: mapper.addRule(prefix + "/Listener", mapper.setProperties());
0360: mapper.addRule(prefix + "/Listener", mapper.addChild(
0361: "addLifecycleListener",
0362: "org.apache.catalina.LifecycleListener"));
0363:
0364: mapper
0365: .addRule(prefix + "/Loader", new CreateLoaderAction(
0366: "org.apache.catalina.loader.WebappLoader",
0367: "className"));
0368: mapper.addRule(prefix + "/Loader", mapper.setProperties());
0369: mapper.addRule(prefix + "/Loader", mapper.addChild("setLoader",
0370: "org.apache.catalina.Loader"));
0371:
0372: mapper.addRule(prefix + "/Logger", mapper.objectCreate(null,
0373: "className"));
0374: mapper.addRule(prefix + "/Logger", mapper.setProperties());
0375: mapper.addRule(prefix + "/Logger", mapper.addChild("setLogger",
0376: "org.apache.catalina.Logger"));
0377:
0378: mapper.addRule(prefix + "/Manager", mapper.objectCreate(
0379: "org.apache.catalina.session.StandardManager",
0380: "className"));
0381: mapper.addRule(prefix + "/Manager", mapper.setProperties());
0382: mapper.addRule(prefix + "/Manager", mapper.addChild(
0383: "setManager", "org.apache.catalina.Manager"));
0384:
0385: mapper
0386: .addRule(
0387: prefix + "/Parameter",
0388: mapper
0389: .objectCreate("org.apache.catalina.deploy.ApplicationParameter"));
0390: mapper.addRule(prefix + "/Parameter", mapper.setProperties());
0391: mapper.addRule(prefix + "/Parameter", mapper.addChild(
0392: "addApplicationParameter",
0393: "org.apache.catalina.deploy.ApplicationParameter"));
0394:
0395: mapper.addRule(prefix + "/Realm", mapper.objectCreate(null,
0396: "className"));
0397: mapper.addRule(prefix + "/Realm", mapper.setProperties());
0398: mapper.addRule(prefix + "/Realm", mapper.addChild("setRealm",
0399: "org.apache.catalina.Realm"));
0400:
0401: mapper
0402: .addRule(
0403: prefix + "/Resource",
0404: mapper
0405: .objectCreate("org.apache.catalina.deploy.ContextResource"));
0406: mapper.addRule(prefix + "/Resource", mapper.setProperties());
0407: mapper.addRule(prefix + "/Resource", mapper.addChild(
0408: "addResource",
0409: "org.apache.catalina.deploy.ContextResource"));
0410:
0411: mapper
0412: .addRule(
0413: prefix + "/ResourceParams",
0414: mapper
0415: .objectCreate("org.apache.catalina.deploy.ResourceParams"));
0416: mapper.addRule(prefix + "/ResourceParams", mapper
0417: .setProperties());
0418: mapper.addRule(prefix + "/ResourceParams", mapper.addChild(
0419: "addResourceParams",
0420: "org.apache.catalina.deploy.ResourceParams"));
0421:
0422: mapper.addRule(prefix + "/ResourceParams/parameter", mapper
0423: .methodSetter("addParameter", 2));
0424: mapper.addRule(prefix + "/ResourceParams/parameter/name",
0425: mapper.methodParam(0));
0426: mapper.addRule(prefix + "/ResourceParams/parameter/value",
0427: mapper.methodParam(1));
0428:
0429: mapper.addRule(prefix + "/Resources", mapper.objectCreate(
0430: "org.apache.naming.resources.FileDirContext",
0431: "className"));
0432: mapper.addRule(prefix + "/Resources", mapper.setProperties());
0433: mapper.addRule(prefix + "/Resources", mapper.addChild(
0434: "setResources", "javax.naming.directory.DirContext"));
0435:
0436: mapper.addRule(prefix + "/Valve", mapper.objectCreate(null,
0437: "className"));
0438: mapper.addRule(prefix + "/Valve", mapper.setProperties());
0439: mapper.addRule(prefix + "/Valve", mapper.addChild("addValve",
0440: "org.apache.catalina.Valve"));
0441:
0442: mapper.addRule(prefix + "/WrapperLifecycle", mapper
0443: .methodSetter("addWrapperLifecycle", 0));
0444:
0445: mapper.addRule(prefix + "/WrapperListener", mapper
0446: .methodSetter("addWrapperListener", 0));
0447:
0448: }
0449:
0450: /**
0451: * Create and configure the XmlMapper we will be using for startup.
0452: */
0453: protected XmlMapper createStartMapper() {
0454:
0455: // Initialize the mapper
0456: XmlMapper mapper = new XmlMapper();
0457: if (debug)
0458: mapper.setDebug(999);
0459: mapper.setValidating(false);
0460:
0461: // Configure the actions we will be using
0462:
0463: mapper
0464: .addRule("Server", mapper.objectCreate(
0465: "org.apache.catalina.core.StandardServer",
0466: "className"));
0467: mapper.addRule("Server", mapper.setProperties());
0468: mapper.addRule("Server", mapper.addChild("setServer",
0469: "org.apache.catalina.Server"));
0470:
0471: mapper.addRule("Server/Listener", mapper.objectCreate(null,
0472: "className"));
0473: mapper.addRule("Server/Listener", mapper.setProperties());
0474: mapper.addRule("Server/Listener", mapper.addChild(
0475: "addLifecycleListener",
0476: "org.apache.catalina.LifecycleListener"));
0477:
0478: mapper.addRule("Server/Service", mapper
0479: .objectCreate(
0480: "org.apache.catalina.core.StandardService",
0481: "className"));
0482: mapper.addRule("Server/Service", mapper.setProperties());
0483: mapper.addRule("Server/Service", mapper.addChild("addService",
0484: "org.apache.catalina.Service"));
0485:
0486: mapper.addRule("Server/Service/Listener", mapper.objectCreate(
0487: null, "className"));
0488: mapper.addRule("Server/Service/Listener", mapper
0489: .setProperties());
0490: mapper.addRule("Server/Service/Listener", mapper.addChild(
0491: "addLifecycleListener",
0492: "org.apache.catalina.LifecycleListener"));
0493:
0494: mapper.addRule("Server/Service/Connector", mapper.objectCreate(
0495: "org.apache.catalina.connector.http.HttpConnector",
0496: "className"));
0497: mapper.addRule("Server/Service/Connector", mapper
0498: .setProperties());
0499: mapper.addRule("Server/Service/Connector", mapper.addChild(
0500: "addConnector", "org.apache.catalina.Connector"));
0501:
0502: mapper
0503: .addRule(
0504: "Server/Service/Connector/Factory",
0505: mapper
0506: .objectCreate(
0507: "org.apache.catalina.net.DefaultServerSocketFactory",
0508: "className"));
0509: mapper.addRule("Server/Service/Connector/Factory", mapper
0510: .setProperties());
0511: mapper.addRule("Server/Service/Connector/Factory", mapper
0512: .addChild("setFactory",
0513: "org.apache.catalina.net.ServerSocketFactory"));
0514:
0515: mapper.addRule("Server/Service/Connector/Listener", mapper
0516: .objectCreate(null, "className"));
0517: mapper.addRule("Server/Service/Connector/Listener", mapper
0518: .setProperties());
0519: mapper.addRule("Server/Service/Connector/Listener", mapper
0520: .addChild("addLifecycleListener",
0521: "org.apache.catalina.LifecycleListener"));
0522:
0523: mapper
0524: .addRule("Server/Service/Engine", mapper.objectCreate(
0525: "org.apache.catalina.core.StandardEngine",
0526: "className"));
0527: mapper.addRule("Server/Service/Engine", mapper.setProperties());
0528: mapper.addRule("Server/Service/Engine",
0529: new LifecycleListenerAction(
0530: "org.apache.catalina.startup.EngineConfig",
0531: "configClass"));
0532: mapper.addRule("Server/Service/Engine",
0533: new SetParentClassLoaderAction(parentClassLoader));
0534: mapper.addRule("Server/Service/Engine", mapper.addChild(
0535: "setContainer", "org.apache.catalina.Container"));
0536:
0537: mapper.addRule("Server/Service/Engine/Listener", mapper
0538: .objectCreate(null, "className"));
0539: mapper.addRule("Server/Service/Engine/Listener", mapper
0540: .setProperties());
0541: mapper.addRule("Server/Service/Engine/Listener", mapper
0542: .addChild("addLifecycleListener",
0543: "org.apache.catalina.LifecycleListener"));
0544:
0545: createStartMapperContext("Server/Service/Engine/Context",
0546: mapper);
0547: createStartMapperDefaultContext(
0548: "Server/Service/Engine/DefaultContext", mapper);
0549:
0550: mapper.addRule("Server/Service/Engine/Host", mapper
0551: .objectCreate("org.apache.catalina.core.StandardHost",
0552: "className"));
0553: mapper.addRule("Server/Service/Engine/Host", mapper
0554: .setProperties());
0555: mapper.addRule("Server/Service/Engine/Host",
0556: new CopyParentClassLoaderAction());
0557: mapper.addRule("Server/Service/Engine/Host",
0558: new LifecycleListenerAction(
0559: "org.apache.catalina.startup.HostConfig",
0560: "configClass"));
0561: mapper.addRule("Server/Service/Engine/Host", mapper.addChild(
0562: "addChild", "org.apache.catalina.Container"));
0563:
0564: mapper.addRule("Server/Service/Engine/Host/Alias", mapper
0565: .methodSetter("addAlias", 0));
0566:
0567: mapper.addRule("Server/Service/Engine/Host/Cluster", mapper
0568: .objectCreate(null, "className"));
0569: mapper.addRule("Server/Service/Engine/Host/Cluster", mapper
0570: .setProperties());
0571: mapper.addRule("Server/Service/Engine/Host/Cluster", mapper
0572: .addChild("setCluster", "org.apache.catalina.Cluster"));
0573:
0574: createStartMapperContext("Server/Service/Engine/Host/Context",
0575: mapper);
0576: createStartMapperDefaultContext(
0577: "Server/Service/Engine/Host/DefaultContext", mapper);
0578:
0579: mapper.addRule(
0580: "Server/Service/Engine/Host/Context/Manager/Store",
0581: mapper.objectCreate(null, "className"));
0582: mapper.addRule(
0583: "Server/Service/Engine/Host/Context/Manager/Store",
0584: mapper.setProperties());
0585: mapper.addRule(
0586: "Server/Service/Engine/Host/Context/Manager/Store",
0587: mapper
0588: .addChild("setStore",
0589: "org.apache.catalina.Store"));
0590:
0591: mapper.addRule("Server/Service/Engine/Host/Listener", mapper
0592: .objectCreate(null, "className"));
0593: mapper.addRule("Server/Service/Engine/Host/Listener", mapper
0594: .setProperties());
0595: mapper.addRule("Server/Service/Engine/Host/Listener", mapper
0596: .addChild("addLifecycleListener",
0597: "org.apache.catalina.LifecycleListener"));
0598:
0599: mapper.addRule("Server/Service/Engine/Host/Logger", mapper
0600: .objectCreate(null, "className"));
0601: mapper.addRule("Server/Service/Engine/Host/Logger", mapper
0602: .setProperties());
0603: mapper.addRule("Server/Service/Engine/Host/Logger", mapper
0604: .addChild("setLogger", "org.apache.catalina.Logger"));
0605:
0606: mapper.addRule("Server/Service/Engine/Host/Realm", mapper
0607: .objectCreate(null, "className"));
0608: mapper.addRule("Server/Service/Engine/Host/Realm", mapper
0609: .setProperties());
0610: mapper.addRule("Server/Service/Engine/Host/Realm", mapper
0611: .addChild("setRealm", "org.apache.catalina.Realm"));
0612:
0613: mapper.addRule("Server/Service/Engine/Host/Resources", mapper
0614: .objectCreate(
0615: "org.apache.naming.resources.FileDirContext",
0616: "className"));
0617: mapper.addRule("Server/Service/Engine/Host/Resources", mapper
0618: .setProperties());
0619: mapper.addRule("Server/Service/Engine/Host/Resources", mapper
0620: .addChild("setResources",
0621: "javax.naming.directory.DirContext"));
0622:
0623: mapper.addRule("Server/Service/Engine/Host/Valve", mapper
0624: .objectCreate(null, "className"));
0625: mapper.addRule("Server/Service/Engine/Host/Valve", mapper
0626: .setProperties());
0627: mapper.addRule("Server/Service/Engine/Host/Valve", mapper
0628: .addChild("addValve", "org.apache.catalina.Valve"));
0629:
0630: mapper.addRule("Server/Service/Engine/Listener", mapper
0631: .objectCreate(null, "className"));
0632: mapper.addRule("Server/Service/Engine/Listener", mapper
0633: .setProperties());
0634: mapper.addRule("Server/Service/Engine/Listener", mapper
0635: .addChild("addLifecycleListener",
0636: "org.apache.catalina.LifecycleListener"));
0637:
0638: mapper.addRule("Server/Service/Engine/Logger", mapper
0639: .objectCreate(null, "className"));
0640: mapper.addRule("Server/Service/Engine/Logger", mapper
0641: .setProperties());
0642: mapper.addRule("Server/Service/Engine/Logger", mapper.addChild(
0643: "setLogger", "org.apache.catalina.Logger"));
0644:
0645: mapper.addRule("Server/Service/Engine/Realm", mapper
0646: .objectCreate(null, "className"));
0647: mapper.addRule("Server/Service/Engine/Realm", mapper
0648: .setProperties());
0649: mapper.addRule("Server/Service/Engine/Realm", mapper.addChild(
0650: "setRealm", "org.apache.catalina.Realm"));
0651:
0652: mapper.addRule("Server/Service/Engine/Resources", mapper
0653: .objectCreate(
0654: "org.apache.naming.resources.FileDirContext",
0655: "className"));
0656: mapper.addRule("Server/Service/Engine/Resources", mapper
0657: .setProperties());
0658: mapper.addRule("Server/Service/Engine/Resources", mapper
0659: .addChild("setResources",
0660: "javax.naming.directory.DirContext"));
0661:
0662: mapper.addRule("Server/Service/Engine/Valve", mapper
0663: .objectCreate(null, "className"));
0664: mapper.addRule("Server/Service/Engine/Valve", mapper
0665: .setProperties());
0666: mapper.addRule("Server/Service/Engine/Valve", mapper.addChild(
0667: "addValve", "org.apache.catalina.Valve"));
0668:
0669: return (mapper);
0670:
0671: }
0672:
0673: /**
0674: * Create the mapper rules for a Context, based on the specified prefix.
0675: *
0676: * @param prefix Prefix to rule selectors to be created
0677: * @param mapper The mapper we are updating
0678: */
0679: protected void createStartMapperContext(String prefix,
0680: XmlMapper mapper) {
0681:
0682: mapper.addRule(prefix + "", mapper
0683: .objectCreate(
0684: "org.apache.catalina.core.StandardContext",
0685: "className"));
0686: mapper.addRule(prefix + "", mapper.setProperties());
0687: mapper.addRule(prefix + "", new CopyParentClassLoaderAction());
0688: mapper.addRule(prefix + "", new LifecycleListenerAction(
0689: "org.apache.catalina.startup.ContextConfig",
0690: "configClass"));
0691: mapper.addRule(prefix + "", mapper.addChild("addChild",
0692: "org.apache.catalina.Container"));
0693:
0694: createContextCommon(prefix, mapper);
0695:
0696: }
0697:
0698: /**
0699: * Create the mapper rules for a DefaultContext, based on the
0700: * specified prefix.
0701: *
0702: * @param prefix Prefix to rule selectors to be created
0703: * @param mapper The mapper we are updating
0704: */
0705: protected void createStartMapperDefaultContext(String prefix,
0706: XmlMapper mapper) {
0707:
0708: mapper
0709: .addRule(prefix + "", mapper.objectCreate(
0710: "org.apache.catalina.core.DefaultContext",
0711: "className"));
0712: mapper.addRule(prefix + "", mapper.setProperties());
0713: mapper.addRule(prefix + "", mapper.addChild(
0714: "addDefaultContext",
0715: "org.apache.catalina.core.DefaultContext"));
0716:
0717: createContextCommon(prefix, mapper);
0718:
0719: }
0720:
0721: /**
0722: * Create and configure the XmlMapper we will be using for shutdown.
0723: */
0724: protected XmlMapper createStopMapper() {
0725:
0726: // Initialize the mapper
0727: XmlMapper mapper = new XmlMapper();
0728: // mapper.setDebug(999);
0729:
0730: // Configure the actions we will be using
0731:
0732: mapper
0733: .addRule("Server", mapper.objectCreate(
0734: "org.apache.catalina.core.StandardServer",
0735: "className"));
0736: mapper.addRule("Server", mapper.setProperties());
0737: mapper.addRule("Server", mapper.addChild("setServer",
0738: "org.apache.catalina.Server"));
0739:
0740: return (mapper);
0741:
0742: }
0743:
0744: /**
0745: * Execute the processing that has been configured from the command line.
0746: */
0747: protected void execute() throws Exception {
0748:
0749: if (starting)
0750: start();
0751: else if (stopping)
0752: stop();
0753:
0754: }
0755:
0756: public void finalize() {
0757: if (_browserProcess != null)
0758: _browserProcess.destroy();
0759: }
0760:
0761: // ----------------------------------------------------------- Main Program
0762:
0763: /**
0764: * The instance main program.
0765: *
0766: * @param args Command line arguments
0767: */
0768: public void process(String args[]) {
0769:
0770: setCatalinaHome();
0771: setCatalinaBase();
0772: try {
0773: if (arguments(args))
0774: execute();
0775: } catch (Exception e) {
0776: e.printStackTrace(System.out);
0777: }
0778:
0779: }
0780:
0781: public void process(String args[], Tomcat40Bootstrap bootstrap) {
0782: _bootstrap = bootstrap;
0783: process(args);
0784: }
0785:
0786: private void runBrowser(String command, String parms) {
0787: Runtime r = Runtime.getRuntime();
0788:
0789: try {
0790: _browserProcess = r.exec('"' + command + '"'
0791: + (parms == null ? "" : (" " + parms)));
0792: IDETool.slurpProcessOutput(_browserProcess);
0793: } catch (IOException e) {
0794: MessageLog.writeErrorMessage("runBrowser", e, this );
0795: }
0796: }
0797:
0798: /**
0799: * Set the <code>catalina.base</code> System property to the current
0800: * working directory if it has not been set.
0801: */
0802: protected void setCatalinaBase() {
0803:
0804: if (System.getProperty("catalina.base") != null)
0805: return;
0806: System.setProperty("catalina.base", System
0807: .getProperty("catalina.home"));
0808:
0809: }
0810:
0811: /**
0812: * Set the <code>catalina.home</code> System property to the current
0813: * working directory if it has not been set.
0814: */
0815: protected void setCatalinaHome() {
0816:
0817: if (System.getProperty("catalina.home") != null)
0818: return;
0819: System.setProperty("catalina.home", System
0820: .getProperty("user.dir"));
0821:
0822: }
0823:
0824: // --------------------------------------------------------- Public Methods
0825:
0826: /**
0827: * Set the shared extensions class loader.
0828: *
0829: * @param parentClassLoader The shared extensions class loader.
0830: */
0831: public void setParentClassLoader(ClassLoader parentClassLoader) {
0832:
0833: this .parentClassLoader = parentClassLoader;
0834:
0835: }
0836:
0837: /**
0838: * Set the server instance we are configuring.
0839: *
0840: * @param server The new server
0841: */
0842: public void setServer(Server server) {
0843:
0844: this .server = server;
0845:
0846: }
0847:
0848: /**
0849: * Start a new server instance.
0850: */
0851: protected void start() {
0852:
0853: // Create and execute our mapper
0854: XmlMapper mapper = createStartMapper();
0855: File file = configFile();
0856: try {
0857: mapper.readXml(file, this );
0858: } catch (InvocationTargetException e) {
0859: System.out
0860: .println("Catalina.start: InvocationTargetException");
0861: e.getTargetException().printStackTrace(System.out);
0862: } catch (Exception e) {
0863: System.out.println("Catalina.start: " + e);
0864: e.printStackTrace(System.out);
0865: System.exit(1);
0866: }
0867:
0868: // Setting additional variables
0869: if (!useNaming) {
0870: System.setProperty("catalina.useNaming", "false");
0871: } else {
0872: System.setProperty("catalina.useNaming", "true");
0873: String value = "org.apache.naming";
0874: String oldValue = System
0875: .getProperty(javax.naming.Context.URL_PKG_PREFIXES);
0876: if (oldValue != null) {
0877: value = value + ":" + oldValue;
0878: }
0879: System.setProperty(javax.naming.Context.URL_PKG_PREFIXES,
0880: value);
0881: value = System
0882: .getProperty(javax.naming.Context.INITIAL_CONTEXT_FACTORY);
0883: if (value == null) {
0884: System.setProperty(
0885: javax.naming.Context.INITIAL_CONTEXT_FACTORY,
0886: "org.apache.naming.java.javaURLContextFactory");
0887: }
0888: }
0889:
0890: // If a SecurityManager is being used, set properties for
0891: // checkPackageAccess() and checkPackageDefinition
0892: if (System.getSecurityManager() != null) {
0893: String access = Security.getProperty("package.access");
0894: if (access != null && access.length() > 0)
0895: access += ",";
0896: else
0897: access = "sun.,";
0898: Security.setProperty("package.access", access
0899: + "org.apache.catalina.,org.apache.jasper.");
0900: String definition = Security
0901: .getProperty("package.definition");
0902: if (definition != null && definition.length() > 0)
0903: definition += ",";
0904: else
0905: definition = "sun.,";
0906: Security
0907: .setProperty("package.definition",
0908: // FIX ME package "javax." was removed to prevent HotSpot
0909: // fatal internal errors
0910: definition
0911: + "java.,org.apache.catalina.,org.apache.jasper.");
0912: }
0913:
0914: // Start the new server
0915: boolean serverStarted = true;
0916: if (server instanceof Lifecycle) {
0917: try {
0918: server.initialize();
0919: ((Lifecycle) server).start();
0920: } catch (LifecycleException e) {
0921: serverStarted = false;
0922: System.out.println("Catalina.start: " + e);
0923: e.printStackTrace(System.out);
0924: if (e.getThrowable() != null) {
0925: System.out.println("----- Root Cause -----");
0926: e.getThrowable().printStackTrace(System.out);
0927: }
0928: }
0929: }
0930:
0931: if (serverStarted && _bootstrap != null) {
0932: if (_bootstrap.getBrowserExe() != null)
0933: runBrowser(_bootstrap.getBrowserExe(), _bootstrap
0934: .getBrowserURL());
0935: _bootstrap.notifyComplete();
0936: }
0937:
0938: // Wait for the server to be told to shut down
0939: server.await();
0940:
0941: //Stop the browser process if there is one
0942: if (_browserProcess != null) {
0943: _browserProcess.destroy();
0944: }
0945:
0946: //It takes too long to stop the server the normal way so we will just terminate the process
0947: System.out.println("Apache Tomcat stopped");
0948: System.exit(0);
0949:
0950: // Shut down the server - commented the original tomcat code
0951: /*if (server instanceof Lifecycle) {
0952: try {
0953: ((Lifecycle) server).stop();
0954: } catch (LifecycleException e) {
0955: System.out.println("Catalina.stop: " + e);
0956: e.printStackTrace(System.out);
0957: if (e.getThrowable() != null) {
0958: System.out.println("----- Root Cause -----");
0959: e.getThrowable().printStackTrace(System.out);
0960: }
0961: }
0962: } */
0963:
0964: }
0965:
0966: /**
0967: * Stop an existing server instance.
0968: */
0969: protected void stop() {
0970:
0971: // Create and execute our mapper
0972: XmlMapper mapper = createStopMapper();
0973: File file = configFile();
0974: try {
0975: mapper.readXml(file, this );
0976: } catch (Exception e) {
0977: System.out.println("Catalina.stop: " + e);
0978: e.printStackTrace(System.out);
0979: System.exit(1);
0980: }
0981:
0982: // Stop the existing server
0983: try {
0984: Socket socket = new Socket("127.0.0.1", server.getPort());
0985: OutputStream stream = socket.getOutputStream();
0986:
0987: String shutdown = server.getShutdown();
0988: for (int i = 0; i < shutdown.length(); i++)
0989: stream.write(shutdown.charAt(i));
0990: stream.flush();
0991: stream.close();
0992: socket.close();
0993: Thread.sleep(500);
0994: } catch (Exception e) {
0995: //System.out.println("Catalina.stop: " + e);
0996: //e.printStackTrace(System.out);
0997: // System.exit(1);
0998: }
0999:
1000: if (_bootstrap != null)
1001: _bootstrap.notifyComplete();
1002: }
1003:
1004: /**
1005: * Print usage information for this application.
1006: */
1007: protected void usage() {
1008:
1009: System.out
1010: .println("usage: java org.apache.catalina.startup.Catalina"
1011: + " [ -config {pathname} ] [ -debug ]"
1012: + " [ -nonaming ] { start | stop }");
1013:
1014: }
1015: }
|