Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package org.omicron.mysql;
- import server.Constants;
- import server.Server;
- import java.sql.ResultSet;
- import java.util.List;
- import java.util.Queue;
- import java.util.Vector;
- import java.util.concurrent.ConcurrentLinkedQueue;
- import java.util.concurrent.atomic.AtomicBoolean;
- /**
- * @author Omicron
- */
- public final class ConnectionProcessor implements Runnable {
- static {
- try {
- Class.forName("com.mysql.jdbc.Driver");
- } catch (ClassNotFoundException e) {
- throw new AssertionError(e);
- }
- }
- private final MySQLConnection connection;
- private final String username;
- private final String password;
- private final Thread thread;
- private boolean running;
- private final ConcurrentLinkedQueue<Item> items = new ConcurrentLinkedQueue<> ();
- private final Object lock = new Object();
- public ConnectionProcessor(MySQLConnection connection, String username, String password) {
- this.connection = connection;
- this.username = username;
- this.password = password;
- this.thread = new Thread(this);
- }
- public void start() {
- if (running) {
- throw new IllegalStateException("The processor is already running.");
- }
- thread.start();
- }
- public void stop() {
- if (!running) {
- throw new IllegalStateException("The processor is already stopped.");
- }
- running = false;
- }
- public boolean isConnected() {
- if (Server.getWorld() == Constants.TEST_WORLD) {
- return true;
- }
- return connection.isConnected();
- }
- @Override
- public void run() {
- if (Server.getWorld() == Constants.TEST_WORLD) {
- return;
- }
- running = true;
- MainLoop:
- while (running) {
- if (!connection.isConnected()) {
- while (!connection.safeConnect(username, password));
- }
- if (!connection.statementConnected()) {
- connection.close();
- while (!connection.safeConnect(username, password));
- }
- synchronized (lock) {
- while (!items.isEmpty()) {
- Item item = items.peek();
- if (!item.canExecute()) {
- items.remove();
- continue;
- }
- if (!item.execute(connection)) {
- continue MainLoop;
- }
- items.remove();
- }
- }
- synchronized (this) {
- try {
- this.wait();
- } catch (InterruptedException ignored) {
- }
- }
- }
- connection.close();
- }
- public boolean executeQuery(Query query) {
- if (Server.getWorld() == Constants.TEST_WORLD) {
- return true;
- }
- if (!connection.isConnected()) {
- synchronized (this) {
- notify();
- }
- return false;
- }
- boolean result = items.offer(query);
- synchronized (this) {
- notify();
- }
- return result;
- }
- public boolean forceQuery(Query query) {
- if (Server.getWorld() == Constants.TEST_WORLD) {
- return true;
- }
- boolean result = items.offer(query);
- synchronized (this) {
- notify();
- }
- return result;
- }
- public boolean executeUpdate(Update update) {
- if (Server.getWorld() == Constants.TEST_WORLD) {
- return true;
- }
- boolean result = items.offer(update);
- synchronized (this) {
- notify();
- }
- return result;
- }
- public ResultSet blockingQuery(String query) {
- synchronized (lock) {
- if (!connection.isConnected()) {
- return null;
- }
- return connection.executeQuery(query);
- }
- }
- }
Add Comment
Please, Sign In to add comment