| %line | %branch | |||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| org.apache.jcs.auxiliary.lateral.socket.tcp.utils.SocketOpener |
|
|
| 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.Socket; |
|
| 24 | ||
| 25 | /** |
|
| 26 | * Socket openere that will timeout on the initial connect rather than block |
|
| 27 | * forever. Technique from core java II. |
|
| 28 | * |
|
| 29 | * @version $Id: SocketOpener.java 536904 2007-05-10 16:03:42Z tv $ |
|
| 30 | */ |
|
| 31 | public class SocketOpener |
|
| 32 | implements Runnable |
|
| 33 | { |
|
| 34 | ||
| 35 | private String host; |
|
| 36 | ||
| 37 | private int port; |
|
| 38 | ||
| 39 | private Socket socket; |
|
| 40 | ||
| 41 | /** |
|
| 42 | * Opens a socket with a connection timeout value. Joins against a backgroud |
|
| 43 | * thread that does the openeing. |
|
| 44 | * |
|
| 45 | * @param host |
|
| 46 | * @param port |
|
| 47 | * @param timeOut |
|
| 48 | * @return Socket |
|
| 49 | */ |
|
| 50 | public static Socket openSocket( String host, int port, class="keyword">int timeOut ) |
|
| 51 | { |
|
| 52 | 34 | SocketOpener opener = new SocketOpener( host, port ); |
| 53 | 34 | Thread t = new Thread( opener ); |
| 54 | 34 | t.start(); |
| 55 | try |
|
| 56 | { |
|
| 57 | 34 | t.join( timeOut ); |
| 58 | } |
|
| 59 | 0 | catch ( InterruptedException ire ) |
| 60 | { |
|
| 61 | // swallow |
|
| 62 | 34 | } |
| 63 | 34 | return opener.getSocket(); |
| 64 | } |
|
| 65 | ||
| 66 | /** |
|
| 67 | * Constructor for the SocketOpener object |
|
| 68 | * |
|
| 69 | * @param host |
|
| 70 | * @param port |
|
| 71 | */ |
|
| 72 | public SocketOpener( String host, int port ) |
|
| 73 | 34 | { |
| 74 | 34 | this.socket = null; |
| 75 | 34 | this.host = host; |
| 76 | 34 | this.port = port; |
| 77 | 34 | } |
| 78 | ||
| 79 | /** Main processing method for the SocketOpener object */ |
|
| 80 | public void run() |
|
| 81 | { |
|
| 82 | try |
|
| 83 | { |
|
| 84 | 34 | socket = new Socket( host, port ); |
| 85 | } |
|
| 86 | 14 | catch ( IOException ioe ) |
| 87 | { |
|
| 88 | // swallow |
|
| 89 | 20 | } |
| 90 | 34 | } |
| 91 | ||
| 92 | /** |
|
| 93 | * |
|
| 94 | * @return The opened socket |
|
| 95 | */ |
|
| 96 | public Socket getSocket() |
|
| 97 | { |
|
| 98 | 34 | return socket; |
| 99 | } |
|
| 100 | } |
| This report is generated by jcoverage, Maven and Maven JCoverage Plugin. |