|
22 | 22 | import com.mongodb.client.MongoClient; |
23 | 23 | import com.mongodb.client.MongoClients; |
24 | 24 | import com.mongodb.client.MongoDatabase; |
25 | | -import org.apache.logging.log4j.Logger; |
26 | 25 | import org.apache.logging.log4j.core.Core; |
27 | 26 | import org.apache.logging.log4j.core.appender.nosql.NoSqlProvider; |
28 | 27 | import org.apache.logging.log4j.core.config.plugins.Plugin; |
@@ -69,32 +68,8 @@ public static class Builder<B extends Builder<B>> extends AbstractFilterable.Bui |
69 | 68 |
|
70 | 69 | @Override |
71 | 70 | public MongoDb4Provider build() { |
72 | | - StatusLogger.getLogger().warn("The {} Appender is deprecated, use the MongoDb Appender.", PLUGIN_NAME); |
73 | | - |
74 | | - ConnectionString connectionString; |
75 | | - try { |
76 | | - connectionString = new ConnectionString(connectionStringSource); |
77 | | - } catch (final IllegalArgumentException e) { |
78 | | - LOGGER.error("Invalid MongoDB connection string `{}`.", connectionStringSource, e); |
79 | | - return null; |
80 | | - } |
81 | | - |
82 | | - String effectiveDatabaseName = databaseName != null ? databaseName : connectionString.getDatabase(); |
83 | | - String effectiveCollectionName = collectionName != null ? collectionName : connectionString.getCollection(); |
84 | | - // Validate the provided databaseName property |
85 | | - try { |
86 | | - MongoNamespace.checkDatabaseNameValidity(effectiveDatabaseName); |
87 | | - } catch (final IllegalArgumentException e) { |
88 | | - LOGGER.error("Invalid MongoDB database name `{}`.", effectiveDatabaseName, e); |
89 | | - return null; |
90 | | - } |
91 | | - // Validate the provided collectionName property |
92 | | - try { |
93 | | - MongoNamespace.checkCollectionNameValidity(effectiveCollectionName); |
94 | | - } catch (final IllegalArgumentException e) { |
95 | | - LOGGER.error("Invalid MongoDB collection name `{}`.", effectiveCollectionName, e); |
96 | | - return null; |
97 | | - } |
| 71 | + StatusLogger.getLogger() |
| 72 | + .warn("The {} Appender is deprecated, use the MongoDb Appender instead.", PLUGIN_NAME); |
98 | 73 | return newMongoDb4Provider(); |
99 | 74 | } |
100 | 75 |
|
@@ -169,14 +144,10 @@ public B setDatabaseName(final String databaseName) { |
169 | 144 | } |
170 | 145 | } |
171 | 146 |
|
172 | | - private static final Logger LOGGER = StatusLogger.getLogger(); |
173 | | - |
174 | | - // @formatter:off |
175 | 147 | private static final CodecRegistry CODEC_REGISTRIES = CodecRegistries.fromRegistries( |
176 | 148 | MongoClientSettings.getDefaultCodecRegistry(), |
177 | 149 | CodecRegistries.fromCodecs(MongoDb4LevelCodec.INSTANCE), |
178 | 150 | CodecRegistries.fromCodecs(new MongoDb4DocumentObjectCodec())); |
179 | | - // @formatter:on |
180 | 151 |
|
181 | 152 | // TODO Where does this number come from? |
182 | 153 | private static final long DEFAULT_COLLECTION_SIZE = 536_870_912; |
@@ -205,99 +176,52 @@ private MongoDb4Provider( |
205 | 176 | final String collectionName, |
206 | 177 | final boolean isCapped, |
207 | 178 | final Long collectionSize) { |
208 | | - ConnectionString connectionString; |
209 | | - try { |
210 | | - connectionString = new ConnectionString(connectionStringSource); |
211 | | - } catch (final IllegalArgumentException e) { |
212 | | - LOGGER.error("Invalid MongoDB connection string `{}`.", connectionStringSource, e); |
213 | | - throw e; |
214 | | - } |
215 | | - |
216 | | - String effectiveDatabaseName = databaseName != null ? databaseName : connectionString.getDatabase(); |
217 | | - String effectiveCollectionName = collectionName != null ? collectionName : connectionString.getCollection(); |
218 | | - // Validate the provided databaseName property |
219 | | - try { |
220 | | - MongoNamespace.checkDatabaseNameValidity(effectiveDatabaseName); |
221 | | - } catch (final IllegalArgumentException e) { |
222 | | - LOGGER.error("Invalid MongoDB database name `{}`.", effectiveDatabaseName, e); |
223 | | - throw e; |
224 | | - } |
225 | | - // Validate the provided collectionName property |
226 | | - try { |
227 | | - MongoNamespace.checkCollectionNameValidity(effectiveCollectionName); |
228 | | - } catch (final IllegalArgumentException e) { |
229 | | - LOGGER.error("Invalid MongoDB collection name `{}`.", effectiveCollectionName, e); |
230 | | - throw e; |
231 | | - } |
232 | | - LOGGER.debug("Creating ConnectionString {}...", connectionStringSource); |
233 | | - this.connectionString = new ConnectionString(connectionStringSource); |
234 | | - LOGGER.debug("Created ConnectionString {}", connectionString); |
235 | | - LOGGER.debug("Creating MongoClientSettings..."); |
236 | | - // @formatter:off |
| 179 | + this.connectionString = createConnectionString(connectionStringSource); |
237 | 180 | final MongoClientSettings settings = MongoClientSettings.builder() |
238 | 181 | .applyConnectionString(this.connectionString) |
239 | 182 | .codecRegistry(CODEC_REGISTRIES) |
240 | 183 | .build(); |
241 | | - // @formatter:on |
242 | | - LOGGER.debug("Created MongoClientSettings {}", settings); |
243 | | - LOGGER.debug("Creating MongoClient {}...", settings); |
244 | 184 | this.mongoClient = MongoClients.create(settings); |
245 | | - LOGGER.debug("Created MongoClient {}", mongoClient); |
246 | | - LOGGER.debug("Getting MongoDatabase {}...", effectiveDatabaseName); |
247 | | - this.mongoDatabase = this.mongoClient.getDatabase(effectiveDatabaseName); |
248 | | - LOGGER.debug("Got MongoDatabase {}", mongoDatabase); |
| 185 | + this.mongoDatabase = createDatabase(connectionString, databaseName, mongoClient); |
249 | 186 | this.isCapped = isCapped; |
250 | 187 | this.collectionSize = collectionSize; |
251 | | - this.collectionName = effectiveCollectionName; |
| 188 | + this.collectionName = getEffectiveCollectionName(connectionString, collectionName); |
252 | 189 | } |
253 | 190 |
|
254 | | - private MongoDb4Provider(final String connectionStringSource, final boolean isCapped, final Long collectionSize) { |
255 | | - |
256 | | - ConnectionString connectionString; |
| 191 | + private static ConnectionString createConnectionString(final String connectionStringSource) { |
257 | 192 | try { |
258 | | - connectionString = new ConnectionString(connectionStringSource); |
259 | | - } catch (final IllegalArgumentException e) { |
260 | | - LOGGER.error("Invalid MongoDB connection string `{}`.", connectionStringSource, e); |
261 | | - throw e; |
| 193 | + return new ConnectionString(connectionStringSource); |
| 194 | + } catch (final IllegalArgumentException error) { |
| 195 | + final String message = String.format("Invalid MongoDB connection string: `%s`", connectionStringSource); |
| 196 | + throw new IllegalArgumentException(message, error); |
262 | 197 | } |
| 198 | + } |
263 | 199 |
|
264 | | - String effectiveDatabaseName = connectionString.getDatabase(); |
265 | | - String effectiveCollectionName = connectionString.getCollection(); |
266 | | - // Validate the provided databaseName property |
| 200 | + private static MongoDatabase createDatabase( |
| 201 | + final ConnectionString connectionString, final String databaseName, final MongoClient client) { |
| 202 | + final String effectiveDatabaseName = databaseName != null ? databaseName : connectionString.getDatabase(); |
267 | 203 | try { |
| 204 | + // noinspection DataFlowIssue |
268 | 205 | MongoNamespace.checkDatabaseNameValidity(effectiveDatabaseName); |
269 | | - } catch (final IllegalArgumentException e) { |
270 | | - LOGGER.error("Invalid MongoDB database name `{}`.", effectiveDatabaseName, e); |
271 | | - throw e; |
| 206 | + } catch (final IllegalArgumentException error) { |
| 207 | + final String message = String.format("Invalid MongoDB database name: `%s`", effectiveDatabaseName); |
| 208 | + throw new IllegalArgumentException(message, error); |
272 | 209 | } |
273 | | - // Validate the provided collectionName property |
| 210 | + return client.getDatabase(effectiveDatabaseName); |
| 211 | + } |
| 212 | + |
| 213 | + private static String getEffectiveCollectionName( |
| 214 | + final ConnectionString connectionString, final String collectionName) { |
| 215 | + final String effectiveCollectionName = |
| 216 | + collectionName != null ? collectionName : connectionString.getCollection(); |
274 | 217 | try { |
| 218 | + // noinspection DataFlowIssue |
275 | 219 | MongoNamespace.checkCollectionNameValidity(effectiveCollectionName); |
276 | | - } catch (final IllegalArgumentException e) { |
277 | | - LOGGER.error("Invalid MongoDB collection name `{}`.", effectiveCollectionName, e); |
278 | | - throw e; |
| 220 | + } catch (final IllegalArgumentException error) { |
| 221 | + final String message = String.format("Invalid MongoDB collection name: `%s`", effectiveCollectionName); |
| 222 | + throw new IllegalArgumentException(message, error); |
279 | 223 | } |
280 | | - |
281 | | - LOGGER.debug("Creating ConnectionString {}...", connectionStringSource); |
282 | | - this.connectionString = new ConnectionString(connectionStringSource); |
283 | | - LOGGER.debug("Created ConnectionString {}", connectionString); |
284 | | - LOGGER.debug("Creating MongoClientSettings..."); |
285 | | - // @formatter:off |
286 | | - final MongoClientSettings settings = MongoClientSettings.builder() |
287 | | - .applyConnectionString(this.connectionString) |
288 | | - .codecRegistry(CODEC_REGISTRIES) |
289 | | - .build(); |
290 | | - // @formatter:on |
291 | | - LOGGER.debug("Created MongoClientSettings {}", settings); |
292 | | - LOGGER.debug("Creating MongoClient {}...", settings); |
293 | | - this.mongoClient = MongoClients.create(settings); |
294 | | - LOGGER.debug("Created MongoClient {}", mongoClient); |
295 | | - LOGGER.debug("Getting MongoDatabase {}...", effectiveDatabaseName); |
296 | | - this.mongoDatabase = this.mongoClient.getDatabase(effectiveCollectionName); |
297 | | - LOGGER.debug("Got MongoDatabase {}", mongoDatabase); |
298 | | - this.isCapped = isCapped; |
299 | | - this.collectionSize = collectionSize; |
300 | | - this.collectionName = effectiveCollectionName; |
| 224 | + return effectiveCollectionName; |
301 | 225 | } |
302 | 226 |
|
303 | 227 | @Override |
|
0 commit comments