KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jnp > interfaces > TimedSocketFactory


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jnp.interfaces;
23
24 import java.io.IOException JavaDoc;
25 import java.net.ConnectException JavaDoc;
26 import java.net.InetAddress JavaDoc;
27 import java.net.Socket JavaDoc;
28 import java.net.UnknownHostException JavaDoc;
29 import java.util.Hashtable JavaDoc;
30 import javax.net.SocketFactory;
31
32 /** A concrete implementation of the SocketFactory that supports a configurable
33  timeout for the initial socket connection as well as the SO_TIMEOUT used to
34  determine how long a read will block waiting for data.
35
36 @author Scott.Stark@jboss.org
37 @version $Revision: 37459 $
38  */

39 public class TimedSocketFactory extends SocketFactory
40 {
41    public static final String JavaDoc JNP_TIMEOUT = "jnp.timeout";
42    public static final String JavaDoc JNP_SO_TIMEOUT = "jnp.sotimeout";
43
44    /** The connection timeout in milliseconds */
45    protected int timeout = 0;
46    /** The SO_TIMEOUT in milliseconds */
47    protected int soTimeout = 0;
48
49    /** Creates a new instance of TimedSocketFactory */
50    public TimedSocketFactory()
51    {
52    }
53    public TimedSocketFactory(Hashtable JavaDoc env)
54    {
55       String JavaDoc value = (String JavaDoc) env.get(JNP_TIMEOUT);
56       if( value != null )
57          timeout = Integer.parseInt(value);
58       value = (String JavaDoc) env.get(JNP_SO_TIMEOUT);
59       if( value != null )
60          soTimeout = Integer.parseInt(value);
61    }
62
63    public Socket JavaDoc createSocket(String JavaDoc host, int port) throws IOException JavaDoc, UnknownHostException JavaDoc
64    {
65       InetAddress JavaDoc hostAddr = InetAddress.getByName(host);
66       return this.createSocket(hostAddr, port, null, 0);
67    }
68    public Socket JavaDoc createSocket(InetAddress JavaDoc hostAddr, int port) throws IOException JavaDoc
69    {
70       return this.createSocket(hostAddr, port, null, 0);
71    }
72
73    public Socket JavaDoc createSocket(String JavaDoc host, int port, InetAddress JavaDoc localAddr, int localPort)
74       throws IOException JavaDoc, UnknownHostException JavaDoc
75    {
76       InetAddress JavaDoc hostAddr = InetAddress.getByName(host);
77       return this.createSocket(hostAddr, port, localAddr, localPort);
78    }
79    public Socket JavaDoc createSocket(InetAddress JavaDoc hostAddr, int port, InetAddress JavaDoc localAddr, int localPort)
80       throws IOException JavaDoc
81    {
82       Socket JavaDoc socket = null;
83       if( timeout <= 0 )
84          socket = new Socket JavaDoc(hostAddr, port, localAddr, localPort);
85       else
86          socket = createSocket(hostAddr, port, localAddr, localPort, timeout);
87
88       socket.setSoTimeout(soTimeout);
89       return socket;
90    }
91
92    protected Socket JavaDoc createSocket(InetAddress JavaDoc hostAddr, int port,
93       InetAddress JavaDoc localAddr, int localPort, int connectTimeout)
94       throws IOException JavaDoc
95    {
96       ConnectThread t = new ConnectThread();
97       Socket JavaDoc socket = t.createSocket(hostAddr, port, localAddr, localPort, connectTimeout);
98       return socket;
99    }
100
101    /** A subclass of Thread used to time the blocking connect operation
102     and notify the thread attempting the socket connect of a timeout.
103     */

104    class ConnectThread extends Thread JavaDoc
105    {
106       IOException JavaDoc ex;
107       InetAddress JavaDoc hostAddr;
108       InetAddress JavaDoc localAddr;
109       int port;
110       int localPort;
111       int connectTimeout;
112       Socket JavaDoc socket;
113
114       ConnectThread()
115       {
116          super("JNP ConnectThread");
117          super.setDaemon(true);
118       }
119
120       /** Perform the connection in a background thread and wait upto
121        connectTimeout milliseconds for it to complete before throwing
122        a ConnectionException
123        */

124       Socket JavaDoc createSocket(InetAddress JavaDoc hostAddr, int port,
125          InetAddress JavaDoc localAddr, int localPort, int connectTimeout)
126          throws IOException JavaDoc
127       {
128          this.hostAddr = hostAddr;
129          this.port = port;
130          this.localAddr = localAddr;
131          this.localPort = localPort;
132          this.connectTimeout = connectTimeout;
133
134          try
135          {
136             synchronized( this )
137             {
138                // Perform the socket connection in a background thread
139
this.start();
140                // Wait for upto connectTimeout milliseconds for the connection
141
this.wait(connectTimeout);
142             }
143          }
144          catch(InterruptedException JavaDoc e)
145          {
146             throw new ConnectException JavaDoc("Connect attempt timed out");
147          }
148
149          // See if the connect thread exited due to an exception
150
if( ex != null )
151             throw ex;
152          // If socket is null we timed out while waiting
153
if( socket == null )
154             throw new ConnectException JavaDoc("Connect attempt timed out");
155
156          return socket;
157       }
158
159       public void run()
160       {
161          try
162          {
163             socket = new Socket JavaDoc(hostAddr, port, localAddr, localPort);
164             synchronized( this )
165             {
166                this.notify();
167             }
168          }
169          catch(IOException JavaDoc e)
170          {
171             this.ex = e;
172          }
173       }
174    }
175 }
176
Popular Tags