KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > net > sockets > DefaultSocketFactory


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.net.sockets;
23
24 import java.io.IOException JavaDoc;
25 import java.io.Serializable JavaDoc;
26 import java.net.InetAddress JavaDoc;
27 import java.net.ServerSocket JavaDoc;
28 import java.net.UnknownHostException JavaDoc;
29 import java.rmi.server.RMIServerSocketFactory JavaDoc;
30 import javax.net.ServerSocketFactory;
31
32 /** An implementation of RMIServerSocketFactory that supports backlog and
33  * bind address settings
34  *
35  * @author Scott.Stark@jboss.org
36  * @version $Revision: 2074 $
37  */

38 public class DefaultSocketFactory extends ServerSocketFactory JavaDoc
39    implements RMIServerSocketFactory JavaDoc, Serializable JavaDoc
40 {
41    static final long serialVersionUID = -7626239955727142958L;
42    private transient InetAddress JavaDoc bindAddress;
43    private int backlog = 200;
44
45    /** Create a socket factory that binds on any address with a default
46     * backlog of 200
47     */

48    public DefaultSocketFactory()
49    {
50       this(null, 200);
51    }
52    /** Create a socket factory with the given bind address
53     */

54    public DefaultSocketFactory(InetAddress JavaDoc bindAddress)
55    {
56       this(bindAddress, 200);
57    }
58    /** Create a socket factory with the given backlog
59     */

60    public DefaultSocketFactory(int backlog)
61    {
62       this(null, backlog);
63    }
64    /** Create a socket factory with the given bind address and backlog
65     */

66    public DefaultSocketFactory(InetAddress JavaDoc bindAddress, int backlog)
67    {
68       this.bindAddress = bindAddress;
69       this.backlog = backlog;
70    }
71
72    public String JavaDoc getBindAddress()
73    {
74       String JavaDoc address = null;
75       if( bindAddress != null )
76          address = bindAddress.getHostAddress();
77       return address;
78    }
79    public void setBindAddress(String JavaDoc host) throws UnknownHostException JavaDoc
80    {
81       bindAddress = InetAddress.getByName(host);
82    }
83
84     /**
85      * Create a server socket on the specified port (port 0 indicates
86      * an anonymous port).
87      * @param port the port number
88      * @return the server socket on the specified port
89      * @exception IOException if an I/O error occurs during server socket
90      * creation
91      * @since 1.2
92      */

93     public ServerSocket JavaDoc createServerSocket(int port) throws IOException JavaDoc
94     {
95       return createServerSocket(port, backlog, bindAddress);
96    }
97
98    /**
99     * @param port - the port to listen to
100     * @param backlog - how many connections are queued
101     * @return A ServerSocket
102     * @throws IOException
103     */

104    public ServerSocket JavaDoc createServerSocket(int port, int backlog)
105       throws IOException JavaDoc
106    {
107       return createServerSocket(port, backlog, null);
108    }
109
110    /**
111     * @param port - the port to listen to
112     * @param backlog - how many connections are queued
113     * @param inetAddress - the network interface address to use
114     * @return
115     * @throws IOException
116     */

117    public ServerSocket JavaDoc createServerSocket(int port, int backlog,
118       InetAddress JavaDoc inetAddress) throws IOException JavaDoc
119    {
120         ServerSocket JavaDoc activeSocket = new ServerSocket JavaDoc(port, backlog, bindAddress);
121         return activeSocket;
122     }
123
124    public boolean equals(Object JavaDoc obj)
125    {
126       boolean equals = obj instanceof DefaultSocketFactory;
127       if( equals && bindAddress != null )
128       {
129          DefaultSocketFactory dsf = (DefaultSocketFactory) obj;
130          InetAddress JavaDoc dsfa = dsf.bindAddress;
131          if( dsfa != null )
132             equals = bindAddress.equals(dsfa);
133          else
134             equals = false;
135       }
136       return equals;
137    }
138    public int hashCode()
139    {
140       int hashCode = getClass().getName().hashCode();
141       if( bindAddress != null )
142          hashCode += bindAddress.toString().hashCode();
143       return hashCode;
144    }
145    public String JavaDoc toString()
146    {
147       StringBuffer JavaDoc tmp = new StringBuffer JavaDoc(super.toString());
148       tmp.append('[');
149       tmp.append("bindAddress=");
150       tmp.append(bindAddress);
151       tmp.append(']');
152       return tmp.toString();
153    }
154 }
155
Popular Tags