1 /***
2 * Copyright 2003-2010 Terracotta, Inc.
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 * Default socket timeouts are unlikely to be suitable for cache replication. Sockets should
27 * fail fast.
28 * <p/>
29 * This class decorates the RMIClientSocketFactory so as to enable customisations to be placed
30 * on newly created sockets.
31 *
32 * @author <a href="mailto:gluck@thoughtworks.com">Greg Luck</a>
33 * @version $Id: ConfigurableRMIClientSocketFactory.html 13146 2011-08-01 17:12:39Z oletizi $
34 * @see "http://java.sun.com/j2se/1.5.0/docs/guide/rmi/socketfactory/#1"
35 */
36 public final class ConfigurableRMIClientSocketFactory implements Serializable, RMIClientSocketFactory {
37
38 private static final int ONE_SECOND = 1000;
39
40 private static final long serialVersionUID = 4920508630517373246L;
41
42 private final int socketTimeoutMillis;
43
44 /***
45 * Construct a new socket factory with the given timeout.
46 *
47 * @param socketTimeoutMillis
48 * @see Socket#setSoTimeout
49 */
50 public ConfigurableRMIClientSocketFactory(Integer socketTimeoutMillis) {
51 if (socketTimeoutMillis == null) {
52 this.socketTimeoutMillis = ONE_SECOND;
53 } else {
54 this.socketTimeoutMillis = socketTimeoutMillis.intValue();
55 }
56 }
57
58 /***
59 * Create a client socket connected to the specified host and port.
60 * <p/>
61 * If necessary this implementation can be changed to specify the outbound address to use
62 * e.g. <code>Socket socket = new Socket(host, port, localInterface , 0);</code>
63 *
64 * @param host the host name
65 * @param port the port number
66 * @return a socket connected to the specified host and port.
67 * @throws java.io.IOException if an I/O error occurs during socket creation
68 * @since 1.2
69 */
70 public Socket createSocket(String host, int port) throws IOException {
71 Socket socket = getConfiguredRMISocketFactory().createSocket(host, port);
72 socket.setSoTimeout(socketTimeoutMillis);
73 return socket;
74 }
75
76 /***
77 * Implements the Object hashCode method.
78 *
79 * @return a hash based on socket options
80 */
81 @Override
82 public int hashCode() {
83 return socketTimeoutMillis;
84 }
85
86 /***
87 * The standard hashCode method which is necessary for SocketFactory classes.
88 * Omitting this method causes RMI to quickly error out
89 * with "too many open files" errors.
90 *
91 * @param object the comparison object
92 * @return equal if the classes are the same and the socket options are the name.
93 */
94 @Override
95 public boolean equals(Object object) {
96 if (object == null) {
97 return false;
98 } else {
99 return (getClass() == object.getClass() &&
100 socketTimeoutMillis == ((ConfigurableRMIClientSocketFactory) object).socketTimeoutMillis);
101 }
102 }
103
104 /***
105 * Return the JVM-level configured {@code RMISocketFactory}.
106 * <p>
107 * If a global socket factory has been set via the
108 * {@link RMISocketFactory#setSocketFactory(RMISocketFactory)} method then
109 * that factory will be returned. Otherwise the default socket factory as
110 * returned by {@link RMISocketFactory#getDefaultSocketFactory()} is used
111 * instead.
112 *
113 * @return the configured @{code {@link RMISocketFactory}
114 */
115 public static RMISocketFactory getConfiguredRMISocketFactory() {
116 RMISocketFactory globalSocketFactory = RMISocketFactory.getSocketFactory();
117 if (globalSocketFactory == null) {
118 return RMISocketFactory.getDefaultSocketFactory();
119 } else {
120 return globalSocketFactory;
121 }
122 }
123 }