/* * Copyright (c) www.bugull.com * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package net.tooan.ynpay.third.mongodb; import com.mongodb.*; import net.tooan.ynpay.third.jfinal.log.Logger; import net.tooan.ynpay.third.mongodb.exception.DBConnectionException; import net.tooan.ynpay.third.mongodb.mapper.CascadeDeleteExecutor; import net.tooan.ynpay.third.mongodb.mapper.StringUtil; import java.net.UnknownHostException; import java.util.List; import java.util.concurrent.ExecutorService; /** * The connection to MongoDB. * <p/> * <p>Singleton Pattern is used here. An application should use only one BuguConnection.</p> * * @author Frank Wen(xbwen@hotmail.com) */ public class BuguConnection { private final static Logger logger = Logger.getLogger(BuguConnection.class); private static BuguConnection instance; private String host; private int port; private List<ServerAddress> replicaSet; private ReadPreference readPreference; private MongoClientOptions options; private String database; private String username; private String password; private MongoClient mc; private DB db; private BuguConnection() { } public static BuguConnection getInstance() { if (instance == null) { instance = new BuguConnection(); } return instance; } public void connect(String host, int port, String database) { this.host = host; this.port = port; this.database = database; connect(); } public void connect(String host, int port, String database, String username, String password) { this.host = host; this.port = port; this.database = database; this.username = username; this.password = password; connect(); } public void connect(List<ServerAddress> replicaSet, String database, String username, String password) { this.replicaSet = replicaSet; this.database = database; this.username = username; this.password = password; connect(); } public void connect() { try { doConnect(); } catch (UnknownHostException ex) { logger.error("Can not connect to host " + host, ex); } catch (DBConnectionException ex) { logger.error(ex.getMessage(), ex); } if (username != null && password != null) { try { auth(); } catch (DBConnectionException ex) { logger.error(ex.getMessage(), ex); } } } private void doConnect() throws UnknownHostException, DBConnectionException { if (host != null && port != 0) { ServerAddress sa = new ServerAddress(host, port); if (options != null) { mc = new MongoClient(sa, options); } else { mc = new MongoClient(sa); } } else if (replicaSet != null) { if (options != null) { mc = new MongoClient(replicaSet, options); } else { mc = new MongoClient(replicaSet); } if (readPreference != null) { mc.setReadPreference(readPreference); } } if (mc != null) { if (!StringUtil.isEmpty(database)) { db = mc.getDB(database); } } else { throw new DBConnectionException("Can not get database instance! Please ensure connected to mongoDB correctly."); } } public void close() { ExecutorService executor = CascadeDeleteExecutor.getInstance().getExecutor(); if (executor != null) { executor.shutdown(); } if (mc != null) { mc.close(); } } private void auth() throws DBConnectionException { boolean auth = db.authenticate(username, password.toCharArray()); if (auth) { logger.info("Connected to mongodb successfully!"); } else { db = null; throw new DBConnectionException("Can not connect to mongoDB. Failed to authenticate!"); } } public BuguConnection setHost(String host) { this.host = host; return this; } public BuguConnection setPort(int port) { this.port = port; return this; } public BuguConnection setDatabase(String database) { this.database = database; return this; } public BuguConnection setUsername(String username) { this.username = username; return this; } public BuguConnection setPassword(String password) { this.password = password; return this; } public BuguConnection setOptions(MongoClientOptions options) { this.options = options; return this; } public BuguConnection setReplicaSet(List<ServerAddress> replicaSet) { this.replicaSet = replicaSet; return this; } public BuguConnection setReadPreference(ReadPreference readPreference) { this.readPreference = readPreference; return this; } public DB getDB() throws DBConnectionException { if (db == null) { throw new DBConnectionException("Can not get database instance! Please ensure connected to mongoDB correctly."); } else { return db; } } public DB getDB(String dbname) throws DBConnectionException { if (mc == null) { throw new DBConnectionException("Can not get database instance! Please ensure connected to mongoDB correctly."); } else { if (StringUtil.isEmpty(dbname)) { return getDB(); } return mc.getDB(dbname); } } }