View Javadoc

1   /*
2    * Copyright 2001-2004 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.jdiagnose.library;
17  
18  import org.jdiagnose.DiagnosticUnit;
19  
20  import java.io.IOException;
21  import java.net.Socket;
22  import java.net.UnknownHostException;
23  
24  /***
25   * Tries to open a Tcp Port and then closes it. This Diagnostic
26   * can be used to check connectivity to a particular host and
27   * port.
28   * 
29   * @author jamie
30   */
31  public class TcpDiagnostic extends DiagnosticUnit {
32      
33      private String host = "";
34      private int port = 80;
35  
36      public TcpDiagnostic() {
37  
38      }
39  
40      /***
41       * Create a new DiagnosticUnit with a Fully Qualified Name
42       *
43       * @param name the name of this diagnostic.
44       */
45      public TcpDiagnostic(String name, String host, int port) {
46          super(name);
47          this.host = host;
48          this.port = port;
49      }
50  
51      public TcpDiagnostic(String host, int port) {
52          this.host = host;
53          this.port = port;
54      }
55  
56      public void diagnoseConnection() throws UnknownHostException, IOException {
57          Socket socket = new Socket(host, port);
58          socket.close();
59      }
60      /***
61       * @return Returns the host.
62       */
63      public String getHost() {
64          return host;
65      }
66      /***
67       * @param host The host to set.
68       */
69      public void setHost(String host) {
70          this.host = host;
71      }
72      /***
73       * @return Returns the port.
74       */
75      public int getPort() {
76          return port;
77      }
78      /***
79       * @param port The port to set.
80       */
81      public void setPort(int port) {
82          this.port = port;
83      }
84  }