1
0
mirror of https://github.com/Zrips/Jobs.git synced 2024-12-01 15:03:36 +01:00

Bringing back older (better) sqlite handling code

This commit is contained in:
Zrips 2021-07-15 17:26:17 +03:00
parent c92c9fe95d
commit 77971390cd
6 changed files with 138 additions and 22 deletions

View File

@ -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<CMIMaterial, BlockOwnerShip> blockOwnerShipsMaterial = new HashMap<>();
private final HashMap<BlockTypes, BlockOwnerShip> 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.
*

View File

@ -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);
}
}

View File

@ -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;
}
if (this.connection == null) {
Connection conn = DriverManager.getConnection(this.url, this.username, this.password);
this.connection = new JobsConnection(conn);
}
return this.connection;
}
return 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();
}
}
}
}

View File

@ -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() {

View File

@ -0,0 +1,70 @@
/**
* Jobs Plugin for Bukkit
* Copyright (C) 2011 Zak Ford <zak.j.ford@gmail.com>
*
* 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 <http://www.gnu.org/licenses/>.
*/
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();
}
}

View File

@ -15,6 +15,7 @@ public class JobsSQLite extends JobsDAO {
}
public JobsSQLite initialize(File dir) {
if (!dir.exists())
dir.mkdirs();
setUp();
return this;
@ -22,6 +23,7 @@ public class JobsSQLite extends JobsDAO {
JobsSQLite(Jobs plugin, File file) {
super(plugin, "org.sqlite.JDBC", "jdbc:sqlite:" + new File(file, "jobs.sqlite.db").getPath(), null, null, "");
if (!file.exists())
file.mkdirs();
setDbType(DataBaseType.SqLite);
}