org.craftercms.commons.mongo.MongoClientFactory Maven / Gradle / Ivy
/*
* Copyright (C) 2007-2020 Crafter Software Corporation. All Rights Reserved.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as published by
* the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
package org.craftercms.commons.mongo;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientOptions;
import com.mongodb.ServerAddress;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.config.AbstractFactoryBean;
/**
* Creates a Mongo Client based on a connection String
* Connection String format host:PORT,[host1:port1],[hostN,portN] the port portion is optional default
* one 27017 will be use.
* If connection String is null or empty or whitespace only, this factory will
* use as if connection String value is "127.0.0.1:27017"
* Will throw IllegalArgumentException if port number is not valid
*/
public class MongoClientFactory extends AbstractFactoryBean {
public static final String DEFAULT_MONGO_HOST = "127.0.0.1";
public static final int DEFAULT_MONGO_PORT = 27017;
private Logger logger = LoggerFactory.getLogger(MongoClientFactory.class);
private MongoClientOptions options;
private String connectionString;
private String username;
private String password;
@Override
public Class> getObjectType() {
return MongoClient.class;
}
@Override
protected MongoClient createInstance() throws Exception {
if (StringUtils.isBlank(connectionString)) {
logger.info("No connection string specified, connecting to {}:{}", connectionString, DEFAULT_MONGO_HOST,
DEFAULT_MONGO_PORT);
return new MongoClient(new ServerAddress(DEFAULT_MONGO_HOST, DEFAULT_MONGO_PORT));
}
StringTokenizer st = new StringTokenizer(connectionString, ",");
List addressList = new ArrayList<>();
while (st.hasMoreElements()) {
String server = st.nextElement().toString();
logger.debug("Processing first server found with string {}", server);
String[] serverAndPort = server.split(":");
if (serverAndPort.length == 2) {
logger.debug("Server string defines host {} and port {}", serverAndPort[0], serverAndPort[1]);
if (StringUtils.isBlank(serverAndPort[0])) {
throw new IllegalArgumentException("Given host can't be empty");
}
int portNumber = NumberUtils.toInt(serverAndPort[1]);
if (portNumber == 0) {
throw new IllegalArgumentException("Given port number " + portNumber + " is not valid");
}
addressList.add(new ServerAddress(serverAndPort[0], portNumber));
} else if (serverAndPort.length == 1) {
logger.debug("Server string defines host {} only. Using default port ", serverAndPort[0]);
if (StringUtils.isBlank(serverAndPort[0])) {
throw new IllegalArgumentException("Given host can't be empty");
}
addressList.add(new ServerAddress(serverAndPort[0], DEFAULT_MONGO_PORT));
} else {
throw new IllegalArgumentException("Given connection string is not valid");
}
}
logger.debug("Creating MongoClient with addresses: {}", addressList);
if (options != null) {
return new MongoClient(addressList, options);
} else {
return new MongoClient(addressList);
}
}
public void setConnectionString(final String connectionString) {
this.connectionString = connectionString;
}
public void setOptions(final MongoClientOptions options) {
this.options = options;
}
public void setUsername(final String username) {
this.username = username;
}
public void setPassword(final String password) {
this.password = password;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy