01: // Copyright (C) 2003,2004,2005 by Object Mentor, Inc. All rights reserved.
02: // Released under the terms of the GNU General Public License version 2 or later.
03: package fitnesse;
04:
05: import fitnesse.components.CommandLine;
06: import fitnesse.http.*;
07:
08: public class Shutdown {
09: public String hostname = "localhost";
10:
11: public int port = Arguments.DEFAULT_PORT;
12:
13: public String username;
14:
15: public String password;
16:
17: private CommandLine commandLine = new CommandLine(
18: "[-h hostname] [-p port] [-c username password]");
19:
20: public static void main(String[] args) throws Exception {
21: Shutdown shutdown = new Shutdown();
22: shutdown.run(args);
23: }
24:
25: private void run(String[] args) throws Exception {
26: if (!parseArgs(args))
27: usage();
28:
29: ResponseParser response = buildAndSendRequest();
30:
31: String status = checkResponse(response);
32: if (!"OK".equals(status)) {
33: System.err.println("Failed to shutdown.");
34: System.err.println(status);
35: System.exit(response.getStatus());
36: }
37: }
38:
39: public ResponseParser buildAndSendRequest() throws Exception {
40: RequestBuilder request = buildRequest();
41: ResponseParser response = ResponseParser.performHttpRequest(
42: hostname, port, request);
43: return response;
44: }
45:
46: public RequestBuilder buildRequest() throws Exception {
47: RequestBuilder request = new RequestBuilder(
48: "/?responder=shutdown");
49: if (username != null)
50: request.addCredentials(username, password);
51: return request;
52: }
53:
54: public String checkResponse(ResponseParser response) {
55: int status = response.getStatus();
56: String serverHeader = response.getHeader("Server");
57: if (serverHeader == null
58: || serverHeader.indexOf("FitNesse") == -1)
59: return "Not a FitNesse server";
60: else if (status != 200)
61: return status + " " + Response.getReasonPhrase(status);
62: else
63: return "OK";
64: }
65:
66: public boolean parseArgs(String[] args) {
67: if (!commandLine.parse(args))
68: return false;
69:
70: if (commandLine.hasOption("h"))
71: hostname = commandLine.getOptionArgument("h", "hostname");
72: if (commandLine.hasOption("p"))
73: port = Integer.parseInt(commandLine.getOptionArgument("p",
74: "port"));
75: if (commandLine.hasOption("c")) {
76: username = commandLine.getOptionArgument("c", "username");
77: password = commandLine.getOptionArgument("c", "password");
78: }
79: return true;
80: }
81:
82: public void usage() {
83: System.err.println("Usage: java fitnesse.Shutdown [-hpc]");
84: System.err.println("\t-h <hostname> {localhost}");
85: System.err.println("\t-p <port number> {"
86: + Arguments.DEFAULT_PORT + "}");
87: System.err
88: .println("\t-c <username> <password> Supply user credentials. Use when FitNesse has authentication activated.");
89: System.exit(-1);
90: }
91: }
|