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