1 |
//---PACKAGE DECLARATION--- |
2 |
package uk.org.iscream.cms.server.filter; |
3 |
|
4 |
//---IMPORTS--- |
5 |
import java.net.*; |
6 |
import java.io.*; |
7 |
|
8 |
/** |
9 |
* A skeleton class for Service Checks to extend. |
10 |
* |
11 |
* @author $Author: tdb1 $ |
12 |
* @version $Id: ServiceCheckSkeleton.java,v 1.4 2001/03/14 23:25:29 tdb1 Exp $ |
13 |
*/ |
14 |
public abstract class ServiceCheckSkeleton implements PluginServiceCheck { |
15 |
|
16 |
//---FINAL ATTRIBUTES--- |
17 |
|
18 |
//---STATIC METHODS--- |
19 |
|
20 |
//---CONSTRUCTORS--- |
21 |
|
22 |
//---PUBLIC METHODS--- |
23 |
|
24 |
/** |
25 |
* Runs a service check on a given hostname. |
26 |
* |
27 |
* @param hostname The hostname to check |
28 |
*/ |
29 |
public abstract String runServiceCheck(String hostname); |
30 |
|
31 |
/** |
32 |
* Checks if a service is returning a valid response, and |
33 |
* constructs suitable XML for inclusion in a packet. |
34 |
* |
35 |
* @param hostname The hostname to query |
36 |
* @param port The port to query |
37 |
* @param expectedSubString The String to look for in the response |
38 |
* @param XMLTag The name of the tag to be put in the XML |
39 |
* @return The String of XML with the response information |
40 |
*/ |
41 |
protected String checkService(String hostname, int port, String expectedSubString, String XMLTag) { |
42 |
String message, status; |
43 |
try { |
44 |
// connect and get response |
45 |
String response = connectToService(hostname, port); |
46 |
// check if the expected text is in the response |
47 |
if(response.indexOf(expectedSubString) != -1) { |
48 |
status = "0"; |
49 |
message = "Good Header received: "+response; |
50 |
} |
51 |
else { |
52 |
status = "1"; |
53 |
message = "Bad Header received: "+response; |
54 |
} |
55 |
} catch (IOException e) { |
56 |
// connect failed |
57 |
status = "1"; |
58 |
message = "Service check failed to establish connection to host: " + e.getMessage(); |
59 |
} |
60 |
return "<"+XMLTag+" status=\"" + status + "\" message=\"" + message + "\"></"+XMLTag+">"; |
61 |
} |
62 |
|
63 |
/** |
64 |
* Connects to a service and returns the first line of text. |
65 |
* |
66 |
* @param hostname The hostname of the service to check |
67 |
* @param port The port of the service to check |
68 |
* @return The first line of text given |
69 |
* @throws IOException if the connection fails for some reason |
70 |
*/ |
71 |
protected String connectToService(String hostname, int port) throws IOException { |
72 |
// connect |
73 |
Socket socket = new Socket(hostname, port); |
74 |
BufferedReader socketIn = new BufferedReader(new InputStreamReader(socket.getInputStream())); |
75 |
// read first line |
76 |
String response = socketIn.readLine(); |
77 |
// close |
78 |
socketIn.close(); |
79 |
socket.close(); |
80 |
// return first line |
81 |
return response; |
82 |
} |
83 |
|
84 |
/** |
85 |
* Used to query the Service Check to see what it does. |
86 |
* |
87 |
* @return the String representation of what the Service Check does |
88 |
*/ |
89 |
public abstract String getDescription(); |
90 |
|
91 |
//---PRIVATE METHODS--- |
92 |
|
93 |
//---ACCESSOR/MUTATOR METHODS--- |
94 |
|
95 |
//---ATTRIBUTES--- |
96 |
|
97 |
//---STATIC ATTRIBUTES--- |
98 |
|
99 |
} |