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    
030    import org.apache.geronimo.gbean.GBeanInfo;
031    import org.apache.geronimo.gbean.GBeanInfoBuilder;
032    import org.apache.geronimo.kernel.Kernel;
033    import org.apache.geronimo.kernel.util.Main;
034    import org.apache.geronimo.system.jmx.KernelDelegate;
035    
036    /**
037     * @version $Rev: 535507 $ $Date: 2007-05-05 07:22:39 -0400 (Sat, 05 May 2007) $
038     */
039    public class StopServer implements Main {
040    
041            public static final String RMI_NAMING_CONFG_ID = "org/apache/geronimo/RMINaming";
042    
043            public static final String DEFAULT_PORT = "1099"; // 1099 is used by java.rmi.registry.Registry
044    
045            String port;
046    
047            String user;
048    
049            String password;
050    
051            private String[] args;
052    
053            public static void main(String[] args) throws Exception {
054                    StopServer cmd = new StopServer();
055                    cmd.execute(args);
056            }
057        
058        public int execute(Object opaque) {
059            if (! (opaque instanceof String[])) {
060                throw new IllegalArgumentException("Argument type is [" + opaque.getClass() + "]; expected [" + 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.print("Locating server on port " + port + "... ");
103                Kernel kernel = null;
104                try {
105                    kernel = getRunningKernel();
106                } catch (IOException e) {
107                    System.out.println("\nCould not communicate with the server.  The server may not be running or the port number may be incorrect.");
108                }
109                if (kernel != null) {
110                    System.out.println("Server found.");
111                    System.out.println("Server shutdown started");
112                    kernel.shutdown();
113                    System.out.println("Server shutdown completed");
114                }
115            } catch (Exception e) {
116                e.printStackTrace();
117                return 1;
118            }
119            return 0;
120        }
121    
122            private boolean argumentHasValue(int i) {
123                    return i + 1 < args.length && !args[i + 1].startsWith("--");
124            }
125    
126            private boolean setParam(int i) {
127                    if (argumentHasValue(i)) {
128                            if (args[i].equals("--user")) {
129                                    user = args[++i];
130                            } else if (args[i].equals("--password")) {
131                                    password = args[++i];
132                            } else if (args[i].equals("--port")) {
133                                    port = args[++i];
134                            } else {
135                                    printUsage();
136                            }
137                            return true;
138                    } else {
139                            printUsage();
140                    }
141                    return false;
142            }
143    
144            public Kernel getRunningKernel() throws IOException {
145                    Map map = new HashMap();
146                    map.put("jmx.remote.credentials", new String[] { user, password });
147                    Kernel kernel = null;
148                    try {
149                            JMXServiceURL address = new JMXServiceURL(
150                                            "service:jmx:rmi:///jndi/rmi://localhost" + ":" + port + "/JMXConnector");
151                            JMXConnector jmxConnector = JMXConnectorFactory.connect(address, map);
152                            MBeanServerConnection mbServerConnection = jmxConnector.getMBeanServerConnection();
153                            kernel = new KernelDelegate(mbServerConnection);
154                    } catch (MalformedURLException e) {
155                            e.printStackTrace();
156                    }
157                    return kernel;
158            }
159    
160            public void printUsage() {
161                    System.out.println();
162                    System.out.println("Command-line shutdown syntax:");
163                    System.out.println("    shutdown [options]");
164                    System.out.println();
165                    System.out.println("The available options are:");
166                    System.out.println("    --user");
167                    System.out.println("    --password");
168                    System.out.println("    --port");
169                    System.exit(1);
170            }
171    
172        public static final GBeanInfo GBEAN_INFO;
173    
174        static {
175            GBeanInfoBuilder infoBuilder = GBeanInfoBuilder.createStatic(StopServer.class, "StopServer");
176    
177            infoBuilder.addInterface(Main.class);
178    
179            infoBuilder.setConstructor(new String[0]);
180    
181            GBEAN_INFO = infoBuilder.getBeanInfo();
182        }
183    
184        public static GBeanInfo getGBeanInfo() {
185            return GBEAN_INFO;
186        }
187    
188    }