KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > sync4j > framework > protocol > SyncPackage


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.protocol;
19
20 import sync4j.framework.core.*;
21 import sync4j.framework.protocol.Flags;
22
23 /**
24  * This is a base abstract class for classes that implement SyncML protocol
25  * packages. A SyncML package is composed of a SyncHeader and a SyncBody which
26  * follows the requirements that the protocol mandates.<br>
27  * SyncPackage and its subclasses are designed to be used in two steps. First a
28  * SyncPackage is created and checked for validity and compliancy with the
29  * protocol. Than <i>getResponse()</i> can be used to get a response message for
30  * the given request. During the request validation process some information about the
31  * request message are cached into instance variables and used in <i>getResponse()</i>.<br>
32  * <p>
33  * Subclasses are required to override the following methods:
34  * <ul>
35  * <li><i>checkHeaderRequirements()</i></li>
36  * <li><i>checkBodyRequirements()</i></li>
37  * <li><i>getResponse()</i></li>
38  * </ul>
39  * <p>
40  * In addition, subclasses are required to call super(header, body) in their
41  * constructors, so that the base package is constructed and validated.
42  *
43  * @author Stefano Fornari @ Funambol
44  * @version $Id: SyncPackage.java,v 1.12 2005/03/02 20:57:38 harrie Exp $
45  */

46 public abstract class SyncPackage
47 implements Flags {
48
49     protected SyncHdr syncHeader = null;
50     protected SyncBody syncBody = null;
51
52     /**
53      * Creates a SyncPackage package.
54      *
55      * @param syncHeader the header of the syncronization packet
56      * @param syncBody the body of the syncronization packet
57      */

58     public SyncPackage(final SyncHdr syncHeader,
59                        final SyncBody syncBody ) {
60             this.syncHeader = syncHeader;
61             this.syncBody = syncBody ;
62     }
63
64     // ------------------------------------------------------------------- Flags
65

66     /**
67      * Flags
68      */

69     private boolean[] flags = new boolean[HOW_MANY_FLAGS];
70
71     /**
72      * Sets the given flag to the given value. If flag is FLAG_ALL, all flags are
73      * set; if flag is FLAG_ALL_RESPONSES_REQUIRED all response flags are set.
74      *
75      * @param flag the flag to be set
76      * @param value the boolean value of the flag
77      */

78     public void setFlagValue(int flag, boolean value) {
79         switch (flag) {
80             case FLAG_ALL:
81                 for (int i=0; i<HOW_MANY_FLAGS; i++) {
82                     flags[i] = value;
83                 }
84                 break;
85             case FLAG_ALL_RESPONSES_REQUIRED:
86                 flags[FLAG_SYNC_RESPONSE_REQUIRED ] =
87                 flags[FLAG_MODIFICATIONS_RESPONSE_REQUIRED] =
88                 flags[FLAG_SYNC_STATUS_REQUIRED ] = value;
89                 break;
90             default:
91                 flags[flag] = value;
92         }
93     }
94
95     /**
96      * Sets the given flag to true.
97      *
98      * @param flag the flag to be set
99      */

100     public void setFlag(int flag) {
101         setFlagValue(flag, true);
102     }
103
104     /**
105      * Sets the given flag to false.
106      *
107      * @param flag the flag to be set
108      */

109     public void unsetFlag(int flag) {
110         setFlagValue(flag, false);
111     }
112
113     /**
114      * Returns the value of the given flag
115      *
116      * @param flag the flag to be returned
117      *
118      * @return the flag value
119      */

120     public boolean isFlag(int flag) {
121         return flags[flag];
122     }
123
124     // -------------------------------------------------------------- Properties
125

126     /**
127      * The command id generator to be used to get new command ids
128      */

129     protected CommandIdGenerator idGenerator = null;
130
131     public CommandIdGenerator getIdGenerator() {
132         return this.idGenerator;
133     }
134
135     public void setIdGenerator(CommandIdGenerator idGenerator) {
136         if (idGenerator == null) {
137             throw new NullPointerException JavaDoc("idGenerator cannot be null");
138         }
139         this.idGenerator = idGenerator;
140     }
141
142     /**
143      * The session id of the session into which the message is processed
144      */

145     public String JavaDoc getSessionId() {
146         return syncHeader.getSessionID().getSessionID();
147     }
148
149     /**
150      * The used DTD version
151      */

152     public VerDTD getDTDVersion() {
153         return syncHeader.getVerDTD();
154     }
155
156     /**
157      * The used protocol version
158      */

159     public VerProto getProtocolVersion() {
160         return syncHeader.getVerProto();
161     }
162
163     // ---------------------------------------------------------- Public methods
164

165     /**
166      * Checks that all requirements for the package are respected. It calls the
167      * abstract methods <i>checkHeaderRequirements()</i> and <i>checkBodyRequirements()</i>.
168      *
169      * @throws ProtocolException
170      */

171     public void checkRequirements()
172     throws ProtocolException
173     {
174         checkHeaderRequirements();
175         checkBodyRequirements();
176     }
177
178     /**
179      * Returns the header of the original message
180      *
181      * @return the header of the original message
182      */

183     public SyncHdr getSyncHeader() {
184         return this.syncHeader;
185     }
186
187     /**
188      * Returns the body of the original message
189      *
190      * @return the body of the original message
191      */

192     public SyncBody getSyncBody() {
193         return this.syncBody;
194     }
195
196     // -------------------------------------------------------- Abstract methods
197

198     /**
199      * Checks that all requirements regarding the header of the initialization
200      * packet are respected.
201      *
202      * @throws ProtocolException
203      */

204     abstract public void checkHeaderRequirements() throws ProtocolException;
205
206     /**
207      * Checks that all requirements regarding the body of the initialization
208      * packet are respected.
209      *
210      * @throws ProtocolException
211      */

212     abstract public void checkBodyRequirements() throws ProtocolException;
213
214     /**
215      * Constructs a proper response message.<br>
216      *
217      * @param msgId the msg id of the response
218      * @return the response message
219      *
220      * @throws ProtocolException in case of error or inconsistency
221      */

222     abstract public SyncML getResponseMessage(String JavaDoc msgId) throws ProtocolException;
223 }
Popular Tags