1 /** 2 * Copyright 2003-2006 Greg Luck 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package net.sf.ehcache.distribution; 18 19 import java.io.IOException; 20 import java.io.Serializable; 21 import java.net.Socket; 22 import java.rmi.server.RMIClientSocketFactory; 23 import java.rmi.server.RMISocketFactory; 24 25 26 /** 27 * Default socket timeouts are unlikely to be suitable for cache replication. Sockets should 28 * fail fast. 29 * <p/> 30 * This class decorates the RMIClientSocketFactory so as to enable customisations to be placed 31 * on newly created sockets. 32 * 33 * @author <a href="mailto:gluck@thoughtworks.com">Greg Luck</a> 34 * @version $Id: ConfigurableRMIClientSocketFactory.java 52 2006-04-24 14:50:03Z gregluck $ 35 * @see "http://java.sun.com/j2se/1.5.0/docs/guide/rmi/socketfactory/#1" 36 * @noinspection SerializableHasSerializationMethods,SerializableHasSerializationMethods 37 */ 38 public final class ConfigurableRMIClientSocketFactory implements Serializable, RMIClientSocketFactory { 39 40 private static final int ONE_SECOND = 1000; 41 42 private static final long serialVersionUID = 4920508630517373246L; 43 44 private final int socketTimeoutMillis; 45 46 /** 47 * Construct a new socket factory with the given timeout. 48 * 49 * @param socketTimeoutMillis 50 * @see Socket#setSoTimeout 51 */ 52 public ConfigurableRMIClientSocketFactory(Integer socketTimeoutMillis) { 53 if (socketTimeoutMillis == null) { 54 this.socketTimeoutMillis = ONE_SECOND; 55 } else { 56 this.socketTimeoutMillis = socketTimeoutMillis.intValue(); 57 } 58 } 59 60 /** 61 * Create a client socket connected to the specified host and port. 62 * <p/> 63 * If necessary this implementation can be changed to specify the outbound address to use 64 * e.g. <code>Socket socket = new Socket(host, port, localInterface , 0);</code> 65 * 66 * @param host the host name 67 * @param port the port number 68 * @return a socket connected to the specified host and port. 69 * @throws java.io.IOException if an I/O error occurs during socket creation 70 * @since 1.2 71 */ 72 public Socket createSocket(String host, int port) throws IOException { 73 Socket socket = RMISocketFactory.getDefaultSocketFactory().createSocket(host, port); 74 75 socket.setSoTimeout(socketTimeoutMillis); 76 77 return socket; 78 } 79 80 /** 81 * Implements the Object hashCode method. 82 * 83 * @return a hash based on socket options 84 */ 85 public int hashCode() { 86 return socketTimeoutMillis; 87 } 88 89 /** 90 * The standard hashCode method which is necessary for SocketFactory classes. 91 * Omitting this method causes RMI to quickly error out 92 * with "too many open files" errors. 93 * 94 * @param object the comparison object 95 * @return equal if the classes are the same and the socket options are the name. 96 */ 97 public boolean equals(Object object) { 98 return (getClass() == object.getClass() && 99 socketTimeoutMillis == ((ConfigurableRMIClientSocketFactory) object).socketTimeoutMillis); 100 } 101 102 } 103 104