2121import java .net .InetAddress ;
2222import java .net .InetSocketAddress ;
2323import java .net .UnknownHostException ;
24- import java .util .ArrayList ;
25- import java .util .List ;
2624
2725/**
2826 * mongo server address
@@ -72,8 +70,7 @@ public ServerAddress( String host , int port )
7270
7371 _host = host ;
7472 _port = port ;
75- _all = _getAddress ( _host );
76- _addr = new InetSocketAddress ( _all [0 ] , _port );
73+ updateInetAddress ();
7774 }
7875
7976 /**
@@ -98,41 +95,11 @@ public ServerAddress( InetAddress addr , int port ){
9895 * @param addr inet socket address containing hostname and port
9996 */
10097 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 ();
105101 }
106102
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-
136103 // --------
137104 // equality, etc...
138105 // --------
@@ -165,7 +132,7 @@ public boolean equals( Object other ){
165132 a ._host .equals ( _host );
166133 }
167134 if ( other instanceof InetSocketAddress ){
168- return _addr .equals ( other );
135+ return _address .equals ( other );
169136 }
170137 return false ;
171138 }
@@ -177,62 +144,51 @@ public int hashCode(){
177144
178145 /**
179146 * Gets the hostname
180- * @return
147+ * @return hostname
181148 */
182149 public String getHost (){
183150 return _host ;
184151 }
185152
186153 /**
187154 * Gets the port number
188- * @return
155+ * @return port
189156 */
190157 public int getPort (){
191158 return _port ;
192159 }
193160
194161 /**
195162 * Gets the underlying socket address
196- * @return
163+ * @return socket address
197164 */
198165 public InetSocketAddress getSocketAddress (){
199- return _addr ;
166+ return _address ;
200167 }
201168
202169 @ Override
203170 public String toString (){
204- return _host + ":" + _port ;
171+ return _address . toString () ;
205172 }
206173
207174 final String _host ;
208175 final int _port ;
209- InetSocketAddress _addr ;
210- InetAddress [] _all ;
176+ volatile InetSocketAddress _address ;
211177
212178 // --------
213179 // static helpers
214180 // --------
215181
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-
226182 /**
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.
229185 */
230186 public static String defaultHost (){
231187 return "127.0.0.1" ;
232188 }
233189
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
236192 */
237193 public static int defaultPort (){
238194 return DBPort .PORT ;
@@ -243,13 +199,9 @@ public static int defaultPort(){
243199 * @return true if host resolved to a new IP that is different from old one, false otherwise
244200 * @throws UnknownHostException
245201 */
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 );
253206 }
254-
255207}
0 commit comments