1 | /*
|
---|
2 | * Copyright 2001-2004 The Apache Software Foundation
|
---|
3 | *
|
---|
4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
---|
5 | * you may not use this file except in compliance with the License.
|
---|
6 | * You may obtain a copy of the License at
|
---|
7 | *
|
---|
8 | * http://www.apache.org/licenses/LICENSE-2.0
|
---|
9 | *
|
---|
10 | * Unless required by applicable law or agreed to in writing, software
|
---|
11 | * distributed under the License is distributed on an "AS IS" BASIS,
|
---|
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
13 | * See the License for the specific language governing permissions and
|
---|
14 | * limitations under the License.
|
---|
15 | *
|
---|
16 | */
|
---|
17 |
|
---|
18 | package org.apache.tools.ant.taskdefs.condition;
|
---|
19 |
|
---|
20 | import java.io.IOException;
|
---|
21 | import org.apache.tools.ant.BuildException;
|
---|
22 | import org.apache.tools.ant.Project;
|
---|
23 | import org.apache.tools.ant.ProjectComponent;
|
---|
24 |
|
---|
25 | /**
|
---|
26 | * Condition to wait for a TCP/IP socket to have a listener. Its attributes are:
|
---|
27 | * server - the name of the server.
|
---|
28 | * port - the port number of the socket.
|
---|
29 | *
|
---|
30 | * @since Ant 1.5
|
---|
31 | */
|
---|
32 | public class Socket extends ProjectComponent implements Condition {
|
---|
33 | private String server = null;
|
---|
34 | private int port = 0;
|
---|
35 |
|
---|
36 | /**
|
---|
37 | * Set the server attribute
|
---|
38 | *
|
---|
39 | * @param server the server name
|
---|
40 | */
|
---|
41 | public void setServer(String server) {
|
---|
42 | this.server = server;
|
---|
43 | }
|
---|
44 |
|
---|
45 | /**
|
---|
46 | * Set the port attribute
|
---|
47 | *
|
---|
48 | * @param port the port number of the socket
|
---|
49 | */
|
---|
50 | public void setPort(int port) {
|
---|
51 | this.port = port;
|
---|
52 | }
|
---|
53 |
|
---|
54 | /**
|
---|
55 | * @return true if a socket can be created
|
---|
56 | * @exception BuildException if the attributes are not set
|
---|
57 | */
|
---|
58 | public boolean eval() throws BuildException {
|
---|
59 | if (server == null) {
|
---|
60 | throw new BuildException("No server specified in socket "
|
---|
61 | + "condition");
|
---|
62 | }
|
---|
63 | if (port == 0) {
|
---|
64 | throw new BuildException("No port specified in socket condition");
|
---|
65 | }
|
---|
66 | log("Checking for listener at " + server + ":" + port,
|
---|
67 | Project.MSG_VERBOSE);
|
---|
68 | java.net.Socket s = null;
|
---|
69 | try {
|
---|
70 | s = new java.net.Socket(server, port);
|
---|
71 | } catch (IOException e) {
|
---|
72 | return false;
|
---|
73 | } finally {
|
---|
74 | if (s != null) {
|
---|
75 | try {
|
---|
76 | s.close();
|
---|
77 | } catch (IOException ioe) {
|
---|
78 | // Intentionally left blank
|
---|
79 | }
|
---|
80 | }
|
---|
81 | }
|
---|
82 | return true;
|
---|
83 | }
|
---|
84 |
|
---|
85 | }
|
---|