001package org.apache.commons.jcs.auxiliary.remote.server;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *   http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import java.io.IOException;
023import java.io.Serializable;
024import java.net.InetSocketAddress;
025import java.net.ServerSocket;
026import java.net.Socket;
027import java.rmi.server.RMISocketFactory;
028
029/**
030 * This can be injected into the the remote cache server as follows:
031 *
032 * <pre>
033 * jcs.remotecache.customrmisocketfactory=org.apache.commons.jcs.auxiliary.remote.server.TimeoutConfigurableRMISocketFactory
034 * jcs.remotecache.customrmisocketfactory.readTimeout=5000
035 * jcs.remotecache.customrmisocketfactory.openTimeout=5000
036 * </pre>
037 */
038public class TimeoutConfigurableRMISocketFactory
039    extends RMISocketFactory
040    implements Serializable
041{
042    /** Don't change. */
043    private static final long serialVersionUID = 1489909775271203334L;
044
045    /** The socket read timeout */
046    private int readTimeout = 5000;
047
048    /** The socket open timeout */
049    private int openTimeout = 5000;
050
051    /**
052     * @param port
053     * @return ServerSocket
054     * @throws IOException
055     */
056    @Override
057    public ServerSocket createServerSocket( int port )
058        throws IOException
059    {
060        return new ServerSocket( port );
061    }
062
063    /**
064     * @param host
065     * @param port
066     * @return Socket
067     * @throws IOException
068     */
069    @Override
070    public Socket createSocket( String host, int port )
071        throws IOException
072    {
073        Socket socket = new Socket();
074        socket.setSoTimeout( readTimeout );
075        socket.setSoLinger( false, 0 );
076        socket.connect( new InetSocketAddress( host, port ), openTimeout );
077        return socket;
078    }
079
080    /**
081     * @param readTimeout the readTimeout to set
082     */
083    public void setReadTimeout( int readTimeout )
084    {
085        this.readTimeout = readTimeout;
086    }
087
088    /**
089     * @return the readTimeout
090     */
091    public int getReadTimeout()
092    {
093        return readTimeout;
094    }
095
096    /**
097     * @param openTimeout the openTimeout to set
098     */
099    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}