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: */package org.apache.geronimo.deployment.cli;
017:
018: import java.io.IOException;
019: import java.net.MalformedURLException;
020: import java.util.HashMap;
021: import java.util.Map;
022:
023: import javax.management.MBeanServerConnection;
024: import javax.management.remote.JMXConnector;
025: import javax.management.remote.JMXConnectorFactory;
026: import javax.management.remote.JMXServiceURL;
027:
028: import org.apache.geronimo.gbean.GBeanInfo;
029: import org.apache.geronimo.gbean.GBeanInfoBuilder;
030: import org.apache.geronimo.kernel.Kernel;
031: import org.apache.geronimo.kernel.util.Main;
032: import org.apache.geronimo.system.jmx.KernelDelegate;
033:
034: /**
035: * @version $Rev: 535507 $ $Date: 2007-05-05 04:22:39 -0700 (Sat, 05 May 2007) $
036: */
037: public class StopServer implements Main {
038:
039: public static final String RMI_NAMING_CONFG_ID = "org/apache/geronimo/RMINaming";
040:
041: public static final String DEFAULT_PORT = "1099"; // 1099 is used by java.rmi.registry.Registry
042:
043: String port;
044:
045: String user;
046:
047: String password;
048:
049: private String[] args;
050:
051: public static void main(String[] args) throws Exception {
052: StopServer cmd = new StopServer();
053: cmd.execute(args);
054: }
055:
056: public int execute(Object opaque) {
057: if (!(opaque instanceof String[])) {
058: throw new IllegalArgumentException("Argument type is ["
059: + opaque.getClass() + "]; expected ["
060: + String[].class + "]");
061: }
062: this .args = (String[]) opaque;
063:
064: int i = 0;
065: while (i < args.length && args[i].startsWith("--")) {
066: if (setParam(i++)) {
067: i++;
068: }
069: }
070:
071: if (i < args.length) {
072: // There was an argument error somewhere.
073: printUsage();
074: }
075:
076: try {
077: if (port != null) {
078: Integer.parseInt(port);
079: }
080: } catch (NumberFormatException e) {
081: System.out.println("Invalid port number specified.");
082: return 1;
083: }
084:
085: try {
086: InputPrompt prompt = new InputPrompt(System.in, System.out);
087: if (user == null) {
088: user = prompt.getInput("Username: ");
089: }
090: if (password == null) {
091: password = prompt.getPassword("Password: ");
092: }
093: } catch (IOException e) {
094: System.out.println("Unable to prompt for login.");
095: return 1;
096: }
097:
098: try {
099: if (port == null) {
100: port = DEFAULT_PORT;
101: }
102: System.out
103: .print("Locating server on port " + port + "... ");
104: Kernel kernel = null;
105: try {
106: kernel = getRunningKernel();
107: } catch (IOException e) {
108: System.out
109: .println("\nCould not communicate with the server. The server may not be running or the port number may be incorrect.");
110: }
111: if (kernel != null) {
112: System.out.println("Server found.");
113: System.out.println("Server shutdown started");
114: kernel.shutdown();
115: System.out.println("Server shutdown completed");
116: }
117: } catch (Exception e) {
118: e.printStackTrace();
119: return 1;
120: }
121: return 0;
122: }
123:
124: private boolean argumentHasValue(int i) {
125: return i + 1 < args.length && !args[i + 1].startsWith("--");
126: }
127:
128: private boolean setParam(int i) {
129: if (argumentHasValue(i)) {
130: if (args[i].equals("--user")) {
131: user = args[++i];
132: } else if (args[i].equals("--password")) {
133: password = args[++i];
134: } else if (args[i].equals("--port")) {
135: port = args[++i];
136: } else {
137: printUsage();
138: }
139: return true;
140: } else {
141: printUsage();
142: }
143: return false;
144: }
145:
146: public Kernel getRunningKernel() throws IOException {
147: Map map = new HashMap();
148: map.put("jmx.remote.credentials",
149: new String[] { user, password });
150: Kernel kernel = null;
151: try {
152: JMXServiceURL address = new JMXServiceURL(
153: "service:jmx:rmi:///jndi/rmi://localhost" + ":"
154: + port + "/JMXConnector");
155: JMXConnector jmxConnector = JMXConnectorFactory.connect(
156: address, map);
157: MBeanServerConnection mbServerConnection = jmxConnector
158: .getMBeanServerConnection();
159: kernel = new KernelDelegate(mbServerConnection);
160: } catch (MalformedURLException e) {
161: e.printStackTrace();
162: }
163: return kernel;
164: }
165:
166: public void printUsage() {
167: System.out.println();
168: System.out.println("Command-line shutdown syntax:");
169: System.out.println(" shutdown [options]");
170: System.out.println();
171: System.out.println("The available options are:");
172: System.out.println(" --user");
173: System.out.println(" --password");
174: System.out.println(" --port");
175: System.exit(1);
176: }
177:
178: public static final GBeanInfo GBEAN_INFO;
179:
180: static {
181: GBeanInfoBuilder infoBuilder = GBeanInfoBuilder.createStatic(
182: StopServer.class, "StopServer");
183:
184: infoBuilder.addInterface(Main.class);
185:
186: infoBuilder.setConstructor(new String[0]);
187:
188: GBEAN_INFO = infoBuilder.getBeanInfo();
189: }
190:
191: public static GBeanInfo getGBeanInfo() {
192: return GBEAN_INFO;
193: }
194:
195: }
|