2011/08/05 - Jakarta Cactus has been retired.

For more information, please explore the Attic.

View Javadoc

1   /* 
2    * ========================================================================
3    * 
4    * Licensed to the Apache Software Foundation (ASF) under one or more
5    * contributor license agreements.  See the NOTICE file distributed with
6    * this work for additional information regarding copyright ownership.
7    * The ASF licenses this file to You under the Apache License, Version 2.0
8    * (the "License"); you may not use this file except in compliance with
9    * the License.  You may obtain a copy of the License at
10   * 
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   * 
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   * 
19   * ========================================================================
20   */
21  package org.apache.cactus.integration.ant.util;
22  
23  import java.io.IOException;
24  import java.io.InputStream;
25  import java.net.HttpURLConnection;
26  import java.net.URL;
27  
28  /**
29   * Support class that probes a URL. 
30   * 
31   * 
32   * @version $Id: HttpProbe.java,v 1.0 2005/08/29 10:19:57 xnguyen Exp $
33   */
34  public class HttpProbe
35  {
36  
37      // Instance Variables ------------------------------------------------------
38  
39      /**
40       * The URL.
41       */
42      private URL url;
43  
44      // Constructors ------------------------------------------------------------
45  
46      /**
47       * Constructor.
48       * 
49       * @param theUrl The Url
50       */
51      public HttpProbe(URL theUrl) 
52      {
53          this.url = theUrl;
54      }
55      
56      /**
57       * In thread tests whether we are able to connect to the 
58       * HTTP server identified by the
59       * specified URL. The caller thread is blocked.
60       * @param theWaitedTime The time waiting 
61       * @param theCheckedInterval The interval to check
62       * @throws InterruptedException If the current Thread is interupted
63       * @throws IOException If there is error with reading
64       * @return the HTTP response code or -1 if no connection could be
65       *         established
66       */
67      public boolean timeout(long theWaitedTime, long theCheckedInterval)
68          throws InterruptedException, IOException
69      {
70          //Ping the container
71          //Continuously try calling the test URL until it succeeds or
72          // until a timeout is reached (we then throw a build exception).
73          long startTime = System.currentTimeMillis();
74          int responseCode = -1;
75          do
76          {
77              if ((System.currentTimeMillis() - startTime) > theWaitedTime)
78              {
79                  return true;
80  
81              }
82  
83              sleep(theCheckedInterval);
84  
85              responseCode = testConnectivity();
86  
87  
88          } while (!isAvailable(responseCode));
89          
90          return false;
91  
92      }
93   
94   
95      /**
96       * Tests whether we are able to connect to the HTTP server identified by the
97       * specified URL.
98       * @throws IOException If there is reading error
99       * @return the HTTP response code or -1 if no connection could be
100      *         established
101      */
102     public int testConnectivity() throws IOException
103     {
104         int code = -1;
105         HttpURLConnection connection = null;
106        
107         connection = (HttpURLConnection) url.openConnection();
108         connection.setRequestProperty("Connection", "close");
109         connection.connect();
110         code = connection.getResponseCode();
111         readFully(connection);
112         connection.disconnect();
113       
114         return code;
115     }
116 
117 
118     /**
119      * Tests whether an HTTP return code corresponds to a valid connection
120      * to the test URL or not. Success is 200 up to but excluding 300.
121      *
122      * @param theCode the HTTP response code to verify
123      * @return <code>true</code> if the test URL could be called without error,
124      *         <code>false</code> otherwise
125      */
126     private boolean isAvailable(int theCode)
127     {
128         boolean result;
129         if ((theCode != -1) && (theCode < 300))
130         {
131             result = true;
132         }
133         else
134         {
135             result = false;
136         }
137         return result;
138     }
139 
140     /**
141      * Retrieves the server name of the container.
142      *
143      * @return The server name, or <code>null</code> if the server name could
144      *         not be retrieved
145      * @throws IOException If there is error when reading
146      */
147     private String retrieveServerName() throws IOException
148     {
149         String retVal = null;
150         
151         HttpURLConnection connection =
152             (HttpURLConnection) url.openConnection();
153         connection.connect();
154         retVal = connection.getHeaderField("Server");
155         connection.disconnect();
156        
157         return retVal;
158     }
159 
160     /**
161      * Fully reads the input stream from the passed HTTP URL connection to
162      * prevent (harmless) server-side exception.
163      *
164      * @param theConnection the HTTP URL connection to read from
165      * @exception IOException if an error happens during the read
166      */
167     private void readFully(HttpURLConnection theConnection)
168                    throws IOException
169     {
170         // Only read if there is data to read ... The problem is that not
171         // all servers return a content-length header. If there is no header
172         // getContentLength() returns -1. It seems to work and it seems
173         // that all servers that return no content-length header also do
174         // not block on read() operations!
175         if (theConnection.getContentLength() != 0)
176         {
177             byte[] buf = new byte[256];
178             InputStream in = theConnection.getInputStream();
179             while (in.read(buf) != -1)
180             {
181                 // Make sure we read all the data in the stream
182             }
183         }
184     }
185 
186     /**
187      * Pauses the current thread for the specified amount.
188      *
189      * @param theMs The time to sleep in milliseconds
190      * @throws InterruptedException If the sleeping thread is interrupted
191      */
192     private void sleep(long theMs) throws InterruptedException
193     {
194 
195             Thread.sleep(theMs);
196  
197     }
198 
199     // Private Methods ---------------------------------------------------------
200 
201 
202 
203 }