KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > sync4j > syncclient > spds > engine > recovery > AbstractRecovery


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.syncclient.spds.engine.recovery;
20
21 import java.io.*;
22
23 import java.util.Date JavaDoc;
24
25 import java.security.Principal JavaDoc;
26
27 import sync4j.syncclient.spds.SyncException;
28 import sync4j.syncclient.spds.engine.SyncItem;
29 import sync4j.syncclient.spds.engine.SyncItemImpl;
30 import sync4j.syncclient.spds.engine.SyncItemState;
31 import sync4j.syncclient.spds.engine.SyncSource;
32
33 /**
34  * This class defines <b>recovery</b> method to manage recovery
35  * database in slowsync status.
36  * From server reads those parameters:
37  * <pre>
38  * &lt;recovery-details>
39  * &lt;source-list>value&lt;/source-list>
40  * &lt;last>value&lt;/last>
41  * &lt;uri>value&lt;/uri>
42  * &lt;/recovery-details>
43  * </pre>
44  *
45  * @author Fabio Maggi
46  * @version $Id: AbstractRecovery.java,v 1.6 2005/07/14 10:18:02 harrie Exp $
47  **/

48
49 public abstract class AbstractRecovery implements SyncSource {
50
51
52     //----------------------------------------------------------------- Constants
53

54     private static final String JavaDoc PROP_RECOVERY_DETAILS = "recovery-details" ;
55
56     private static final String JavaDoc PROP_SOURCE_LIST = "source-list" ;
57     private static final String JavaDoc PROP_LAST = "last" ;
58     private static final String JavaDoc PROP_URI = "uri" ;
59
60     //----------------------------------------------------------------- Protected data
61

62     protected String JavaDoc sourceList = null;
63     protected String JavaDoc last = null;
64     protected String JavaDoc uri = null;
65
66     //----------------------------------------------------------------- Public methods
67

68     /** Getter for property sourceList.
69      * @return Value of property sourceList.
70      */

71     public String JavaDoc getSourceList() {
72         return this.sourceList;
73     }
74
75     /** Setter for property sourceList.
76      * @param sourceList New value of property sourceList.
77      */

78     public void setSourceList(String JavaDoc sourceList) {
79         this.sourceList=sourceList;
80     }
81
82     /** Getter for property last.
83      * @return Value of property last.
84      */

85     public String JavaDoc getLast() {
86         return this.last;
87     }
88
89     /** Setter for property last.
90      * @param last New value of property last.
91      */

92     public void setLast(String JavaDoc last) {
93         this.last=last;
94     }
95
96     /** Getter for property uri.
97      * @return Value of property uri.
98      */

99     public String JavaDoc getUri() {
100         return this.uri;
101     }
102
103     /** Setter for property uri.
104      * @param uri New value of property uri.
105      */

106     public void setUri(String JavaDoc uri) {
107         this.uri=uri;
108     }
109
110     /**
111      * This method manage recovery
112      * database in slowsync status
113      * @param p Principal
114      */

115     public abstract void recovery (Principal JavaDoc p);
116
117
118     /**
119      * Returns all items in the data store belonging to the given principal.
120      *
121      * @param principal the principal for which the data has to be considered
122      * Null means all principals
123      *
124      * @return an array of all <i>SyncItem</i>s stored in this source. If there
125      * are no items an empty array is returned.
126      *
127      * @throws SyncException in case of error (for instance if the
128      * underlying data store runs into problems)
129      */

130     public SyncItem[] getAllSyncItems(Principal JavaDoc principal) throws SyncException {
131
132         SyncItem[] syncItems = new SyncItem[0];
133
134         return syncItems;
135     }
136
137     /**
138      * Returns all deleted items belonging to the given principal and deleted
139      * after the given point in time.
140      *
141      * @param principal the principal for which the data has to be considered
142      * Null means all principals
143      * @param since consider the changes since this point in time. Null means
144      * all items regardless when they were changed.
145      *
146      * @return an array of keys containing the <i>SyncItem</i>'s key of the deleted
147      * items after the last synchronizazion. If there are no deleted
148      * items an empty array is returned.
149      *
150      * @throws SyncException in case of error (for instance if the
151      * underlying data store runs into problems)
152      */

153     public SyncItem[] getDeletedSyncItems(Principal JavaDoc principal,
154                                           Date JavaDoc since ) throws SyncException {
155         SyncItem[] syncItems = new SyncItem[0];
156
157         return syncItems;
158
159     }
160
161     /**
162      * Returns all new items belonging to the given principal and created
163      * after the given point in time.
164      *
165      * @param principal the principal for which the data has to be considered
166      * Null means all principals
167      * @param since consider the changes since this point in . Null means
168      * all items regardless when they were changed.
169      *
170      * @return an array of items containing representing the newly created items.
171      * If there are no new items an empty array MUST BE returned.
172      *
173      * @throws SyncException in case of error (for instance if the
174      * underlying data store runs into problems)
175      */

176     public SyncItem[] getNewSyncItems(Principal JavaDoc principal,
177                                       Date JavaDoc since ) throws SyncException {
178         SyncItem[] syncItems = new SyncItem[0];
179
180         return syncItems;
181     }
182
183     /**
184      * Returns all updated items belonging to the given principal and modified
185      * after the given point in time.
186      *
187      * @param principal the principal for which the data has to be considered
188      * Null means all principals
189      * @param since consider the changes since this point in time. Null means
190      * all items regardless when they were changed.
191      *
192      * @return an array of keys containing the <i>SyncItem</I>'s key of the updated
193      * items after the last synchronizazion. It MUST NOT return null for
194      * no keys, but instad an empty array.
195      */

196     public SyncItem[] getUpdatedSyncItems(Principal JavaDoc principal,
197                                           Date JavaDoc since ) throws SyncException {
198         SyncItem[] syncItems = new SyncItem[0];
199
200         return syncItems;
201
202     }
203
204     /**
205      * Removes a SyncItem given its key.
206      *
207      * @param principal the entity that wants to do the operation
208      * @param syncItem the item to remove
209      *
210      * @throws SyncException in case of error (for instance if the
211      * underlying data store runs into problems)
212      */

213     public void removeSyncItem(Principal JavaDoc principal, SyncItem syncItem) throws SyncException {
214
215     }
216
217     /**
218      * Method implements start sync operations
219      */

220     public void beginSync(int syncMode) throws SyncException {
221
222     }
223
224     /**
225      * Method implements end sync operations
226      */

227     public void commitSync() throws SyncException {
228
229     }
230
231     /**
232      * Replaces an existing <i>SyncItem</i> or adds a new <i>SyncItem</i> if it
233      * does not exist.
234      *
235      * @return the inserted
236      *
237      * @throws SyncException in case of error (for instance if the
238      * underlying data store runs into problems)
239      */

240     public SyncItem setSyncItem(Principal JavaDoc principal, SyncItem syncItem)
241     throws SyncException {
242
243         String JavaDoc xmlValue = null;
244         String JavaDoc xmlRecoveryDetails = null;
245
246         xmlValue = new String JavaDoc((byte[])syncItem.getPropertyValue(SyncItem.PROPERTY_BINARY_CONTENT));
247
248         try {
249             xmlRecoveryDetails = getXMLTagValue(xmlValue, PROP_RECOVERY_DETAILS);
250         } catch (Exception JavaDoc e) {
251             throw new SyncException("Tag not found: " + PROP_RECOVERY_DETAILS);
252         }
253
254         try {
255             this.sourceList = getXMLTagValue(xmlRecoveryDetails, PROP_SOURCE_LIST);
256         } catch (Exception JavaDoc e) {
257             throw new SyncException("Tag not found: " + PROP_SOURCE_LIST);
258         }
259
260         try {
261             this.last = getXMLTagValue(xmlRecoveryDetails, PROP_LAST);
262         } catch (Exception JavaDoc e) {
263             throw new SyncException("Tag not found: " + PROP_LAST);
264         }
265
266         try {
267             this.uri = getXMLTagValue(xmlRecoveryDetails, PROP_URI);
268         } catch (Exception JavaDoc e) {
269             throw new SyncException("Tag not found: " + PROP_URI);
270         }
271
272         recovery(principal);
273
274         SyncItem newSyncItem =
275                         new SyncItemImpl(this, syncItem.getKey().getKeyAsString(), SyncItemState.NEW);
276
277         newSyncItem.setProperties(syncItem.getProperties());
278
279         return newSyncItem;
280
281     }
282
283     //----------------------------------------------------------------- Private methods
284

285     /**
286      * Make a String by value of <i>tag</i>.
287      *
288      * @param xml xml msg
289      * @param tag tag to find
290      * @return tag value
291      **/

292     private String JavaDoc getXMLTagValue(String JavaDoc xml, String JavaDoc tag) {
293
294         String JavaDoc startTag = null;
295         String JavaDoc endTag = null;
296
297         String JavaDoc value = null;
298
299         startTag = "(" + tag + ")";
300         endTag = "(/" + tag + ")";
301
302         value = xml.substring(xml.indexOf(startTag) + startTag.length(), xml.indexOf(endTag));
303
304         return value;
305     }
306
307 }
308
Popular Tags