KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * JBoss, Home of Professional Open Source
3  * Copyright 2006, Red Hat Middleware LLC, and individual contributors
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.rmi.server.RMIClientSocketFactory JavaDoc;
27 import java.net.InetAddress JavaDoc;
28 import java.net.Socket JavaDoc;
29 import java.net.UnknownHostException JavaDoc;
30
31 /**
32  * A RMIClientSocketFactory that adds a bind address override of the server
33  * host to control what the address the client uses.
34  *
35  * @author Scott.Stark@jboss.org
36  * @version $Revision: 30203 $
37  */

38 public class DefaultClientSocketFactory
39    implements RMIClientSocketFactory JavaDoc, Serializable JavaDoc
40 {
41    private static final long serialVersionUID = -920483051658660269L;
42    /** An override of the server address */
43    private InetAddress JavaDoc bindAddress;
44
45    public DefaultClientSocketFactory()
46    {
47    }
48
49    public String JavaDoc getBindAddress()
50    {
51       String JavaDoc address = null;
52       if( bindAddress != null )
53          address = bindAddress.getHostAddress();
54       return address;
55    }
56    public void setBindAddress(String JavaDoc host) throws UnknownHostException JavaDoc
57    {
58       bindAddress = InetAddress.getByName(host);
59    }
60
61    /**
62     * Create a server socket on the specified port (port 0 indicates
63     * an anonymous port).
64     * @param port the port number
65     * @return the server socket on the specified port
66     * @exception java.io.IOException if an I/O error occurs during server socket
67     * creation
68     * @since 1.2
69     */

70    public Socket JavaDoc createSocket(String JavaDoc host, int port) throws IOException JavaDoc
71    {
72       InetAddress JavaDoc addr = null;
73       if( bindAddress != null )
74          addr = bindAddress;
75       else
76          addr = InetAddress.getByName(host);
77       Socket JavaDoc s = new Socket JavaDoc(addr, port);
78       return s;
79    }
80
81    public boolean equals(Object JavaDoc obj)
82    {
83       boolean equals = obj instanceof DefaultClientSocketFactory;
84       if( equals && bindAddress != null )
85       {
86          DefaultClientSocketFactory dcsf = (DefaultClientSocketFactory) obj;
87          InetAddress JavaDoc dcsfa = dcsf.bindAddress;
88          if( dcsfa != null )
89             equals = bindAddress.equals(dcsfa);
90          else
91             equals = false;
92       }
93       return equals;
94    }
95    public int hashCode()
96    {
97       int hashCode = getClass().getName().hashCode();
98       if( bindAddress != null )
99          hashCode += bindAddress.toString().hashCode();
100       return hashCode;
101    }
102    public String JavaDoc toString()
103    {
104       StringBuffer JavaDoc tmp = new StringBuffer JavaDoc(super.toString());
105       tmp.append('[');
106       tmp.append("bindAddress=");
107       tmp.append(bindAddress);
108       tmp.append(']');
109       return tmp.toString();
110    }
111 }
112
Popular Tags