lyf666 2017-11-25 15:12:38 +08:00
parent 85a8c09b24
commit 6717eda5cc
2 changed files with 113 additions and 39 deletions

View File

@ -9,10 +9,14 @@ import com.mongodb.MongoCredential;
import com.mongodb.ServerAddress;
import com.mongodb.client.MongoDatabase;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.Properties;
import static cn.cloudowr.dict.Main.*;
import static java.lang.System.exit;
/**
* Created by lyf66 on 2017/2/16.
@ -54,9 +58,17 @@ public class Config extends com.jfinal.config.JFinalConfig{
@Override
public void afterJFinalStart() {
MongoCredential mongoCredential = MongoCredential.createCredential(username, database, password.toCharArray());
MongoClient mongoClient = new MongoClient(new ServerAddress(host, port), Arrays.asList(mongoCredential));
mongoDatabase = mongoClient.getDatabase(dabaseName);
Properties p = new Properties();
try {
p.load(new FileInputStream(new File(configFilename)));
} catch (IOException e) {
e.printStackTrace();
exit(0);
}
MongoCredential mongoCredential = MongoCredential.createCredential(p.getProperty("mongoUser"), "admin", p.getProperty("mongoPasswd").toCharArray());
MongoClient mongoClient = new MongoClient(new ServerAddress(p.getProperty("mongoUrl"), toIntOrNull(p.getProperty("mongoPort"))), Arrays.asList(mongoCredential));
mongoDatabase = mongoClient.getDatabase(p.getProperty("mongoDatabase"));
}
public static MongoDatabase getMongoDataBase() {

View File

@ -10,50 +10,112 @@ import java.io.*;
import java.util.EnumSet;
import java.util.Properties;
import static java.lang.System.exit;
/**
* Created by lyf66 on 2017/2/16.
*/
public class Main {
public static String username;
public static String database;
public static String password;
public static String host;
public static int port;
public static String dabaseName;
private static int AppPort;
public static String configFilename = "usercenter-config.properties";
public static void main(String[] args) throws IOException {
read();
public static void main(String[] args) throws Exception {
Properties p = null;
try {
File configFile = new File(configFilename);
if (!configFile.exists()) {
configFile.createNewFile();
p = createDefaultProperties();
p.store(new FileOutputStream(configFile), "");
System.out.println("已生成配置文件,请进行正确配置后重启应用");
exit(0);
}
p = new Properties();
p.load(new FileInputStream(configFile));
if (!checkProperties(p)) {
System.out.println("配置文件不正确");
exit(0);
}
} catch (Exception e) {
System.err.println("配置文件不正确");
}
// JFinal.start("", toIntOrNull(p.getProperty("port")), "/");
EnumSet<DispatcherType> all = EnumSet.of(DispatcherType.ASYNC, DispatcherType.ERROR,
DispatcherType.FORWARD, DispatcherType.INCLUDE, DispatcherType.REQUEST);
final Server server = new Server(AppPort);
try {
WebAppContext context = new WebAppContext("/", "/");
FilterHolder filter = new FilterHolder(new JFinalFilter());
filter.setInitParameter("configClass", "cn.cloudowr.dict.Config");
context.addFilter(filter, "/*", all);
server.setHandler(context);
server.start();
server.join();
} catch (Exception e) {
e.printStackTrace();
}
final Server server = new Server(toIntOrNull(p.getProperty("port")));
WebAppContext context = new WebAppContext("/", "/");
FilterHolder filter = new FilterHolder(new JFinalFilter());
filter.setInitParameter("configClass", "cn.cloudowr.usercenter.Config");
context.addFilter(filter, "/*", all);
server.setHandler(context);
server.start();
server.join();
}
public static void read() {
try {
Properties pro = new Properties();
pro.load(Config.class.getResourceAsStream("/config.properties"));
username = pro.getProperty("username");
database = pro.getProperty("database");
password = pro.getProperty("password");
host = pro.getProperty("host");
port = Integer.parseInt(pro.getProperty("port"));
dabaseName = pro.getProperty("dabaseName");
AppPort = Integer.parseInt(pro.getProperty("AppPort"));
} catch (Exception e) {
e.printStackTrace();
System.exit(0);
}
private static Properties createDefaultProperties() {
Properties p = new Properties();
p.setProperty("port", "");
p.setProperty("mysqlUrl", "");
p.setProperty("mysqlUser", "");
p.setProperty("mysqlPasswd", "");
p.setProperty("mongoPort", "");
p.setProperty("mongoUrl", "");
p.setProperty("mongoUser", "");
p.setProperty("mongoPasswd", "");
p.setProperty("mongoDatabase", "");
return p;
}
private static boolean checkProperties(Properties p) {
if (p.getProperty("port") == null ||
p.getProperty("mysqlUrl") == null ||
p.getProperty("mysqlUser") == null ||
p.getProperty("mysqlPasswd") == null ||
p.getProperty("mongoPort") == null ||
p.getProperty("mongoUrl") == null ||
p.getProperty("mongoUser") == null ||
p.getProperty("mongoPasswd") == null ||
p.getProperty("mongoDatabase") == null) {
return false;
}
Integer port = toIntOrNull(p.getProperty("port"));
if (port == null) return false;
if (port <= 1024) return false;
String mysqlUrl = p.getProperty("mysqlUrl");
if (mysqlUrl.isEmpty()) return false;
String mysqlUser = p.getProperty("mysqlUser");
if (mysqlUser.isEmpty()) return false;
String mysqlPasswd = p.getProperty("mysqlPasswd");
if (mysqlPasswd.isEmpty()) return false;
String mongoUrl = p.getProperty("mongoUrl");
if (mongoUrl.isEmpty()) return false;
String mongoUser = p.getProperty("mongoUser");
if (mongoUser.isEmpty()) return false;
String mongoPasswd = p.getProperty("mongoPasswd");
if (mongoPasswd.isEmpty()) return false;
String mongoDatabase = p.getProperty("mongoDatabase");
if (mongoDatabase.isEmpty()) return false;
Integer mongoPort = toIntOrNull(p.getProperty("mongoPort"));
if (mongoPort == null) return false;
if (mongoPort <= 1024) return false;
return true;
}
public static Integer toIntOrNull(String str) {
String trim = str.trim();
if (trim.matches("^-?[0-9]+$")) {
return Integer.valueOf(trim);
}
return null;
}
}