01: /* tjws - SimpleAcceptor.java
02: * Copyright (C) 1999-2007 Dmitriy Rogatkin. All rights reserved.
03: * Redistribution and use in source and binary forms, with or without
04: * modification, are permitted provided that the following conditions
05: * are met:
06: * 1. Redistributions of source code must retain the above copyright
07: * notice, this list of conditions and the following disclaimer.
08: * 2. Redistributions in binary form must reproduce the above copyright
09: * notice, this list of conditions and the following disclaimer in the
10: * documentation and/or other materials provided with the distribution.
11: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
12: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
13: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
14: * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
15: * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
16: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
17: * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
18: * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
19: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
20: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
21: * SUCH DAMAGE.
22: *
23: * Visit http://tjws.sourceforge.net to get the latest infromation
24: * about Rogatkin's products.
25: * $Id: SimpleAcceptor.java,v 1.5 2007/07/30 16:05:15 rogatkin Exp $
26: * Created on Jun 12, 2007
27: * @author dmitriy
28: */
29: package Acme.Serve;
30:
31: import java.net.ServerSocket;
32: import java.net.Socket;
33: import java.net.InetAddress;
34: import java.io.IOException;
35: import java.net.InetAddress;
36: import java.net.InetSocketAddress;
37: import java.util.Map;
38: import Acme.Serve.Serve;
39:
40: public class SimpleAcceptor implements Serve.Acceptor {
41: public Socket accept() throws IOException {
42: return socket.accept();
43: }
44:
45: public void destroy() throws IOException {
46: socket.close();
47: }
48:
49: public void init(Map inProperties, Map outProperties)
50: throws IOException {
51: int port = inProperties.get(Serve.ARG_PORT) != null ? ((Integer) inProperties
52: .get(Serve.ARG_PORT)).intValue()
53: : Serve.DEF_PORT;
54: String bindAddrStr = (String) inProperties
55: .get(Serve.ARG_BINDADDRESS);
56: InetSocketAddress bindAddr = bindAddrStr != null ? new InetSocketAddress(
57: InetAddress.getByName(bindAddrStr), port)
58: : null;
59: String backlogStr = (String) inProperties
60: .get(Serve.ARG_BACKLOG);
61: int backlog = backlogStr != null ? Integer.parseInt(backlogStr)
62: : -1;
63: if (bindAddr != null) {
64: socket = new ServerSocket();
65: if (backlog < 0)
66: socket.bind(bindAddr);
67: else
68: socket.bind(bindAddr, backlog);
69: } else {
70: if (backlog < 0)
71: socket = new ServerSocket(port);
72: else
73: socket = new ServerSocket(port, backlog);
74: }
75: if (outProperties != null)
76: if (socket.isBound())
77: outProperties.put(Serve.ARG_BINDADDRESS, socket
78: .getInetAddress().getHostName());
79: else
80: outProperties.put(Serve.ARG_BINDADDRESS, InetAddress
81: .getLocalHost().getHostName());
82: }
83:
84: public String toString() {
85: return "SimpleAcceptor " + socket;
86: }
87:
88: private ServerSocket socket;
89: }
|