Guest User

Untitled

a guest
Jun 29th, 2018
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.21 KB | None | 0 0
  1. package org.omicron.mysql;
  2.  
  3. import server.Constants;
  4. import server.Server;
  5.  
  6. import java.sql.ResultSet;
  7. import java.util.List;
  8. import java.util.Queue;
  9. import java.util.Vector;
  10. import java.util.concurrent.ConcurrentLinkedQueue;
  11. import java.util.concurrent.atomic.AtomicBoolean;
  12.  
  13. /**
  14. * @author Omicron
  15. */
  16. public final class ConnectionProcessor implements Runnable {
  17.  
  18. static {
  19. try {
  20. Class.forName("com.mysql.jdbc.Driver");
  21. } catch (ClassNotFoundException e) {
  22. throw new AssertionError(e);
  23. }
  24. }
  25.  
  26. private final MySQLConnection connection;
  27. private final String username;
  28. private final String password;
  29. private final Thread thread;
  30. private boolean running;
  31. private final ConcurrentLinkedQueue<Item> items = new ConcurrentLinkedQueue<> ();
  32. private final Object lock = new Object();
  33.  
  34. public ConnectionProcessor(MySQLConnection connection, String username, String password) {
  35. this.connection = connection;
  36. this.username = username;
  37. this.password = password;
  38. this.thread = new Thread(this);
  39. }
  40.  
  41. public void start() {
  42. if (running) {
  43. throw new IllegalStateException("The processor is already running.");
  44. }
  45. thread.start();
  46. }
  47.  
  48. public void stop() {
  49. if (!running) {
  50. throw new IllegalStateException("The processor is already stopped.");
  51. }
  52. running = false;
  53. }
  54.  
  55. public boolean isConnected() {
  56. if (Server.getWorld() == Constants.TEST_WORLD) {
  57. return true;
  58. }
  59. return connection.isConnected();
  60. }
  61.  
  62. @Override
  63. public void run() {
  64. if (Server.getWorld() == Constants.TEST_WORLD) {
  65. return;
  66. }
  67. running = true;
  68. MainLoop:
  69. while (running) {
  70. if (!connection.isConnected()) {
  71. while (!connection.safeConnect(username, password));
  72. }
  73. if (!connection.statementConnected()) {
  74. connection.close();
  75. while (!connection.safeConnect(username, password));
  76. }
  77. synchronized (lock) {
  78. while (!items.isEmpty()) {
  79. Item item = items.peek();
  80. if (!item.canExecute()) {
  81. items.remove();
  82. continue;
  83. }
  84. if (!item.execute(connection)) {
  85. continue MainLoop;
  86. }
  87. items.remove();
  88. }
  89. }
  90. synchronized (this) {
  91. try {
  92. this.wait();
  93. } catch (InterruptedException ignored) {
  94. }
  95. }
  96. }
  97. connection.close();
  98. }
  99.  
  100. public boolean executeQuery(Query query) {
  101. if (Server.getWorld() == Constants.TEST_WORLD) {
  102. return true;
  103. }
  104. if (!connection.isConnected()) {
  105. synchronized (this) {
  106. notify();
  107. }
  108. return false;
  109. }
  110. boolean result = items.offer(query);
  111. synchronized (this) {
  112. notify();
  113. }
  114. return result;
  115. }
  116.  
  117. public boolean forceQuery(Query query) {
  118. if (Server.getWorld() == Constants.TEST_WORLD) {
  119. return true;
  120. }
  121. boolean result = items.offer(query);
  122. synchronized (this) {
  123. notify();
  124. }
  125. return result;
  126. }
  127.  
  128. public boolean executeUpdate(Update update) {
  129. if (Server.getWorld() == Constants.TEST_WORLD) {
  130. return true;
  131. }
  132. boolean result = items.offer(update);
  133. synchronized (this) {
  134. notify();
  135. }
  136. return result;
  137. }
  138.  
  139. public ResultSet blockingQuery(String query) {
  140. synchronized (lock) {
  141. if (!connection.isConnected()) {
  142. return null;
  143. }
  144. return connection.executeQuery(query);
  145. }
  146. }
  147. }
Add Comment
Please, Sign In to add comment