From 77971390cdaec7140780abfa1641135f0e6c8370 Mon Sep 17 00:00:00 2001 From: Zrips Date: Thu, 15 Jul 2021 17:26:17 +0300 Subject: [PATCH] Bringing back older (better) sqlite handling code --- src/main/java/com/gamingmesh/jobs/Jobs.java | 9 +++ .../gamingmesh/jobs/dao/JobsClassLoader.java | 28 ++++++++ .../jobs/dao/JobsConnectionPool.java | 37 +++++----- .../java/com/gamingmesh/jobs/dao/JobsDAO.java | 8 ++- .../com/gamingmesh/jobs/dao/JobsDrivers.java | 70 +++++++++++++++++++ .../com/gamingmesh/jobs/dao/JobsSQLite.java | 8 ++- 6 files changed, 138 insertions(+), 22 deletions(-) create mode 100644 src/main/java/com/gamingmesh/jobs/dao/JobsClassLoader.java create mode 100644 src/main/java/com/gamingmesh/jobs/dao/JobsDrivers.java diff --git a/src/main/java/com/gamingmesh/jobs/Jobs.java b/src/main/java/com/gamingmesh/jobs/Jobs.java index 41b2238c..d94c22ee 100644 --- a/src/main/java/com/gamingmesh/jobs/Jobs.java +++ b/src/main/java/com/gamingmesh/jobs/Jobs.java @@ -82,6 +82,7 @@ import com.gamingmesh.jobs.container.Quest; import com.gamingmesh.jobs.container.QuestProgression; import com.gamingmesh.jobs.container.blockOwnerShip.BlockOwnerShip; import com.gamingmesh.jobs.container.blockOwnerShip.BlockTypes; +import com.gamingmesh.jobs.dao.JobsClassLoader; import com.gamingmesh.jobs.dao.JobsDAO; import com.gamingmesh.jobs.dao.JobsDAOData; import com.gamingmesh.jobs.dao.JobsManager; @@ -140,6 +141,8 @@ public final class Jobs extends JavaPlugin { private static PermissionHandler permissionHandler; private static PermissionManager permissionManager; + private static JobsClassLoader classLoader; + private final HashMap blockOwnerShipsMaterial = new HashMap<>(); private final HashMap blockOwnerShipsBlockType = new HashMap<>(); @@ -171,6 +174,12 @@ public final class Jobs extends JavaPlugin { return kyoriSupported; } + public static JobsClassLoader getJobsClassloader() { + if (classLoader == null) + classLoader = new JobsClassLoader(Jobs.getInstance()); + return classLoader; + } + /** * Returns the block owner ship for specific {@link CMIMaterial} type. * diff --git a/src/main/java/com/gamingmesh/jobs/dao/JobsClassLoader.java b/src/main/java/com/gamingmesh/jobs/dao/JobsClassLoader.java new file mode 100644 index 00000000..2aa53693 --- /dev/null +++ b/src/main/java/com/gamingmesh/jobs/dao/JobsClassLoader.java @@ -0,0 +1,28 @@ +package com.gamingmesh.jobs.dao; + +import java.io.File; +import java.io.IOException; +import java.net.URL; +import java.net.URLClassLoader; + +import com.gamingmesh.jobs.Jobs; + +public class JobsClassLoader extends URLClassLoader { + + public JobsClassLoader(Jobs core) { + super(new URL[0], core.getClass().getClassLoader()); + } + + public void addFile(File f) throws IOException { + addURL(f.toURI().toURL()); + } + + @Override + public void addURL(URL url) { + for (URL u : getURLs()) + if (url.sameFile(u)) + return; + + super.addURL(url); + } +} diff --git a/src/main/java/com/gamingmesh/jobs/dao/JobsConnectionPool.java b/src/main/java/com/gamingmesh/jobs/dao/JobsConnectionPool.java index 18293a76..ed47487a 100644 --- a/src/main/java/com/gamingmesh/jobs/dao/JobsConnectionPool.java +++ b/src/main/java/com/gamingmesh/jobs/dao/JobsConnectionPool.java @@ -1,8 +1,12 @@ package com.gamingmesh.jobs.dao; +import java.sql.Connection; +import java.sql.Driver; import java.sql.DriverManager; import java.sql.SQLException; +import com.gamingmesh.jobs.Jobs; + public class JobsConnectionPool { private JobsConnection connection; @@ -10,37 +14,36 @@ public class JobsConnectionPool { private String username; private String password; - public JobsConnectionPool(String url, String username, String password) { + public JobsConnectionPool(String driverName, String url, String username, String password) throws InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException { + Driver driver = (Driver) Class.forName(driverName, true, Jobs.getJobsClassloader()).newInstance(); + JobsDrivers jDriver = new JobsDrivers(driver); + DriverManager.registerDriver(jDriver); this.url = url; this.username = username; this.password = password; } public synchronized JobsConnection getConnection() throws SQLException { - if (connection != null && (connection.isClosed() || !connection.isValid(1))) { + if (this.connection != null && (this.connection.isClosed() || !this.connection.isValid(1))) { try { - connection.closeConnection(); - } catch (SQLException e) {} - connection = null; - } - - if (connection == null) { - try { - connection = new JobsConnection(DriverManager.getConnection(url, username, password)); + this.connection.closeConnection(); } catch (SQLException e) { } + this.connection = null; } - - return connection; + if (this.connection == null) { + Connection conn = DriverManager.getConnection(this.url, this.username, this.password); + this.connection = new JobsConnection(conn); + } + return this.connection; } - public void closeConnection() { - if (connection != null) { + public synchronized void closeConnection() { + if (this.connection != null) try { - connection.closeConnection(); + this.connection.closeConnection(); } catch (SQLException e) { e.printStackTrace(); } - } } -} +} \ No newline at end of file diff --git a/src/main/java/com/gamingmesh/jobs/dao/JobsDAO.java b/src/main/java/com/gamingmesh/jobs/dao/JobsDAO.java index 1f9406ff..34b5170e 100644 --- a/src/main/java/com/gamingmesh/jobs/dao/JobsDAO.java +++ b/src/main/java/com/gamingmesh/jobs/dao/JobsDAO.java @@ -465,11 +465,15 @@ public abstract class JobsDAO { try { Class.forName(driverName); } catch (ClassNotFoundException c) { - plugin.getLogger().log(java.util.logging.Level.WARNING, c.getLocalizedMessage()); + c.printStackTrace(); return; } - pool = new JobsConnectionPool(url, username, password); + try { + pool = new JobsConnectionPool(driverName, url, username, password); + } catch (Exception e) { + e.printStackTrace(); + } } public final synchronized void setUp() { diff --git a/src/main/java/com/gamingmesh/jobs/dao/JobsDrivers.java b/src/main/java/com/gamingmesh/jobs/dao/JobsDrivers.java new file mode 100644 index 00000000..de20ea77 --- /dev/null +++ b/src/main/java/com/gamingmesh/jobs/dao/JobsDrivers.java @@ -0,0 +1,70 @@ +/** + * Jobs Plugin for Bukkit + * Copyright (C) 2011 Zak Ford + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package com.gamingmesh.jobs.dao; + +import java.sql.Connection; +import java.sql.Driver; +import java.sql.DriverPropertyInfo; +import java.sql.SQLException; +import java.sql.SQLFeatureNotSupportedException; +import java.util.Properties; +import java.util.logging.Logger; + +public class JobsDrivers implements Driver { + private Driver driver; + + public JobsDrivers(Driver driver) { + this.driver = driver; + } + + @Override + public Connection connect(String url, Properties info) throws SQLException { + return driver.connect(url, info); + } + + @Override + public boolean acceptsURL(String url) throws SQLException { + return driver.acceptsURL(url); + } + + @Override + public DriverPropertyInfo[] getPropertyInfo(String url, Properties info) throws SQLException { + return driver.getPropertyInfo(url, info); + } + + @Override + public int getMajorVersion() { + return driver.getMajorVersion(); + } + + @Override + public int getMinorVersion() { + return driver.getMinorVersion(); + } + + @Override + public boolean jdbcCompliant() { + return driver.jdbcCompliant(); + } + + @Override + public Logger getParentLogger() throws SQLFeatureNotSupportedException { + return driver.getParentLogger(); + } +} diff --git a/src/main/java/com/gamingmesh/jobs/dao/JobsSQLite.java b/src/main/java/com/gamingmesh/jobs/dao/JobsSQLite.java index 2292b23c..aafc7d59 100644 --- a/src/main/java/com/gamingmesh/jobs/dao/JobsSQLite.java +++ b/src/main/java/com/gamingmesh/jobs/dao/JobsSQLite.java @@ -15,14 +15,16 @@ public class JobsSQLite extends JobsDAO { } public JobsSQLite initialize(File dir) { - dir.mkdirs(); + if (!dir.exists()) + dir.mkdirs(); setUp(); return this; } JobsSQLite(Jobs plugin, File file) { super(plugin, "org.sqlite.JDBC", "jdbc:sqlite:" + new File(file, "jobs.sqlite.db").getPath(), null, null, ""); - file.mkdirs(); + if (!file.exists()) + file.mkdirs(); setDbType(DataBaseType.SqLite); } @@ -35,7 +37,7 @@ public class JobsSQLite extends JobsDAO { } executeSQL("CREATE TABLE `" + getPrefix() - + "users` (`id` INTEGER PRIMARY KEY AUTOINCREMENT, `player_uuid` varchar(36) NOT NULL, `username` varchar(20), `data` text);"); + + "users` (`id` INTEGER PRIMARY KEY AUTOINCREMENT, `player_uuid` varchar(36) NOT NULL, `username` varchar(20), `data` text);"); } @Override