View Javadoc
1   package org.apache.commons.jcs.auxiliary.remote.server;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.IOException;
23  import java.io.Serializable;
24  import java.net.InetSocketAddress;
25  import java.net.ServerSocket;
26  import java.net.Socket;
27  import java.rmi.server.RMISocketFactory;
28  
29  /**
30   * This can be injected into the the remote cache server as follows:
31   *
32   * <pre>
33   * jcs.remotecache.customrmisocketfactory=org.apache.commons.jcs.auxiliary.remote.server.TimeoutConfigurableRMISocketFactory
34   * jcs.remotecache.customrmisocketfactory.readTimeout=5000
35   * jcs.remotecache.customrmisocketfactory.openTimeout=5000
36   * </pre>
37   */
38  public class TimeoutConfigurableRMISocketFactory
39      extends RMISocketFactory
40      implements Serializable
41  {
42      /** Don't change. */
43      private static final long serialVersionUID = 1489909775271203334L;
44  
45      /** The socket read timeout */
46      private int readTimeout = 5000;
47  
48      /** The socket open timeout */
49      private int openTimeout = 5000;
50  
51      /**
52       * @param port
53       * @return ServerSocket
54       * @throws IOException
55       */
56      @Override
57      public ServerSocket createServerSocket( int port )
58          throws IOException
59      {
60          return new ServerSocket( port );
61      }
62  
63      /**
64       * @param host
65       * @param port
66       * @return Socket
67       * @throws IOException
68       */
69      @Override
70      public Socket createSocket( String host, int port )
71          throws IOException
72      {
73          Socket socket = new Socket();
74          socket.setSoTimeout( readTimeout );
75          socket.setSoLinger( false, 0 );
76          socket.connect( new InetSocketAddress( host, port ), openTimeout );
77          return socket;
78      }
79  
80      /**
81       * @param readTimeout the readTimeout to set
82       */
83      public void setReadTimeout( int readTimeout )
84      {
85          this.readTimeout = readTimeout;
86      }
87  
88      /**
89       * @return the readTimeout
90       */
91      public int getReadTimeout()
92      {
93          return readTimeout;
94      }
95  
96      /**
97       * @param openTimeout the openTimeout to set
98       */
99      public void setOpenTimeout( int openTimeout )
100     {
101         this.openTimeout = openTimeout;
102     }
103 
104     /**
105      * @return the openTimeout
106      */
107     public int getOpenTimeout()
108     {
109         return openTimeout;
110     }
111 }