Skip to content

Commit 3b12c2a

Browse files
committed
JAVA-249: Removed calls to InetAddress.getLocalHost and InetAddress.getAllByName, using InetAddress.getByName instead
Simplified code. Fixed Javadoc. Removed unused methods. Made it thread-safe by making _address volatile
1 parent d355b36 commit 3b12c2a

File tree

5 files changed

+22
-73
lines changed

5 files changed

+22
-73
lines changed

src/main/com/mongodb/DBTCPConnector.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -503,7 +503,7 @@ public String debugString(){
503503
buf.append( "replica set : " ).append( _allHosts );
504504
} else {
505505
ServerAddress master = getAddress();
506-
buf.append( master ).append( " " ).append( master != null ? master._addr : null );
506+
buf.append( master ).append( " " ).append( master != null ? master.getSocketAddress() : null );
507507
}
508508

509509
return buf.toString();

src/main/com/mongodb/ReplicaSetStatus.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -490,7 +490,7 @@ static class UpdatableNode {
490490

491491
private void updateAddr() {
492492
try {
493-
if (_addr.updateInetAddr()) {
493+
if (_addr.updateInetAddress()) {
494494
// address changed, need to use new ports
495495
_port = new DBPort(_addr, null, _mongoOptions);
496496
_mongo.getConnector().updatePortPool(_addr);

src/main/com/mongodb/ServerAddress.java

Lines changed: 19 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@
2121
import java.net.InetAddress;
2222
import java.net.InetSocketAddress;
2323
import 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
}

src/test/com/mongodb/DBAddressTest.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ public void testCTOR()
3131
DBAddress bar = new DBAddress( foo, "some.other.host" );
3232
assertEquals( foo.sameHost( "www.10gen.com:1000" ), true );
3333
assertEquals( foo.getSocketAddress().hashCode(), bar.getSocketAddress().hashCode() );
34-
assertEquals( foo.toString(), "www.10gen.com:1000/some-host" );
3534
}
3635

3736
@Test(groups = {"basic"})
@@ -63,8 +62,6 @@ public void testBasics()
6362
assertEquals( 27017 , new ServerAddress().getPort() );
6463
assertEquals( 27017 , new ServerAddress( "localhost" ).getPort() );
6564
assertEquals( 9999 , new ServerAddress( "localhost:9999" ).getPort() );
66-
67-
assertEquals( false , new ServerAddress( "localhost:9999" ).isPaired() );
6865
}
6966

7067
@Test

src/test/com/mongodb/JavaClientTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public class JavaClientTest extends TestCase {
3535

3636
public JavaClientTest()
3737
throws IOException , MongoException {
38-
_mongo = new Mongo( "127.0.0.1" );
38+
_mongo = new Mongo( "localhost" );
3939
cleanupMongo = _mongo;
4040
cleanupDB = "com_mongodb_unittest_JavaClientTest";
4141
_db = cleanupMongo.getDB( cleanupDB );

0 commit comments

Comments
 (0)