hainan
parent
85a8c09b24
commit
6717eda5cc
|
|
@ -9,10 +9,14 @@ import com.mongodb.MongoCredential;
|
||||||
import com.mongodb.ServerAddress;
|
import com.mongodb.ServerAddress;
|
||||||
import com.mongodb.client.MongoDatabase;
|
import com.mongodb.client.MongoDatabase;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileInputStream;
|
||||||
|
import java.io.IOException;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Properties;
|
import java.util.Properties;
|
||||||
|
|
||||||
import static cn.cloudowr.dict.Main.*;
|
import static cn.cloudowr.dict.Main.*;
|
||||||
|
import static java.lang.System.exit;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Created by lyf66 on 2017/2/16.
|
* Created by lyf66 on 2017/2/16.
|
||||||
|
|
@ -54,9 +58,17 @@ public class Config extends com.jfinal.config.JFinalConfig{
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void afterJFinalStart() {
|
public void afterJFinalStart() {
|
||||||
MongoCredential mongoCredential = MongoCredential.createCredential(username, database, password.toCharArray());
|
Properties p = new Properties();
|
||||||
MongoClient mongoClient = new MongoClient(new ServerAddress(host, port), Arrays.asList(mongoCredential));
|
try {
|
||||||
mongoDatabase = mongoClient.getDatabase(dabaseName);
|
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() {
|
public static MongoDatabase getMongoDataBase() {
|
||||||
|
|
|
||||||
|
|
@ -10,50 +10,112 @@ import java.io.*;
|
||||||
import java.util.EnumSet;
|
import java.util.EnumSet;
|
||||||
import java.util.Properties;
|
import java.util.Properties;
|
||||||
|
|
||||||
|
import static java.lang.System.exit;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Created by lyf66 on 2017/2/16.
|
* Created by lyf66 on 2017/2/16.
|
||||||
*/
|
*/
|
||||||
public class Main {
|
public class Main {
|
||||||
public static String username;
|
public static String configFilename = "usercenter-config.properties";
|
||||||
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 void main(String[] args) throws IOException {
|
public static void main(String[] args) throws Exception {
|
||||||
read();
|
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,
|
EnumSet<DispatcherType> all = EnumSet.of(DispatcherType.ASYNC, DispatcherType.ERROR,
|
||||||
DispatcherType.FORWARD, DispatcherType.INCLUDE, DispatcherType.REQUEST);
|
DispatcherType.FORWARD, DispatcherType.INCLUDE, DispatcherType.REQUEST);
|
||||||
final Server server = new Server(AppPort);
|
final Server server = new Server(toIntOrNull(p.getProperty("port")));
|
||||||
try {
|
|
||||||
WebAppContext context = new WebAppContext("/", "/");
|
WebAppContext context = new WebAppContext("/", "/");
|
||||||
FilterHolder filter = new FilterHolder(new JFinalFilter());
|
FilterHolder filter = new FilterHolder(new JFinalFilter());
|
||||||
filter.setInitParameter("configClass", "cn.cloudowr.dict.Config");
|
filter.setInitParameter("configClass", "cn.cloudowr.usercenter.Config");
|
||||||
context.addFilter(filter, "/*", all);
|
context.addFilter(filter, "/*", all);
|
||||||
server.setHandler(context);
|
server.setHandler(context);
|
||||||
server.start();
|
server.start();
|
||||||
server.join();
|
server.join();
|
||||||
} catch (Exception e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void read() {
|
private static Properties createDefaultProperties() {
|
||||||
try {
|
Properties p = new Properties();
|
||||||
Properties pro = new Properties();
|
p.setProperty("port", "");
|
||||||
pro.load(Config.class.getResourceAsStream("/config.properties"));
|
p.setProperty("mysqlUrl", "");
|
||||||
username = pro.getProperty("username");
|
p.setProperty("mysqlUser", "");
|
||||||
database = pro.getProperty("database");
|
p.setProperty("mysqlPasswd", "");
|
||||||
password = pro.getProperty("password");
|
p.setProperty("mongoPort", "");
|
||||||
host = pro.getProperty("host");
|
p.setProperty("mongoUrl", "");
|
||||||
port = Integer.parseInt(pro.getProperty("port"));
|
p.setProperty("mongoUser", "");
|
||||||
dabaseName = pro.getProperty("dabaseName");
|
p.setProperty("mongoPasswd", "");
|
||||||
AppPort = Integer.parseInt(pro.getProperty("AppPort"));
|
p.setProperty("mongoDatabase", "");
|
||||||
} catch (Exception e) {
|
return p;
|
||||||
e.printStackTrace();
|
|
||||||
System.exit(0);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue