Add 'exists' path check
This commit is contained in:
parent
3800678138
commit
56a2d7892e
7 changed files with 72 additions and 53 deletions
|
|
@ -13,6 +13,7 @@ import org.zhdev.varioutil.bukkit.command.PreparedPluginCommand;
|
||||||
import org.zhdev.varioutil.config.BukkitYamlConfig;
|
import org.zhdev.varioutil.config.BukkitYamlConfig;
|
||||||
import org.zhdev.varioutil.config.Config;
|
import org.zhdev.varioutil.config.Config;
|
||||||
import org.zhdev.varioutil.config.ConfigSection;
|
import org.zhdev.varioutil.config.ConfigSection;
|
||||||
|
import org.zhdev.varioutil.config.YamlConfig;
|
||||||
import org.zhdev.varioutil.language.Language;
|
import org.zhdev.varioutil.language.Language;
|
||||||
import org.zhdev.varioutil.sql.SqlAdapter;
|
import org.zhdev.varioutil.sql.SqlAdapter;
|
||||||
import org.zhdev.varioutil.util.BukkitUtils;
|
import org.zhdev.varioutil.util.BukkitUtils;
|
||||||
|
|
@ -124,10 +125,10 @@ public abstract class BukkitPreparedPlugin extends BukkitPlugin implements Liste
|
||||||
protected void loadPhrases() {
|
protected void loadPhrases() {
|
||||||
ConfigSection languageSection = defaultConfig.getOrCreateSection("language");
|
ConfigSection languageSection = defaultConfig.getOrCreateSection("language");
|
||||||
String locale = languageSection.getString("locale", "default");
|
String locale = languageSection.getString("locale", "default");
|
||||||
Config languageConfig = new BukkitYamlConfig("language/" + locale + ".yml");
|
Config languageConfig = new YamlConfig("language/" + locale + ".yml");
|
||||||
loadConfig(languageConfig, "language.yml");
|
loadConfig(languageConfig, "language.yml", true);
|
||||||
loadConfig(languageConfig, "language/default.yml");
|
loadConfig(languageConfig, "language/default.yml", true);
|
||||||
loadConfig(languageConfig);
|
loadConfig(languageConfig, true);
|
||||||
ConfigUtils.addPhrases(language, languageConfig, ColorUtils::translateAlternateColorCodes);
|
ConfigUtils.addPhrases(language, languageConfig, ColorUtils::translateAlternateColorCodes);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -26,41 +26,44 @@ public class ResourceUtils {
|
||||||
return getResource(path, ResourceUtils.class.getClassLoader());
|
return getResource(path, ResourceUtils.class.getClassLoader());
|
||||||
}
|
}
|
||||||
|
|
||||||
public static boolean saveResource(String resourcePath, Path outPath, ClassLoader loader) throws IllegalStateException {
|
public static boolean saveResource(InputStream inputStream, Path outPath) throws IOException {
|
||||||
if (resourcePath == null || resourcePath.equals("")) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
resourcePath = resourcePath.replace('\\', '/');
|
|
||||||
InputStream in = getResource(resourcePath, loader);
|
|
||||||
if (in == null) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
Path parent = outPath.getParent();
|
Path parent = outPath.getParent();
|
||||||
if (parent != null && Files.notExists(parent)) {
|
if (parent != null && Files.notExists(parent)) {
|
||||||
Files.createDirectories(parent);
|
Files.createDirectories(parent);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (Files.notExists(outPath) || Files.size(outPath) == 0) {
|
if (Files.notExists(outPath) || Files.size(outPath) == 0) {
|
||||||
OutputStream stream = Files.newOutputStream(outPath);
|
OutputStream outputStream = Files.newOutputStream(outPath);
|
||||||
byte[] buf = new byte[1024];
|
byte[] buf = new byte[1024];
|
||||||
int len;
|
int len;
|
||||||
while ((len = in.read(buf)) > 0) {
|
while ((len = inputStream.read(buf)) > 0) {
|
||||||
stream.write(buf, 0, len);
|
outputStream.write(buf, 0, len);
|
||||||
}
|
}
|
||||||
stream.close();
|
outputStream.close();
|
||||||
in.close();
|
inputStream.close();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
} catch (IOException e) {
|
|
||||||
throw new IllegalStateException("Could not save resource " + resourcePath + " to " + outPath, e);
|
|
||||||
}
|
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static boolean saveResource(String resourcePath, Path outPath, ClassLoader loader) throws IllegalStateException {
|
||||||
|
if (resourcePath == null || resourcePath.isEmpty()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
resourcePath = resourcePath.replace('\\', '/');
|
||||||
|
InputStream inputStream = getResource(resourcePath, loader);
|
||||||
|
if (inputStream == null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
return saveResource(inputStream, outPath);
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new IllegalStateException("Could not save resource " + resourcePath + " to " + outPath, e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public static boolean saveResource(String resourcePath, Path outPath) throws IllegalStateException {
|
public static boolean saveResource(String resourcePath, Path outPath) throws IllegalStateException {
|
||||||
return saveResource(resourcePath, outPath, ResourceUtils.class.getClassLoader());
|
return saveResource(resourcePath, outPath, ResourceUtils.class.getClassLoader());
|
||||||
|
|
|
||||||
|
|
@ -9,7 +9,6 @@ class NullProvider implements ConnectionProvider {
|
||||||
this.message = message;
|
this.message = message;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Connection getConnection() {
|
public Connection getConnection() {
|
||||||
throw new SqlException(message);
|
throw new SqlException(message);
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,9 @@ package org.zhdev.varioutil.util;
|
||||||
import org.zhdev.varioutil.sql.SqlException;
|
import org.zhdev.varioutil.sql.SqlException;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.io.UnsupportedEncodingException;
|
||||||
|
import java.net.URLEncoder;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.nio.file.Paths;
|
import java.nio.file.Paths;
|
||||||
|
|
@ -15,13 +18,17 @@ public class SqlUtils {
|
||||||
private static boolean h2;
|
private static boolean h2;
|
||||||
private static boolean mysql;
|
private static boolean mysql;
|
||||||
|
|
||||||
|
private static String encodeUrlValue(String value) throws UnsupportedEncodingException {
|
||||||
|
return URLEncoder.encode(value, StandardCharsets.UTF_8.toString());
|
||||||
|
}
|
||||||
|
|
||||||
public static Connection createMysqlConnection(String address, String dbname, String username, String password, boolean ssl) throws SqlException {
|
public static Connection createMysqlConnection(String address, String dbname, String username, String password, boolean ssl) throws SqlException {
|
||||||
try {
|
try {
|
||||||
if (!address.contains(":")) {
|
if (!address.contains(":")) {
|
||||||
address = address + ":3306";
|
address = address + ":3306";
|
||||||
}
|
}
|
||||||
return DriverManager.getConnection("jdbc:mysql://" + address + '/' + dbname + "?useSSL=" + ssl, username, password);
|
return DriverManager.getConnection("jdbc:mysql://" + encodeUrlValue(address) + '/' + encodeUrlValue(dbname) + "?useSSL=" + ssl, username, password);
|
||||||
} catch (SQLException e) {
|
} catch (SQLException | UnsupportedEncodingException e) {
|
||||||
throw new SqlException(e);
|
throw new SqlException(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -30,14 +37,14 @@ public class SqlUtils {
|
||||||
try {
|
try {
|
||||||
Connection connection;
|
Connection connection;
|
||||||
if (username != null) {
|
if (username != null) {
|
||||||
connection = DriverManager.getConnection("jdbc:h2:./" + path + ";mode=MySQL;AUTO_SERVER=TRUE", username, password);
|
connection = DriverManager.getConnection("jdbc:h2:./" + encodeUrlValue(path) + ";mode=MySQL;AUTO_SERVER=TRUE", username, password);
|
||||||
} else {
|
} else {
|
||||||
connection = DriverManager.getConnection("jdbc:h2:./" + path + ";mode=MySQL;AUTO_SERVER=TRUE", "sa", "");
|
connection = DriverManager.getConnection("jdbc:h2:./" + encodeUrlValue(path) + ";mode=MySQL;AUTO_SERVER=TRUE", "sa", "");
|
||||||
}
|
}
|
||||||
return connection;
|
return connection;
|
||||||
} catch (NoClassDefFoundError e) {
|
} catch (NoClassDefFoundError e) {
|
||||||
throw new SqlException("No suitable driver");
|
throw new SqlException("No suitable driver");
|
||||||
} catch (SQLException e) {
|
} catch (SQLException | UnsupportedEncodingException e) {
|
||||||
throw new SqlException(e);
|
throw new SqlException(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -56,8 +63,8 @@ public class SqlUtils {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
return DriverManager.getConnection("jdbc:sqlite:" + path);
|
return DriverManager.getConnection("jdbc:sqlite:" + encodeUrlValue(path.toString()));
|
||||||
} catch (SQLException e) {
|
} catch (SQLException | UnsupportedEncodingException e) {
|
||||||
throw new SqlException(e);
|
throw new SqlException(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -10,6 +10,7 @@ import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
|
import java.nio.file.Paths;
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
public interface PreparedPlugin {
|
public interface PreparedPlugin {
|
||||||
|
|
@ -23,26 +24,35 @@ public interface PreparedPlugin {
|
||||||
|
|
||||||
Path getDataDirectory();
|
Path getDataDirectory();
|
||||||
|
|
||||||
default Path loadConfig(Config config, String pathname) {
|
default Path loadConfig(Config config, String pathname, boolean resourcePreload) {
|
||||||
ClassLoader classLoader = getClass().getClassLoader();
|
ClassLoader classLoader = getClass().getClassLoader();
|
||||||
InputStream stream = ResourceUtils.getResource(pathname, classLoader);
|
Path path = Paths.get(pathname);
|
||||||
|
try (InputStream stream = ResourceUtils.getResource(pathname, classLoader)) {
|
||||||
if (stream != null) {
|
if (stream != null) {
|
||||||
|
if (resourcePreload) {
|
||||||
config.load(stream);
|
config.load(stream);
|
||||||
}
|
}
|
||||||
|
|
||||||
Path path = config.load(getDataDirectory().toString() + '/' + pathname);
|
|
||||||
try {
|
|
||||||
if (Files.notExists(path) || Files.size(path) == 0) {
|
if (Files.notExists(path) || Files.size(path) == 0) {
|
||||||
if (ResourceUtils.saveResource(pathname, path, classLoader)) {
|
ResourceUtils.saveResource(stream, path);
|
||||||
config.load(path);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (Files.exists(path)) config.load(path);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
throw new ConfigException(e);
|
throw new ConfigException(e);
|
||||||
}
|
}
|
||||||
return path;
|
return path;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
default Path loadConfig(Config config, String pathname) {
|
||||||
|
return loadConfig(config, pathname, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
default Path loadConfig(Config config, boolean resourcePreload) {
|
||||||
|
return loadConfig(config, config.getKey(), resourcePreload);
|
||||||
|
}
|
||||||
|
|
||||||
default Path loadConfig(Config config) {
|
default Path loadConfig(Config config) {
|
||||||
return loadConfig(config, config.getKey());
|
return loadConfig(config, config.getKey());
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,6 @@ import org.zhdev.varioutil.sql.*;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collections;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.function.Function;
|
import java.util.function.Function;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -32,9 +32,9 @@ public abstract class VelocityPreparedPlugin extends VelocityPlugin implements P
|
||||||
ConfigSection languageSection = defaultConfig.getOrCreateSection("language");
|
ConfigSection languageSection = defaultConfig.getOrCreateSection("language");
|
||||||
String locale = languageSection.getString("locale", "default");
|
String locale = languageSection.getString("locale", "default");
|
||||||
Config languageConfig = new VelocityTomlConfig("language/" + locale + ".toml");
|
Config languageConfig = new VelocityTomlConfig("language/" + locale + ".toml");
|
||||||
loadConfig(languageConfig, "language.toml");
|
loadConfig(languageConfig, "language.toml", true);
|
||||||
loadConfig(languageConfig, "language/default.toml");
|
loadConfig(languageConfig, "language/default.toml", true);
|
||||||
loadConfig(languageConfig);
|
loadConfig(languageConfig, true);
|
||||||
ConfigUtils.addPhrases(language, languageConfig, ColorUtils::translateAlternateColorCodes);
|
ConfigUtils.addPhrases(language, languageConfig, ColorUtils::translateAlternateColorCodes);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue