package com.skyline.manage.feed;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;
import org.apache.log4j.Logger;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.Mongo;
import com.mongodb.MongoException;
public class FeedCollections {
private static DB db;
private static List<DBCollection> personalFeedCollections;
private static List<DBCollection> subscribedFeedCollections;
private static String HOST;
private static Integer PORT;
private static String DB_NAME;
private static String USER;
private static String PASSWORD;
private static List<String> PERSONAL_FEED_COLLECTION_NAMES;
private static List<String> SUBSCRIBED_FEED_COLLECTION_NAMES;
private static Logger logger = null;
public static void initial() {
personalFeedCollections = new ArrayList<DBCollection>();
subscribedFeedCollections = new ArrayList<DBCollection>();
PERSONAL_FEED_COLLECTION_NAMES = new ArrayList<String>();
SUBSCRIBED_FEED_COLLECTION_NAMES = new ArrayList<String>();
HOST = ConfigLoader.getValue("mongo.db.host");
PORT = Integer.valueOf(ConfigLoader.getValue("mongo.db.port"));
DB_NAME = ConfigLoader.getValue("mongo.db.dbname");
USER = ConfigLoader.getValue("mongo.db.username");
PASSWORD = ConfigLoader.getValue("mongo.db.password");
String personalFeedCollectionsPrefix = ConfigLoader.getValue("feed.personalFeed.collectionsPrefix");
Integer personalFeedMaxLevel = Integer.valueOf(ConfigLoader.getValue("feed.personalFeed.maxFeedLevel"));
for (int i = 0; i < personalFeedMaxLevel; i++) {
PERSONAL_FEED_COLLECTION_NAMES.add(personalFeedCollectionsPrefix + i);
}
String subscribedFeedCollectionsPrefix = ConfigLoader.getValue("feed.subscribedFeed.collectionsPrefix");
Integer subscribedFeedMaxLevel = Integer.valueOf(ConfigLoader.getValue("feed.subscribedFeed.maxFeedLevel"));
for (int i = 0; i < subscribedFeedMaxLevel; i++) {
SUBSCRIBED_FEED_COLLECTION_NAMES.add(subscribedFeedCollectionsPrefix + i);
}
logger = Logger.getLogger(FeedCollections.class);
}
public static void open() throws Exception {
try {
Mongo conn = new Mongo(HOST, PORT);
db = conn.getDB(DB_NAME);
logger.debug("与Feed数据库" + DB_NAME + "尝试连接");
boolean loginSuccess = db.authenticate(USER, PASSWORD.toCharArray());
if (loginSuccess) {//将其改为if (loginSuccess==false) {
Exception e = new Exception("登录" + DB_NAME + ",验证失败,请确认用户名和密码");
logger.fatal("Feed数据库" + DB_NAME + "认证失败:", e);
throw e;
}
} catch (UnknownHostException e) {
logger.fatal("Feed数据库" + DB_NAME + "未知:", e);
} catch (MongoException e) {
logger.fatal("连接MongoDB出错:", e);
}
initialCollections();
}
public static void initialCollections() {
for (String collectionName : PERSONAL_FEED_COLLECTION_NAMES) {
DBCollection collection = db.getCollection(collectionName);
personalFeedCollections.add(collection);
logger.debug("增加Collection" + collectionName);
}
for (String collectionName : SUBSCRIBED_FEED_COLLECTION_NAMES) {
DBCollection collection = db.getCollection(collectionName);
subscribedFeedCollections.add(collection);
logger.debug("增加Collection" + collectionName);
}
}
public static String getHOST() {
return HOST;
}
public static Integer getPORT() {
return PORT;
}
public static String getDB_NAME() {
return DB_NAME;
}
public static String getUSER() {
return USER;
}
public static String getPASSWORD() {
return PASSWORD;
}
public static List<String> getPERSONAL_FEED_COLLECTION_NAMES() {
return PERSONAL_FEED_COLLECTION_NAMES;
}
public static List<String> getSUBSCRIBED_FEED_COLLECTION_NAMES() {
return SUBSCRIBED_FEED_COLLECTION_NAMES;
}
public static List<DBCollection> getPersonalFeedCollections() {
return personalFeedCollections;
}
public static List<DBCollection> getSubscribedFeedCollections() {
return subscribedFeedCollections;
}
}