1 18 19 package sync4j.server.store; 20 21 import java.util.Map ; 22 import java.util.HashMap ; 23 import java.util.Iterator ; 24 import java.util.logging.Logger ; 25 import java.util.logging.Level ; 26 27 import java.sql.*; 28 29 import sync4j.framework.tools.DBTools; 30 import sync4j.framework.logging.Sync4jLogger; 31 32 import sync4j.framework.server.LastTimestamp; 33 import sync4j.framework.server.ClientMapping; 34 import sync4j.framework.server.store.Clause; 35 import sync4j.framework.server.store.PersistentStore; 36 import sync4j.framework.server.store.BasePersistentStore; 37 import sync4j.framework.server.store.NotFoundException; 38 import sync4j.framework.server.store.PersistentStoreException; 39 40 58 public class SyncPersistentStore 59 extends BasePersistentStore 60 implements PersistentStore, java.io.Serializable { 61 62 64 66 68 70 72 public boolean store(Object o) 73 throws PersistentStoreException { 74 if (o instanceof LastTimestamp) { 75 storeLastTimestamp((LastTimestamp) o); 76 return true; 77 } else if (o instanceof ClientMapping) { 78 storeClientMapping((ClientMapping) o); 79 return true; 80 } 81 82 return false; 83 } 84 85 public boolean read(Object o) 86 throws PersistentStoreException { 87 if (o instanceof LastTimestamp) { 88 readLastTimestamp((LastTimestamp) o); 89 return true; 90 } else if (o instanceof ClientMapping) { 91 readClientMapping((ClientMapping) o); 92 return true; 93 } 94 95 return false; 96 } 97 98 109 public Object [] read(Class objClass) throws PersistentStoreException { 110 return null; 114 } 115 116 117 120 131 private void storeLastTimestamp(LastTimestamp l) 132 throws PersistentStoreException { 133 Connection conn = null; 134 PreparedStatement stmt = null; 135 136 try { 137 conn = dataSource.getConnection(); 138 139 stmt = conn.prepareStatement(sqlUpdateLastTimestamp); 140 141 stmt.setString (1, l.tagServer ); 142 stmt.setString (2, l.tagClient ); 143 stmt.setTimestamp(3, new Timestamp(l.start)); 144 stmt.setTimestamp(4, new Timestamp(l.end) ); 145 stmt.setString (5, l.principal ); 146 stmt.setString (6, l.database ); 147 148 int n = stmt.executeUpdate(); 149 150 if (n == 0) { 151 stmt.close(); 155 156 stmt = conn.prepareStatement(sqlInsertLastTimestamp); 157 158 stmt.setString (1, l.principal ); 159 stmt.setString (2, l.database ); 160 stmt.setString (3, l.tagServer ); 161 stmt.setString (4, l.tagClient ); 162 stmt.setTimestamp(5, new Timestamp(l.start)); 163 stmt.setTimestamp(6, new Timestamp(l.end) ); 164 165 stmt.executeUpdate(); 166 } 167 } catch (SQLException e) { 168 throw new PersistentStoreException("Error storing last timestamp", e); 169 } finally { 170 DBTools.close(conn, stmt, null); 171 } 172 } 173 174 protected void readLastTimestamp(LastTimestamp l) 175 throws PersistentStoreException { 176 Connection conn = null; 177 PreparedStatement stmt = null; 178 ResultSet rs = null; 179 180 try { 181 conn = dataSource.getConnection(); 182 183 stmt = conn.prepareStatement(sqlSelectLastTimestamp); 184 185 stmt.setString(1, l.principal); 186 stmt.setString(2, l.database); 187 188 rs = stmt.executeQuery(); 189 190 if (rs.next() == false) { 191 throw new NotFoundException("Last timestamp not found for " 192 + l.toString() 193 ); 194 } 195 196 l.tagServer = rs.getString (1) ; 197 l.tagClient = rs.getString (2) ; 198 l.start = rs.getTimestamp(3).getTime(); 199 l.end = rs.getTimestamp(4).getTime(); 200 } catch (SQLException e) { 201 throw new PersistentStoreException("Error reading last timestamp", e); 202 } finally { 203 DBTools.close(conn, stmt, rs); 204 } 205 } 206 207 private void readClientMapping(ClientMapping clientMapping) 208 throws PersistentStoreException { 209 Connection conn = null; 210 PreparedStatement stmt = null; 211 ResultSet rs = null; 212 213 try { 214 conn = dataSource.getConnection(); 215 216 stmt = conn.prepareStatement(sqlSelectClientMapping); 217 stmt.setString(1, clientMapping.getPrincipal().getId()); 218 stmt.setString(2, clientMapping.getDbURI() ); 219 rs = stmt.executeQuery(); 220 HashMap mapping = new HashMap (); 221 while (rs.next()) { 222 mapping.put(rs.getString("luid"), rs.getString("guid")); 223 } 224 clientMapping.initializeFromMapping(mapping); 225 } catch (SQLException e) { 226 throw new PersistentStoreException("Error reading mapping", e); 227 } finally { 228 DBTools.close(conn, stmt, rs); 229 } 230 } 231 232 private void storeClientMapping(ClientMapping clientMapping) 233 throws PersistentStoreException { 234 Connection conn = null; 235 PreparedStatement stmt = null, stmtIns = null; 236 237 String principal = clientMapping.getPrincipal().getId(); 238 String dbURI = clientMapping.getDbURI() ; 239 240 assert ((principal != null) && (dbURI != null)); 241 242 try { 243 conn = dataSource.getConnection(); 244 245 if (clientMapping.isDeleted()) { 246 stmt = conn.prepareStatement(sqlDeleteClientMapping); 247 stmt.setString(1, principal); 248 stmt.setString(2, dbURI ); 249 Map clientMap = clientMapping.getDeletedEntries(); 250 Iterator i = clientMap.keySet().iterator(); 251 while (i.hasNext()) { 252 String key = (String ) i.next(); 253 stmt.setString(3, key); 254 stmt.executeUpdate(); 255 } 256 } 257 258 if (clientMapping.isModified()) { 259 stmt = conn.prepareStatement(sqlUpdateClientMapping); 260 stmt.setString(2, principal); 261 stmt.setString(3, dbURI ); 262 Map clientMap = clientMapping.getModifiedEntries(); 263 Iterator i = clientMap.keySet().iterator(); 264 while (i.hasNext()) { 265 int n = -1; 266 267 String key = (String ) i.next(); 268 stmt.setString(1, key); 269 stmt.setString(4, (String ) clientMap.get(key)); 270 n = stmt.executeUpdate(); 271 272 if (n == 0) { 273 stmtIns = conn.prepareStatement(sqlInsertClientMapping); 277 stmtIns.setString(1, principal); 278 stmtIns.setString(2, dbURI ); 279 280 stmtIns.setString(3, key); 281 stmtIns.setString(4, (String ) clientMap.get(key)); 282 stmtIns.executeUpdate(); 283 stmtIns.close(); 284 } 285 286 } 287 } 288 289 } catch (SQLException e) { 290 throw new PersistentStoreException("Error storing client mapping", e); 291 } finally { 292 DBTools.close(conn, stmt, null); 293 } 294 295 } 296 297 299 302 private String sqlInsertLastTimestamp = 303 "insert into sync4j_last_sync (principal, sync_source, last_anchor_server, last_anchor_client, start_sync, end_sync) values(?, ?, ?, ?, ?, ?)"; 304 305 309 public String getSqlInsertLastTimestamp() { 310 return sqlInsertLastTimestamp; 311 } 312 313 317 public void setSqlInsertLastTimestamp(String sqlInsertLastTimestamp) { 318 this.sqlInsertLastTimestamp = sqlInsertLastTimestamp; 319 } 320 321 324 private String sqlUpdateLastTimestamp = 325 "update sync4j_last_sync set last_anchor_server=?,last_anchor_client=?,start_sync=?,end_sync=? where principal=? and sync_source=?"; 326 327 331 public String getSqlUpdateLastTimestamp() { 332 return sqlUpdateLastTimestamp; 333 } 334 335 339 public void setSqlUpdateLastTimestamp(String sqlUpdateLastTimestamp) { 340 this.sqlUpdateLastTimestamp = sqlUpdateLastTimestamp; 341 } 342 343 346 private String sqlSelectLastTimestamp = 347 "select last_anchor_server, last_anchor_client,start_sync,end_sync from sync4j_last_sync where principal=? and sync_source=?"; 348 349 353 public String getSqlSelectLastTimestamp() { 354 return sqlSelectLastTimestamp; 355 } 356 357 361 public void setSqlSelectLastTimestamp(String sqlSelectLastTimestamp) { 362 this.sqlSelectLastTimestamp = sqlSelectLastTimestamp; 363 } 364 365 private String sqlInsertClientMapping = 367 "insert into sync4j_client_mapping (principal, sync_source, luid, guid) values(?, ?, ?, ?)"; 368 private String sqlDeleteClientMapping = 369 "delete from sync4j_client_mapping where principal=? and sync_source=? and luid=?"; 370 private String sqlUpdateClientMapping = 371 "update sync4j_client_mapping set luid=? where principal=? and sync_source=? and guid=?"; 372 private String sqlSelectClientMapping = 373 "select luid,guid from sync4j_client_mapping where principal=? and sync_source=?"; 374 375 379 public String getSqlInsertClientMapping() { 380 return sqlInsertClientMapping; 381 } 382 383 387 public void setSqlInsertClientMapping(String sqlInsertClientMapping) { 388 this.sqlInsertClientMapping = sqlInsertClientMapping; 389 } 390 391 395 public String getSqlDeleteClientMapping() { 396 return sqlDeleteClientMapping; 397 } 398 399 403 public void setSqlDeleteClientMapping(String sqlDeleteClientMapping) { 404 this.sqlDeleteClientMapping = sqlDeleteClientMapping; 405 } 406 407 411 public String getSqlUpdateClientMapping() { 412 return sqlUpdateClientMapping; 413 } 414 415 419 public void setSqlUpdateClientMapping(String sqlUpdateClientMapping) { 420 this.sqlUpdateClientMapping = sqlUpdateClientMapping; 421 } 422 423 427 public String getSqlSelectClientMapping() { 428 return sqlSelectClientMapping; 429 } 430 431 435 public void setSqlSelectClientMapping(String sqlSelectClientMapping) { 436 this.sqlSelectClientMapping = sqlSelectClientMapping; 437 } 438 439 public boolean delete(Object o) throws PersistentStoreException 440 { 441 return false; 442 } 443 444 public Object [] read(Object o, Clause clause) throws PersistentStoreException 445 { 446 return null; 447 } 448 449 public boolean store(String id, Object o, String operation) throws PersistentStoreException 450 { 451 return false; 452 } 453 454 public int readCounter(String idSpace, int increment) 455 throws PersistentStoreException { 456 throw new PersistentStoreException(ERROR_MSG_NOT_SUPPORTED); 457 } 458 } | Popular Tags |