View Javadoc

1   /**
2    *  Copyright 2005 The Apache Software Foundation
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
15   */
16  
17  package org.apache.geronimo.deployment.cli;
18  
19  import java.io.IOException;
20  import java.net.MalformedURLException;
21  import java.util.HashMap;
22  import java.util.Map;
23  
24  import javax.management.MBeanServerConnection;
25  import javax.management.remote.JMXConnector;
26  import javax.management.remote.JMXConnectorFactory;
27  import javax.management.remote.JMXServiceURL;
28  
29  import org.apache.geronimo.kernel.Kernel;
30  import org.apache.geronimo.system.jmx.KernelDelegate;
31  
32  /**
33   * @version $Rev: 430136 $ $Date: 2006-08-09 13:21:52 -0700 (Wed, 09 Aug 2006) $
34   */
35  public class StopServer {
36  
37  	public static final String RMI_NAMING_CONFG_ID = "org/apache/geronimo/RMINaming";
38  
39  	public static final String DEFAULT_PORT = "1099";
40  
41  	String port;
42  
43  	String user;
44  
45  	String password;
46  
47  	private String[] args;
48  
49  	public static void main(String[] args) throws Exception {
50  		StopServer cmd = new StopServer();
51  		cmd.execute(args);
52  	}
53  
54  	public void execute(String args[]) throws IOException {
55          this.args = args;
56  
57          int i = 0;
58          while (i < args.length && args[i].startsWith("--")) {
59              if (setParam(i++)) {
60                  i++;
61              }
62          }
63  
64          if (i < args.length) {
65              // There was an argument error somewhere.
66              printUsage();
67          }
68  
69          try {
70              if (port != null) {
71                  Integer.parseInt(port);
72              }
73          } catch (NumberFormatException e) {
74              System.out.println("Invalid port number specified.");
75              System.exit(1);
76          }
77  
78          InputPrompt prompt = new InputPrompt(System.in, System.out);
79          try {
80              if (user == null) {
81                  user = prompt.getInput("Username: ");
82              }
83              if (password == null) {
84                  password = prompt.getPassword("Password: ");
85              }
86          } catch (IOException e) {
87              System.out.println("Unable to prompt for login.");
88              System.exit(1);
89          }
90  
91          try {
92              if (port == null) {
93                  port = DEFAULT_PORT;
94              }
95              System.out.print("Locating server on port " + port + "... ");
96              Kernel kernel = null;
97              try {
98                  kernel = getRunningKernel();
99              } 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 }