forked from LogBlock/LogBlock
Fixed queue dump imports to not crash after a single error
This commit is contained in:
@@ -3,10 +3,11 @@ package de.diddiz.LogBlock;
|
|||||||
import static de.diddiz.util.Utils.download;
|
import static de.diddiz.util.Utils.download;
|
||||||
import static org.bukkit.Bukkit.getLogger;
|
import static org.bukkit.Bukkit.getLogger;
|
||||||
import java.io.BufferedReader;
|
import java.io.BufferedReader;
|
||||||
|
import java.io.BufferedWriter;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileNotFoundException;
|
import java.io.FileNotFoundException;
|
||||||
import java.io.FileReader;
|
import java.io.FileReader;
|
||||||
import java.io.FilenameFilter;
|
import java.io.FileWriter;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.sql.Connection;
|
import java.sql.Connection;
|
||||||
import java.sql.ResultSet;
|
import java.sql.ResultSet;
|
||||||
@@ -30,6 +31,8 @@ import com.nijiko.permissions.PermissionHandler;
|
|||||||
import com.nijikokun.bukkit.Permissions.Permissions;
|
import com.nijikokun.bukkit.Permissions.Permissions;
|
||||||
import de.diddiz.LogBlock.QueryParams.BlockChangeType;
|
import de.diddiz.LogBlock.QueryParams.BlockChangeType;
|
||||||
import de.diddiz.util.MySQLConnectionPool;
|
import de.diddiz.util.MySQLConnectionPool;
|
||||||
|
import de.diddiz.util.Utils;
|
||||||
|
import de.diddiz.util.Utils.ExtensionFilenameFilter;
|
||||||
|
|
||||||
public class LogBlock extends JavaPlugin
|
public class LogBlock extends JavaPlugin
|
||||||
{
|
{
|
||||||
@@ -77,44 +80,7 @@ public class LogBlock extends JavaPlugin
|
|||||||
if (updater.update())
|
if (updater.update())
|
||||||
config = new Config(this);
|
config = new Config(this);
|
||||||
updater.checkTables();
|
updater.checkTables();
|
||||||
final File[] imports = new File("plugins/LogBlock/import/").listFiles(new FilenameFilter() {
|
doImports();
|
||||||
@Override
|
|
||||||
public boolean accept(File dir, String name) {
|
|
||||||
return name.toLowerCase().endsWith(".sql");
|
|
||||||
}
|
|
||||||
});
|
|
||||||
if (imports != null && imports.length > 0) {
|
|
||||||
getLogger().info("[LogBlock] Found " + imports.length + "imports.");
|
|
||||||
Connection conn = null;
|
|
||||||
try {
|
|
||||||
conn = getConnection();
|
|
||||||
conn.setAutoCommit(false);
|
|
||||||
final Statement st = conn.createStatement();
|
|
||||||
for (final File sqlFile : imports)
|
|
||||||
try {
|
|
||||||
getLogger().info("[LogBlock] Trying to import " + sqlFile.getName() + " ...");
|
|
||||||
final BufferedReader reader = new BufferedReader(new FileReader(sqlFile));
|
|
||||||
String line = null;
|
|
||||||
while ((line = reader.readLine()) != null)
|
|
||||||
st.addBatch(line);
|
|
||||||
st.executeBatch();
|
|
||||||
conn.commit();
|
|
||||||
reader.close();
|
|
||||||
sqlFile.delete();
|
|
||||||
getLogger().info("[LogBlock] Successfully imported " + sqlFile.getName() + ".");
|
|
||||||
} catch (final Exception ex) {
|
|
||||||
getLogger().log(Level.WARNING, "[LogBlock] Failed to import " + sqlFile.getName() + ": ", ex);
|
|
||||||
file.renameTo(new File("plugins/LogBlock/import/" + sqlFile.getName() + ".failed"));
|
|
||||||
}
|
|
||||||
st.close();
|
|
||||||
getLogger().info("[LogBlock] Successfully imported stored queue.");
|
|
||||||
} catch (final Exception ex) {
|
|
||||||
getLogger().log(Level.WARNING, "[LogBlock] Error while importing: ", ex);
|
|
||||||
} finally {
|
|
||||||
if (conn != null)
|
|
||||||
conn.close();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch (final Exception ex) {
|
} catch (final Exception ex) {
|
||||||
getLogger().log(Level.SEVERE, "[LogBlock] Error while loading: ", ex);
|
getLogger().log(Level.SEVERE, "[LogBlock] Error while loading: ", ex);
|
||||||
errorAtLoading = true;
|
errorAtLoading = true;
|
||||||
@@ -123,6 +89,49 @@ public class LogBlock extends JavaPlugin
|
|||||||
consumer = new Consumer(this);
|
consumer = new Consumer(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void doImports() {
|
||||||
|
final File[] imports = new File("plugins/LogBlock/import/").listFiles(new ExtensionFilenameFilter("sql"));
|
||||||
|
if (imports != null && imports.length > 0) {
|
||||||
|
getLogger().info("[LogBlock] Found " + imports.length + " imports.");
|
||||||
|
Connection conn = null;
|
||||||
|
try {
|
||||||
|
conn = getConnection();
|
||||||
|
conn.setAutoCommit(false);
|
||||||
|
final Statement st = conn.createStatement();
|
||||||
|
final BufferedWriter writer = new BufferedWriter(new FileWriter(new File(getDataFolder(), "import/failed.txt")));
|
||||||
|
int successes = 0, errors = 0;
|
||||||
|
for (final File sqlFile : imports) {
|
||||||
|
getLogger().info("[LogBlock] Trying to import " + sqlFile.getName() + " ...");
|
||||||
|
final BufferedReader reader = new BufferedReader(new FileReader(sqlFile));
|
||||||
|
String line = null;
|
||||||
|
while ((line = reader.readLine()) != null)
|
||||||
|
try {
|
||||||
|
st.execute(line);
|
||||||
|
successes++;
|
||||||
|
} catch (final Exception ex) {
|
||||||
|
getLogger().warning("[LogBlock] Error while importing: '" + line + "': " + ex.getMessage());
|
||||||
|
writer.write(line + Utils.newline);
|
||||||
|
errors++;
|
||||||
|
}
|
||||||
|
conn.commit();
|
||||||
|
reader.close();
|
||||||
|
sqlFile.delete();
|
||||||
|
getLogger().info("[LogBlock] Successfully imported " + sqlFile.getName() + ".");
|
||||||
|
}
|
||||||
|
writer.close();
|
||||||
|
st.close();
|
||||||
|
getLogger().info("[LogBlock] Successfully imported stored queue. (" + successes + " rows imported, " + errors + " errors)");
|
||||||
|
} catch (final Exception ex) {
|
||||||
|
getLogger().log(Level.WARNING, "[LogBlock] Error while importing: ", ex);
|
||||||
|
} finally {
|
||||||
|
if (conn != null)
|
||||||
|
try {
|
||||||
|
conn.close();
|
||||||
|
} catch (final SQLException ex) {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onEnable() {
|
public void onEnable() {
|
||||||
final PluginManager pm = getServer().getPluginManager();
|
final PluginManager pm = getServer().getPluginManager();
|
||||||
|
@@ -4,6 +4,7 @@ import java.io.BufferedOutputStream;
|
|||||||
import java.io.BufferedReader;
|
import java.io.BufferedReader;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileOutputStream;
|
import java.io.FileOutputStream;
|
||||||
|
import java.io.FilenameFilter;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.InputStreamReader;
|
import java.io.InputStreamReader;
|
||||||
@@ -15,6 +16,8 @@ import java.util.logging.Logger;
|
|||||||
|
|
||||||
public class Utils
|
public class Utils
|
||||||
{
|
{
|
||||||
|
public static String newline = System.getProperty("line.separator");
|
||||||
|
|
||||||
public static void download(Logger log, URL url, File file) throws IOException {
|
public static void download(Logger log, URL url, File file) throws IOException {
|
||||||
if (!file.getParentFile().exists())
|
if (!file.getParentFile().exists())
|
||||||
file.getParentFile().mkdir();
|
file.getParentFile().mkdir();
|
||||||
@@ -141,4 +144,18 @@ public class Utils
|
|||||||
buffer.append(delimiter).append(s[i]);
|
buffer.append(delimiter).append(s[i]);
|
||||||
return buffer.toString();
|
return buffer.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static class ExtensionFilenameFilter implements FilenameFilter
|
||||||
|
{
|
||||||
|
private final String ext;
|
||||||
|
|
||||||
|
public ExtensionFilenameFilter(String ext) {
|
||||||
|
this.ext = ext;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean accept(File dir, String name) {
|
||||||
|
return name.toLowerCase().endsWith(ext);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user