KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > sync4j > framework > engine > source > AbstractSyncSource


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.framework.engine.source;
20
21 import java.util.logging.Logger JavaDoc;
22 import java.util.logging.Level JavaDoc;
23 import java.security.Principal JavaDoc;
24 import java.sql.Timestamp JavaDoc;
25
26 import sync4j.framework.logging.Sync4jLogger;
27 import sync4j.framework.engine.*;
28 import sync4j.framework.engine.source.SyncSource;
29
30 /**
31  * Implements the basic functionalities of a <i>SyncSource</i> like naming.
32  *
33  * @author Stefano Fornari @ Funambol
34  *
35  * @version $Id: AbstractSyncSource.java,v 1.20 2005/05/27 16:10:21 nichele Exp $
36  */

37 public abstract class AbstractSyncSource implements SyncSource, java.io.Serializable JavaDoc {
38     // --------------------------------------------------------------- Constants
39

40     public static final String JavaDoc LOG_NAME = "engine.source";
41
42     // ---------------------------------------------------------- Protected data
43

44     protected String JavaDoc name = null;
45     protected String JavaDoc type = null;
46     protected String JavaDoc sourceURI = null;
47     protected String JavaDoc sourceQuery= null;
48     protected SyncSourceInfo info = null;
49
50     /**
51      * How many items were added ?
52      */

53     protected long howManyAdded;
54
55     /**
56      * How many items were deleted ?
57      */

58     protected long howManyDeleted;
59
60     /**
61      * How many items were updated ?
62      */

63     protected long howManyUpdated;
64
65     // ------------------------------------------------------------ Constructors
66

67     /** Creates a new instance of AbstractSyncSource */
68     protected AbstractSyncSource() {
69     }
70
71     public AbstractSyncSource(String JavaDoc name, String JavaDoc type, String JavaDoc sourceURI) {
72         if (name == null) {
73             throw new NullPointerException JavaDoc("name is null!");
74         }
75
76         this.name = name;
77         this.type = (type == null) ? "unknown" : type;
78         setSourceURI(sourceURI);
79     }
80
81     public AbstractSyncSource(String JavaDoc name) {
82         this(name, null, null);
83     }
84
85     // ---------------------------------------------------------- Public methods
86

87     public String JavaDoc getName() {
88         return name;
89     }
90
91     public void setName(String JavaDoc name) {
92         this.name = name;
93     }
94
95     public String JavaDoc getType() {
96         return this.type;
97     }
98
99     public void setType(String JavaDoc type) {
100         this.type = type;
101     }
102
103     /** Getter for property query.
104      * @return Value of property query.
105      */

106     public String JavaDoc getSourceQuery() {
107         return sourceQuery;
108     }
109
110     /** Getter for property uri.
111      * @return Value of property uri.
112      */

113     public String JavaDoc getSourceURI() {
114         return sourceURI;
115     }
116
117     /** Setter for property uri.
118      * @param sourceURI New value of property uri.
119      */

120     public void setSourceURI(String JavaDoc sourceURI) {
121         int qMark = sourceURI.indexOf('?');
122         if (qMark == -1) {
123             this.sourceURI = sourceURI;
124             this.sourceQuery = "";
125         } else {
126             this.sourceURI = sourceURI.substring(0, qMark);
127             this.sourceQuery = sourceURI.substring(qMark);
128         }
129     }
130
131     /** Returns the type info of the content handled by this source
132      *
133      * @return the type info of the content handled by this source
134      *
135      */

136     public SyncSourceInfo getInfo() {
137         return this.info;
138     }
139
140     /**
141      * Setter for the property <i>info</i>
142      */

143     public void setInfo(SyncSourceInfo info) {
144         this.info = info;
145     }
146
147     /**
148      * Returns a string representation of this object.
149      *
150      * @return a string representation of this object.
151      */

152     public String JavaDoc toString() {
153         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(super.toString());
154
155         sb.append(" - {name: ").append(getName() );
156         sb.append(" type: " ).append(getType() );
157         sb.append(" uri: " ).append(getSourceURI());
158         sb.append("}" );
159
160         return sb.toString();
161     }
162
163     /**
164      * This resets the howMany counters. It should then be called by extending
165      * classes before anything else.
166      *
167      * @param principal user/device
168      * @param syncMode sync mode
169      *
170      * @throws SyncSourceException in case of any error
171      *
172      * @see SyncSource
173      */

174     public void beginSync(Principal JavaDoc principal, int syncMode) throws SyncSourceException {
175         howManyAdded = howManyDeleted = howManyUpdated = 0;
176     }
177
178     /**
179      * It logs the howMany counters. It should then be called by extending
180      * classes before anything else.
181      *
182      * @param principal user/device
183      *
184      * @throws SyncSourceException in case of any error
185      *
186      * @see SyncSource
187      */

188     public void endSync(Principal JavaDoc principal) throws SyncSourceException {
189         Logger JavaDoc log = Sync4jLogger.getLogger(LOG_NAME);
190
191         if (log.isLoggable(Level.INFO)) {
192             log.info(getSourceURI()
193                     + ": "
194                     + howManyAdded
195                     + " new items added, "
196                     + howManyUpdated
197                     + " existing items updated, "
198                     + howManyDeleted
199                     + " items deleted."
200                     );
201         }
202     }
203
204     // -------------------------------------------------------- Abstract methods
205

206     public abstract SyncItem[] getAllSyncItems(Principal JavaDoc principal) throws SyncSourceException;
207
208     public abstract SyncItemKey[] getDeletedSyncItemKeys(Principal JavaDoc principal,
209                                                          Timestamp JavaDoc since ) throws SyncSourceException;
210
211     public abstract SyncItem[] getDeletedSyncItems(Principal JavaDoc principal,
212                                                    Timestamp JavaDoc since ) throws SyncSourceException;
213
214     public abstract SyncItemKey[] getNewSyncItemKeys(Principal JavaDoc principal,
215                                                      Timestamp JavaDoc since ) throws SyncSourceException;
216
217     public abstract SyncItem[] getNewSyncItems(Principal JavaDoc principal,
218                                                Timestamp JavaDoc since ) throws SyncSourceException;
219
220     public abstract SyncItem getSyncItemFromId(Principal JavaDoc principal, SyncItemKey syncItemKey) throws SyncSourceException;
221
222     public abstract SyncItem[] getSyncItemsFromIds(Principal JavaDoc principal, SyncItemKey[] syncItemKeys) throws SyncSourceException;
223
224     public abstract SyncItem[] getUpdatedSyncItems(Principal JavaDoc principal,
225                                                    Timestamp JavaDoc since ) throws SyncSourceException;
226
227     public abstract void removeSyncItem(Principal JavaDoc principal, SyncItem syncItem) throws SyncSourceException;
228
229     public abstract void removeSyncItems(Principal JavaDoc principal, SyncItem[] syncItems) throws SyncSourceException;
230
231     public abstract SyncItem setSyncItem(Principal JavaDoc principal, SyncItem syncInstance) throws SyncSourceException;
232
233     public abstract SyncItem[] setSyncItems(Principal JavaDoc principal, SyncItem[] syncItems) throws SyncSourceException;
234
235     public abstract SyncItem getSyncItemFromTwin(Principal JavaDoc principal, SyncItem syncItem) throws SyncSourceException;
236
237     public abstract SyncItem[] getSyncItemsFromTwins(Principal JavaDoc principal, SyncItem[] syncItems) throws SyncSourceException;
238 }
239
Popular Tags