/driver-core/src/main/com/mongodb/ServerAddress.java

https://github.com/jyemin/mongo-java-driver · Java · 243 lines · 130 code · 27 blank · 86 comment · 27 complexity · 204035914fd02d60e1eb8d0c337cce51 MD5 · raw file

  1. /*
  2. * Copyright 2008-present MongoDB, Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.mongodb;
  17. import com.mongodb.annotations.Immutable;
  18. import com.mongodb.lang.Nullable;
  19. import java.io.Serializable;
  20. import java.net.InetAddress;
  21. import java.net.InetSocketAddress;
  22. import java.net.UnknownHostException;
  23. import java.util.ArrayList;
  24. import java.util.List;
  25. /**
  26. * Represents the location of a Mongo server - i.e. server name and port number
  27. */
  28. @Immutable
  29. public class ServerAddress implements Serializable {
  30. private static final long serialVersionUID = 4027873363095395504L;
  31. /**
  32. * The host.
  33. */
  34. private final String host;
  35. /**
  36. * The port.
  37. */
  38. private final int port;
  39. /**
  40. * Creates a ServerAddress with default host and port
  41. */
  42. public ServerAddress() {
  43. this(defaultHost(), defaultPort());
  44. }
  45. /**
  46. * Creates a ServerAddress with default port
  47. *
  48. * @param host hostname
  49. */
  50. public ServerAddress(@Nullable final String host) {
  51. this(host, defaultPort());
  52. }
  53. /**
  54. * Creates a ServerAddress with default port
  55. *
  56. * @param inetAddress host address
  57. */
  58. public ServerAddress(final InetAddress inetAddress) {
  59. this(inetAddress.getHostName(), defaultPort());
  60. }
  61. /**
  62. * Creates a ServerAddress
  63. *
  64. * @param inetAddress host address
  65. * @param port mongod port
  66. */
  67. public ServerAddress(final InetAddress inetAddress, final int port) {
  68. this(inetAddress.getHostName(), port);
  69. }
  70. /**
  71. * Creates a ServerAddress
  72. *
  73. * @param inetSocketAddress inet socket address containing hostname and port
  74. */
  75. public ServerAddress(final InetSocketAddress inetSocketAddress) {
  76. this(inetSocketAddress.getAddress(), inetSocketAddress.getPort());
  77. }
  78. /**
  79. * Creates a ServerAddress
  80. *
  81. * @param host hostname
  82. * @param port mongod port
  83. */
  84. public ServerAddress(@Nullable final String host, final int port) {
  85. String hostToUse = host;
  86. if (hostToUse == null) {
  87. hostToUse = defaultHost();
  88. }
  89. hostToUse = hostToUse.trim();
  90. if (hostToUse.length() == 0) {
  91. hostToUse = defaultHost();
  92. }
  93. int portToUse = port;
  94. if (hostToUse.startsWith("[")) {
  95. int idx = host.indexOf("]");
  96. if (idx == -1) {
  97. throw new IllegalArgumentException("an IPV6 address must be encosed with '[' and ']'"
  98. + " according to RFC 2732.");
  99. }
  100. int portIdx = host.indexOf("]:");
  101. if (portIdx != -1) {
  102. if (port != defaultPort()) {
  103. throw new IllegalArgumentException("can't specify port in construct and via host");
  104. }
  105. portToUse = Integer.parseInt(host.substring(portIdx + 2));
  106. }
  107. hostToUse = host.substring(1, idx);
  108. } else {
  109. int idx = hostToUse.indexOf(":");
  110. int lastIdx = hostToUse.lastIndexOf(":");
  111. if (idx == lastIdx && idx > 0) {
  112. if (port != defaultPort()) {
  113. throw new IllegalArgumentException("can't specify port in construct and via host");
  114. }
  115. try {
  116. portToUse = Integer.parseInt(hostToUse.substring(idx + 1));
  117. } catch (NumberFormatException e) {
  118. throw new MongoException("host and port should be specified in host:port format");
  119. }
  120. hostToUse = hostToUse.substring(0, idx).trim();
  121. }
  122. }
  123. this.host = hostToUse.toLowerCase();
  124. this.port = portToUse;
  125. }
  126. @Override
  127. public boolean equals(final Object o) {
  128. if (this == o) {
  129. return true;
  130. }
  131. if (o == null || getClass() != o.getClass()) {
  132. return false;
  133. }
  134. ServerAddress that = (ServerAddress) o;
  135. if (port != that.port) {
  136. return false;
  137. }
  138. if (!host.equals(that.host)) {
  139. return false;
  140. }
  141. return true;
  142. }
  143. @Override
  144. public int hashCode() {
  145. int result = host.hashCode();
  146. result = 31 * result + port;
  147. return result;
  148. }
  149. /**
  150. * Gets the hostname
  151. *
  152. * @return hostname
  153. */
  154. public String getHost() {
  155. return host;
  156. }
  157. /**
  158. * Gets the port number
  159. *
  160. * @return port
  161. */
  162. public int getPort() {
  163. return port;
  164. }
  165. /**
  166. * Gets the underlying socket address
  167. *
  168. * @return socket address
  169. */
  170. public InetSocketAddress getSocketAddress() {
  171. try {
  172. return new InetSocketAddress(InetAddress.getByName(host), port);
  173. } catch (UnknownHostException e) {
  174. throw new MongoSocketException(e.getMessage(), this, e);
  175. }
  176. }
  177. /**
  178. * Gets all underlying socket addresses
  179. *
  180. * @return array of socket addresses
  181. *
  182. * @since 3.9
  183. */
  184. public List<InetSocketAddress> getSocketAddresses() {
  185. try {
  186. InetAddress[] inetAddresses = InetAddress.getAllByName(host);
  187. List<InetSocketAddress> inetSocketAddressList = new ArrayList<InetSocketAddress>();
  188. for (InetAddress inetAddress : inetAddresses) {
  189. inetSocketAddressList.add(new InetSocketAddress(inetAddress, port));
  190. }
  191. return inetSocketAddressList;
  192. } catch (UnknownHostException e) {
  193. throw new MongoSocketException(e.getMessage(), this, e);
  194. }
  195. }
  196. @Override
  197. public String toString() {
  198. return host + ":" + port;
  199. }
  200. /**
  201. * Returns the default database host: "127.0.0.1"
  202. *
  203. * @return IP address of default host.
  204. */
  205. public static String defaultHost() {
  206. return "127.0.0.1"; // NOPMD
  207. }
  208. /**
  209. * Returns the default database port: 27017
  210. *
  211. * @return the default port
  212. */
  213. public static int defaultPort() {
  214. return 27017;
  215. }
  216. }