PageRenderTime 48ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/EssentialsProtect/src/com/earth2me/essentials/protect/data/ProtectedBlockJDBC.java

https://github.com/BodyshotzVG/Essentials
Java | 426 lines | 403 code | 23 blank | 0 comment | 44 complexity | 00b4cea6f1812032747d6678e7d140ea MD5 | raw file
  1. package com.earth2me.essentials.protect.data;
  2. import com.mchange.v2.c3p0.ComboPooledDataSource;
  3. import java.beans.PropertyVetoException;
  4. import java.sql.Connection;
  5. import java.sql.PreparedStatement;
  6. import java.sql.ResultSet;
  7. import java.sql.SQLException;
  8. import java.util.ArrayList;
  9. import java.util.List;
  10. import java.util.logging.Level;
  11. import java.util.logging.Logger;
  12. import org.bukkit.block.Block;
  13. public abstract class ProtectedBlockJDBC implements IProtectedBlock
  14. {
  15. protected static final Logger LOGGER = Logger.getLogger("Minecraft");
  16. protected final transient ComboPooledDataSource cpds;
  17. protected abstract PreparedStatement getStatementCreateTable(Connection conn) throws SQLException;
  18. protected abstract PreparedStatement getStatementUpdateFrom2_0Table(Connection conn) throws SQLException;
  19. protected abstract PreparedStatement getStatementDeleteAll(Connection conn) throws SQLException;
  20. protected abstract PreparedStatement getStatementInsert(Connection conn, String world, int x, int y, int z, String playerName) throws SQLException;
  21. protected abstract PreparedStatement getStatementPlayerCountByLocation(Connection conn, String world, int x, int y, int z, String playerName) throws SQLException;
  22. protected abstract PreparedStatement getStatementPlayersByLocation(Connection conn, String name, int x, int y, int z) throws SQLException;
  23. protected abstract PreparedStatement getStatementDeleteByLocation(Connection conn, String world, int x, int y, int z) throws SQLException;
  24. protected abstract PreparedStatement getStatementAllBlocks(Connection conn) throws SQLException;
  25. public ProtectedBlockJDBC(String driver, String url) throws PropertyVetoException
  26. {
  27. this(driver, url, null, null);
  28. }
  29. public ProtectedBlockJDBC(String driver, String url, String username, String password) throws PropertyVetoException
  30. {
  31. cpds = new ComboPooledDataSource();
  32. cpds.setDriverClass(driver);
  33. cpds.setJdbcUrl(url);
  34. if (username != null)
  35. {
  36. cpds.setUser(username);
  37. cpds.setPassword(password);
  38. }
  39. cpds.setMaxStatements(20);
  40. createAndConvertTable();
  41. }
  42. private void createAndConvertTable()
  43. {
  44. Connection conn = null;
  45. PreparedStatement ps = null;
  46. try
  47. {
  48. conn = cpds.getConnection();
  49. ps = getStatementCreateTable(conn);
  50. ps.execute();
  51. ps.close();
  52. ps = getStatementUpdateFrom2_0Table(conn);
  53. ps.execute();
  54. }
  55. catch (SQLException ex)
  56. {
  57. LOGGER.log(Level.SEVERE, null, ex);
  58. }
  59. finally
  60. {
  61. if (ps != null)
  62. {
  63. try
  64. {
  65. ps.close();
  66. }
  67. catch (SQLException ex)
  68. {
  69. LOGGER.log(Level.SEVERE, null, ex);
  70. }
  71. }
  72. if (conn != null)
  73. {
  74. try
  75. {
  76. conn.close();
  77. }
  78. catch (SQLException ex)
  79. {
  80. LOGGER.log(Level.SEVERE, null, ex);
  81. }
  82. }
  83. }
  84. }
  85. public void clearProtections()
  86. {
  87. Connection conn = null;
  88. PreparedStatement ps = null;
  89. try
  90. {
  91. conn = cpds.getConnection();
  92. ps = getStatementDeleteAll(conn);
  93. ps.executeUpdate();
  94. }
  95. catch (SQLException ex)
  96. {
  97. LOGGER.log(Level.SEVERE, null, ex);
  98. }
  99. finally
  100. {
  101. if (ps != null)
  102. {
  103. try
  104. {
  105. ps.close();
  106. }
  107. catch (SQLException ex)
  108. {
  109. LOGGER.log(Level.SEVERE, null, ex);
  110. }
  111. }
  112. if (conn != null)
  113. {
  114. try
  115. {
  116. conn.close();
  117. }
  118. catch (SQLException ex)
  119. {
  120. LOGGER.log(Level.SEVERE, null, ex);
  121. }
  122. }
  123. }
  124. }
  125. public void importProtections(List<OwnedBlock> blocks)
  126. {
  127. for (OwnedBlock ownedBlock : blocks)
  128. {
  129. if (ownedBlock.playerName == null)
  130. {
  131. continue;
  132. }
  133. protectBlock(ownedBlock.world, ownedBlock.x, ownedBlock.y, ownedBlock.z, ownedBlock.playerName);
  134. }
  135. }
  136. public List<OwnedBlock> exportProtections()
  137. {
  138. Connection conn = null;
  139. PreparedStatement ps = null;
  140. ResultSet rs = null;
  141. List<OwnedBlock> blocks = new ArrayList<OwnedBlock>();
  142. try
  143. {
  144. conn = cpds.getConnection();
  145. ps = getStatementAllBlocks(conn);
  146. rs = ps.executeQuery();
  147. while (rs.next())
  148. {
  149. OwnedBlock ob = new OwnedBlock(
  150. rs.getInt(2),
  151. rs.getInt(3),
  152. rs.getInt(4),
  153. rs.getString(1),
  154. rs.getString(5));
  155. blocks.add(ob);
  156. }
  157. return blocks;
  158. }
  159. catch (SQLException ex)
  160. {
  161. LOGGER.log(Level.SEVERE, null, ex);
  162. return blocks;
  163. }
  164. finally
  165. {
  166. if (rs != null)
  167. {
  168. try
  169. {
  170. rs.close();
  171. }
  172. catch (SQLException ex)
  173. {
  174. LOGGER.log(Level.SEVERE, null, ex);
  175. }
  176. }
  177. if (ps != null)
  178. {
  179. try
  180. {
  181. ps.close();
  182. }
  183. catch (SQLException ex)
  184. {
  185. LOGGER.log(Level.SEVERE, null, ex);
  186. }
  187. }
  188. if (conn != null)
  189. {
  190. try
  191. {
  192. conn.close();
  193. }
  194. catch (SQLException ex)
  195. {
  196. LOGGER.log(Level.SEVERE, null, ex);
  197. }
  198. }
  199. }
  200. }
  201. public void protectBlock(Block block, String playerName)
  202. {
  203. protectBlock(block.getWorld().getName(), block.getX(), block.getY(), block.getZ(), playerName);
  204. }
  205. private void protectBlock(String world, int x, int y, int z, String playerName)
  206. {
  207. Connection conn = null;
  208. PreparedStatement ps = null;
  209. try
  210. {
  211. conn = cpds.getConnection();
  212. ps = getStatementInsert(conn, world, x, y, z, playerName);
  213. ps.executeUpdate();
  214. }
  215. catch (SQLException ex)
  216. {
  217. LOGGER.log(Level.SEVERE, null, ex);
  218. }
  219. finally
  220. {
  221. if (ps != null)
  222. {
  223. try
  224. {
  225. ps.close();
  226. }
  227. catch (SQLException ex)
  228. {
  229. LOGGER.log(Level.SEVERE, null, ex);
  230. }
  231. }
  232. if (conn != null)
  233. {
  234. try
  235. {
  236. conn.close();
  237. }
  238. catch (SQLException ex)
  239. {
  240. LOGGER.log(Level.SEVERE, null, ex);
  241. }
  242. }
  243. }
  244. }
  245. public boolean isProtected(Block block, String playerName)
  246. {
  247. Connection conn = null;
  248. PreparedStatement ps = null;
  249. ResultSet rs = null;
  250. try
  251. {
  252. conn = cpds.getConnection();
  253. ps = getStatementPlayerCountByLocation(conn, block.getWorld().getName(), block.getX(), block.getY(), block.getZ(), playerName);
  254. rs = ps.executeQuery();
  255. return rs.next() && rs.getInt(1) > 0 && rs.getInt(2) == 0;
  256. }
  257. catch (SQLException ex)
  258. {
  259. LOGGER.log(Level.SEVERE, null, ex);
  260. return true;
  261. }
  262. finally
  263. {
  264. if (rs != null)
  265. {
  266. try
  267. {
  268. rs.close();
  269. }
  270. catch (SQLException ex)
  271. {
  272. LOGGER.log(Level.SEVERE, null, ex);
  273. }
  274. }
  275. if (ps != null)
  276. {
  277. try
  278. {
  279. ps.close();
  280. }
  281. catch (SQLException ex)
  282. {
  283. LOGGER.log(Level.SEVERE, null, ex);
  284. }
  285. }
  286. if (conn != null)
  287. {
  288. try
  289. {
  290. conn.close();
  291. }
  292. catch (SQLException ex)
  293. {
  294. LOGGER.log(Level.SEVERE, null, ex);
  295. }
  296. }
  297. }
  298. }
  299. public List<String> getOwners(Block block)
  300. {
  301. Connection conn = null;
  302. PreparedStatement ps = null;
  303. ResultSet rs = null;
  304. List<String> owners = new ArrayList<String>();
  305. try
  306. {
  307. conn = cpds.getConnection();
  308. ps = getStatementPlayersByLocation(conn, block.getWorld().getName(), block.getX(), block.getY(), block.getZ());
  309. rs = ps.executeQuery();
  310. while (rs.next())
  311. {
  312. owners.add(rs.getString(1));
  313. }
  314. return owners;
  315. }
  316. catch (SQLException ex)
  317. {
  318. LOGGER.log(Level.SEVERE, null, ex);
  319. return owners;
  320. }
  321. finally
  322. {
  323. if (rs != null)
  324. {
  325. try
  326. {
  327. rs.close();
  328. }
  329. catch (SQLException ex)
  330. {
  331. LOGGER.log(Level.SEVERE, null, ex);
  332. }
  333. }
  334. if (ps != null)
  335. {
  336. try
  337. {
  338. ps.close();
  339. }
  340. catch (SQLException ex)
  341. {
  342. LOGGER.log(Level.SEVERE, null, ex);
  343. }
  344. }
  345. if (conn != null)
  346. {
  347. try
  348. {
  349. conn.close();
  350. }
  351. catch (SQLException ex)
  352. {
  353. LOGGER.log(Level.SEVERE, null, ex);
  354. }
  355. }
  356. }
  357. }
  358. public int unprotectBlock(Block block)
  359. {
  360. Connection conn = null;
  361. PreparedStatement ps = null;
  362. try
  363. {
  364. conn = cpds.getConnection();
  365. ps = getStatementDeleteByLocation(conn, block.getWorld().getName(), block.getX(), block.getY(), block.getZ());
  366. return ps.executeUpdate();
  367. }
  368. catch (SQLException ex)
  369. {
  370. LOGGER.log(Level.SEVERE, null, ex);
  371. return 0;
  372. }
  373. finally
  374. {
  375. if (ps != null)
  376. {
  377. try
  378. {
  379. ps.close();
  380. }
  381. catch (SQLException ex)
  382. {
  383. LOGGER.log(Level.SEVERE, null, ex);
  384. }
  385. }
  386. if (conn != null)
  387. {
  388. try
  389. {
  390. conn.close();
  391. }
  392. catch (SQLException ex)
  393. {
  394. LOGGER.log(Level.SEVERE, null, ex);
  395. }
  396. }
  397. }
  398. }
  399. public void onPluginDeactivation()
  400. {
  401. cpds.close();
  402. }
  403. }