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: public int port = Arguments.DEFAULT_PORT;
11: public String username;
12: public String password;
13: private CommandLine commandLine = new CommandLine(
14: "[-h hostname] [-p port] [-c username password]");;
15:
16: public static void main(String[] args) throws Exception {
17: Shutdown shutdown = new Shutdown();
18: shutdown.run(args);
19: }
20:
21: private void run(String[] args) throws Exception {
22: if (!parseArgs(args))
23: usage();
24:
25: ResponseParser response = buildAndSendRequest();
26:
27: String status = checkResponse(response);
28: if (!"OK".equals(status)) {
29: System.err.println("Failed to shutdown.");
30: System.err.println(status);
31: System.exit(response.getStatus());
32: }
33: }
34:
35: public ResponseParser buildAndSendRequest() throws Exception {
36: RequestBuilder request = buildRequest();
37: ResponseParser response = ResponseParser.performHttpRequest(
38: hostname, port, request);
39: return response;
40: }
41:
42: public RequestBuilder buildRequest() throws Exception {
43: RequestBuilder request = new RequestBuilder(
44: "/?responder=shutdown");
45: if (username != null)
46: request.addCredentials(username, password);
47: return request;
48: }
49:
50: public String checkResponse(ResponseParser response) {
51: int status = response.getStatus();
52: String serverHeader = response.getHeader("Server");
53: if (serverHeader == null
54: || serverHeader.indexOf("FitNesse") == -1)
55: return "Not a FitNesse server";
56: else if (status != 200)
57: return status + " " + Response.getReasonPhrase(status);
58: else
59: return "OK";
60: }
61:
62: public boolean parseArgs(String[] args) {
63: if (!commandLine.parse(args))
64: return false;
65:
66: if (commandLine.hasOption("h"))
67: hostname = commandLine.getOptionArgument("h", "hostname");
68: if (commandLine.hasOption("p"))
69: port = Integer.parseInt(commandLine.getOptionArgument("p",
70: "port"));
71: if (commandLine.hasOption("c")) {
72: username = commandLine.getOptionArgument("c", "username");
73: password = commandLine.getOptionArgument("c", "password");
74: }
75: return true;
76: }
77:
78: public void usage() {
79: System.err.println("Usage: java fitnesse.Shutdown [-hpc]");
80: System.err.println("\t-h <hostname> {localhost}");
81: System.err.println("\t-p <port number> {"
82: + Arguments.DEFAULT_PORT + "}");
83: System.err
84: .println("\t-c <username> <password> Supply user credentials. Use when FitNesse has authentication activated.");
85: System.exit(-1);
86: }
87: }
|