|
16 | 16 |
|
17 | 17 | package org.springframework.boot.autoconfigure.mongo;
|
18 | 18 |
|
19 |
| -import java.util.Collections; |
20 | 19 | import java.util.List;
|
21 | 20 |
|
22 |
| -import com.mongodb.MongoClient; |
23 |
| -import com.mongodb.MongoClientOptions; |
24 |
| -import com.mongodb.MongoClientOptions.Builder; |
25 |
| -import com.mongodb.MongoClientURI; |
26 |
| -import com.mongodb.MongoCredential; |
27 |
| -import com.mongodb.ServerAddress; |
| 21 | +import com.mongodb.MongoClientSettings; |
| 22 | +import com.mongodb.client.MongoClient; |
| 23 | +import com.mongodb.client.MongoClients; |
28 | 24 |
|
29 | 25 | import org.springframework.core.env.Environment;
|
30 | 26 |
|
|
39 | 35 | * @author Stephane Nicoll
|
40 | 36 | * @author Nasko Vasilev
|
41 | 37 | * @author Mark Paluch
|
| 38 | + * @author Christoph Strobl |
42 | 39 | * @since 2.0.0
|
43 | 40 | */
|
44 |
| -public class MongoClientFactory { |
| 41 | +public class MongoClientFactory extends MongoClientFactorySupport<MongoClient> { |
45 | 42 |
|
46 |
| - private final MongoProperties properties; |
47 |
| - |
48 |
| - private final Environment environment; |
49 |
| - |
50 |
| - public MongoClientFactory(MongoProperties properties, Environment environment) { |
51 |
| - this.properties = properties; |
52 |
| - this.environment = environment; |
53 |
| - } |
54 |
| - |
55 |
| - /** |
56 |
| - * Creates a {@link MongoClient} using the given {@code options}. If the environment |
57 |
| - * contains a {@code local.mongo.port} property, it is used to configure a client to |
58 |
| - * an embedded MongoDB instance. |
59 |
| - * @param options the options |
60 |
| - * @return the Mongo client |
61 |
| - */ |
62 |
| - public MongoClient createMongoClient(MongoClientOptions options) { |
63 |
| - Integer embeddedPort = getEmbeddedPort(); |
64 |
| - if (embeddedPort != null) { |
65 |
| - return createEmbeddedMongoClient(options, embeddedPort); |
66 |
| - } |
67 |
| - return createNetworkMongoClient(options); |
68 |
| - } |
69 |
| - |
70 |
| - private Integer getEmbeddedPort() { |
71 |
| - if (this.environment != null) { |
72 |
| - String localPort = this.environment.getProperty("local.mongo.port"); |
73 |
| - if (localPort != null) { |
74 |
| - return Integer.valueOf(localPort); |
75 |
| - } |
76 |
| - } |
77 |
| - return null; |
78 |
| - } |
79 |
| - |
80 |
| - private MongoClient createEmbeddedMongoClient(MongoClientOptions options, int port) { |
81 |
| - if (options == null) { |
82 |
| - options = MongoClientOptions.builder().build(); |
83 |
| - } |
84 |
| - String host = (this.properties.getHost() != null) ? this.properties.getHost() : "localhost"; |
85 |
| - return new MongoClient(Collections.singletonList(new ServerAddress(host, port)), options); |
86 |
| - } |
87 |
| - |
88 |
| - private MongoClient createNetworkMongoClient(MongoClientOptions options) { |
89 |
| - MongoProperties properties = this.properties; |
90 |
| - if (properties.getUri() != null) { |
91 |
| - return createMongoClient(properties.getUri(), options); |
92 |
| - } |
93 |
| - if (hasCustomAddress() || hasCustomCredentials()) { |
94 |
| - if (options == null) { |
95 |
| - options = MongoClientOptions.builder().build(); |
96 |
| - } |
97 |
| - MongoCredential credentials = getCredentials(properties); |
98 |
| - String host = getValue(properties.getHost(), "localhost"); |
99 |
| - int port = getValue(properties.getPort(), MongoProperties.DEFAULT_PORT); |
100 |
| - List<ServerAddress> seeds = Collections.singletonList(new ServerAddress(host, port)); |
101 |
| - return (credentials != null) ? new MongoClient(seeds, credentials, options) |
102 |
| - : new MongoClient(seeds, options); |
103 |
| - } |
104 |
| - return createMongoClient(MongoProperties.DEFAULT_URI, options); |
105 |
| - } |
106 |
| - |
107 |
| - private MongoClient createMongoClient(String uri, MongoClientOptions options) { |
108 |
| - return new MongoClient(new MongoClientURI(uri, builder(options))); |
109 |
| - } |
110 |
| - |
111 |
| - private <T> T getValue(T value, T fallback) { |
112 |
| - return (value != null) ? value : fallback; |
113 |
| - } |
114 |
| - |
115 |
| - private boolean hasCustomAddress() { |
116 |
| - return this.properties.getHost() != null || this.properties.getPort() != null; |
117 |
| - } |
118 |
| - |
119 |
| - private MongoCredential getCredentials(MongoProperties properties) { |
120 |
| - if (!hasCustomCredentials()) { |
121 |
| - return null; |
122 |
| - } |
123 |
| - String username = properties.getUsername(); |
124 |
| - String database = getValue(properties.getAuthenticationDatabase(), properties.getMongoClientDatabase()); |
125 |
| - char[] password = properties.getPassword(); |
126 |
| - return MongoCredential.createCredential(username, database, password); |
| 43 | + public MongoClientFactory(MongoProperties properties, Environment environment, |
| 44 | + List<MongoClientSettingsBuilderCustomizer> builderCustomizers) { |
| 45 | + super(properties, environment, builderCustomizers); |
127 | 46 | }
|
128 | 47 |
|
129 |
| - private boolean hasCustomCredentials() { |
130 |
| - return this.properties.getUsername() != null && this.properties.getPassword() != null; |
| 48 | + protected MongoClient createNetworkMongoClient(MongoClientSettings settings) { |
| 49 | + return MongoClients.create(settings, driverInformation()); |
131 | 50 | }
|
132 | 51 |
|
133 |
| - private Builder builder(MongoClientOptions options) { |
134 |
| - if (options != null) { |
135 |
| - return MongoClientOptions.builder(options); |
136 |
| - } |
137 |
| - return MongoClientOptions.builder(); |
| 52 | + @Override |
| 53 | + protected MongoClient createEmbeddedMongoClient(MongoClientSettings settings) { |
| 54 | + return MongoClients.create(settings, driverInformation()); |
138 | 55 | }
|
139 | 56 |
|
140 | 57 | }
|
0 commit comments