KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > sync4j > framework > server > session > SyncState


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.session;
19
20 import java.util.*;
21
22 import sync4j.framework.core.*;
23 import sync4j.framework.security.Sync4jPrincipal;
24 import sync4j.framework.security.SecurityConstants;
25 import sync4j.framework.server.Sync4jDevice;
26
27 /**
28  * This class represents the state of the Syncronization process because cache
29  * all informations that could be used in that process.
30  * It is set during the Initialization process.
31  *
32  * @author Luigia Fassina @ funambol.com
33  * @version $Id: SyncState.java,v 1.12 2005/03/02 20:57:38 harrie Exp $
34  */

35 public class SyncState
36 implements SecurityConstants {
37     
38     // ------------------------------------------------------------- Public data
39

40     /**
41      * Cache the Sync4jDevice object (it contains Sync4j information and nonces
42      */

43     public Sync4jDevice device;
44     
45     /**
46      * Authenticated credential. If null no credential has authenticated.
47      */

48     public Cred loggedCredential;
49     
50     /**
51      * Authenticated principal. If null, no principal has authenticated
52      */

53     public Sync4jPrincipal loggedPrincipal;
54     
55     /**
56      * Authentication state. One of the following values:
57      * <ul>
58      * <li>AUTH_UNAUTHENTICATED</li>
59      * <li>AUTH_MISSING_CREDENTIALS</li>
60      * <li>AUTH_INVALID_CREDENTIALS</li>
61      * <li>AUTH_AUTHENTICATED</li>
62      * </ul>
63      */

64     public int authenticationState;
65     
66     /**
67      * Server authentication state. One of the following values:
68      * <ul>
69      * <li>AUTH_UNAUTHENTICATED</li>
70      * <li>AUTH_RETRY_1</li>
71      * <li>AUTH_RETRY_2</li>
72      * </ul>
73      */

74     public int serverAuthenticationState;
75     
76     /**
77      * Has the session been started ?
78      */

79     public boolean started;
80     
81     /**
82      * Cache the SyncML Protocol version used by device
83      */

84     public String JavaDoc syncMLVerProto;
85     
86     /**
87      * Is Sync with initialization?
88      */

89     public boolean syncWithInit;
90     
91     /**
92      * Cache the response with initialization
93      */

94     public SyncML responseInit;
95         
96     /**
97      * Cache the maximum message size
98      * If the client doesn't specify it, its value is Long.MAX_VALUE
99      */

100     public long maxMsgSize = Long.MAX_VALUE;
101     
102     /**
103      * Cache overhead for SyncHdr object
104      */

105     public long overheadHdr;
106
107     /**
108      * Cache overhead of Status for SyncHdr
109      */

110     public long sizeStatusSyncHdr;
111     
112     /**
113      * Cache Status for SyncHdr
114      */

115     public Status statusSyncHdr;
116     
117     /**
118      * Cache the maximum object size
119      */

120     public long maxObjSize = 6000;
121         
122     /**
123      * Cache received large object
124      */

125     public String JavaDoc receivedLargeObject;
126
127     /**
128      * Cache size of received large object
129      */

130     public Long JavaDoc sizeOfReceivedLargeObject;
131     
132     /**
133      * Cache Target LocURI of Sync command and Source LocURI of the Item that
134      * contains the previous Large Object: this to check that in the next message
135      * there is the end of the previous chuncked data object
136      */

137     public String JavaDoc syncLocURI;
138     public String JavaDoc itemLocURI;
139     
140     /**
141      * Large object URI of a LO being sent by the server
142      */

143     public String JavaDoc sendingLOURI;
144         
145     /**
146      * This will cache commands to be returned in response to client PKG1
147      */

148     public ArrayList cmdCache1;
149
150     /**
151      * This will cache commands to be returned in response to client PKG3
152      */

153     public ArrayList cmdCache3;
154     
155     /**
156      * The device capabilities sent by the client. Null if no capabilities are
157      * sent.
158      */

159     public DevInf devInf;
160
161     // ---------------------------------------------------------- Public Methods
162

163     public SyncState() {
164         reset();
165     }
166     
167     /**
168      * Reset this object to an initial state
169      */

170     public void reset() {
171         device = null ;
172         loggedCredential = null ;
173         loggedPrincipal = null ;
174         authenticationState = AUTH_UNAUTHENTICATED;
175         serverAuthenticationState = AUTH_UNAUTHENTICATED;
176         started = false ;
177         syncMLVerProto = null ;
178         syncWithInit = false ;
179         responseInit = null ;
180         maxMsgSize = Integer.MAX_VALUE ;
181         overheadHdr = 0 ;
182         sizeStatusSyncHdr = 0 ;
183         statusSyncHdr = null ;
184         maxObjSize = 0 ;
185         receivedLargeObject = null ;
186         sizeOfReceivedLargeObject = null ;
187         syncLocURI = null ;
188         itemLocURI = null ;
189         sendingLOURI = null ;
190         cmdCache1 = new ArrayList() ;
191         cmdCache3 = new ArrayList() ;
192         devInf = null ;
193     }
194
195     /**
196      * Cache the Alert command
197      */

198     private ArrayList listClientAlerts = new ArrayList();
199    
200     public void addClientAlerts(Alert[] clientAlerts) {
201         for (int i=0; ((clientAlerts != null) && (i<clientAlerts.length)); i++) {
202             listClientAlerts.add(clientAlerts[i]);
203         }
204     }
205     
206     public void removeClientAlert(Alert alert) {
207         listClientAlerts.remove(alert);
208     }
209     
210     public Alert[] getClientAlerts() {
211         return (Alert[])listClientAlerts.toArray(new Alert[listClientAlerts.size()]);
212     }
213     
214     /**
215      * Cache the server modification commands: in the case of multimessage these
216      * commands must be sent only when the client has sent its package
217      */

218     private ArrayList listServerModifications = new ArrayList();
219     
220     public void setServerModifications(AbstractCommand[] serverModifications) {
221         
222         int size = listServerModifications.size();
223         
224         for (int i=0; ((serverModifications != null) && (i<serverModifications.length)); i++) {
225             boolean isAdded = false;
226             Sync sync = (Sync)serverModifications[i];
227             
228             for (int y=0; y<size; y++) {
229                 Sync syncOld = (Sync)listServerModifications.get(y);
230                 
231                 if (sync.getTarget().getLocURI().equalsIgnoreCase(syncOld.getTarget().getLocURI()) &&
232                     sync.getSource().getLocURI().equalsIgnoreCase(syncOld.getSource().getLocURI()) ) {
233                     
234                     syncOld.getCommands().addAll(sync.getCommands());
235                     isAdded = true;
236                     break;
237                 }
238             }
239             if (!isAdded) {
240                 listServerModifications.add(serverModifications[i]);
241             }
242         }
243     }
244     
245     public AbstractCommand[] getServerModifications() {
246         return (AbstractCommand[])listServerModifications.toArray(
247                     new AbstractCommand[listServerModifications.size()]
248                );
249     }
250     
251     public void clearServerModifications() {
252         listServerModifications.clear();
253     }
254     
255     /**
256      * Cache the Status command presents in the response message
257      */

258     private LinkedList listStatusCmdOut = new LinkedList();
259     
260     public void addStatusCmdOut(List statusList) {
261         listStatusCmdOut.addAll(statusList);
262     }
263     
264     public LinkedList getStatusCmdOut() {
265         return listStatusCmdOut;
266     }
267     
268     public void removeStatusCmdOut(List statusCommand) {
269         listStatusCmdOut.removeAll(statusCommand);
270     }
271     
272     /**
273      * Cache the Status command for Map presents in the response message
274      */

275     private LinkedList listMapStatusOut = new LinkedList();
276     
277     public void addMapStatusOut(List mapStatusList) {
278         listMapStatusOut.addAll(mapStatusList);
279     }
280     
281     public LinkedList getMapStatusOut() {
282         return listMapStatusOut;
283     }
284     
285     public void removeMapStatusOut(List mapStatus) {
286         listMapStatusOut.removeAll(mapStatus);
287     }
288     
289     /**
290      * Cache the Alert command presents in the response message
291      */

292     private LinkedList listAlertCmdOut = new LinkedList();
293     
294     public void addAlertCmdOut(List alertList) {
295         listAlertCmdOut.addAll(alertList);
296     }
297     
298     public void addAlertCmdOut(Alert alert) {
299         listAlertCmdOut.add(alert);
300     }
301     
302     public LinkedList getAlertCmdOut() {
303         return listAlertCmdOut;
304     }
305     
306     public void removeAlertCmdOut(List alertList) {
307         listAlertCmdOut.removeAll(alertList);
308     }
309     
310     /**
311      * Cache the AbstractCommand presents in the response message
312      */

313     private LinkedList listCmdOut = new LinkedList();
314     
315     public void addCmdOut(List cmdList) {
316         listCmdOut.addAll(cmdList);
317     }
318     
319     public LinkedList getCmdOut() {
320         return listCmdOut;
321     }
322     
323     public void removeCmdOut(List abstractCommand) {
324         listCmdOut.removeAll(abstractCommand);
325     }
326
327     public void removeCmdOut(AbstractCommand abstractCommand) {
328         listCmdOut.remove(abstractCommand);
329     }
330     
331     /**
332      * Cache the Status for large object
333      */

334     private LinkedList listStatusCmdNotProcessed = null;
335     public void addStatusCmdNotProcessed(Status status) {
336         if (listStatusCmdNotProcessed == null) {
337             listStatusCmdNotProcessed = new LinkedList();
338         }
339         listStatusCmdNotProcessed.add(status);
340     }
341     public LinkedList getListStatusCmdNotProcessed() {
342         return listStatusCmdNotProcessed;
343     }
344     public void clearListStatusCmdNotProcessed() {
345         listStatusCmdNotProcessed.clear();
346     }
347     
348     /**
349      * Cache the commands of Sync command doesn't sent
350      */

351     private LinkedList cmdsNotSent = new LinkedList();
352     public void setCmdsNotSent(List cmdsNotSent) {
353         this.cmdsNotSent.clear();
354         this.cmdsNotSent.addAll(cmdsNotSent);
355     }
356     public LinkedList getCmdsNotSent() {
357         return this.cmdsNotSent;
358     }
359     
360     /**
361      * Cache the part of Sync command that must still be to send
362      */

363     private Sync syncSplittedToSend = null;
364     public void setSyncSplittedToSend(Sync syncSplittedToSend) {
365         AbstractCommand[] cmds =
366             (AbstractCommand[])this.getCmdsNotSent().toArray(
367                 new AbstractCommand[0]
368             );
369         this.syncSplittedToSend = new Sync(
370             syncSplittedToSend.getCmdID(),
371             syncSplittedToSend.isNoResp(),
372             syncSplittedToSend.getCred(),
373             syncSplittedToSend.getTarget(),
374             syncSplittedToSend.getSource(),
375             syncSplittedToSend.getMeta(),
376             syncSplittedToSend.getNumberOfChanges(),
377             cmds
378         );
379     }
380     public Sync getSyncSplittedToSend() {
381         return this.syncSplittedToSend;
382     }
383 }
384
Popular Tags