KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > deployment > cli > StopServer


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 package org.apache.geronimo.deployment.cli;
19
20 import java.io.IOException JavaDoc;
21 import java.net.MalformedURLException JavaDoc;
22 import java.util.HashMap JavaDoc;
23 import java.util.Map JavaDoc;
24
25 import javax.management.MBeanServerConnection JavaDoc;
26 import javax.management.remote.JMXConnector JavaDoc;
27 import javax.management.remote.JMXConnectorFactory JavaDoc;
28 import javax.management.remote.JMXServiceURL JavaDoc;
29
30 import org.apache.geronimo.kernel.Kernel;
31 import org.apache.geronimo.system.jmx.KernelDelegate;
32
33 /**
34  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
35  */

36 public class StopServer {
37
38     public static final String JavaDoc RMI_NAMING_CONFG_ID = "org/apache/geronimo/RMINaming";
39
40     public static final String JavaDoc DEFAULT_PORT = "1099";
41
42     String JavaDoc port;
43
44     String JavaDoc user;
45
46     String JavaDoc password;
47
48     private String JavaDoc[] args;
49
50     public static void main(String JavaDoc[] args) throws Exception JavaDoc {
51         StopServer cmd = new StopServer();
52         cmd.execute(args);
53     }
54
55     public void execute(String JavaDoc args[]) throws IOException JavaDoc {
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             // There was an argument error somewhere.
67
printUsage();
68         }
69
70         try {
71             if (port != null) {
72                 Integer.parseInt(port);
73             }
74         } catch (NumberFormatException JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc {
137         Map JavaDoc map = new HashMap JavaDoc();
138         map.put("jmx.remote.credentials", new String JavaDoc[] { user, password });
139         Kernel kernel = null;
140         try {
141             JMXServiceURL JavaDoc address = new JMXServiceURL JavaDoc(
142                     "service:jmx:rmi:///jndi/rmi://localhost" + ":" + port + "/JMXConnector");
143             JMXConnector JavaDoc jmxConnector = JMXConnectorFactory.connect(address, map);
144             MBeanServerConnection JavaDoc mbServerConnection = jmxConnector.getMBeanServerConnection();
145             kernel = new KernelDelegate(mbServerConnection);
146         } catch (MalformedURLException JavaDoc 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