Compare commits

...

4 Commits

Author SHA1 Message Date
lyf666 17e38fb717 . 2017-11-25 15:13:06 +08:00
lyf666 6717eda5cc . 2017-11-25 15:12:38 +08:00
lyf666 85a8c09b24 修改pom, 修改服务器启动方式,改为代码而不是web.xml 2017-11-23 20:14:34 +08:00
author cf9207aaae 初始化提交 2017-11-23 15:59:10 +08:00
5 changed files with 179 additions and 39 deletions

37
pom.xml
View File

@ -7,8 +7,13 @@
<groupId>cn.cloudowr</groupId>
<artifactId>dict</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<packaging>jar</packaging>
<build>
<resources>
<resource>
<directory>${basedir}/src/main/webapp</directory>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
@ -53,6 +58,19 @@
<mainClass>cn.cloudowr.dict.Main</mainClass>
</configuration>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>cn.cloudowr.dict.Main</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
@ -62,10 +80,21 @@
<version>3.0</version>
</dependency>
<dependency>
<groupId>com.jfinal</groupId>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
<version>8.1.8</version>
<version>9.4.1.v20170120</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-servlet</artifactId>
<version>9.4.1.v20170120</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-webapp</artifactId>
<version>9.4.1.v20170120</version>
</dependency>
<!--<dependency>-->
<!--<groupId>mysql</groupId>-->
<!--<artifactId>mysql-connector-java</artifactId>-->
@ -85,8 +114,6 @@
<groupId>cn.cloudowr</groupId>
<artifactId>sdk</artifactId>
<version>1.4.16</version>
<scope>system</scope>
<systemPath>${basedir}/lib/sdk-1.4.16.jar</systemPath>
</dependency>
</dependencies>
</project>

View File

@ -11,21 +11,20 @@ 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.
*/
public class Config extends com.jfinal.config.JFinalConfig{
private static MongoDatabase mongoDatabase;
private static String username;
private static String database;
private static String password;
private static String host;
private static int port;
private static String dabaseName;
@Override
public void configConstant(Constants me) {
@ -59,28 +58,21 @@ public class Config extends com.jfinal.config.JFinalConfig{
@Override
public void afterJFinalStart() {
read();
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() {
return mongoDatabase;
}
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");
} catch (Exception e) {
e.printStackTrace();
System.exit(0);
}
}
}

View File

@ -1,12 +1,121 @@
package cn.cloudowr.dict;
import com.jfinal.core.JFinal;
import com.jfinal.core.JFinalFilter;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.FilterHolder;
import org.eclipse.jetty.webapp.WebAppContext;
import javax.servlet.DispatcherType;
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 void main(String[] args) {
JFinal.start("src/main/webapp", 4200, "/");
public static String configFilename = "usercenter-config.properties";
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(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();
}
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;
}
}

View File

@ -1,6 +0,0 @@
username=root
database=admin
password=CoWR1111
host=120.24.5.249
port=3717
dabaseName=dict

18
webDir/WEB-INF/web.xml Normal file
View File

@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<filter>
<filter-name>cn.cloudowr.dict</filter-name>
<filter-class>com.jfinal.core.JFinalFilter</filter-class>
<init-param>
<param-name>configClass</param-name>
<param-value>cn.cloudowr.dict.Config</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>cn.cloudowr.dict</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>