KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > sync4j > framework > server > ClientMapping


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 package sync4j.framework.server;
19
20 import java.util.Map JavaDoc;
21 import java.util.HashMap JavaDoc;
22 import java.util.ArrayList JavaDoc;
23
24 import sync4j.framework.logging.Sync4jLogger;
25
26 import java.util.logging.Logger JavaDoc;
27 import java.util.logging.Level JavaDoc;
28
29 import sync4j.framework.security.Sync4jPrincipal;
30
31 import org.apache.commons.lang.builder.ToStringBuilder;
32
33 /**
34  * Represent the mapping between the LUID from the client Items and
35  * the GUID from the server Items.
36  *
37  * @version $Id: ClientMapping.java,v 1.8 2005/03/02 20:57:38 harrie Exp $
38  */

39 public class ClientMapping {
40
41     //Contain GUID -> LUID for each client
42
private HashMap JavaDoc clientMapping = new HashMap JavaDoc();
43
44     //Contain the principal of the client device
45
private Sync4jPrincipal principal = null;
46
47     // the requested database
48
private String JavaDoc dbURI;
49
50     //When the Client mapping are modified or added
51
private boolean modified = false;
52     private ArrayList JavaDoc modifiedKeys = new ArrayList JavaDoc();
53
54     //When entries in the mapping are deleted
55
private boolean deleted = false;
56     private ArrayList JavaDoc deletedKeys = new ArrayList JavaDoc();
57
58     //Sync4j Logging faciiy
59
//Transient keyword will disable serialisation for this member
60
private transient Logger log = Sync4jLogger.getLogger();
61
62     // ------------------------------------------------------------- Contructors
63

64     /**
65      * Construct a client mapping to a device Id the mapping must be populate
66      * with data from the persistence store by calling
67      * PersistenceStoreManager.read.
68      *
69      */

70     public ClientMapping(Sync4jPrincipal principal, String JavaDoc dbURI) {
71         this.principal = principal;
72         this.dbURI = dbURI ;
73     }
74
75     // ---------------------------------------------------------- Public methods
76

77     /**
78      * @return true if the mapping where modified or added since initialization
79      */

80     public boolean isModified() {
81         return modified;
82     }
83
84     /**
85      * @return true if item where deleted since the initialisation
86      */

87     public boolean isDeleted() {
88         return deleted;
89     }
90
91     /**
92      * Initialize the client mapping with data from the Persistence Store
93      * @param mapping A structure that contain GUID -> LUID
94      */

95     public void initializeFromMapping(Map JavaDoc mapping) {
96         resetMapping();
97         clientMapping.putAll(mapping);
98     }
99
100     /**
101      * Get the deleted LUIDs from the client mapping
102      * @return a String[] of LUIDs deleted
103      */

104     public String JavaDoc[] getDeletedLuids() {
105         if (deleted) {
106             return (String JavaDoc[])deletedKeys.toArray(new String JavaDoc[deletedKeys.size()]);
107         }
108
109         return new String JavaDoc[0];
110     }
111
112     /**
113      * Get the modified LUIDs from the client mapping
114      * @return a String[] of LUIDs modified
115      */

116     public String JavaDoc[] getModifiedLuids() {
117         if (modified) {
118             return (String JavaDoc[])modifiedKeys.toArray(new String JavaDoc[modifiedKeys.size()]);
119         }
120
121         return new String JavaDoc[0];
122     }
123
124     /**
125      * Get the current mapping.
126      *
127      * @return the current mapping as a Map
128      */

129     public Map JavaDoc getMapping() {
130         Map JavaDoc ret = new HashMap JavaDoc();
131         ret.putAll(clientMapping);
132         return ret;
133     }
134
135     /**
136      * Get the Client Device Id that correspond to that Client
137      * @return The Device Id
138      */

139     public String JavaDoc getClientDeviceId() {
140         return principal.getDeviceId();
141     }
142
143     /**
144      * Get the principal
145      *
146      * @return the principal
147      */

148     public Sync4jPrincipal getPrincipal() {
149         return principal;
150     }
151
152     /**
153      * Get the database uri
154      *
155      * @return <i>dbURI</i>
156      */

157     public String JavaDoc getDbURI() {
158         return dbURI;
159     }
160
161     /**
162      * Add a mapping between a GUID and a LUID from the server
163      * @param guid The GUID of the server item
164      * @param luid The LUID of the client item
165      */

166     private void addMapping(String JavaDoc guid, String JavaDoc luid) {
167
168         if (log.isLoggable(Level.FINEST)) {
169             log.finest("Adding mapping LUID-GUID " + luid + "-" + guid);
170         }
171
172         //
173
// if the same guid was already mapped to a luid, we have to remmeber
174
// the former one and remove it from the list of updated/deleted luids
175
//
176
String JavaDoc oldLuid = (String JavaDoc)clientMapping.put(guid, luid);
177         modifiedKeys.add(luid); modified = true;
178         
179         if (oldLuid != null) {
180             removeDeletedKey(oldLuid);
181             removeModifiedKey(oldLuid);
182         }
183
184         //
185
// If we add an entry that was considered deleted,
186
// remove it from the deleted entries.
187
//
188
removeDeletedKey(luid);
189     }
190
191     /**
192      * Get the mapped GUID for the given LUID.
193      * @param luid The LUID of the client item
194      * @return The mapped value for the key in input
195      */

196     public String JavaDoc getMappedValueForLuid(String JavaDoc luid) {
197         String JavaDoc result = null;
198
199         if (clientMapping.containsValue(luid)) {
200             java.util.Set JavaDoc keys = clientMapping.keySet();
201             java.util.Iterator JavaDoc itKeys = keys.iterator();
202             while (itKeys.hasNext()) {
203                 String JavaDoc key = (String JavaDoc)itKeys.next();
204                 String JavaDoc value = (String JavaDoc)clientMapping.get(key);
205                 if (value.equals(luid)) {
206                     return key;
207                 }
208             }
209         }
210
211         if (log.isLoggable(Level.FINEST)) {
212             log.finest("No mapping found for LUID: " + luid);
213         }
214         return result;
215     }
216
217     /**
218      * Get the mapped LUID key for the given GUID value.
219      * @param guid The GUID of the client item
220      * @return The mapped value for the key in input
221      */

222     public String JavaDoc getMappedValueForGuid(String JavaDoc guid) {
223         String JavaDoc result = null;
224         if ((result = (String JavaDoc) clientMapping.get(guid)) == null) {
225             if (log.isLoggable(Level.FINEST)) {
226                 log.finest("No mapping found for GUID: " + guid);
227             }
228         }
229         return result;
230     }
231
232     /**
233      * Remove a mapped values from the cache given a LUID.
234      * @param luid The LUID for the item from the client
235      */

236     public void removeMappedValuesForLuid(String JavaDoc luid) {
237         String JavaDoc guid = getMappedValueForLuid(luid);
238         if (guid != null) {
239             clientMapping.remove(guid);
240             addDeletedKey(luid);
241         }
242     }
243
244     /**
245      * Remove a mapped values from the cache given a GUID
246      * @param guid The GUID for the item from the client
247      */

248     public void removeMappedValuesForGuid(String JavaDoc guid) {
249         String JavaDoc luid = getMappedValueForGuid(guid);
250         if (luid != null) {
251             clientMapping.remove(guid);
252             addDeletedKey(luid);
253         }
254     }
255
256     /**
257      * Updates a key value mapping. If the mapping does not exist, it calls
258      * <code>addMapping(guid, luid)</code>, otherwise the existing mapping is
259      * updated.
260      *
261      * @param guid The GUID value from the server item
262      * @param luid The LUID value from the client item
263      */

264     public void updateMapping(String JavaDoc guid, String JavaDoc luid) {
265
266         if (log.isLoggable(Level.FINEST)) {
267             log.finest("Updating mapping LUID-GUID " + luid + "-" + guid);
268         }
269
270         //
271
// If new, just call addMapping
272
//
273
if (clientMapping.containsKey(guid) == false) {
274             addMapping(guid, luid);
275             return;
276         }
277
278         //
279
// if the same guid was already mapped to a luid, we have to remmeber
280
// the former one and remove it from the list of updated/deleted luids
281
//
282
String JavaDoc oldLuid = (String JavaDoc)clientMapping.put(guid, luid);
283         modifiedKeys.add(luid); modified = true;
284         
285         if (oldLuid != null) {
286             removeDeletedKey(oldLuid);
287             removeModifiedKey(oldLuid);
288         }
289         
290         //
291
// If we add an entry that was considered deleted,
292
// remove it from the deleted entries.
293
//
294
removeDeletedKey(luid);
295     }
296
297     /**
298      * Clears these mappings moving the existing and new mappings to deleted.
299      * This is used for example to re-initialize the LUID-GUID mapping for a
300      * slow sync.
301      */

302     public void clearMappings() {
303
304         deletedKeys.addAll(clientMapping.values());
305         deletedKeys.addAll(modifiedKeys);
306
307         clientMapping.clear();
308
309         modified = false;
310         deleted = true;
311     }
312
313     public String JavaDoc toString() {
314          ToStringBuilder sb = new ToStringBuilder(this);
315
316          sb.append("clientMapping", clientMapping);
317          sb.append("modifiedKeys" , modifiedKeys );
318          sb.append("deletedKeys" , deletedKeys );
319
320          return sb.toString();
321     }
322     
323     /**
324      * Resets the modified (updated/deleted) uids
325      */

326     public void resetModifiedKeys() {
327         //If we have modified item to the map
328
if (modified) {
329             modifiedKeys.clear();
330             modified = false;
331         }
332         //If we have deleted item to the map
333
if (deleted) {
334             deletedKeys.clear();
335             deleted = false;
336         }
337     }
338
339     // --------------------------------------------------------- Private methods
340

341     /**
342      * Reset the content of the Client mapping HashMap and the state
343      * of the current mapping.
344      */

345     private void resetMapping() {
346         //We clean the map
347
if (!clientMapping.isEmpty()) {
348             clientMapping.clear();
349         }
350         
351         resetModifiedKeys();
352     }
353
354     /**
355      * Remove LUID from the deleted entries
356      * @param luid The LUID for the item from the client
357      */

358     private void removeDeletedKey(String JavaDoc luid) {
359         if (deleted && deletedKeys.contains(luid)) {
360             if (log.isLoggable(Level.FINEST)) {
361                 log.finest("Removing deleted LUID: " + luid);
362             }
363             deletedKeys.remove(luid);
364             if (deletedKeys.size() == 0) {
365                 deleted = false;
366             }
367         }
368     }
369
370     /**
371      * Remove LUID from the modified entries
372      * @param luid The LUID for the item from the client
373      */

374     private void removeModifiedKey(String JavaDoc luid) {
375         if (modified && modifiedKeys.contains(luid)) {
376             if (log.isLoggable(Level.FINEST)) {
377                 log.finest("Removing deleted LUID: " + luid);
378             }
379             modifiedKeys.remove(luid);
380             if (modifiedKeys.size() == 0) {
381                 modified = false;
382             }
383         }
384     }
385
386     /**
387      * Add LUID into the deleted keys.
388      * @param luid The LUID for the item from the client
389      */

390     private void addDeletedKey(String JavaDoc luid) {
391         removeModifiedKey(luid);
392         deleted = true;
393         deletedKeys.add(luid);
394     }
395 }
396
Popular Tags