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.1
Committed: Wed Mar 7 23:19:13 2001 UTC (23 years, 2 months ago) by tdb
Branch: MAIN
Log Message:
Made an abstract skeleton class for the service checks. They all do very similar
things, so it seemed worth putting all the behaviour in one place. This also has
another advantage, namely that service checks are now much quicker to produce,
and that in the future the checks could even be moved to a general checker that
reads from the configuration.

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     * @author $Author$
12     * @version $Id$
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, 22);
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 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     }