KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jdon > util > URLUtil


1 /*
2  * $Id: URLUtil.java,v 1.2 2005/01/31 05:27:54 jdon Exp $
3  *
4  * Copyright (c) 2001, 2002 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */

24
25 package com.jdon.util;
26
27 import java.net.*;
28 import java.io.*;
29
30 public class URLUtil {
31
32   private URLConnection connection = null;
33   private URL url = null;
34   private boolean timedOut = false;
35
36   protected URLUtil() {}
37
38   protected URLUtil(URL url) {
39     this.url = url;
40   }
41
42   protected synchronized URLConnection openConnection(int timeout) throws
43       IOException {
44     Thread JavaDoc t = new Thread JavaDoc(new URLConnectorThread());
45     t.start();
46
47     try {
48       this.wait(timeout);
49     } catch (InterruptedException JavaDoc e) {
50       if (connection == null)
51         timedOut = true;
52       else
53         close(connection);
54       throw new IOException("Connection never established");
55     }
56
57     if (connection != null) {
58       return connection;
59     } else {
60       timedOut = true;
61       throw new IOException("Connection timed out");
62     }
63   }
64
65   public static URLConnection openConnection(URL url) throws IOException {
66     return openConnection(url, 30000);
67   }
68
69   public static URLConnection openConnection(URL url, int timeout) throws
70       IOException {
71     URLUtil uc = new URLUtil(url);
72     return uc.openConnection(timeout);
73   }
74
75   // special thread to open the connection
76
private class URLConnectorThread implements Runnable JavaDoc {
77     public void run() {
78       URLConnection con = null;
79       try {
80         con = url.openConnection();
81       } catch (IOException e) {}
82
83       synchronized (URLUtil.this) {
84         if (timedOut && con != null)
85           close(con);
86         else {
87           connection = con;
88           URLUtil.this.notify();
89         }
90       }
91     }
92   }
93
94   // closes the HttpURLConnection does nothing to others
95
private static void close(URLConnection con) {
96     if (con instanceof HttpURLConnection) {
97       ( (HttpURLConnection) con).disconnect();
98     }
99   }
100
101
102 }
103
Popular Tags