|
49 | 49 | import com.mongodb.operation.DropDatabaseOperation; |
50 | 50 | import com.mongodb.operation.ReadOperation; |
51 | 51 | import com.mongodb.operation.WriteOperation; |
| 52 | +import com.mongodb.selector.ServerSelector; |
52 | 53 | import org.bson.BsonDocument; |
53 | 54 | import org.bson.BsonDocumentWrapper; |
54 | 55 | import org.bson.BsonInt32; |
@@ -103,25 +104,30 @@ public static boolean clusterIsType(final ClusterType clusterType) { |
103 | 104 | return getCluster().getDescription().getType() == clusterType; |
104 | 105 | } |
105 | 106 |
|
106 | | - @SuppressWarnings("deprecation") |
107 | 107 | public static ServerVersion getServerVersion() { |
108 | | - return getCluster().getDescription().getAny().get(0).getVersion(); |
| 108 | + return getCluster().selectServer(new ServerSelector() { |
| 109 | + @Override |
| 110 | + @SuppressWarnings("deprecation") |
| 111 | + public List<ServerDescription> select(final ClusterDescription clusterDescription) { |
| 112 | + return clusterDescription.getAny(); |
| 113 | + } |
| 114 | + }).getDescription().getVersion(); |
109 | 115 | } |
110 | 116 |
|
111 | 117 | public static boolean serverVersionAtLeast(final List<Integer> versionArray) { |
112 | | - return getConnectedServerVersion().compareTo(new ServerVersion(versionArray)) >= 0; |
| 118 | + return getServerVersion().compareTo(new ServerVersion(versionArray)) >= 0; |
113 | 119 | } |
114 | 120 |
|
115 | 121 | public static boolean serverVersionAtLeast(final int majorVersion, final int minorVersion) { |
116 | 122 | return serverVersionAtLeast(asList(majorVersion, minorVersion, 0)); |
117 | 123 | } |
118 | 124 |
|
119 | 125 | public static boolean serverVersionLessThan(final String versionString) { |
120 | | - return getConnectedServerVersion().compareTo(new ServerVersion(getVersionList(versionString).subList(0, 3))) < 0; |
| 126 | + return getServerVersion().compareTo(new ServerVersion(getVersionList(versionString).subList(0, 3))) < 0; |
121 | 127 | } |
122 | 128 |
|
123 | 129 | public static boolean serverVersionGreaterThan(final String versionString) { |
124 | | - return getConnectedServerVersion().compareTo(new ServerVersion(getVersionList(versionString).subList(0, 3))) > 0; |
| 130 | + return getServerVersion().compareTo(new ServerVersion(getVersionList(versionString).subList(0, 3))) > 0; |
125 | 131 | } |
126 | 132 |
|
127 | 133 | private static List<Integer> getVersionList(final String versionString) { |
@@ -163,25 +169,6 @@ public static boolean supportsFsync() { |
163 | 169 | return storageEngine != null && !storageEngine.get("name").equals("inMemory"); |
164 | 170 | } |
165 | 171 |
|
166 | | - @SuppressWarnings("deprecation") |
167 | | - public static ServerVersion getConnectedServerVersion() { |
168 | | - ClusterDescription clusterDescription = getCluster().getDescription(); |
169 | | - int retries = 0; |
170 | | - while (clusterDescription.getAny().isEmpty() && retries <= 3) { |
171 | | - try { |
172 | | - Thread.sleep(1000); |
173 | | - retries++; |
174 | | - } catch (InterruptedException e) { |
175 | | - throw new RuntimeException("Interrupted", e); |
176 | | - } |
177 | | - clusterDescription = getCluster().getDescription(); |
178 | | - } |
179 | | - if (clusterDescription.getAny().isEmpty()) { |
180 | | - throw new RuntimeException("There are no servers available in " + clusterDescription); |
181 | | - } |
182 | | - return clusterDescription.getAny().get(0).getVersion(); |
183 | | - } |
184 | | - |
185 | 172 | public static boolean isNotAtLeastJava7() { |
186 | 173 | return javaVersionStartsWith("1.6"); |
187 | 174 | } |
|
0 commit comments