init
This commit is contained in:
@@ -0,0 +1,20 @@
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>org.fengfei</groupId>
|
||||
<artifactId>yxwlkjnwct</artifactId>
|
||||
<version>0.1</version>
|
||||
</parent>
|
||||
<artifactId>proxy-common</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
<name>proxy-common</name>
|
||||
<url>http://maven.apache.org</url>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.google.code.gson</groupId>
|
||||
<artifactId>gson</artifactId>
|
||||
<version>2.7</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
@@ -0,0 +1,120 @@
|
||||
package org.fengfei.lanproxy.common;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
/**
|
||||
* 读取配置文件 默认的config.properties 和自定义都支持
|
||||
*
|
||||
*/
|
||||
public class Config {
|
||||
|
||||
private static final String DEFAULT_CONF = "config.properties";
|
||||
|
||||
private static Map<String, Config> instances = new ConcurrentHashMap<String, Config>();
|
||||
|
||||
private Properties configuration = new Properties();
|
||||
|
||||
private Config() {
|
||||
initConfig(DEFAULT_CONF);
|
||||
}
|
||||
|
||||
private Config(String configFile) {
|
||||
initConfig(configFile);
|
||||
}
|
||||
|
||||
private void initConfig(String configFile) {
|
||||
InputStream is = Config.class.getClassLoader().getResourceAsStream(configFile);
|
||||
try {
|
||||
configuration.load(is);
|
||||
is.close();
|
||||
} catch (IOException ex) {
|
||||
throw new RuntimeException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得Configuration实例。 默认为config.property
|
||||
*
|
||||
* @return Configuration实例
|
||||
*/
|
||||
public static Config getInstance() {
|
||||
return getInstance(DEFAULT_CONF);
|
||||
}
|
||||
|
||||
/**
|
||||
* 自定义文件解析**.property
|
||||
*
|
||||
* @param configFile
|
||||
* @return
|
||||
*/
|
||||
public static Config getInstance(String configFile) {
|
||||
Config config = instances.get(configFile);
|
||||
if (config == null) {
|
||||
synchronized (instances) {
|
||||
config = instances.get(configFile);
|
||||
if (config == null) {
|
||||
config = new Config(configFile);
|
||||
instances.put(configFile, config);
|
||||
}
|
||||
}
|
||||
}
|
||||
return config;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得配置项。
|
||||
*
|
||||
* @param key 配置关键字
|
||||
*
|
||||
* @return 配置项
|
||||
*/
|
||||
public String getStringValue(String key) {
|
||||
return configuration.getProperty(key);
|
||||
}
|
||||
|
||||
public String getStringValue(String key, String defaultValue) {
|
||||
String value = this.getStringValue(key);
|
||||
if (value == null) {
|
||||
return defaultValue;
|
||||
} else {
|
||||
return value;
|
||||
}
|
||||
}
|
||||
|
||||
public int getIntValue(String key, int defaultValue) {
|
||||
return LangUtil.parseInt(configuration.getProperty(key), defaultValue);
|
||||
}
|
||||
|
||||
public int getIntValue(String key) {
|
||||
return LangUtil.parseInt(configuration.getProperty(key));
|
||||
}
|
||||
|
||||
public double getDoubleValue(String key, Double defaultValue) {
|
||||
return LangUtil.parseDouble(configuration.getProperty(key), defaultValue);
|
||||
}
|
||||
|
||||
public double getDoubleValue(String key) {
|
||||
return LangUtil.parseDouble(configuration.getProperty(key));
|
||||
}
|
||||
|
||||
public double getLongValue(String key, Long defaultValue) {
|
||||
return LangUtil.parseLong(configuration.getProperty(key), defaultValue);
|
||||
}
|
||||
|
||||
public double getLongValue(String key) {
|
||||
return LangUtil.parseLong(configuration.getProperty(key));
|
||||
}
|
||||
|
||||
public Boolean getBooleanValue(String key, Boolean defaultValue) {
|
||||
return LangUtil.parseBoolean(configuration.getProperty(key), defaultValue);
|
||||
}
|
||||
|
||||
public Boolean getBooleanValue(String key) {
|
||||
return LangUtil.parseBoolean(configuration.getProperty(key));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package org.fengfei.lanproxy.common;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.reflect.TypeToken;
|
||||
|
||||
/**
|
||||
* JSON与POJO转换工具类.
|
||||
*
|
||||
* @author fengfei
|
||||
*
|
||||
*/
|
||||
public class JsonUtil {
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public static <T> T json2object(String json, TypeToken<T> typeToken) {
|
||||
try {
|
||||
Gson gson = new Gson();
|
||||
return (T) gson.fromJson(json, typeToken.getType());
|
||||
} catch (Exception e) {
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* java对象转为json对象
|
||||
*
|
||||
*/
|
||||
public static String object2json(Object obj) {
|
||||
Gson gson = new Gson();
|
||||
return gson.toJson(obj);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,146 @@
|
||||
package org.fengfei.lanproxy.common;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
*
|
||||
* 类型转换工具类.
|
||||
*
|
||||
* @author fengfei
|
||||
*
|
||||
*/
|
||||
public class LangUtil {
|
||||
|
||||
private static Logger logger = LoggerFactory.getLogger(LangUtil.class);
|
||||
|
||||
public static Boolean parseBoolean(Object value) {
|
||||
if (value != null) {
|
||||
if (value instanceof Boolean) {
|
||||
return (Boolean) value;
|
||||
} else if (value instanceof String) {
|
||||
return Boolean.valueOf((String) value);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static boolean parseBoolean(Object value, boolean defaultValue) {
|
||||
if (value != null) {
|
||||
if (value instanceof Boolean) {
|
||||
return (Boolean) value;
|
||||
} else if (value instanceof String) {
|
||||
try {
|
||||
return Boolean.valueOf((String) value);
|
||||
} catch (Exception e) {
|
||||
logger.warn("parse boolean value({}) failed.", value);
|
||||
}
|
||||
}
|
||||
}
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* @Title: parseInt
|
||||
* @Description: Int解析方法,可传入Integer或String值
|
||||
* @param value Integer或String值
|
||||
* @return Integer 返回类型
|
||||
*/
|
||||
public static Integer parseInt(Object value) {
|
||||
if (value != null) {
|
||||
if (value instanceof Integer) {
|
||||
return (Integer) value;
|
||||
} else if (value instanceof String) {
|
||||
return Integer.valueOf((String) value);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static Integer parseInt(Object value, Integer defaultValue) {
|
||||
if (value != null) {
|
||||
if (value instanceof Integer) {
|
||||
return (Integer) value;
|
||||
} else if (value instanceof String) {
|
||||
try {
|
||||
return Integer.valueOf((String) value);
|
||||
} catch (Exception e) {
|
||||
logger.warn("parse Integer value({}) failed.", value);
|
||||
}
|
||||
}
|
||||
}
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
/***
|
||||
*
|
||||
* @Title: parseLong
|
||||
* @Description: long解析方法,可传入Long或String值
|
||||
* @param value Integer或String值
|
||||
* @param @return
|
||||
* @return Long 返回类型
|
||||
*/
|
||||
public static Long parseLong(Object value) {
|
||||
if (value != null) {
|
||||
if (value instanceof Long) {
|
||||
return (Long) value;
|
||||
} else if (value instanceof String) {
|
||||
return Long.valueOf((String) value);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static Long parseLong(Object value, Long defaultValue) {
|
||||
if (value != null) {
|
||||
if (value instanceof Long) {
|
||||
return (Long) value;
|
||||
} else if (value instanceof String) {
|
||||
try {
|
||||
return Long.valueOf((String) value);
|
||||
} catch (NumberFormatException e) {
|
||||
logger.warn("parse Long value({}) failed.", value);
|
||||
}
|
||||
}
|
||||
}
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* @Title: parseDouble
|
||||
* @Description: Double解析方法,可传入Double或String值
|
||||
* @param value Double或String值
|
||||
* @return Double 返回类型
|
||||
*/
|
||||
public static Double parseDouble(Object value) {
|
||||
if (value != null) {
|
||||
if (value instanceof Double) {
|
||||
return (Double) value;
|
||||
} else if (value instanceof String) {
|
||||
return Double.valueOf((String) value);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @Title: parseDouble
|
||||
* @Description: Double解析方法,可传入Double或String值
|
||||
* @param value Double或String值
|
||||
* @return Double 返回类型
|
||||
*/
|
||||
public static Double parseDouble(Object value, Double defaultValue) {
|
||||
if (value != null) {
|
||||
if (value instanceof Double) {
|
||||
return (Double) value;
|
||||
} else if (value instanceof String) {
|
||||
try {
|
||||
return Double.valueOf((String) value);
|
||||
} catch (NumberFormatException e) {
|
||||
logger.warn("parse Double value({}) failed.", value);
|
||||
}
|
||||
}
|
||||
}
|
||||
return defaultValue;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package org.fengfei.lanproxy.common.container;
|
||||
|
||||
public interface Container {
|
||||
|
||||
void start();
|
||||
|
||||
void stop();
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
package org.fengfei.lanproxy.common.container;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
* 容器启动工具类.
|
||||
*
|
||||
* @author fengfei
|
||||
*
|
||||
*/
|
||||
public class ContainerHelper {
|
||||
|
||||
private static Logger logger = LoggerFactory.getLogger(ContainerHelper.class);
|
||||
|
||||
private static volatile boolean running = true;
|
||||
|
||||
private static List<Container> cachedContainers;
|
||||
|
||||
public static void start(List<Container> containers) {
|
||||
|
||||
cachedContainers = containers;
|
||||
|
||||
// 启动所有容器
|
||||
startContainers();
|
||||
|
||||
Runtime.getRuntime().addShutdownHook(new Thread() {
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
|
||||
synchronized (ContainerHelper.class) {
|
||||
|
||||
// 停止所有容器.
|
||||
stopContainers();
|
||||
running = false;
|
||||
ContainerHelper.class.notify();
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
synchronized (ContainerHelper.class) {
|
||||
while (running) {
|
||||
try {
|
||||
ContainerHelper.class.wait();
|
||||
} catch (Throwable e) {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void startContainers() {
|
||||
for (Container container : cachedContainers) {
|
||||
logger.info("starting container [{}]", container.getClass().getName());
|
||||
container.start();
|
||||
logger.info("container [{}] started", container.getClass().getName());
|
||||
}
|
||||
}
|
||||
|
||||
private static void stopContainers() {
|
||||
for (Container container : cachedContainers) {
|
||||
logger.info("stopping container [{}]", container.getClass().getName());
|
||||
try {
|
||||
container.stop();
|
||||
logger.info("container [{}] stopped", container.getClass().getName());
|
||||
} catch (Exception ex) {
|
||||
logger.warn("container stopped with error", ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
#Generated by Maven
|
||||
#Wed Jul 05 11:03:47 CST 2023
|
||||
groupId=org.fengfei
|
||||
artifactId=proxy-common
|
||||
version=0.1
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
org\fengfei\lanproxy\common\container\Container.class
|
||||
org\fengfei\lanproxy\common\JsonUtil.class
|
||||
org\fengfei\lanproxy\common\LangUtil.class
|
||||
org\fengfei\lanproxy\common\container\ContainerHelper$1.class
|
||||
org\fengfei\lanproxy\common\Config.class
|
||||
org\fengfei\lanproxy\common\container\ContainerHelper.class
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
E:\yx\lanproxy-0.1\proxy-common\src\main\java\org\fengfei\lanproxy\common\LangUtil.java
|
||||
E:\yx\lanproxy-0.1\proxy-common\src\main\java\org\fengfei\lanproxy\common\container\Container.java
|
||||
E:\yx\lanproxy-0.1\proxy-common\src\main\java\org\fengfei\lanproxy\common\container\ContainerHelper.java
|
||||
E:\yx\lanproxy-0.1\proxy-common\src\main\java\org\fengfei\lanproxy\common\Config.java
|
||||
E:\yx\lanproxy-0.1\proxy-common\src\main\java\org\fengfei\lanproxy\common\JsonUtil.java
|
||||
Reference in New Issue
Block a user