1 /* 2 * $Header: /u/cvs/Projects/EnhydraOrg/enhydra3x/Enhydra/modules/Tomcat/src/share/org/apache/tomcat/shell/Attic/Shutdown.java,v 1.2 2000/02/26 02:32:22 shawn Exp $ 3 * $Revision: 1.2 $ 4 * $Date: 2000/02/26 02:32:22 $ 5 * 6 * ==================================================================== 7 * 8 * The Apache Software License, Version 1.1 9 * 10 * Copyright (c) 1999 The Apache Software Foundation. All rights 11 * reserved. 12 * 13 * Redistribution and use in source and binary forms, with or without 14 * modification, are permitted provided that the following conditions 15 * are met: 16 * 17 * 1. Redistributions of source code must retain the above copyright 18 * notice, this list of conditions and the following disclaimer. 19 * 20 * 2. Redistributions in binary form must reproduce the above copyright 21 * notice, this list of conditions and the following disclaimer in 22 * the documentation and/or other materials provided with the 23 * distribution. 24 * 25 * 3. The end-user documentation included with the redistribution, if 26 * any, must include the following acknowlegement: 27 * "This product includes software developed by the 28 * Apache Software Foundation (http://www.apache.org/)." 29 * Alternately, this acknowlegement may appear in the software itself, 30 * if and wherever such third-party acknowlegements normally appear. 31 * 32 * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software 33 * Foundation" must not be used to endorse or promote products derived 34 * from this software without prior written permission. For written 35 * permission, please contact apache@apache.org. 36 * 37 * 5. Products derived from this software may not be called "Apache" 38 * nor may "Apache" appear in their names without prior written 39 * permission of the Apache Group. 40 * 41 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED 42 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 43 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 44 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR 45 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 46 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 47 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 48 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 49 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 50 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 51 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 52 * SUCH DAMAGE. 53 * ==================================================================== 54 * 55 * This software consists of voluntary contributions made by many 56 * individuals on behalf of the Apache Software Foundation. For more 57 * information on the Apache Software Foundation, please see 58 * <http://www.apache.org/>. 59 * 60 * [Additional notices, if required by prior licensing conditions] 61 * 62 */ 63 64 65 package org.apache.tomcat.shell; 66 67 /** 68 * 69 * @author James Todd [gonzo@eng.sun.com] 70 */ 71 72 import org.apache.tomcat.server; 73 import org.apache.tomcat.util; 74 import org.apache.tomcat.shell.deployment; 75 import java.io; 76 import java.net; 77 import java.util; 78 import java.rmi; 79 import java.rmi.registry; 80 81 /** 82 * 83 * @author James Duncan Davidson [duncan@eng.sun.com] 84 * @author James Todd [gonzo@eng.sun.com] 85 */ 86 87 public class Shutdown { 88 89 private StringManager sm = 90 StringManager.getManager(Constants.Package); 91 92 // add war protocol handler to system properties 93 94 static { 95 String warPackage = Constants.Protocol.WAR.PACKAGE; 96 String protocolKey = Constants.Protocol.WAR.SYSTEM_PROPERTY; 97 String protocolHandlers = 98 System.getProperties().getProperty(protocolKey); 99 System.getProperties().put(protocolKey, 100 (protocolHandlers == null) ? 101 warPackage : protocolHandlers + "|" + warPackage); 102 }; 103 104 /** 105 * 106 */ 107 108 public Shutdown(String[] args) 109 throws StartupException { 110 Config config = getConfig(args); 111 112 if (config == null) { 113 return; 114 } 115 116 String portStr = null; 117 118 if (config.isArg(Constants.Arg.AdminPort)) { 119 portStr = (String)config.getConfig().getAttribute( 120 Constants.Attribute.AdminPort); 121 } else { 122 Properties props = getProperties(); 123 124 portStr = props.getProperty(Constants.Property.AdminPort); 125 } 126 127 Registry registry = getRegistry(portStr); 128 String[] services = getServices(registry); 129 int counter = 0; 130 131 for (int i = 0; i < services.length; i++) { 132 String service = services[i]; 133 134 if (service.startsWith(Constants.Registry.Service + ":")) { 135 boolean exit = 136 (++counter == services.length) ? true : false; 137 138 try { 139 ((Admin)(registry.lookup(service))).stopService(exit); 140 registry.unbind(service); 141 } catch (Exception e) { 142 System.out.println(sm.getString("shutdown.service.e")); 143 System.out.println(e.toString()); 144 145 e.printStackTrace(); 146 } 147 } 148 } 149 150 services = getServices(registry); 151 152 if (services.length == 0) { 153 File f = null; 154 155 try { 156 f = new File(Constants.Server.LogFile); 157 } catch (NullPointerException npe) { 158 System.out.println(sm.getString("shutdown.log.npe", 159 Constants.Server.ConfigFile)); 160 } 161 162 try { 163 if (f.exists() && 164 f.isFile()) { 165 f.delete(); 166 } 167 } catch (SecurityException se) { 168 System.out.println(sm.getString("shutdown.log.se", 169 Constants.Server.LogFile)); 170 } 171 } 172 } 173 174 /** 175 * 176 */ 177 178 private Properties getProperties() { 179 Properties properties = new Properties(); 180 File f = null; 181 182 try { 183 f = new File(Constants.Server.LogFile); 184 } catch (NullPointerException npe) { 185 System.out.println(sm.getString("shutdown.log.npe", 186 Constants.Server.ConfigFile)); 187 } 188 189 if (f != null) { 190 try { 191 FileInputStream fis = new FileInputStream(f); 192 193 properties.load(fis); 194 } catch (FileNotFoundException se) { 195 System.out.println(sm.getString("shutdown.log.fnfe", 196 Constants.Server.LogFile)); 197 } catch (SecurityException se) { 198 System.out.println(sm.getString("shutdown.log.se", 199 Constants.Server.LogFile)); 200 } catch (IOException ioe) { 201 System.out.println(sm.getString("shutdown.log.ioe", 202 Constants.Server.LogFile)); 203 } 204 } 205 206 return properties; 207 } 208 209 /** 210 * 211 */ 212 213 private Registry getRegistry(String portStr) 214 throws StartupException { 215 Registry registry = null; 216 int port = -1; 217 218 try { 219 port = Integer.parseInt(portStr); 220 } catch (NumberFormatException nfe) { 221 String msg = sm.getString("shutdown.setport.nfe", portStr); 222 223 throw new StartupException(msg); 224 } 225 226 try { 227 registry = LocateRegistry.getRegistry(port); 228 } catch (Exception e) { 229 String msg = sm.getString("shutdown.registry.e", portStr); 230 231 throw new StartupException(msg); 232 } 233 234 return registry; 235 } 236 237 /** 238 * 239 */ 240 241 private String[] getServices(Registry registry) 242 throws StartupException { 243 String[] services = null; 244 245 try { 246 services = registry.list(); 247 } catch (AccessException ae) { 248 String msg = sm.getString("shutdown.registry.ae"); 249 250 throw new StartupException(msg); 251 } catch (RemoteException re) { 252 String msg = sm.getString("shutdown.registry.re"); 253 254 throw new StartupException(msg); 255 } 256 257 return services; 258 } 259 260 /** 261 * 262 */ 263 264 public static void main(String[] args) { 265 try { 266 new Shutdown(args); 267 } catch (StartupException e) { 268 System.out.println(e.getMessage()); 269 } 270 } 271 272 /** 273 * file and command line configuration processing 274 */ 275 276 private Config getConfig(String args[]) 277 throws StartupException { 278 Config config = new Config(args); 279 280 System.out.println(sm.getString("startup.banner")); 281 282 if (config.isArg(Constants.Arg.Help)) { 283 284 System.out.println(sm.getString("startup.help")); 285 System.out.println(); 286 287 return null; 288 } 289 290 String configFile = Constants.Server.ConfigFile; 291 292 if (config.isArg(Constants.Arg.Config)) { 293 configFile = config.getArg(Constants.Arg.Config); 294 } 295 296 config.loadConfig(configFile); 297 298 return config; 299 } 300 }