KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > sync4j > server > store > SyncPersistentStore


1 /**
2  * Copyright (C) 2003-2005 Funambol
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18
19 package sync4j.server.store;
20
21 import java.util.Map JavaDoc;
22 import java.util.HashMap JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.logging.Logger JavaDoc;
25 import java.util.logging.Level JavaDoc;
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 /**
41  * This is the store for information regarding the synchronization process and
42  * status. It persists the following classes:
43  *
44  * <ul>
45  * <li>sync4j.framework.server.LastTimestamp</li>
46  * <li>sync4j.framework.server.ClientMapping</li>
47  * </ul>
48  *
49  * This <i>PersistentStore</i> is configured with the following map key:
50  * <ul>
51  * <li>jndiDataSourceName</li>
52  * </ul>
53  *
54  * @author Stefano Fornari @ Funambol
55  *
56  * @version $Id: SyncPersistentStore.java,v 1.1 2005/05/16 17:32:57 nichele Exp $
57  */

58 public class SyncPersistentStore
59 extends BasePersistentStore
60 implements PersistentStore, java.io.Serializable JavaDoc {
61
62     // --------------------------------------------------------------- Constants
63

64     // -------------------------------------------------------------- Properties
65

66     // ------------------------------------------------------------ Private data
67

68     // ------------------------------------------------------------ Constructors
69

70     // ---------------------------------------------------------- Public methods
71

72     public boolean store(Object JavaDoc 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 JavaDoc 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     /** Read all objects stored the persistent media.
99      *
100      * @param objClass the object class handled by the persistent store
101      *
102      * @return an array containing the objects read. If no objects are found an
103      * empty array is returned. If the persistent store has not
104      * processed the quest, null is returned.
105      *
106      * @throws PersistentStoreException
107      *
108      */

109     public Object JavaDoc[] read(Class JavaDoc objClass) throws PersistentStoreException {
110         //
111
// TO DO (not used yet)
112
//
113
return null;
114     }
115
116
117     // ------------------------------------------------------- Protected methods
118
// --------------------------------------------------------- Private methods
119

120     /**
121      * Store the last timestamp.
122      * Into tagServer there is the Next anchor sent by client: now this become
123      * a Last anchor of the server.
124      * Into tagClient there is the Next anchor sent by server: now this become
125      * a Last anchor of the client.
126      *
127      * @param l the LastTimestamp object with all information about start and
128      * end sync timestamp
129      *
130      */

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                 //
152
// The first time!!!
153
//
154
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 JavaDoc mapping = new HashMap JavaDoc();
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 JavaDoc principal = clientMapping.getPrincipal().getId();
238         String JavaDoc 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 JavaDoc clientMap = clientMapping.getDeletedEntries();
250                 Iterator JavaDoc i = clientMap.keySet().iterator();
251                 while (i.hasNext()) {
252                     String JavaDoc key = (String JavaDoc) 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 JavaDoc clientMap = clientMapping.getModifiedEntries();
263                 Iterator JavaDoc i = clientMap.keySet().iterator();
264                 while (i.hasNext()) {
265                     int n = -1;
266
267                     String JavaDoc key = (String JavaDoc) i.next();
268                     stmt.setString(1, key);
269                     stmt.setString(4, (String JavaDoc) clientMap.get(key));
270                     n = stmt.executeUpdate();
271
272                     if (n == 0) {
273                         //
274
// insert new mapping
275
//
276
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 JavaDoc) 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     // ---------------------------------------------------------- SQL properties
298

299     /**
300      * The SQL query to insert the last timestamp
301      */

302     private String JavaDoc sqlInsertLastTimestamp =
303     "insert into sync4j_last_sync (principal, sync_source, last_anchor_server, last_anchor_client, start_sync, end_sync) values(?, ?, ?, ?, ?, ?)";
304
305     /** Getter for property sqlInsertLastTimestamp.
306      * @return Value of property sqlInsertLastTimestamp.
307      *
308      */

309     public String JavaDoc getSqlInsertLastTimestamp() {
310         return sqlInsertLastTimestamp;
311     }
312
313     /** Setter for property sqlInsertLastTimestamp.
314      * @param sqlInsertLastTimestamp New value of property sqlInsertLastTimestamp.
315      *
316      */

317     public void setSqlInsertLastTimestamp(String JavaDoc sqlInsertLastTimestamp) {
318         this.sqlInsertLastTimestamp = sqlInsertLastTimestamp;
319     }
320
321     /**
322      * The SQL query to update the last timestamp
323      */

324     private String JavaDoc sqlUpdateLastTimestamp =
325     "update sync4j_last_sync set last_anchor_server=?,last_anchor_client=?,start_sync=?,end_sync=? where principal=? and sync_source=?";
326
327     /** Getter for property sqlUpdateLastTimestamp.
328      * @return Value of property sqlUpdateLastTimestamp.
329      *
330      */

331     public String JavaDoc getSqlUpdateLastTimestamp() {
332         return sqlUpdateLastTimestamp;
333     }
334
335     /** Setter for property sqlUpdateLastTimestamp.
336      * @param sqlUpdateLastTimestamp New value of property sqlUpdateLastTimestamp.
337      *
338      */

339     public void setSqlUpdateLastTimestamp(String JavaDoc sqlUpdateLastTimestamp) {
340         this.sqlUpdateLastTimestamp = sqlUpdateLastTimestamp;
341     }
342
343     /**
344      * The SQL query to select the last timestamp
345      */

346     private String JavaDoc sqlSelectLastTimestamp =
347     "select last_anchor_server, last_anchor_client,start_sync,end_sync from sync4j_last_sync where principal=? and sync_source=?";
348
349     /** Getter for property sqlUpdateLastTimestamp.
350      * @return Value of property sqlUpdateLastTimestamp.
351      *
352      */

353     public String JavaDoc getSqlSelectLastTimestamp() {
354         return sqlSelectLastTimestamp;
355     }
356
357     /** Setter for property sqlUpdateLastTimestamp.
358      * @param sqlSelectLastTimestamp New value of property sqlUpdateLastTimestamp.
359      *
360      */

361     public void setSqlSelectLastTimestamp(String JavaDoc sqlSelectLastTimestamp) {
362         this.sqlSelectLastTimestamp = sqlSelectLastTimestamp;
363     }
364
365     // Configurable SQL queries for Client Mapping persistence
366
private String JavaDoc sqlInsertClientMapping =
367     "insert into sync4j_client_mapping (principal, sync_source, luid, guid) values(?, ?, ?, ?)";
368     private String JavaDoc sqlDeleteClientMapping =
369     "delete from sync4j_client_mapping where principal=? and sync_source=? and luid=?";
370     private String JavaDoc sqlUpdateClientMapping =
371     "update sync4j_client_mapping set luid=? where principal=? and sync_source=? and guid=?";
372     private String JavaDoc sqlSelectClientMapping =
373     "select luid,guid from sync4j_client_mapping where principal=? and sync_source=?";
374
375     /** Getter for property sqlInsertClientMapping.
376      * @return Value of property sqlInsertClientMapping.
377      *
378      */

379     public String JavaDoc getSqlInsertClientMapping() {
380         return sqlInsertClientMapping;
381     }
382
383     /** Setter for property sqlInsertClientMapping.
384      * @param sqlInsertClientMapping New value of property sqlInsertClientMapping.
385      *
386      */

387     public void setSqlInsertClientMapping(String JavaDoc sqlInsertClientMapping) {
388         this.sqlInsertClientMapping = sqlInsertClientMapping;
389     }
390
391     /** Getter for property sqlDeleteClientMapping.
392      * @return Value of property sqlDeleteClientMapping.
393      *
394      */

395     public String JavaDoc getSqlDeleteClientMapping() {
396         return sqlDeleteClientMapping;
397     }
398
399     /** Setter for property sqlDeleteClientMapping.
400      * @param sqlDeleteClientMapping New value of property sqlDeleteClientMapping.
401      *
402      */

403     public void setSqlDeleteClientMapping(String JavaDoc sqlDeleteClientMapping) {
404         this.sqlDeleteClientMapping = sqlDeleteClientMapping;
405     }
406
407     /** Getter for property sqlUpdateClientMapping.
408      * @return Value of property sqlUpdateClientMapping.
409      *
410      */

411     public String JavaDoc getSqlUpdateClientMapping() {
412         return sqlUpdateClientMapping;
413     }
414
415     /** Setter for property sqlUpdateClientMapping.
416      * @param sqlUpdateClientMapping New value of property sqlUpdateClientMapping.
417      *
418      */

419     public void setSqlUpdateClientMapping(String JavaDoc sqlUpdateClientMapping) {
420         this.sqlUpdateClientMapping = sqlUpdateClientMapping;
421     }
422
423     /** Getter for property sqlSelectClientMapping.
424      * @return Value of property sqlSelectClientMapping.
425      *
426      */

427     public String JavaDoc getSqlSelectClientMapping() {
428         return sqlSelectClientMapping;
429     }
430
431     /** Setter for property sqlSelectClientMapping.
432      * @param sqlSelectClientMapping New value of property sqlSelectClientMapping.
433      *
434      */

435     public void setSqlSelectClientMapping(String JavaDoc sqlSelectClientMapping) {
436         this.sqlSelectClientMapping = sqlSelectClientMapping;
437     }
438
439     public boolean delete(Object JavaDoc o) throws PersistentStoreException
440     {
441         return false;
442     }
443
444     public Object JavaDoc[] read(Object JavaDoc o, Clause clause) throws PersistentStoreException
445     {
446         return null;
447     }
448
449     public boolean store(String JavaDoc id, Object JavaDoc o, String JavaDoc operation) throws PersistentStoreException
450     {
451         return false;
452     }
453
454     public int readCounter(String JavaDoc idSpace, int increment)
455         throws PersistentStoreException {
456         throw new PersistentStoreException(ERROR_MSG_NOT_SUPPORTED);
457     }
458 }
Popular Tags