KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > sync4j > framework > protocol > v11 > BasicRequirements


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.v11;
19
20 import sync4j.framework.core.*;
21
22 import sync4j.framework.protocol.ProtocolException;
23
24 /**
25  *
26  * @author Stefano Fornari @ Funambol
27  *
28  * @version $Id: BasicRequirements.java,v 1.10 2005/05/18 08:14:45 luigiafassina Exp $
29  */

30 public class BasicRequirements
31 implements Errors {
32     // ---------------------------------------------------- Contraint values
33

34     public static final VerDTD[] SUPPORTED_DTD_VERSIONS
35         = new VerDTD[] { Constants.DTD_1_0, Constants.DTD_1_1 };
36     public static final VerProto[] SUPPORTED_PROTOCOL_VERSIONS
37         = new VerProto[] { Constants.PROT_1_0, Constants.PROT_1_1, Constants.PROT_1_1_1 };
38         
39     public static final String JavaDoc[] CAPABILITIES_SOURCE
40         = new String JavaDoc[] {"./devinf11", "./devinf10"};
41     public static final String JavaDoc CAPABILITIES_TARGET = "./devinf11";
42     public static final String JavaDoc SERVER_CAPABILITIES = "server";
43     public static final String JavaDoc CLIENT_CAPABILITIES = "client";
44
45     //----------------------------------------------------------- Public methods
46

47     static public void checkDTDVersion(VerDTD version)
48     throws ProtocolException {
49         for (int i=0; i<SUPPORTED_DTD_VERSIONS.length; ++i) {
50             if (SUPPORTED_DTD_VERSIONS[i].equals(version)) {
51                 return; // OK!
52
}
53         }
54         String JavaDoc[] args = new String JavaDoc[] { version.toString() };
55         throw new ProtocolException(ERRMSG_DTD_VER_NOT_SUPPORTED, args);
56     }
57
58     static public void checkProtocolVersion(VerProto version)
59     throws ProtocolException {
60         for (int i=0; i<SUPPORTED_PROTOCOL_VERSIONS.length; ++i) {
61             if (SUPPORTED_PROTOCOL_VERSIONS[i].equals(version)) {
62                 return; // OK!
63
}
64         }
65         String JavaDoc[] args = new String JavaDoc[] { version.toString() };
66         throw new ProtocolException(ERRMSG_PROTOCOL_VER_NOT_SUPPORTED, args);
67     }
68
69     static public void checkSessionId(SessionID id)
70     throws ProtocolException {
71         if ((id == null) || (id.toString().trim().length() == 0)) {
72             throw new ProtocolException(ERRMSG_NO_SESSION_ID);
73         }
74     }
75
76     static public void checkMessageId(String JavaDoc id)
77     throws ProtocolException {
78         if ((id == null) || (id.trim().length() == 0)) {
79             throw new ProtocolException(ERRMSG_NO_MESSAGE_ID);
80         }
81     }
82
83     /**
84      * A target is valid if it is not null and one of <i>location name</i> or
85      * <i>URI</i> is specified.
86      *
87      * @param target the target to be checked - NULL
88      *
89      * @throws ProtocolException
90      */

91     static public void checkTarget(Target target) throws ProtocolException {
92         boolean valid = target != null;
93         boolean location = false;
94         boolean uri = false;
95
96         if (valid) {
97             location = ( (target.getLocName() != null)
98                        && (target.getLocName().trim().length() != 0) );
99             uri = (target.getLocURI() != null);
100
101             valid = location || uri;
102         }
103
104         if (!valid) {
105             String JavaDoc[] args = { ((target == null) ? "null" : target.getLocURI()) };
106             throw new ProtocolException(ERRMSG_INVALID_TARGET, args);
107         }
108     }
109
110     /**
111      * A source is valid if it is not null and one of <i>location name</i> or
112      * <i>URI</i> is specified.
113      *
114      * @param source the source to be checked - NULL
115      *
116      * @throws ProtocolException
117      */

118     static public void checkSource(Source source) throws ProtocolException {
119         boolean valid = source != null;
120         boolean location = false;
121         boolean uri = false;
122
123         if (valid) {
124             location = ( (source.getLocName() != null)
125                        && (source.getLocName().trim().length() != 0) );
126             uri = (source.getLocURI() != null);
127             valid = location || uri;
128         }
129
130         if (!valid) {
131             String JavaDoc[] args = { ((source == null) ? "null" : source.getLocURI()) };
132             throw new ProtocolException(ERRMSG_INVALID_SOURCE, args);
133         }
134     }
135
136     static public void checkCommandId(CmdID commandId)
137     throws ProtocolException {
138         String JavaDoc cmdValue = null;
139
140         if ( ( commandId == null )
141            || ((cmdValue = commandId.getCmdID()) == null)
142            || ( cmdValue.trim().length() == 0) ) {
143             throw new ProtocolException(ERRMSG_NO_MESSAGE_ID);
144         }
145     }
146
147     static public void checkAlertCommand(Alert alert) throws ProtocolException {
148         //
149
// Check the command id
150
//
151
String JavaDoc[] args = new String JavaDoc[] {"alert is null!"};
152
153         if (alert == null) {
154             throw new ProtocolException(ERRMSG_INVALID_ALERT, args);
155         }
156
157         try {
158             checkCommandId(alert.getCmdID());
159         } catch (ProtocolException e) {
160             args = new String JavaDoc[] { e.getMessage() };
161             throw new ProtocolException(ERRMSG_INVALID_ALERT, args);
162         }
163     }
164
165     /**
166      * Checks if the given command contains valid device information.
167      *
168      * @param cmd the command containing data
169      * @param device specifies if the command should contain client or server
170      * capabilities
171      *
172      * @throws ProtocolException
173      */

174     static public void checkCapabilities(ItemizedCommand cmd, String JavaDoc device)
175     throws ProtocolException {
176         //
177
// Checks command id
178
//
179
try {
180             checkCommandId(cmd.getCmdID());
181         } catch (ProtocolException e) {
182             String JavaDoc[] args = new String JavaDoc[] { device, e.getMessage() };
183             throw new ProtocolException(ERRMSG_INVALID_CAPABILITIES, args);
184         }
185
186         //
187
// Checks source
188
//
189
Item[] items = (Item[])cmd.getItems().toArray(new Item[0]);
190
191         if ((items == null) || (items.length ==0)) {
192             String JavaDoc[] args = new String JavaDoc[] { device, ERRMSG_MISSING_ITEM};
193             throw new ProtocolException(ERRMSG_INVALID_CAPABILITIES, args);
194         }
195
196         try {
197             checkSource(items[0].getSource());
198         } catch (ProtocolException e) {
199             String JavaDoc[] args = new String JavaDoc[] { device, "missing source" };
200             throw new ProtocolException(ERRMSG_INVALID_CAPABILITIES, args);
201         }
202
203         boolean isOk = false;
204         for (int i=0; i<CAPABILITIES_SOURCE.length; ++i) {
205             if (CAPABILITIES_SOURCE[i].equals(items[0].getSource().getLocURI().toString())) {
206                 isOk = true;
207                 break;
208             }
209         }
210         if (!isOk) {
211             String JavaDoc[] args = new String JavaDoc[] { device, "URI not " + CAPABILITIES_SOURCE.toString()};
212             throw new ProtocolException(ERRMSG_INVALID_CAPABILITIES, args);
213         }
214     }
215 }
Popular Tags