1 package org.jahia.services.lock; 2 3 import org.jahia.exceptions.JahiaException; 4 import java.sql.Connection ; 5 import java.sql.ResultSet ; 6 import java.sql.PreparedStatement ; 7 import java.sql.SQLException ; 8 import org.jahia.services.usermanager.JahiaUser; 9 import org.jahia.registries.ServicesRegistry; 10 11 20 21 class LockDB { 22 23 private static org.apache.log4j.Logger logger = 24 org.apache.log4j.Logger.getLogger(LockDB.class); 25 26 private static LockDB singletonInstance = null; 27 28 static private final String GET_LOCK_BYKEY_QUERY = "SELECT owner_locks, contextid_locks, timeout_locks, stolen_locks FROM jahia_locks WHERE name_locks=? AND targetid_locks=? AND action_locks=?"; 29 static private final String UPDATE_LOCK_BYKEY_QUERY = "UPDATE jahia_locks SET owner_locks=?, contextid_locks=?, timeout_locks=?, stolen_locks=? WHERE name_locks=? AND targetid_locks=? AND action_locks=?"; 30 static private final String INSERT_LOCK_QUERY = "INSERT INTO jahia_locks (name_locks, targetid_locks, action_locks, owner_locks, contextid_locks, timeout_locks, stolen_locks) VALUES (?, ?, ?, ?, ?, ?, ?)"; 31 static private final String REMOVE_LOCK_BYKEY_QUERY = "DELETE FROM jahia_locks WHERE name_locks=? AND targetid_locks=? AND action_locks=?"; 32 static private final String REMOVE_ALLLOCKS_QUERY = "DELETE FROM jahia_locks"; 33 34 private LockDB() { 35 } 36 37 40 public static synchronized LockDB getInstance () { 41 if (singletonInstance == null) { 42 singletonInstance = new LockDB(); 43 } 44 return singletonInstance; 45 } 46 47 49 protected Lock getLock (LockKey lockKey) 50 throws JahiaException { 51 52 Connection dbConn = null; 53 PreparedStatement stmt = null; 54 ResultSet rs = null; 55 Lock lock = null; 56 57 try { 58 59 dbConn = org.jahia.services.database.ConnectionDispenser.getConnection(); 60 stmt = dbConn.prepareStatement(GET_LOCK_BYKEY_QUERY); 61 stmt.setString(1, lockKey.getName()); 62 stmt.setInt(2, lockKey.getId()); 63 stmt.setString(3, lockKey.getAction()); 64 rs = stmt.executeQuery(); 65 66 if (rs.next()) { 68 String ownerName = rs.getString("owner_locks"); 70 String contextID = rs.getString("contextid_locks"); 71 int timeout = rs.getInt("timeout_locks"); 72 String booleanStr = rs.getString("stolen_locks"); 73 boolean stolen = false; 74 if ("true".equals(booleanStr)) { 75 stolen = true; 76 } 77 78 JahiaUser owner = ServicesRegistry.getInstance().getJahiaUserManagerService().lookupUser(ownerName); 79 80 lock = new Lock(owner, contextID, timeout, stolen); 81 } 82 83 } catch (SQLException se) { 84 throw new JahiaException("Cannot load lock " + lockKey.toString() + 85 "from the database", 86 se.getMessage(), 87 JahiaException.DATABASE_ERROR, 88 JahiaException.ERROR_SEVERITY, se); 89 } finally { 90 try { 91 if (stmt != null) 92 stmt.close(); 93 } catch (SQLException ex) { 94 throw new JahiaException("Cannot free resources", 95 "Cannot free resources", 96 JahiaException.DATABASE_ERROR, 97 JahiaException.WARNING_SEVERITY, ex); 98 } 99 } 100 101 return lock; 102 } 103 104 protected void createLock (LockKey lockKey, Lock lock) 105 throws JahiaException { 106 Connection dbConn = null; 107 PreparedStatement stmt = null; 108 ResultSet rs = null; 109 110 try { 111 dbConn = org.jahia.services.database.ConnectionDispenser.getConnection(); 112 stmt = dbConn.prepareStatement(INSERT_LOCK_QUERY); 113 stmt.setString(1, lockKey.getName()); 114 stmt.setInt(2, lockKey.getId()); 115 stmt.setString(3, lockKey.getAction()); 116 117 stmt.setString(4, lock.getOwner().getUserKey()); 118 stmt.setString(5, lock.getID()); 119 long secondsTimeout = lock.getTimeout() / 1000; 120 stmt.setInt(6, new Long (secondsTimeout).intValue()); 121 if (lock.isStealed()) { 122 stmt.setString(7, "true"); 123 } else { 124 stmt.setString(7, "false"); 125 } 126 127 stmt.executeUpdate(); 128 } catch (SQLException se) { 129 throw new JahiaException("Cannot store lock in the database", 130 se.getMessage(), 131 JahiaException.DATABASE_ERROR, 132 JahiaException.ERROR_SEVERITY, se); 133 } finally { 134 try { 135 if (stmt != null) 136 stmt.close(); 137 } catch (SQLException ex) { 138 throw new JahiaException("Cannot free resources", 139 "Cannot free resources", 140 JahiaException.DATABASE_ERROR, 141 JahiaException.WARNING_SEVERITY, ex); 142 } 143 } 144 } 145 146 protected void updateLock (LockKey lockKey, Lock lock) 147 throws JahiaException { 148 Connection dbConn = null; 149 PreparedStatement stmt = null; 150 ResultSet rs = null; 151 152 try { 153 dbConn = org.jahia.services.database.ConnectionDispenser.getConnection(); 154 stmt = dbConn.prepareStatement(UPDATE_LOCK_BYKEY_QUERY); 155 stmt.setString(1, lock.getOwner().getUserKey()); 156 stmt.setString(2, lock.getID()); 157 long secondsTimeout = lock.getTimeout() / 1000; 158 stmt.setInt(3, new Long (secondsTimeout).intValue()); 159 if (lock.isStealed()) { 160 stmt.setString(4, "true"); 161 } else { 162 stmt.setString(4, "false"); 163 } 164 165 stmt.setString(5, lockKey.getName()); 166 stmt.setInt(6, lockKey.getId()); 167 stmt.setString(7, lockKey.getAction()); 168 169 stmt.executeUpdate(); 170 } catch (SQLException se) { 171 throw new JahiaException("Cannot update lock "+lockKey+" in the database", 172 se.getMessage(), 173 JahiaException.DATABASE_ERROR, 174 JahiaException.ERROR_SEVERITY, se); 175 } finally { 176 try { 177 if (stmt != null) 178 stmt.close(); 179 } catch (SQLException ex) { 180 throw new JahiaException("Cannot free resources", 181 "Cannot free resources", 182 JahiaException.DATABASE_ERROR, 183 JahiaException.WARNING_SEVERITY, ex); 184 } 185 } 186 } 187 188 189 protected void removeLock (LockKey lockKey) 190 throws JahiaException { 191 192 Connection dbConn = null; 193 PreparedStatement stmt = null; 194 ResultSet rs = null; 195 196 try { 197 dbConn = org.jahia.services.database.ConnectionDispenser.getConnection(); 198 stmt = dbConn.prepareStatement(REMOVE_LOCK_BYKEY_QUERY); 199 stmt.setString(1, lockKey.getName()); 200 stmt.setInt(2, lockKey.getId()); 201 stmt.setString(3, lockKey.getAction()); 202 stmt.executeUpdate(); 203 204 } catch (SQLException se) { 205 throw new JahiaException("Cannot remove lock "+lockKey+" from database", 206 se.getMessage(), 207 JahiaException.DATABASE_ERROR, 208 JahiaException.ERROR_SEVERITY, se); 209 } finally { 210 try { 211 if (stmt != null) 212 stmt.close(); 213 } catch (SQLException ex) { 214 throw new JahiaException("Cannot free resources", 215 "Cannot free resources", 216 JahiaException.DATABASE_ERROR, 217 JahiaException.WARNING_SEVERITY, ex); 218 } 219 } 220 } 221 222 protected void removeAllLocks () 223 throws JahiaException { 224 225 Connection dbConn = null; 226 PreparedStatement stmt = null; 227 ResultSet rs = null; 228 229 try { 230 dbConn = org.jahia.services.database.ConnectionDispenser.getConnection(); 231 stmt = dbConn.prepareStatement(REMOVE_ALLLOCKS_QUERY); 232 stmt.executeUpdate(); 233 234 } catch (SQLException se) { 235 throw new JahiaException("Cannot remove all locks from database", 236 se.getMessage(), 237 JahiaException.DATABASE_ERROR, 238 JahiaException.ERROR_SEVERITY, se); 239 } finally { 240 try { 241 if (stmt != null) 242 stmt.close(); 243 } catch (SQLException ex) { 244 throw new JahiaException("Cannot free resources", 245 "Cannot free resources", 246 JahiaException.DATABASE_ERROR, 247 JahiaException.WARNING_SEVERITY, ex); 248 } 249 } 250 } 251 252 } | Popular Tags |