diff options
author | ementalo <ementalodev@gmx.co.uk> | 2012-06-27 13:35:39 +0100 |
---|---|---|
committer | ementalo <ementalodev@gmx.co.uk> | 2012-06-27 13:35:39 +0100 |
commit | 16d0b5c228dd8d981a0d79944c70248ea813d63b (patch) | |
tree | b76e6966bfc2b4b9b668b89b13e30307e0b81988 /EssentialsProtect/src/net/ess3/protect/data/ProtectedBlockJDBC.java | |
parent | c1230bd6ae4e17539e1f6f2c82691fa7a8395e8c (diff) | |
download | Essentials-16d0b5c228dd8d981a0d79944c70248ea813d63b.tar Essentials-16d0b5c228dd8d981a0d79944c70248ea813d63b.tar.gz Essentials-16d0b5c228dd8d981a0d79944c70248ea813d63b.tar.lz Essentials-16d0b5c228dd8d981a0d79944c70248ea813d63b.tar.xz Essentials-16d0b5c228dd8d981a0d79944c70248ea813d63b.zip |
package name change to net.ess3
Diffstat (limited to 'EssentialsProtect/src/net/ess3/protect/data/ProtectedBlockJDBC.java')
-rw-r--r-- | EssentialsProtect/src/net/ess3/protect/data/ProtectedBlockJDBC.java | 434 |
1 files changed, 434 insertions, 0 deletions
diff --git a/EssentialsProtect/src/net/ess3/protect/data/ProtectedBlockJDBC.java b/EssentialsProtect/src/net/ess3/protect/data/ProtectedBlockJDBC.java new file mode 100644 index 000000000..70475c6f5 --- /dev/null +++ b/EssentialsProtect/src/net/ess3/protect/data/ProtectedBlockJDBC.java @@ -0,0 +1,434 @@ +package net.ess3.protect.data; + +import com.mchange.v2.c3p0.ComboPooledDataSource; +import java.beans.PropertyVetoException; +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.List; +import java.util.logging.Level; +import java.util.logging.Logger; +import org.bukkit.block.Block; + + +public abstract class ProtectedBlockJDBC implements IProtectedBlock +{ + protected static final Logger LOGGER = Logger.getLogger("Minecraft"); + protected final transient ComboPooledDataSource cpds; + + protected abstract PreparedStatement getStatementCreateTable(Connection conn) throws SQLException; + + protected abstract PreparedStatement getStatementUpdateFrom2_0Table(Connection conn) throws SQLException; + + protected abstract PreparedStatement getStatementDeleteAll(Connection conn) throws SQLException; + + protected abstract PreparedStatement getStatementInsert(Connection conn, String world, int x, int y, int z, String playerName) throws SQLException; + + protected abstract PreparedStatement getStatementPlayerCountByLocation(Connection conn, String world, int x, int y, int z, String playerName) throws SQLException; + + protected abstract PreparedStatement getStatementPlayersByLocation(Connection conn, String name, int x, int y, int z) throws SQLException; + + protected abstract PreparedStatement getStatementDeleteByLocation(Connection conn, String world, int x, int y, int z) throws SQLException; + + protected abstract PreparedStatement getStatementAllBlocks(Connection conn) throws SQLException; + + public ProtectedBlockJDBC(final String driver, final String url) throws PropertyVetoException + { + this(driver, url, null, null); + } + + public ProtectedBlockJDBC(final String driver, final String url, final String username, final String password) throws PropertyVetoException + { + cpds = new ComboPooledDataSource(); + cpds.setDriverClass(driver); + cpds.setJdbcUrl(url); + if (username != null) + { + cpds.setUser(username); + cpds.setPassword(password); + } + cpds.setMaxStatements(20); + createAndConvertTable(); + } + + private void createAndConvertTable() + { + Connection conn = null; + PreparedStatement ps = null; + try + { + conn = cpds.getConnection(); + ps = getStatementCreateTable(conn); + ps.execute(); + ps.close(); + ps = getStatementUpdateFrom2_0Table(conn); + ps.execute(); + } + catch (SQLException ex) + { + LOGGER.log(Level.SEVERE, null, ex); + } + finally + { + if (ps != null) + { + try + { + ps.close(); + } + catch (SQLException ex) + { + LOGGER.log(Level.SEVERE, null, ex); + } + } + if (conn != null) + { + try + { + conn.close(); + } + catch (SQLException ex) + { + LOGGER.log(Level.SEVERE, null, ex); + } + } + } + } + + @Override + public void clearProtections() + { + Connection conn = null; + PreparedStatement ps = null; + try + { + conn = cpds.getConnection(); + ps = getStatementDeleteAll(conn); + ps.executeUpdate(); + } + catch (SQLException ex) + { + LOGGER.log(Level.SEVERE, null, ex); + } + finally + { + if (ps != null) + { + try + { + ps.close(); + } + catch (SQLException ex) + { + LOGGER.log(Level.SEVERE, null, ex); + } + } + if (conn != null) + { + try + { + conn.close(); + } + catch (SQLException ex) + { + LOGGER.log(Level.SEVERE, null, ex); + } + } + } + } + + @Override + public void importProtections(final List<OwnedBlock> blocks) + { + for (OwnedBlock ownedBlock : blocks) + { + if (ownedBlock.playerName == null) + { + continue; + } + protectBlock(ownedBlock.world, ownedBlock.x, ownedBlock.y, ownedBlock.z, ownedBlock.playerName); + } + } + + @Override + public List<OwnedBlock> exportProtections() + { + Connection conn = null; + PreparedStatement ps = null; + ResultSet rs = null; + List<OwnedBlock> blocks = new ArrayList<OwnedBlock>(); + try + { + conn = cpds.getConnection(); + ps = getStatementAllBlocks(conn); + rs = ps.executeQuery(); + while (rs.next()) + { + OwnedBlock ob = new OwnedBlock( + rs.getInt(2), + rs.getInt(3), + rs.getInt(4), + rs.getString(1), + rs.getString(5)); + blocks.add(ob); + } + return blocks; + } + catch (SQLException ex) + { + LOGGER.log(Level.SEVERE, null, ex); + return blocks; + } + finally + { + if (rs != null) + { + try + { + rs.close(); + } + catch (SQLException ex) + { + LOGGER.log(Level.SEVERE, null, ex); + } + } + if (ps != null) + { + try + { + ps.close(); + } + catch (SQLException ex) + { + LOGGER.log(Level.SEVERE, null, ex); + } + } + if (conn != null) + { + try + { + conn.close(); + } + catch (SQLException ex) + { + LOGGER.log(Level.SEVERE, null, ex); + } + } + } + } + + @Override + public void protectBlock(final Block block, final String playerName) + { + protectBlock(block.getWorld().getName(), block.getX(), block.getY(), block.getZ(), playerName); + } + + private void protectBlock(final String world, final int x, final int y, final int z, final String playerName) + { + Connection conn = null; + PreparedStatement ps = null; + try + { + conn = cpds.getConnection(); + ps = getStatementInsert(conn, world, x, y, z, playerName); + ps.executeUpdate(); + } + catch (SQLException ex) + { + LOGGER.log(Level.SEVERE, null, ex); + } + finally + { + if (ps != null) + { + try + { + ps.close(); + } + catch (SQLException ex) + { + LOGGER.log(Level.SEVERE, null, ex); + } + } + if (conn != null) + { + try + { + conn.close(); + } + catch (SQLException ex) + { + LOGGER.log(Level.SEVERE, null, ex); + } + } + } + } + + @Override + public boolean isProtected(final Block block, final String playerName) + { + Connection conn = null; + PreparedStatement ps = null; + ResultSet rs = null; + try + { + conn = cpds.getConnection(); + ps = getStatementPlayerCountByLocation(conn, block.getWorld().getName(), block.getX(), block.getY(), block.getZ(), playerName); + rs = ps.executeQuery(); + return rs.next() && rs.getInt(1) > 0 && rs.getInt(2) == 0; + } + catch (SQLException ex) + { + LOGGER.log(Level.SEVERE, null, ex); + return true; + } + finally + { + if (rs != null) + { + try + { + rs.close(); + } + catch (SQLException ex) + { + LOGGER.log(Level.SEVERE, null, ex); + } + } + if (ps != null) + { + try + { + ps.close(); + } + catch (SQLException ex) + { + LOGGER.log(Level.SEVERE, null, ex); + } + } + if (conn != null) + { + try + { + conn.close(); + } + catch (SQLException ex) + { + LOGGER.log(Level.SEVERE, null, ex); + } + } + } + } + + @Override + public List<String> getOwners(final Block block) + { + Connection conn = null; + PreparedStatement ps = null; + ResultSet rs = null; + List<String> owners = new ArrayList<String>(); + try + { + conn = cpds.getConnection(); + ps = getStatementPlayersByLocation(conn, block.getWorld().getName(), block.getX(), block.getY(), block.getZ()); + rs = ps.executeQuery(); + while (rs.next()) + { + owners.add(rs.getString(1)); + } + return owners; + } + catch (SQLException ex) + { + LOGGER.log(Level.SEVERE, null, ex); + return owners; + } + finally + { + if (rs != null) + { + try + { + rs.close(); + } + catch (SQLException ex) + { + LOGGER.log(Level.SEVERE, null, ex); + } + } + if (ps != null) + { + try + { + ps.close(); + } + catch (SQLException ex) + { + LOGGER.log(Level.SEVERE, null, ex); + } + } + if (conn != null) + { + try + { + conn.close(); + } + catch (SQLException ex) + { + LOGGER.log(Level.SEVERE, null, ex); + } + } + } + } + + @Override + public int unprotectBlock(final Block block) + { + Connection conn = null; + PreparedStatement ps = null; + try + { + conn = cpds.getConnection(); + ps = getStatementDeleteByLocation(conn, block.getWorld().getName(), block.getX(), block.getY(), block.getZ()); + return ps.executeUpdate(); + } + catch (SQLException ex) + { + LOGGER.log(Level.SEVERE, null, ex); + return 0; + } + finally + { + if (ps != null) + { + try + { + ps.close(); + } + catch (SQLException ex) + { + LOGGER.log(Level.SEVERE, null, ex); + } + } + if (conn != null) + { + try + { + conn.close(); + } + catch (SQLException ex) + { + LOGGER.log(Level.SEVERE, null, ex); + } + } + } + } + + @Override + public void onPluginDeactivation() + { + cpds.close(); + } +} |