1 18 19 package sync4j.server.store; 20 21 import java.util.ArrayList ; 22 import java.sql.*; 23 24 import sync4j.framework.tools.Base64; 25 import sync4j.framework.tools.DBTools; 26 import sync4j.framework.server.store.*; 27 import sync4j.framework.server.Sync4jDevice; 28 import sync4j.framework.server.Capabilities; 29 30 import sync4j.server.tools.IdSpaceGenerator; 31 32 44 public class DevicePersistentStore 45 extends BasePersistentStore 46 implements PersistentStore, java.io.Serializable { 47 48 50 private static final String DEFAULT_SERVER_PASSWORD = "sync4j"; 51 52 private static final String OPT_INSERT = "INSERT"; 53 54 public static final int SQL_SELECT_ALL_DEVICES = 0; 55 public static final int SQL_GET_DEVICE = 1; 56 public static final int SQL_INSERT_DEVICE = 2; 57 public static final int SQL_UPDATE_DEVICE = 3; 58 public static final int SQL_DELETE_DEVICE = 4; 59 public static final int SQL_COUNT_DEVICES = 5; 60 61 public static final String NS_DEVICE = "device" ; 62 63 65 protected String [] sql = null; 66 67 public void setSql(String [] sql) { 68 this.sql = sql; 69 } 70 71 public String [] getSql() { 72 return this.sql; 73 } 74 75 private static final String SEARCH_COUNT_DEVICES = "SCD"; 77 78 81 88 public boolean delete(Object o) throws PersistentStoreException { 89 if (o instanceof Sync4jDevice) { 90 Sync4jDevice d = (Sync4jDevice) o; 91 92 Connection conn = null; 93 PreparedStatement stmt = null; 94 95 try { 96 conn = dataSource.getConnection(); 97 98 stmt = conn.prepareStatement(sql[SQL_DELETE_DEVICE]); 99 stmt.setString(1, d.getDeviceId()); 100 stmt.executeUpdate(); 101 102 return true; 103 } catch (SQLException e) { 104 throw new PersistentStoreException("Error deleting the device " + d, e); 105 } finally { 106 DBTools.close(conn, stmt, null); 107 } 108 } 109 return false; 110 } 111 112 public boolean store(Object o, String operation) throws PersistentStoreException { 113 return false; 114 } 115 116 123 public boolean store(Object o) throws PersistentStoreException { 124 if (o instanceof Sync4jDevice) { 125 Sync4jDevice d = (Sync4jDevice) o; 126 127 Connection conn = null; 128 PreparedStatement stmt = null; 129 int n = 0; 130 131 try { 132 conn = dataSource.getConnection(); 133 134 String serverPwd = DEFAULT_SERVER_PASSWORD; 135 136 if (d.getServerPassword() != null) { 137 serverPwd = d.getServerPassword(); 138 } 139 140 if (d.getDeviceId() != null && !d.getDeviceId().equals("")) { 141 stmt = conn.prepareStatement(sql[SQL_UPDATE_DEVICE]); 142 143 stmt.setString(1, d.getDescription()); 144 stmt.setString(2, d.getType()); 145 byte[] nonce = d.getClientNonce(); 146 stmt.setString(3, ((nonce != null) ? new String (Base64.encode(nonce)) : null)); 147 nonce = d.getServerNonce(); 148 stmt.setString(4, ((nonce != null) ? new String (Base64.encode(nonce)) : null)); 149 stmt.setString(5, serverPwd); 150 stmt.setString(6, d.getTimeZone()); 151 stmt.setString(7, (d.getConvertDate() ? "Y" : "N" )); 152 stmt.setString(8, d.getCharset()); 153 stmt.setString(9, d.getDeviceId()); 154 155 n = stmt.executeUpdate(); 156 stmt.close(); stmt = null; 157 } 158 159 if (n == 0) { 160 if (d.getDeviceId() == null || d.getDeviceId().equals("")) { 162 int deviceId = getDeviceId(); 163 d.setDeviceId("" + deviceId); 164 } 165 166 stmt = conn.prepareStatement(sql[SQL_INSERT_DEVICE]); 167 stmt.setString(1, d.getDeviceId()); 168 stmt.setString(2, d.getDescription()); 169 stmt.setString(3, d.getType()); 170 byte[] nonce = d.getClientNonce(); 171 stmt.setString(4, ((nonce != null) ? new String (Base64.encode(nonce)) : null)); 172 nonce = d.getServerNonce(); 173 stmt.setString(5, ((nonce != null) ? new String (Base64.encode(nonce)) : null)); 174 175 stmt.setString(6, serverPwd); 176 177 stmt.setInt(7, 2); 181 stmt.setString(8, d.getTimeZone()); 182 stmt.setString(9, (d.getConvertDate() ? "Y" : "N" )); 183 stmt.setString(10, d.getCharset()); 184 185 stmt.executeUpdate(); 186 } 187 } catch (SQLException e) { 188 throw new PersistentStoreException("Error storing the device " + d, e); 189 } finally { 190 DBTools.close(conn, stmt, null); 191 } 192 return true; 193 } 194 return false; 195 } 196 197 204 public boolean read(Object o) throws PersistentStoreException { 205 if (o instanceof Sync4jDevice) { 206 Sync4jDevice d = (Sync4jDevice) o; 207 208 Connection conn = null; 209 PreparedStatement stmt = null; 210 ResultSet rs = null; 211 212 try { 213 conn = dataSource.getConnection(); 214 215 stmt = conn.prepareStatement(sql[SQL_GET_DEVICE]); 216 stmt.setString(1, d.getDeviceId()); 217 rs = stmt.executeQuery(); 218 219 if (rs.next() == false) { 220 throw new NotFoundException("Device not found for '" 221 + d.getDeviceId() + "'"); 222 } 223 224 d.setDescription(rs.getString(1)); 225 d.setType (rs.getString(2)); 226 String value = rs.getString(3); 227 if (value == null) { 228 d.setClientNonce(null); 229 } else if (value.equals("")) { 230 d.setClientNonce(new byte[0]); 231 } else { 232 d.setClientNonce(Base64.decode(value.getBytes())); 233 } 234 value = rs.getString(4); 235 if (value == null) { 236 d.setServerNonce(null); 237 } else if (value.equals("")) { 238 d.setServerNonce(new byte[0]); 239 } else { 240 d.setServerNonce(Base64.decode(value.getBytes())); 241 } 242 d.setServerPassword(rs.getString(5)); 243 d.setTimeZone(rs.getString(6)); 244 d.setConvertDate("Y".equals(rs.getString(7))); 245 d.setCharset(rs.getString(8)); 246 247 248 Capabilities caps = new Capabilities(); 249 caps.setId (rs.getString(9)); 250 caps.setSupportLargeObject ("Y".equals(rs.getString(10))); 251 caps.setSupportNumberOfChanges("Y".equals(rs.getString(11))); 252 d.setCapabilities(caps); 253 254 } catch (SQLException e) { 255 throw new PersistentStoreException("Error reading the device " + d, e); 256 } finally { 257 DBTools.close(conn, stmt, rs); 258 } 259 return true; 260 } 261 return false; 262 } 263 264 public Object [] read(Class objClass) throws PersistentStoreException { 265 return null; 266 } 267 268 275 public Object [] read(Object o, Clause clause) throws PersistentStoreException { 276 if (o instanceof Sync4jDevice) { 277 Connection conn = null; 278 PreparedStatement stmt = null; 279 ResultSet rs = null; 280 281 ArrayList ret = new ArrayList (); 282 283 try { 284 conn = dataSource.getConnection(); 285 286 PreparedWhere where = clause.getPreparedWhere(); 287 String query = sql[SQL_SELECT_ALL_DEVICES]; 288 if (where.sql.length() > 0) { 289 query += " where " + where.sql; 290 } 291 stmt = conn.prepareStatement(query); 292 for (int i=0; i<where.parameters.length; ++i) { 293 stmt.setObject(i+1, where.parameters[i]); 294 } 295 rs = stmt.executeQuery(); 296 297 while (rs.next()) { 298 Sync4jDevice d = new Sync4jDevice(); 299 d.setDeviceId (rs.getString(1)); 300 d.setDescription (rs.getString(2)); 301 d.setType (rs.getString(3)); 302 303 String value = rs.getString(4); 304 if (value == null) { 305 d.setClientNonce(null); 306 } else if (value.equals("")) { 307 d.setClientNonce(new byte[0]); 308 } else { 309 d.setClientNonce(Base64.decode(value.getBytes())); 310 } 311 312 value = rs.getString(5); 313 if (value == null) { 314 d.setServerNonce(null); 315 } else if (value.equals("")) { 316 d.setServerNonce(new byte[0]); 317 } else { 318 d.setServerNonce(Base64.decode(value.getBytes())); 319 } 320 d.setServerPassword(rs.getString(6)); 321 d.setTimeZone(rs.getString(7)); 322 d.setConvertDate("Y".equals(rs.getString(8))); 323 d.setCharset(rs.getString(9)); 324 325 ret.add(d); 326 } 327 return ret.toArray(new Sync4jDevice[ret.size()]); 328 } catch (SQLException e) { 329 throw new PersistentStoreException("Error reading devices", e); 330 } finally { 331 DBTools.close(conn, stmt, rs); 332 } 333 } 334 return null; 335 } 336 337 338 343 public int count(Object o, Clause clause) throws PersistentStoreException { 344 if (o instanceof Sync4jDevice) { 345 Connection conn = null; 346 PreparedStatement stmt = null; 347 ResultSet rs = null; 348 int n = 0; 349 350 try { 351 conn = dataSource.getConnection(); 352 353 PreparedWhere where = clause.getPreparedWhere(); 354 String query = sql[SQL_COUNT_DEVICES]; 355 if (where.sql.length()>0) { 356 query += " where " + where.sql; 357 } 358 stmt = conn.prepareStatement(query); 359 for (int i=0; i<where.parameters.length; ++i) { 360 stmt.setObject(i+1, where.parameters[i]); 361 } 362 rs = stmt.executeQuery(); 363 364 while (rs.next()) { 365 n = rs.getInt(1); 366 } 367 return n; 368 } catch (SQLException e) { 369 throw new PersistentStoreException("Error reading count devices ", e); 370 } finally { 371 DBTools.close(conn, stmt, rs); 372 } 373 } 374 return -1; 375 } 376 378 382 private int getDeviceId() { 383 IdSpaceGenerator idGenerator = new IdSpaceGenerator(NS_DEVICE); 384 return Integer.parseInt(idGenerator.next()); 385 } 386 } 387 388 | Popular Tags |