Selaa lähdekoodia

update to v4.1.0-RELEASE

deason 3 vuotta sitten
vanhempi
commit
85000a8f11

+ 1 - 1
src/main/java/cn/com/qmth/dp/examcloud/oe/modules/get_continued_count/GetStudentContinutedCountService.java

@@ -1,7 +1,7 @@
 package cn.com.qmth.dp.examcloud.oe.modules.get_continued_count;
 
 import cn.com.qmth.examcloud.commons.helpers.poi.ExcelWriter;
-import cn.com.qmth.examcloud.commons.util.PropertiesUtil;
+import cn.com.qmth.dp.examcloud.oe.util.PropertiesUtil;
 import cn.com.qmth.examcloud.support.examing.ExamRecordData;
 import cn.com.qmth.examcloud.web.redis.RedisClient;
 import com.google.common.collect.Lists;

+ 1 - 1
src/main/java/cn/com/qmth/dp/examcloud/oe/modules/import_data/ImportData.java

@@ -9,6 +9,7 @@ import java.sql.Connection;
 import java.sql.PreparedStatement;
 import java.sql.SQLException;
 
+import cn.com.qmth.dp.examcloud.oe.util.DBUtil;
 import org.apache.commons.io.FileUtils;
 import org.apache.commons.io.IOUtils;
 import org.springframework.stereotype.Component;
@@ -19,7 +20,6 @@ import com.google.gson.JsonParser;
 import cn.com.qmth.examcloud.commons.helpers.Counter;
 import cn.com.qmth.examcloud.commons.logging.ExamCloudLog;
 import cn.com.qmth.examcloud.commons.logging.ExamCloudLogFactory;
-import cn.com.qmth.examcloud.commons.util.DBUtil;
 import cn.com.qmth.examcloud.commons.util.DateUtil;
 import cn.com.qmth.examcloud.commons.util.DateUtil.DatePatterns;
 import cn.com.qmth.examcloud.commons.util.PathUtil;

+ 1 - 1
src/main/java/cn/com/qmth/dp/examcloud/oe/modules/import_paper_dzkd/ImportPaperDzkdService.java

@@ -6,7 +6,7 @@ import cn.com.qmth.dp.examcloud.oe.enums.question.PaperType;
 import cn.com.qmth.dp.examcloud.oe.enums.question.QuesStructType;
 import cn.com.qmth.dp.examcloud.oe.util.FileUtil;
 import cn.com.qmth.examcloud.commons.exception.StatusException;
-import cn.com.qmth.examcloud.commons.util.PropertiesUtil;
+import cn.com.qmth.dp.examcloud.oe.util.PropertiesUtil;
 import com.alibaba.fastjson.JSONObject;
 import org.apache.commons.lang3.StringUtils;
 import org.springframework.beans.BeanUtils;

+ 1 - 1
src/main/java/cn/com/qmth/dp/examcloud/oe/modules/import_paper_gkd/ImportPaperGkdService.java

@@ -7,6 +7,7 @@ import java.util.Date;
 import java.util.List;
 import java.util.UUID;
 
+import cn.com.qmth.dp.examcloud.oe.util.PropertiesUtil;
 import org.apache.commons.lang3.StringUtils;
 import org.apache.poi.xssf.usermodel.XSSFRow;
 import org.apache.poi.xssf.usermodel.XSSFSheet;
@@ -35,7 +36,6 @@ import cn.com.qmth.dp.examcloud.oe.enums.question.PaperType;
 import cn.com.qmth.dp.examcloud.oe.enums.question.QuesStructType;
 import cn.com.qmth.dp.examcloud.oe.util.FileUtil;
 import cn.com.qmth.examcloud.commons.exception.StatusException;
-import cn.com.qmth.examcloud.commons.util.PropertiesUtil;
 
 /**
  * 广开学试卷导入

+ 1 - 1
src/main/java/cn/com/qmth/dp/examcloud/oe/modules/update_correct_answer/FixCorrectAnswerAndResetScoreService.java

@@ -6,7 +6,7 @@ import cn.com.qmth.dp.examcloud.oe.modules.update_correct_answer.util.QmthUtil;
 import cn.com.qmth.examcloud.commons.exception.StatusException;
 import cn.com.qmth.examcloud.commons.util.HttpMethod;
 import cn.com.qmth.examcloud.commons.util.JsonUtil;
-import cn.com.qmth.examcloud.commons.util.PropertiesUtil;
+import cn.com.qmth.dp.examcloud.oe.util.PropertiesUtil;
 import com.mongodb.client.result.UpdateResult;
 import com.mysql.cj.util.StringUtils;
 import okhttp3.Response;

+ 1 - 1
src/main/java/cn/com/qmth/dp/examcloud/oe/modules/update_correct_answer/ResetScoreService.java

@@ -3,7 +3,7 @@ package cn.com.qmth.dp.examcloud.oe.modules.update_correct_answer;
 import cn.com.qmth.dp.examcloud.oe.modules.export_exam_student_score.vo.ExamStudentVO;
 import cn.com.qmth.dp.examcloud.oe.modules.export_exam_student_score.vo.ScoreVO;
 import cn.com.qmth.dp.examcloud.oe.modules.update_correct_answer.entity.ExamRecordDataEntity;
-import cn.com.qmth.examcloud.commons.util.PropertiesUtil;
+import cn.com.qmth.dp.examcloud.oe.util.PropertiesUtil;
 import org.apache.commons.collections.CollectionUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.dao.EmptyResultDataAccessException;

+ 1 - 1
src/main/java/cn/com/qmth/dp/examcloud/oe/modules/update_correct_answer/util/QmthUtil.java

@@ -1,7 +1,7 @@
 package cn.com.qmth.dp.examcloud.oe.modules.update_correct_answer.util;
 
+import cn.com.qmth.dp.examcloud.oe.util.PropertiesUtil;
 import cn.com.qmth.examcloud.commons.util.ByteUtil;
-import cn.com.qmth.examcloud.commons.util.PropertiesUtil;
 import cn.com.qmth.examcloud.commons.util.SHA256;
 
 import java.io.UnsupportedEncodingException;

+ 1 - 1
src/main/java/cn/com/qmth/dp/examcloud/oe/modules/update_question_score/UpdateQuestionScoreService.java

@@ -7,7 +7,7 @@ import cn.com.qmth.dp.examcloud.oe.modules.update_question_score.entity.*;
 import cn.com.qmth.examcloud.commons.exception.StatusException;
 import cn.com.qmth.examcloud.commons.util.HttpMethod;
 import cn.com.qmth.examcloud.commons.util.JsonUtil;
-import cn.com.qmth.examcloud.commons.util.PropertiesUtil;
+import cn.com.qmth.dp.examcloud.oe.util.PropertiesUtil;
 import com.mongodb.client.result.UpdateResult;
 import com.mysql.cj.util.StringUtils;
 import okhttp3.Response;

+ 263 - 0
src/main/java/cn/com/qmth/dp/examcloud/oe/util/DBUtil.java

@@ -0,0 +1,263 @@
+package cn.com.qmth.dp.examcloud.oe.util;
+
+import cn.com.qmth.examcloud.commons.exception.ExamCloudRuntimeException;
+import cn.com.qmth.examcloud.commons.logging.ExamCloudLog;
+import cn.com.qmth.examcloud.commons.logging.ExamCloudLogFactory;
+import org.apache.commons.lang3.StringUtils;
+
+import javax.sql.DataSource;
+import java.sql.Connection;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.sql.Statement;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+
+/**
+ * 类注释
+ *
+ * @author WANGWEI
+ */
+public class DBUtil {
+
+    /**
+     * 属性注释
+     */
+    private static final ExamCloudLog LOG = ExamCloudLogFactory.getLog(DBUtil.class);
+
+    /**
+     * 属性注释
+     */
+    private static final Object LOCK = new Object();
+
+    /**
+     * 属性注释
+     */
+    private static Map<String, DataSource> dataSources = new ConcurrentHashMap<String, DataSource>();
+
+    /**
+     * 初始化数据源(C3P0)
+     *
+     * @param dataSourceName
+     * @return
+     * @author WANGWEI
+     */
+    public static DataSource initDataSource(final String dataSourceName) {
+        Class<?> c = null;
+        try {
+            c = Class.forName("com.alibaba.druid.pool.DruidDataSource");
+        } catch (ClassNotFoundException e) {
+            // ignore
+        }
+
+        if (null == c) {
+            try {
+                c = Class.forName("com.mchange.v2.c3p0.ComboPooledDataSource");
+            } catch (ClassNotFoundException e) {
+                // ignore
+            }
+        }
+
+        if (null == c) {
+            throw new ExamCloudRuntimeException("no class about dataSource implementation.");
+        }
+
+        @SuppressWarnings("unchecked")
+        Class<? extends DataSource> z = (Class<? extends DataSource>) c;
+
+        return initDataSource(z, dataSourceName);
+    }
+
+    /**
+     * 初始化数据源
+     *
+     * @param implementation 数据源实现类
+     * @param dataSourceName 数据源名称
+     * @return
+     * @author WANGWEI
+     */
+    public static DataSource initDataSource(Class<? extends DataSource> implementation,
+                                            final String dataSourceName) {
+
+        String dbDriver = PropertiesUtil.getString(dataSourceName + ".driver");
+        String url = PropertiesUtil.getString(dataSourceName + ".url");
+        String userName = PropertiesUtil.getString(dataSourceName + ".username");
+        String password = PropertiesUtil.getString(dataSourceName + ".password");
+        int initialPoolSize = PropertiesUtil.getInt(dataSourceName + ".initialPoolSize", 1);
+        int minPoolSize = PropertiesUtil.getInt(dataSourceName + ".minPoolSize", 1);
+        int maxPoolSize = PropertiesUtil.getInt(dataSourceName + ".maxPoolSize", 10);
+
+        DataSource dataSource = null;
+        String simpleName = implementation.getSimpleName();
+        try {
+            // C3P0
+            if (simpleName.equals("ComboPooledDataSource")) {
+                DataSource c3po = implementation.newInstance();
+                dataSource = c3po;
+                implementation.getMethod("setDriverClass", String.class).invoke(c3po, dbDriver);
+                implementation.getMethod("setJdbcUrl", String.class).invoke(c3po, url);
+                implementation.getMethod("setUser", String.class).invoke(c3po, userName);
+                implementation.getMethod("setPassword", String.class).invoke(c3po, password);
+                implementation.getMethod("setInitialPoolSize", int.class).invoke(c3po,
+                        initialPoolSize);
+                implementation.getMethod("setMinPoolSize", int.class).invoke(c3po, minPoolSize);
+                implementation.getMethod("setMaxPoolSize", int.class).invoke(c3po, maxPoolSize);
+                implementation.getMethod("setPreferredTestQuery", String.class).invoke(c3po,
+                        "select 1 from dual");
+            }
+            // druid
+            else if (simpleName.equals("DruidDataSource")) {
+                DataSource druid = implementation.newInstance();
+                dataSource = druid;
+                implementation.getMethod("setDriverClassName", String.class).invoke(druid,
+                        dbDriver);
+                implementation.getMethod("setUrl", String.class).invoke(druid, url);
+                implementation.getMethod("setUsername", String.class).invoke(druid, userName);
+                implementation.getMethod("setPassword", String.class).invoke(druid, password);
+                implementation.getMethod("setInitialSize", int.class).invoke(druid,
+                        initialPoolSize);
+                implementation.getMethod("setMaxActive", int.class).invoke(druid, maxPoolSize);
+
+                implementation.getMethod("setValidationQuery", String.class).invoke(druid,
+                        "select 1 from dual");
+            }
+
+        } catch (Exception e) {
+            LOG.error("[JDBC] Fail to init dataSource. dataSourceName=" + dataSourceName, e);
+            throw new RuntimeException(e);
+        }
+
+        Connection conn = null;
+        try {
+            conn = dataSource.getConnection();
+        } catch (Exception e) {
+            LOG.error("[JDBC] Fail to get connection. dataSourceName=" + dataSourceName, e);
+            throw new RuntimeException(e);
+        } finally {
+            close(conn);
+        }
+
+        return dataSource;
+    }
+
+    /**
+     * 方法注释
+     *
+     * @param dataSourceName
+     * @return
+     * @author WANGWEI
+     */
+    public static DataSource getDataSource(String dataSourceName) {
+        DataSource dataSource = dataSources.get(dataSourceName);
+        if (null == dataSource) {
+            synchronized (LOCK) {
+                dataSource = dataSources.get(dataSourceName);
+                if (null == dataSource) {
+                    dataSource = initDataSource(dataSourceName);
+                    dataSources.put(dataSourceName, dataSource);
+                }
+            }
+        }
+        return dataSource;
+    }
+
+    /**
+     * 方法注释
+     *
+     * @param dataSourceName
+     * @return
+     * @author WANGWEI
+     */
+    public static Connection getConnection(String dataSourceName) {
+        Connection conn = null;
+        try {
+            conn = getDataSource(dataSourceName).getConnection();
+        } catch (SQLException e) {
+            LOG.error("[JDBC] Fail to get connection.", e);
+        }
+
+        return conn;
+    }
+
+    /**
+     * @param conn
+     */
+    public static void close(Connection conn, Statement statement, ResultSet rs) {
+        close(rs);
+        close(conn, statement);
+    }
+
+    /**
+     * @param conn
+     * @param statement
+     */
+    public static void close(Connection conn, Statement statement) {
+        close(statement);
+        close(conn);
+    }
+
+    /**
+     *
+     */
+    public static void close(Statement statement, ResultSet rs) {
+        close(statement);
+        close(rs);
+    }
+
+    /**
+     * @param conn
+     */
+    public static void close(Connection conn) {
+        if (conn != null) {
+            try {
+                conn.close();
+            } catch (SQLException e) {
+                LOG.error("[JDBC] Fail to close Connection.", e);
+            }
+        }
+    }
+
+    /**
+     * @param statement
+     */
+    public static void close(Statement statement) {
+        if (statement != null) {
+            try {
+                statement.close();
+            } catch (SQLException e) {
+                LOG.error("[JDBC] Fail to close Statement.", e);
+            }
+        }
+    }
+
+    /**
+     * @param rs
+     */
+    public static void close(ResultSet rs) {
+        if (rs != null) {
+            try {
+                rs.close();
+            } catch (SQLException e) {
+                LOG.error("[JDBC] Fail to close ResultSet.", e);
+            }
+        }
+    }
+
+    /**
+     * 转换为数据库模糊查询匹配模式
+     *
+     * @param column
+     * @return
+     * @author WANGWEI
+     */
+    public static String toSqlSearchPattern(String column) {
+        if (StringUtils.isBlank(column)) {
+            return "%";
+        } else {
+            column = column.trim().replaceAll("\\s", "%");
+            column = "%" + column + "%";
+            return column;
+        }
+    }
+
+}

+ 239 - 0
src/main/java/cn/com/qmth/dp/examcloud/oe/util/PropertiesUtil.java

@@ -0,0 +1,239 @@
+package cn.com.qmth.dp.examcloud.oe.util;
+
+import cn.com.qmth.examcloud.commons.exception.ExamCloudRuntimeException;
+import cn.com.qmth.examcloud.commons.logging.ExamCloudLog;
+import cn.com.qmth.examcloud.commons.logging.ExamCloudLogFactory;
+import cn.com.qmth.examcloud.commons.util.PathUtil;
+import com.google.common.collect.Sets;
+import org.apache.commons.io.IOUtils;
+import org.apache.commons.lang.StringUtils;
+
+import java.io.*;
+import java.util.Map.Entry;
+import java.util.Properties;
+import java.util.Set;
+
+/**
+ * 类注释
+ *
+ * @author WANGWEI
+ * @date 2018年10月8日
+ * @Copyright (c) 2018-2020 http://www.qmth.com.cn/ All Rights Reserved.
+ */
+public class PropertiesUtil {
+
+    private static final ExamCloudLog LOG = ExamCloudLogFactory.getLog(PropertiesUtil.class);
+
+    private static final Properties PROPS = new Properties();
+
+    private static final Set<String> PROP_FILES = Sets.newConcurrentHashSet();
+
+    /**
+     * 构造函数
+     */
+    private PropertiesUtil() {
+    }
+
+    /**
+     * 设置属性
+     *
+     * @param key
+     * @param value
+     * @author WANGWEI
+     */
+    public static void setProperty(String key, String value) {
+        PROPS.setProperty(key, value);
+    }
+
+    /**
+     * 加载配置文件
+     *
+     * @param resourceName
+     * @author WANGWEI
+     */
+    public static void loadFromResource(String resourceName) {
+        loadFromPath(PathUtil.getResoucePath(resourceName));
+    }
+
+    /**
+     * 加载配置文件
+     *
+     * @param path
+     * @author WANGWEI
+     */
+    public static synchronized void loadFromPath(String path) {
+        if (StringUtils.isBlank(path)) {
+            return;
+        }
+        try {
+            File file = new File(path);
+
+            if (PROP_FILES.contains(file.getCanonicalPath())) {
+                return;
+            }
+
+            loadFromFile(file, PROPS);
+
+            PROP_FILES.add(file.getCanonicalPath());
+        } catch (Exception e) {
+            LOG.error("Fail to load and watch file [" + path + "].", e);
+        }
+    }
+
+    /**
+     * @param resourceName
+     * @param props
+     */
+    public static void loadFromResource(String resourceName, Properties props) {
+        if (null != PropertiesUtil.class.getClassLoader()) {
+            InputStream in = PropertiesUtil.class.getClassLoader()
+                    .getResourceAsStream(resourceName);
+
+            loadFromStream(in, props);
+        } else {
+            throw new ExamCloudRuntimeException("fail to get class loader");
+        }
+    }
+
+    /**
+     * @param file
+     * @param props
+     * @throws IOException
+     */
+    public static void loadFromFile(File file, Properties props) {
+        if (null == file) {
+            LOG.error("file is null.");
+            return;
+        }
+        if (!file.isFile()) {
+            LOG.error("file is not a normal file.");
+            return;
+        }
+
+        String path = PathUtil.getCanonicalPath(file);
+        LOG.info("Loading properties from file [" + path + "]");
+
+        try {
+            loadFromStream(new FileInputStream(file), props);
+        } catch (Exception e) {
+            throw new ExamCloudRuntimeException(e);
+        }
+    }
+
+    /**
+     * @param is
+     * @param props
+     */
+    public static void loadFromStream(InputStream is, Properties props) {
+        BufferedReader reader = null;
+        try {
+            reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
+            Properties temp = new Properties();
+            temp.load(reader);
+            for (Entry<Object, Object> entry : temp.entrySet()) {
+                Object key = entry.getKey();
+                Object value = entry.getValue();
+                if (key instanceof String) {
+                    key = ((String) key).trim();
+                }
+                if (value instanceof String) {
+                    value = ((String) value).trim();
+                }
+                props.put(key, value);
+            }
+
+        } catch (IOException e) {
+            throw new ExamCloudRuntimeException(e);
+        } finally {
+            IOUtils.closeQuietly(reader);
+            IOUtils.closeQuietly(is);
+        }
+    }
+
+    /**
+     * @param key
+     * @return
+     */
+    public static String getString(String key) {
+        String value = PROPS.getProperty(key);
+        if (StringUtils.isNotBlank(value)) {
+            return value.trim();
+        } else {
+            if (LOG.isDebugEnabled()) {
+                LOG.debug("No property value, key = " + key);
+            }
+            return null;
+        }
+    }
+
+    /**
+     * @param key
+     * @param defaultValue
+     * @return
+     */
+    public static String getString(String key, String defaultValue) {
+        String value = getString(key);
+        if (null != value) {
+            return value;
+        }
+        return defaultValue;
+    }
+
+    /**
+     * @param key
+     * @param defaultValue
+     * @return
+     */
+    public static int getInt(String key, int defaultValue) {
+        String value = getString(key);
+        if (null != value) {
+            try {
+                return Integer.parseInt(value);
+            } catch (NumberFormatException e) {
+                PROPS.setProperty(key, String.valueOf(defaultValue));
+                return defaultValue;
+            }
+        }
+        return defaultValue;
+    }
+
+    /**
+     * @param key
+     * @param defaultValue
+     * @return
+     */
+    public static long getLong(String key, long defaultValue) {
+        String value = getString(key);
+        if (null != value) {
+            try {
+                return Long.parseLong(value);
+            } catch (NumberFormatException e) {
+                return defaultValue;
+            }
+        }
+        return defaultValue;
+    }
+
+    /**
+     * 获取boolean
+     *
+     * @param key
+     * @param defaultVale
+     * @return
+     * @author WANGWEI
+     */
+    public static boolean getBoolean(String key, boolean defaultVale) {
+        String value = getString(key);
+        if (null == value) {
+            return defaultVale;
+        }
+        if (value.equals("true")) {
+            return true;
+        } else if (value.equals("false")) {
+            return false;
+        } else {
+            return defaultVale;
+        }
+    }
+
+}