001    /**
002     *  Licensed to the Apache Software Foundation (ASF) under one
003     *  or more contributor license agreements.  See the NOTICE file
004     *  distributed with this work for additional information
005     *  regarding copyright ownership.  The ASF licenses this file
006     *  to you under the Apache License, Version 2.0 (the
007     *  "License"); you may not use this file except in compliance
008     *  with the License.  You may obtain a copy of the License at
009     *
010     *    http://www.apache.org/licenses/LICENSE-2.0
011     *
012     *  Unless required by applicable law or agreed to in writing,
013     *  software distributed under the License is distributed on an
014     *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015     *  KIND, either express or implied.  See the License for the
016     *  specific language governing permissions and limitations
017     *  under the License.
018     */
019    
020    package org.apache.geronimo.kernel.rmi;
021    
022    import java.io.IOException;
023    import java.io.Serializable;
024    import java.net.InetSocketAddress;
025    import java.net.Socket;
026    import java.rmi.server.RMIClientSocketFactory;
027    
028    public class GeronimoRMIClientSocketFactory implements RMIClientSocketFactory, Serializable {
029    
030        private static final long serialVersionUID = 8238444722121747980L;
031        
032        private int connectionTimeout = -1;
033        private int readTimeout = -1;
034    
035        public GeronimoRMIClientSocketFactory(int connectionTimeout, int readTimeout) {
036            this.connectionTimeout = connectionTimeout;
037            this.readTimeout = readTimeout;
038        }
039        
040        public Socket createSocket(String host, int port) throws IOException {
041            Socket socket = new Socket();
042            socket.bind(null);
043            socket.connect(new InetSocketAddress(host, port), (this.connectionTimeout > 0) ? this.connectionTimeout : 0);        
044            if (this.readTimeout >= 0) {
045                socket.setSoTimeout(this.readTimeout);
046            }
047            return socket;
048        }
049    
050        @Override
051        public int hashCode() {
052            int prime = 31;
053            int result = 1;
054            result = prime * result + connectionTimeout;
055            result = prime * result + readTimeout;
056            return result;
057        }
058    
059        @Override
060        public boolean equals(Object obj) {
061            if (this == obj) {
062                return true;
063            }
064            if (obj == null) {
065                return false;
066            }
067            if (getClass() != obj.getClass()) {
068                return false;
069            }
070            GeronimoRMIClientSocketFactory other = (GeronimoRMIClientSocketFactory) obj;
071            if (connectionTimeout != other.connectionTimeout) {
072                return false;
073            }
074            if (readTimeout != other.readTimeout) {
075                return false;
076            }
077            return true;
078        }
079    
080    }