KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > taskdefs > condition > Socket


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */

18
19 package org.apache.tools.ant.taskdefs.condition;
20
21 import java.io.IOException JavaDoc;
22 import org.apache.tools.ant.BuildException;
23 import org.apache.tools.ant.Project;
24 import org.apache.tools.ant.ProjectComponent;
25
26 /**
27  * Condition to wait for a TCP/IP socket to have a listener. Its attributes are:
28  * server - the name of the server.
29  * port - the port number of the socket.
30  *
31  * @since Ant 1.5
32  */

33 public class Socket extends ProjectComponent implements Condition {
34     private String JavaDoc server = null;
35     private int port = 0;
36
37     /**
38      * Set the server attribute
39      *
40      * @param server the server name
41      */

42     public void setServer(String JavaDoc server) {
43         this.server = server;
44     }
45
46     /**
47      * Set the port attribute
48      *
49      * @param port the port number of the socket
50      */

51     public void setPort(int port) {
52         this.port = port;
53     }
54
55     /**
56      * @return true if a socket can be created
57      * @exception BuildException if the attributes are not set
58      */

59     public boolean eval() throws BuildException {
60         if (server == null) {
61             throw new BuildException("No server specified in socket "
62                                      + "condition");
63         }
64         if (port == 0) {
65             throw new BuildException("No port specified in socket condition");
66         }
67         log("Checking for listener at " + server + ":" + port,
68             Project.MSG_VERBOSE);
69         java.net.Socket JavaDoc s = null;
70         try {
71             s = new java.net.Socket JavaDoc(server, port);
72         } catch (IOException JavaDoc e) {
73             return false;
74         } finally {
75           if (s != null) {
76             try {
77               s.close();
78             } catch (IOException JavaDoc ioe) {
79               // Intentionally left blank
80
}
81           }
82         }
83         return true;
84     }
85
86 }
87
Popular Tags