1 17 18 package org.apache.geronimo.deployment.cli; 19 20 import java.io.IOException ; 21 import java.net.MalformedURLException ; 22 import java.util.HashMap ; 23 import java.util.Map ; 24 25 import javax.management.MBeanServerConnection ; 26 import javax.management.remote.JMXConnector ; 27 import javax.management.remote.JMXConnectorFactory ; 28 import javax.management.remote.JMXServiceURL ; 29 30 import org.apache.geronimo.kernel.Kernel; 31 import org.apache.geronimo.system.jmx.KernelDelegate; 32 33 36 public class StopServer { 37 38 public static final String RMI_NAMING_CONFG_ID = "org/apache/geronimo/RMINaming"; 39 40 public static final String DEFAULT_PORT = "1099"; 41 42 String port; 43 44 String user; 45 46 String password; 47 48 private String [] args; 49 50 public static void main(String [] args) throws Exception { 51 StopServer cmd = new StopServer(); 52 cmd.execute(args); 53 } 54 55 public void execute(String args[]) throws IOException { 56 this.args = args; 57 58 int i = 0; 59 while (i < args.length && args[i].startsWith("--")) { 60 if (setParam(i++)) { 61 i++; 62 } 63 } 64 65 if (i < args.length) { 66 printUsage(); 68 } 69 70 try { 71 if (port != null) { 72 Integer.parseInt(port); 73 } 74 } catch (NumberFormatException e) { 75 System.out.println("Invalid port number specified."); 76 System.exit(1); 77 } 78 79 InputPrompt prompt = new InputPrompt(System.in, System.out); 80 try { 81 if (user == null) { 82 user = prompt.getInput("Username: "); 83 } 84 if (password == null) { 85 password = prompt.getPassword("Password: "); 86 } 87 } catch (IOException e) { 88 System.out.println("Unable to prompt for login."); 89 System.exit(1); 90 } 91 92 try { 93 if (port == null) { 94 port = DEFAULT_PORT; 95 } 96 System.out.print("Locating server on port " + port + "... "); 97 Kernel kernel = null; 98 try { 99 kernel = getRunningKernel(); 100 } catch (IOException e) { 101 System.out.println("\nCould not communicate with the server. The server may not be running or the port number may be incorrect."); 102 } 103 if (kernel != null) { 104 System.out.println("Server found."); 105 System.out.println("Server shutdown begun"); 106 kernel.shutdown(); 107 System.out.println("Server shutdown completed"); 108 } 109 } catch (Exception e) { 110 e.printStackTrace(); 111 } 112 } 113 114 private boolean argumentHasValue(int i) { 115 return i + 1 < args.length && !args[i + 1].startsWith("--"); 116 } 117 118 private boolean setParam(int i) { 119 if (argumentHasValue(i)) { 120 if (args[i].equals("--user")) { 121 user = args[++i]; 122 } else if (args[i].equals("--password")) { 123 password = args[++i]; 124 } else if (args[i].equals("--port")) { 125 port = args[++i]; 126 } else { 127 printUsage(); 128 } 129 return true; 130 } else { 131 printUsage(); 132 } 133 return false; 134 } 135 136 public Kernel getRunningKernel() throws IOException { 137 Map map = new HashMap (); 138 map.put("jmx.remote.credentials", new String [] { user, password }); 139 Kernel kernel = null; 140 try { 141 JMXServiceURL address = new JMXServiceURL ( 142 "service:jmx:rmi:///jndi/rmi://localhost" + ":" + port + "/JMXConnector"); 143 JMXConnector jmxConnector = JMXConnectorFactory.connect(address, map); 144 MBeanServerConnection mbServerConnection = jmxConnector.getMBeanServerConnection(); 145 kernel = new KernelDelegate(mbServerConnection); 146 } catch (MalformedURLException e) { 147 e.printStackTrace(); 148 } 149 return kernel; 150 } 151 152 public void printUsage() { 153 System.out.println(); 154 System.out.println("Command-line shutdown syntax:"); 155 System.out.println(" shutdown [options]"); 156 System.out.println(); 157 System.out.println("The available options are:"); 158 System.out.println(" --user"); 159 System.out.println(" --password"); 160 System.out.println(" --port"); 161 System.exit(1); 162 } 163 164 } 165 | Popular Tags |