001 /** 002 * Copyright 2005 The Apache Software Foundation 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017 package org.apache.geronimo.deployment.cli; 018 019 import java.io.IOException; 020 import java.net.MalformedURLException; 021 import java.util.HashMap; 022 import java.util.Map; 023 024 import javax.management.MBeanServerConnection; 025 import javax.management.remote.JMXConnector; 026 import javax.management.remote.JMXConnectorFactory; 027 import javax.management.remote.JMXServiceURL; 028 029 import org.apache.geronimo.kernel.Kernel; 030 import org.apache.geronimo.system.jmx.KernelDelegate; 031 032 /** 033 * @version $Rev: 430136 $ $Date: 2006-08-09 13:21:52 -0700 (Wed, 09 Aug 2006) $ 034 */ 035 public class StopServer { 036 037 public static final String RMI_NAMING_CONFG_ID = "org/apache/geronimo/RMINaming"; 038 039 public static final String DEFAULT_PORT = "1099"; 040 041 String port; 042 043 String user; 044 045 String password; 046 047 private String[] args; 048 049 public static void main(String[] args) throws Exception { 050 StopServer cmd = new StopServer(); 051 cmd.execute(args); 052 } 053 054 public void execute(String args[]) throws IOException { 055 this.args = args; 056 057 int i = 0; 058 while (i < args.length && args[i].startsWith("--")) { 059 if (setParam(i++)) { 060 i++; 061 } 062 } 063 064 if (i < args.length) { 065 // There was an argument error somewhere. 066 printUsage(); 067 } 068 069 try { 070 if (port != null) { 071 Integer.parseInt(port); 072 } 073 } catch (NumberFormatException e) { 074 System.out.println("Invalid port number specified."); 075 System.exit(1); 076 } 077 078 InputPrompt prompt = new InputPrompt(System.in, System.out); 079 try { 080 if (user == null) { 081 user = prompt.getInput("Username: "); 082 } 083 if (password == null) { 084 password = prompt.getPassword("Password: "); 085 } 086 } catch (IOException e) { 087 System.out.println("Unable to prompt for login."); 088 System.exit(1); 089 } 090 091 try { 092 if (port == null) { 093 port = DEFAULT_PORT; 094 } 095 System.out.print("Locating server on port " + port + "... "); 096 Kernel kernel = null; 097 try { 098 kernel = getRunningKernel(); 099 } catch (IOException e) { 100 System.out.println("\nCould not communicate with the server. The server may not be running or the port number may be incorrect."); 101 } 102 if (kernel != null) { 103 System.out.println("Server found."); 104 System.out.println("Server shutdown begun"); 105 kernel.shutdown(); 106 System.out.println("Server shutdown completed"); 107 } 108 } catch (Exception e) { 109 e.printStackTrace(); 110 } 111 } 112 113 private boolean argumentHasValue(int i) { 114 return i + 1 < args.length && !args[i + 1].startsWith("--"); 115 } 116 117 private boolean setParam(int i) { 118 if (argumentHasValue(i)) { 119 if (args[i].equals("--user")) { 120 user = args[++i]; 121 } else if (args[i].equals("--password")) { 122 password = args[++i]; 123 } else if (args[i].equals("--port")) { 124 port = args[++i]; 125 } else { 126 printUsage(); 127 } 128 return true; 129 } else { 130 printUsage(); 131 } 132 return false; 133 } 134 135 public Kernel getRunningKernel() throws IOException { 136 Map map = new HashMap(); 137 map.put("jmx.remote.credentials", new String[] { user, password }); 138 Kernel kernel = null; 139 try { 140 JMXServiceURL address = new JMXServiceURL( 141 "service:jmx:rmi:///jndi/rmi://localhost" + ":" + port + "/JMXConnector"); 142 JMXConnector jmxConnector = JMXConnectorFactory.connect(address, map); 143 MBeanServerConnection mbServerConnection = jmxConnector.getMBeanServerConnection(); 144 kernel = new KernelDelegate(mbServerConnection); 145 } catch (MalformedURLException e) { 146 e.printStackTrace(); 147 } 148 return kernel; 149 } 150 151 public void printUsage() { 152 System.out.println(); 153 System.out.println("Command-line shutdown syntax:"); 154 System.out.println(" shutdown [options]"); 155 System.out.println(); 156 System.out.println("The available options are:"); 157 System.out.println(" --user"); 158 System.out.println(" --password"); 159 System.out.println(" --port"); 160 System.exit(1); 161 } 162 163 }