21
21
import java .net .InetAddress ;
22
22
import java .net .InetSocketAddress ;
23
23
import java .net .UnknownHostException ;
24
- import java .util .ArrayList ;
25
- import java .util .List ;
26
24
27
25
/**
28
26
* mongo server address
@@ -72,8 +70,7 @@ public ServerAddress( String host , int port )
72
70
73
71
_host = host ;
74
72
_port = port ;
75
- _all = _getAddress ( _host );
76
- _addr = new InetSocketAddress ( _all [0 ] , _port );
73
+ updateInetAddress ();
77
74
}
78
75
79
76
/**
@@ -98,41 +95,11 @@ public ServerAddress( InetAddress addr , int port ){
98
95
* @param addr inet socket address containing hostname and port
99
96
*/
100
97
public ServerAddress ( InetSocketAddress addr ){
101
- _addr = addr ;
102
- _host = _addr .getHostName ();
103
- _port = _addr .getPort ();
104
- _all = null ;
98
+ _address = addr ;
99
+ _host = _address .getHostName ();
100
+ _port = _address .getPort ();
105
101
}
106
102
107
- // --------
108
- // pairing
109
- // --------
110
-
111
- /**
112
- * Determines if the database at this address is paired.
113
- * @return if this address connects to a set of paired databases
114
- */
115
- boolean isPaired (){
116
- return _all != null && _all .length > 1 ;
117
- }
118
-
119
- /**
120
- * If this is the address of a paired database, returns addresses for
121
- * all of the databases with which it is paired.
122
- * @return the addresses
123
- * @throws RuntimeException if this address is not one of a paired database
124
- */
125
- List <ServerAddress > explode (){
126
- if ( _all == null || _all .length <= 1 )
127
- throw new RuntimeException ( "not replica set mode. num addresses : " + ((_all == null ) ? 0 : _all .length ) );
128
-
129
- List <ServerAddress > s = new ArrayList <ServerAddress >();
130
- for ( int i =0 ; i <_all .length ; i ++ ){
131
- s .add ( new ServerAddress ( _all [i ] , _port ) );
132
- }
133
- return s ;
134
- }
135
-
136
103
// --------
137
104
// equality, etc...
138
105
// --------
@@ -165,7 +132,7 @@ public boolean equals( Object other ){
165
132
a ._host .equals ( _host );
166
133
}
167
134
if ( other instanceof InetSocketAddress ){
168
- return _addr .equals ( other );
135
+ return _address .equals ( other );
169
136
}
170
137
return false ;
171
138
}
@@ -177,62 +144,51 @@ public int hashCode(){
177
144
178
145
/**
179
146
* Gets the hostname
180
- * @return
147
+ * @return hostname
181
148
*/
182
149
public String getHost (){
183
150
return _host ;
184
151
}
185
152
186
153
/**
187
154
* Gets the port number
188
- * @return
155
+ * @return port
189
156
*/
190
157
public int getPort (){
191
158
return _port ;
192
159
}
193
160
194
161
/**
195
162
* Gets the underlying socket address
196
- * @return
163
+ * @return socket address
197
164
*/
198
165
public InetSocketAddress getSocketAddress (){
199
- return _addr ;
166
+ return _address ;
200
167
}
201
168
202
169
@ Override
203
170
public String toString (){
204
- return _host + ":" + _port ;
171
+ return _address . toString () ;
205
172
}
206
173
207
174
final String _host ;
208
175
final int _port ;
209
- InetSocketAddress _addr ;
210
- InetAddress [] _all ;
176
+ volatile InetSocketAddress _address ;
211
177
212
178
// --------
213
179
// static helpers
214
180
// --------
215
181
216
- private static InetAddress [] _getAddress ( String host )
217
- throws UnknownHostException {
218
-
219
- if ( host .toLowerCase ().equals ("localhost" ) ){
220
- return new InetAddress [] { InetAddress .getLocalHost ()};
221
- }
222
-
223
- return InetAddress .getAllByName ( host );
224
- }
225
-
226
182
/**
227
- * Returns the default database host: db_ip environment variable, or "127.0.0.1"
228
- * @return
183
+ * Returns the default database host: "127.0.0.1"
184
+ * @return IP address of default host.
229
185
*/
230
186
public static String defaultHost (){
231
187
return "127.0.0.1" ;
232
188
}
233
189
234
- /** Returns the default database port: db_port environment variable, or 27017 as a default
235
- * @return
190
+ /** Returns the default database port: 27017
191
+ * @return the default port
236
192
*/
237
193
public static int defaultPort (){
238
194
return DBPort .PORT ;
@@ -243,13 +199,9 @@ public static int defaultPort(){
243
199
* @return true if host resolved to a new IP that is different from old one, false otherwise
244
200
* @throws UnknownHostException
245
201
*/
246
- boolean updateInetAddr () throws UnknownHostException {
247
- InetSocketAddress oldaddr = _addr ;
248
- _all = _getAddress ( _host );
249
- _addr = new InetSocketAddress ( _all [0 ] , _port );
250
- if (!_addr .equals (oldaddr ))
251
- return true ;
252
- return false ;
202
+ boolean updateInetAddress () throws UnknownHostException {
203
+ InetSocketAddress oldAddress = _address ;
204
+ _address = new InetSocketAddress ( InetAddress .getByName (_host ) , _port );
205
+ return !_address .equals (oldAddress );
253
206
}
254
-
255
207
}
0 commit comments