View Javadoc

1   package org.apache.jcs.auxiliary.lateral.socket.tcp.utils;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.IOException;
23  import java.net.InetSocketAddress;
24  import java.net.Socket;
25  
26  /**
27   * Since 1.4, we can specify the timeout in the connect block, we no longer need the extra thread to
28   * join against the Socket creation.
29   */
30  public class SocketOpener
31      implements Runnable
32  {
33      /** The host */
34      private String host;
35  
36      /** The port. */
37      private int port;
38  
39      /** the open timeOut */
40      private int timeOut;
41  
42      /** The socket */
43      private Socket socket;
44  
45      /**
46       * Opens a socket with a connection timeout value.
47       * <p>
48       * @param host
49       * @param port
50       * @param timeOut
51       * @return Socket
52       */
53      public static Socket openSocket( String host, int port, int timeOut )
54      {
55          // TODO get rid of the extra object
56          SocketOpener opener = new SocketOpener( host, port, timeOut );
57          opener.connect();
58          return opener.getSocket();
59      }
60  
61      /**
62       * Constructor for the SocketOpener object
63       * @param host
64       * @param port
65       * @param timeout connect timeout
66       */
67      public SocketOpener( String host, int port, int timeout )
68      {
69          this.socket = null;
70          this.host = host;
71          this.port = port;
72          this.timeOut = timeout;
73      }
74  
75      /** Main processing method for the SocketOpener object */
76      public void run()
77      {
78          connect();
79      }
80  
81      /**
82       * Creates an InetSocketAddress. Creates an unconnected Socket. Connects the Socket to the
83       * address.
84       */
85      private void connect()
86      {
87          try
88          {
89              InetSocketAddress address = new InetSocketAddress( host, port );
90              socket = new Socket();
91              socket.connect( address, timeOut );
92          }
93          catch ( IOException ioe )
94          {
95              // swallow
96          }
97      }
98  
99      /**
100      * @return The opened socket
101      */
102     public Socket getSocket()
103     {
104         return socket;
105     }
106 }