ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/cms/source/server/uk/org/iscream/cms/server/filter/ServiceCheckSkeleton.java
Revision: 1.2
Committed: Wed Mar 7 23:28:08 2001 UTC (23 years, 3 months ago) by tdb
Branch: MAIN
Changes since 1.1: +3 -3 lines
Log Message:
A minor, but huge mistake. We were only checking the SSH port :)

File Contents

# User Rev Content
1 tdb 1.1 //---PACKAGE DECLARATION---
2     package uk.ac.ukc.iscream.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 tdb 1.2 * @author $Author: tdb1 $
12     * @version $Id: ServiceCheckSkeleton.java,v 1.1 2001/03/07 23:19:13 tdb1 Exp $
13 tdb 1.1 */
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 tdb 1.2 Socket socket = new Socket(hostname, port);
74 tdb 1.1 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 String getDescription(){
90     return DESC;
91     }
92    
93     //---PRIVATE METHODS---
94    
95     //---ACCESSOR/MUTATOR METHODS---
96    
97     //---ATTRIBUTES---
98    
99     private String DESC;
100    
101     //---STATIC ATTRIBUTES---
102    
103     }