001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.invocation.pooled.interfaces;
023:
024: import java.io.Serializable;
025: import java.io.IOException;
026: import javax.net.SocketFactory;
027:
028: /**
029: * This class encapsulates all the required information for a client to
030: * establish a connection with the server.
031: *
032: * It also attempts to provide a fast hash() function since this object
033: * is used as a key in a hashmap mainted by the ConnectionManager.
034: *
035: * @author Bill Burke
036: * @author Scott.Stark@jboss.org
037: * @version $Revision: 57209 $
038: */
039: public class ServerAddress implements Serializable {
040: /** The serialVersionUID @since 1.1.4.1 */
041: private static final long serialVersionUID = -7206359745950445445L;
042:
043: /**
044: * Address of host to connect to
045: * @serial
046: */
047: public String address;
048:
049: /**
050: * Port the service is listening on
051: * @serial
052: */
053: public int port;
054:
055: /**
056: * If the TcpNoDelay option should be used on the socket.
057: * @serial
058: */
059: public boolean enableTcpNoDelay = false;
060:
061: /**
062: * Timeout of setSoTimeout
063: * @serial
064: */
065: public int timeout = 60000;
066: /** An option socket factory for connecting to the server
067: * @serial
068: */
069: public SocketFactory clientSocketFactory;
070:
071: /**
072: * This object is used as a key in a hashmap,
073: * so we precompute the hascode for faster lookups.
074: */
075: private transient int hashCode;
076:
077: /**
078: * The server address/port representation.
079: *
080: * @param address - hostname/ip of the server
081: * @param port - the invoker port
082: * @param enableTcpNoDelay - the Socket.setTcpNoDelay flag
083: * @param timeout - the Socket.setSoTimeout value
084: * @param clientSocketFactory - optional SocketFactory
085: */
086: public ServerAddress(String address, int port,
087: boolean enableTcpNoDelay, int timeout,
088: SocketFactory clientSocketFactory) {
089: this .address = address;
090: this .port = port;
091: this .enableTcpNoDelay = enableTcpNoDelay;
092: this .hashCode = address.hashCode() + port;
093: if (enableTcpNoDelay)
094: this .hashCode++;
095: this .timeout = timeout;
096: this .clientSocketFactory = clientSocketFactory;
097: }
098:
099: public String toString() {
100: return "[address:" + address + ",port:" + port
101: + ",enableTcpNoDelay:" + enableTcpNoDelay + "]";
102: }
103:
104: public boolean equals(Object obj) {
105: try {
106: // Compare this to obj
107: ServerAddress o = (ServerAddress) obj;
108: if (port != o.port)
109: return false;
110: if (address.equals(o.address) == false)
111: return false;
112: if (enableTcpNoDelay != o.enableTcpNoDelay)
113: return false;
114: return true;
115: } catch (Throwable e) {
116: return false;
117: }
118: }
119:
120: public int hashCode() {
121: return hashCode;
122: }
123:
124: /**
125: * Create the transient hashCode
126: * @param in
127: * @throws IOException
128: * @throws ClassNotFoundException
129: */
130: private void readObject(java.io.ObjectInputStream in)
131: throws IOException, ClassNotFoundException {
132: // Trigger default serialization
133: in.defaultReadObject();
134: // Build the hashCode
135: this.hashCode = address.hashCode() + port;
136: if (enableTcpNoDelay)
137: this.hashCode++;
138: }
139: }
|