001 /** 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 018 package org.apache.geronimo.deployment.cli; 019 020 import java.io.IOException; 021 import java.net.MalformedURLException; 022 import java.util.HashMap; 023 import java.util.Map; 024 025 import javax.management.MBeanServerConnection; 026 import javax.management.remote.JMXConnector; 027 import javax.management.remote.JMXConnectorFactory; 028 import javax.management.remote.JMXServiceURL; 029 import javax.management.remote.rmi.RMIConnectorServer; 030 import javax.rmi.ssl.SslRMIClientSocketFactory; 031 032 import org.apache.geronimo.gbean.GBeanInfo; 033 import org.apache.geronimo.gbean.GBeanInfoBuilder; 034 import org.apache.geronimo.kernel.Kernel; 035 import org.apache.geronimo.kernel.util.Main; 036 import org.apache.geronimo.system.jmx.KernelDelegate; 037 038 /** 039 * @version $Rev: 706640 $ $Date: 2008-10-21 14:44:05 +0000 (Tue, 21 Oct 2008) $ 040 */ 041 public class StopServer implements Main { 042 043 public static final String RMI_NAMING_CONFG_ID = "org/apache/geronimo/RMINaming"; 044 045 public static final String DEFAULT_PORT = "1099"; // 1099 is used by java.rmi.registry.Registry 046 047 String host = "localhost"; 048 049 String port; 050 051 String user; 052 053 String password; 054 055 boolean secure = false; 056 057 private String[] args; 058 059 public static void main(String[] args) throws Exception { 060 StopServer cmd = new StopServer(); 061 cmd.execute(args); 062 } 063 064 public int execute(Object opaque) { 065 if (! (opaque instanceof String[])) { 066 throw new IllegalArgumentException("Argument type is [" + opaque.getClass() + "]; expected [" + String[].class + "]"); 067 } 068 this.args = (String[]) opaque; 069 070 int i = 0; 071 while (i < args.length && args[i].startsWith("--")) { 072 if (setParam(i++)) { 073 i++; 074 } 075 } 076 077 if (i < args.length) { 078 // There was an argument error somewhere. 079 printUsage(); 080 } 081 082 try { 083 if (port != null) { 084 Integer.parseInt(port); 085 } 086 } catch (NumberFormatException e) { 087 System.out.println("Invalid port number specified."); 088 return 1; 089 } 090 091 try { 092 InputPrompt prompt = new InputPrompt(System.in, System.out); 093 if (user == null) { 094 user = prompt.getInput("Username: "); 095 } 096 if (password == null) { 097 password = prompt.getPassword("Password: "); 098 } 099 } catch (IOException e) { 100 System.out.println("Unable to prompt for login."); 101 return 1; 102 } 103 104 try { 105 if (port == null) { 106 port = DEFAULT_PORT; 107 } 108 System.out.print("Locating server on port " + port + "... "); 109 Kernel kernel = null; 110 try { 111 kernel = getRunningKernel(); 112 } catch (IOException e) { 113 System.out.println(); 114 System.out.println("Could not communicate with the server. The server may not be running or the port number may be incorrect (" + e.getMessage() + ")"); 115 } 116 if (kernel != null) { 117 System.out.println("Server found."); 118 System.out.println("Server shutdown started"); 119 kernel.shutdown(); 120 System.out.println("Server shutdown completed"); 121 } 122 } catch (Exception e) { 123 e.printStackTrace(); 124 return 1; 125 } 126 return 0; 127 } 128 129 private boolean argumentHasValue(int i) { 130 return i + 1 < args.length && !args[i + 1].startsWith("--"); 131 } 132 133 private boolean setParam(int i) { 134 if (argumentHasValue(i)) { 135 if (args[i].equals("--user")) { 136 user = args[++i]; 137 } else if (args[i].equals("--password")) { 138 password = args[++i]; 139 } else if (args[i].equals("--port")) { 140 port = args[++i]; 141 } else if (args[i].equals("--host")) { 142 host = args[++i]; 143 } else { 144 printUsage(); 145 } 146 return true; 147 } else if (args[i].equals("--secure")) { 148 secure = true; 149 } else { 150 printUsage(); 151 } 152 return false; 153 } 154 155 public Kernel getRunningKernel() throws IOException { 156 Map map = new HashMap(); 157 map.put(JMXConnector.CREDENTIALS, new String[] { user, password }); 158 String connectorName = "/JMXConnector"; 159 if (secure) { 160 connectorName = "/JMXSecureConnector"; 161 SslRMIClientSocketFactory csf = new SslRMIClientSocketFactory(); 162 map.put(RMIConnectorServer.RMI_CLIENT_SOCKET_FACTORY_ATTRIBUTE, csf); 163 } 164 Kernel kernel = null; 165 try { 166 JMXServiceURL address = new JMXServiceURL( 167 "service:jmx:rmi:///jndi/rmi://" + host + ":" + port + connectorName); 168 JMXConnector jmxConnector = JMXConnectorFactory.connect(address, map); 169 MBeanServerConnection mbServerConnection = jmxConnector.getMBeanServerConnection(); 170 kernel = new KernelDelegate(mbServerConnection); 171 } catch (MalformedURLException e) { 172 e.printStackTrace(); 173 } 174 return kernel; 175 } 176 177 public void printUsage() { 178 System.out.println(); 179 System.out.println("Command-line shutdown syntax:"); 180 System.out.println(" shutdown [options]"); 181 System.out.println(); 182 System.out.println("The available options are:"); 183 System.out.println(" --user <username>"); 184 System.out.println(" --password <password>"); 185 System.out.println(" --host <hostname>"); 186 System.out.println(" --port <port>"); 187 System.out.println(" --secure"); 188 System.exit(1); 189 } 190 191 public static final GBeanInfo GBEAN_INFO; 192 193 static { 194 GBeanInfoBuilder infoBuilder = GBeanInfoBuilder.createStatic(StopServer.class, "StopServer"); 195 196 infoBuilder.addInterface(Main.class); 197 198 infoBuilder.setConstructor(new String[0]); 199 200 GBEAN_INFO = infoBuilder.getBeanInfo(); 201 } 202 203 public static GBeanInfo getGBeanInfo() { 204 return GBEAN_INFO; 205 } 206 207 }