01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * 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;
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 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 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(
66: "No port specified in socket condition");
67: }
68: log("Checking for listener at " + server + ":" + port,
69: Project.MSG_VERBOSE);
70: java.net.Socket s = null;
71: try {
72: s = new java.net.Socket(server, port);
73: } catch (IOException e) {
74: return false;
75: } finally {
76: if (s != null) {
77: try {
78: s.close();
79: } catch (IOException ioe) {
80: // Intentionally left blank
81: }
82: }
83: }
84: return true;
85: }
86:
87: }
|