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
18package tests.support;
19
20import java.io.IOException;
21import java.net.ServerSocket;
22import java.net.Socket;
23
24/**
25 * This class implements the Support_ServerSocket interface using java.net
26 * Serversockets
27 */
28
29public class Support_HttpServerSocket implements Support_ServerSocket {
30    private ServerSocket instance = null;
31
32    private int port = -1;
33
34    private int timeout = 8000;
35
36    /**
37     * Blocks until a connection is made, or the socket times out.
38     *
39     * @see tests.support.Support_ServerSocket#accept()
40     */
41    public Support_Socket accept() throws IOException {
42        if (port == -1) {
43            return null;
44        }
45        if (instance == null) {
46            return null;
47        }
48        instance.setSoTimeout(timeout);
49        Socket s = instance.accept();
50        return new Support_HttpSocket(s);
51    }
52
53    /**
54     * @see tests.support.Support_ServerSocket#setTimeout(int) Sets the
55     *      timeout for the server.
56     */
57    public void setTimeout(int timeout) {
58        this.timeout = timeout;
59    }
60
61    /**
62     * @see tests.support.Support_ServerSocket#setPort(int)
63     */
64    public void setPort(int port) {
65        this.port = port;
66    }
67
68    public void open() throws IOException {
69        instance = new ServerSocket(port);
70    }
71
72    /**
73     * @see tests.support.Support_ServerSocket#close()
74     */
75    public void close() throws IOException {
76        if (instance != null) {
77            instance.close();
78        }
79    }
80
81}
82