diff --git a/pom.xml b/pom.xml index 9459b71..b3e97ff 100644 --- a/pom.xml +++ b/pom.xml @@ -95,7 +95,12 @@ org.apache.commons commons-email - 1.5 + 1.5 + + + org.apache.commons + commons-lang3 + 3.12.0 diff --git a/src/main/java/cc/baka9/catseedlogin/bukkit/database/BufferStatement.java b/src/main/java/cc/baka9/catseedlogin/bukkit/database/BufferStatement.java index fe6c6c9..b6c55e9 100644 --- a/src/main/java/cc/baka9/catseedlogin/bukkit/database/BufferStatement.java +++ b/src/main/java/cc/baka9/catseedlogin/bukkit/database/BufferStatement.java @@ -6,73 +6,30 @@ import java.util.Arrays; public class BufferStatement { - private Object[] values; - private String query; - private Exception stacktrace; + private final Object[] values; + private final String query; + private final Exception stacktrace; - /** - * Represents a PreparedStatement in a state before preparing it (E.g. No - * file I/O Required) - * - * @param query - * The query to execute. E.g. INSERT INTO accounts (user, passwd) - * VALUES (?, ?) - * @param values - * The values to replace ? with in - * query. These are in order. - */ - private static final Exception sharedException = new Exception(); + public BufferStatement(String query, Object... values) { + this.query = query; + this.values = values; + this.stacktrace = new Exception(); + } - public BufferStatement(String query, Object... values) { - this.query = query; - this.values = values; - this.stacktrace = sharedException; // 重复利用一个已存在的 Exception 对象 - this.stacktrace.fillInStackTrace(); - } + public PreparedStatement prepareStatement(Connection con) throws SQLException { + PreparedStatement ps = con.prepareStatement(query); + for (int i = 0; i < values.length; i++) { + ps.setObject(i + 1, values[i]); + } + return ps; + } - /** - * Returns a prepared statement using the given connection. Will try to - * return an empty statement if something went wrong. If that fails, returns - * null. - * - * This method escapes everything automatically. - * - * @param con - * The connection to prepare this on using - * con.prepareStatement(..) - * @return The prepared statement, ready for execution. - */ - public PreparedStatement prepareStatement(Connection con) throws SQLException { - PreparedStatement ps; - int valuesLength = values.length; // 缓存数组长度 - ps = con.prepareStatement(query); - for (int i = 0; i < valuesLength; i++) { - ps.setObject(i + 1, values[i]); - } - return ps; - } + public StackTraceElement[] getStackTrace() { + return stacktrace.getStackTrace(); + } - /** - * Used for debugging. This stacktrace is recorded when the statement is - * created, so printing it to the screen will provide useful debugging - * information about where the query came from, if something went wrong - * while executing it. - * - * @return The stacktrace elements. - */ - public StackTraceElement[] getStackTrace() { - return stacktrace.getStackTrace(); - } - - /** - * @return A string representation of this statement. Returns - * "Query: " + query + ", values: " + - * Arrays.toString(values). - */ - @Override - public String toString() { - StringBuilder sb = new StringBuilder(); - sb.append("Query: ").append(query).append(", values: ").append(Arrays.toString(values)); - return sb.toString(); - } -} \ No newline at end of file + @Override + public String toString() { + return "Query: " + query + ", values: " + Arrays.toString(values); + } +} diff --git a/src/main/java/cc/baka9/catseedlogin/bukkit/database/Cache.java b/src/main/java/cc/baka9/catseedlogin/bukkit/database/Cache.java index 59ec127..6403c80 100644 --- a/src/main/java/cc/baka9/catseedlogin/bukkit/database/Cache.java +++ b/src/main/java/cc/baka9/catseedlogin/bukkit/database/Cache.java @@ -1,27 +1,27 @@ package cc.baka9.catseedlogin.bukkit.database; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + import cc.baka9.catseedlogin.bukkit.CatSeedLogin; import cc.baka9.catseedlogin.bukkit.object.LoginPlayer; -import java.util.*; - public class Cache { - private static final Hashtable PLAYER_HASHTABLE = new Hashtable<>(); + private static final Map PLAYER_HASHTABLE = new ConcurrentHashMap<>(); public static volatile boolean isLoaded = false; public static List getAllLoginPlayer(){ synchronized (PLAYER_HASHTABLE) { return new ArrayList<>(PLAYER_HASHTABLE.values()); } - } public static LoginPlayer getIgnoreCase(String name){ - return PLAYER_HASHTABLE.get(name.toLowerCase()); } - public static void refreshAll(){ isLoaded = false; CatSeedLogin.instance.runTaskAsync(() -> { @@ -45,10 +45,12 @@ public static void refresh(String name){ try { LoginPlayer newLp = CatSeedLogin.sql.get(name); String key = name.toLowerCase(); - if (newLp != null) { - PLAYER_HASHTABLE.put(key, newLp); - } else { - PLAYER_HASHTABLE.remove(key); + synchronized (PLAYER_HASHTABLE) { + if (newLp != null) { + PLAYER_HASHTABLE.put(key, newLp); + } else { + PLAYER_HASHTABLE.remove(key); + } } CatSeedLogin.instance.getLogger().info("缓存加载 " + PLAYER_HASHTABLE.size() + " 个数据"); } catch (Exception e) { @@ -57,4 +59,4 @@ public static void refresh(String name){ } }); } -} \ No newline at end of file +} diff --git a/src/main/java/cc/baka9/catseedlogin/util/Util.java b/src/main/java/cc/baka9/catseedlogin/util/Util.java index ebeae8b..55f1771 100644 --- a/src/main/java/cc/baka9/catseedlogin/util/Util.java +++ b/src/main/java/cc/baka9/catseedlogin/util/Util.java @@ -6,6 +6,8 @@ import java.util.Random; import java.util.regex.Pattern; +import org.apache.commons.lang3.RandomStringUtils; + public class Util { private static final Pattern passwordDifficultyRegex = Pattern.compile("^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,16}$"); private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); @@ -26,19 +28,11 @@ public static boolean checkMail(String e_mail) { } public static String randomStr() { - int leftLimit = 97; // letter 'a' - int rightLimit = 122; // letter 'z' - int targetStringLength = 10; - StringBuilder buffer = new StringBuilder(targetStringLength); - for (int i = 0; i < targetStringLength; i++) { - int randomLimitedInt = leftLimit + random.nextInt(rightLimit - leftLimit + 1); - buffer.append((char) randomLimitedInt); - } - return buffer.toString(); + return RandomStringUtils.randomAlphanumeric(10); } public static boolean isOSLinux() { String os = System.getProperty("os.name"); return os != null && os.toLowerCase().contains("linux"); } -} \ No newline at end of file +}