1 22 package org.jboss.net.sockets; 23 24 import java.io.IOException ; 25 import java.io.Serializable ; 26 import java.net.InetAddress ; 27 import java.net.ServerSocket ; 28 import java.net.UnknownHostException ; 29 import java.rmi.server.RMIServerSocketFactory ; 30 import javax.net.ServerSocketFactory; 31 32 38 public class DefaultSocketFactory extends ServerSocketFactory 39 implements RMIServerSocketFactory , Serializable 40 { 41 static final long serialVersionUID = -7626239955727142958L; 42 private transient InetAddress bindAddress; 43 private int backlog = 200; 44 45 48 public DefaultSocketFactory() 49 { 50 this(null, 200); 51 } 52 54 public DefaultSocketFactory(InetAddress bindAddress) 55 { 56 this(bindAddress, 200); 57 } 58 60 public DefaultSocketFactory(int backlog) 61 { 62 this(null, backlog); 63 } 64 66 public DefaultSocketFactory(InetAddress bindAddress, int backlog) 67 { 68 this.bindAddress = bindAddress; 69 this.backlog = backlog; 70 } 71 72 public String getBindAddress() 73 { 74 String address = null; 75 if( bindAddress != null ) 76 address = bindAddress.getHostAddress(); 77 return address; 78 } 79 public void setBindAddress(String host) throws UnknownHostException 80 { 81 bindAddress = InetAddress.getByName(host); 82 } 83 84 93 public ServerSocket createServerSocket(int port) throws IOException 94 { 95 return createServerSocket(port, backlog, bindAddress); 96 } 97 98 104 public ServerSocket createServerSocket(int port, int backlog) 105 throws IOException 106 { 107 return createServerSocket(port, backlog, null); 108 } 109 110 117 public ServerSocket createServerSocket(int port, int backlog, 118 InetAddress inetAddress) throws IOException 119 { 120 ServerSocket activeSocket = new ServerSocket (port, backlog, bindAddress); 121 return activeSocket; 122 } 123 124 public boolean equals(Object obj) 125 { 126 boolean equals = obj instanceof DefaultSocketFactory; 127 if( equals && bindAddress != null ) 128 { 129 DefaultSocketFactory dsf = (DefaultSocketFactory) obj; 130 InetAddress 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 toString() 146 { 147 StringBuffer tmp = new StringBuffer (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 |