KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > invocation > pooled > interfaces > ServerAddress


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.invocation.pooled.interfaces;
23
24 import java.io.Serializable JavaDoc;
25 import java.io.IOException JavaDoc;
26 import javax.net.SocketFactory;
27
28 /**
29  * This class encapsulates all the required information for a client to
30  * establish a connection with the server.
31  *
32  * It also attempts to provide a fast hash() function since this object
33  * is used as a key in a hashmap mainted by the ConnectionManager.
34  *
35  * @author Bill Burke
36  * @author Scott.Stark@jboss.org
37  * @version $Revision: 44892 $
38  */

39 public class ServerAddress implements Serializable JavaDoc
40 {
41    /** The serialVersionUID @since 1.1.4.1 */
42    private static final long serialVersionUID = -7206359745950445445L;
43
44    /**
45     * Address of host to connect to
46     * @serial
47     */

48    public String JavaDoc address;
49
50    /**
51     * Port the service is listening on
52     * @serial
53     */

54    public int port;
55
56    /**
57     * If the TcpNoDelay option should be used on the socket.
58     * @serial
59     */

60    public boolean enableTcpNoDelay = false;
61
62    /**
63     * Timeout of setSoTimeout
64     * @serial
65     */

66    public int timeout = 60000;
67    /** An option socket factory for connecting to the server
68     * @serial
69     */

70    public SocketFactory clientSocketFactory;
71
72    /**
73     * This object is used as a key in a hashmap,
74     * so we precompute the hascode for faster lookups.
75     */

76    private transient int hashCode;
77
78    /**
79     * The server address/port representation.
80     *
81     * @param address - hostname/ip of the server
82     * @param port - the invoker port
83     * @param enableTcpNoDelay - the Socket.setTcpNoDelay flag
84     * @param timeout - the Socket.setSoTimeout value
85     * @param clientSocketFactory - optional SocketFactory
86     */

87    public ServerAddress(String JavaDoc address, int port, boolean enableTcpNoDelay,
88       int timeout, SocketFactory clientSocketFactory)
89    {
90       this.address = address;
91       this.port = port;
92       this.enableTcpNoDelay = enableTcpNoDelay;
93       this.hashCode = address.hashCode() + port;
94       if( enableTcpNoDelay )
95          this.hashCode ++;
96       this.timeout = timeout;
97       this.clientSocketFactory = clientSocketFactory;
98    }
99
100    public String JavaDoc toString()
101    {
102       return "[address:" + address + ",port:" + port + ",enableTcpNoDelay:" + enableTcpNoDelay + "]";
103    }
104
105    public boolean equals(Object JavaDoc obj)
106    {
107       try
108       {
109          // Compare this to obj
110
ServerAddress o = (ServerAddress) obj;
111          if (port != o.port)
112             return false;
113          if (address.equals(o.address) == false)
114             return false;
115          if (enableTcpNoDelay != o.enableTcpNoDelay)
116             return false;
117          return true;
118       }
119       catch (Throwable JavaDoc e)
120       {
121          return false;
122       }
123    }
124
125    public int hashCode()
126    {
127       return hashCode;
128    }
129
130    /**
131     * Create the transient hashCode
132     * @param in
133     * @throws IOException
134     * @throws ClassNotFoundException
135     */

136    private void readObject(java.io.ObjectInputStream JavaDoc in)
137      throws IOException JavaDoc, ClassNotFoundException JavaDoc
138    {
139       // Trigger default serialization
140
in.defaultReadObject();
141       // Build the hashCode
142
this.hashCode = address.hashCode() + port;
143       if( enableTcpNoDelay )
144          this.hashCode ++;
145    }
146 }
147
Popular Tags