1 18 package sync4j.framework.protocol.v11; 19 20 import sync4j.framework.core.*; 21 22 import sync4j.framework.protocol.ProtocolException; 23 24 30 public class BasicRequirements 31 implements Errors { 32 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 [] CAPABILITIES_SOURCE 40 = new String [] {"./devinf11", "./devinf10"}; 41 public static final String CAPABILITIES_TARGET = "./devinf11"; 42 public static final String SERVER_CAPABILITIES = "server"; 43 public static final String CLIENT_CAPABILITIES = "client"; 44 45 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; } 53 } 54 String [] args = new String [] { 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; } 64 } 65 String [] args = new String [] { 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 id) 77 throws ProtocolException { 78 if ((id == null) || (id.trim().length() == 0)) { 79 throw new ProtocolException(ERRMSG_NO_MESSAGE_ID); 80 } 81 } 82 83 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 [] args = { ((target == null) ? "null" : target.getLocURI()) }; 106 throw new ProtocolException(ERRMSG_INVALID_TARGET, args); 107 } 108 } 109 110 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 [] 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 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 String [] args = new String [] {"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 [] { e.getMessage() }; 161 throw new ProtocolException(ERRMSG_INVALID_ALERT, args); 162 } 163 } 164 165 174 static public void checkCapabilities(ItemizedCommand cmd, String device) 175 throws ProtocolException { 176 try { 180 checkCommandId(cmd.getCmdID()); 181 } catch (ProtocolException e) { 182 String [] args = new String [] { device, e.getMessage() }; 183 throw new ProtocolException(ERRMSG_INVALID_CAPABILITIES, args); 184 } 185 186 Item[] items = (Item[])cmd.getItems().toArray(new Item[0]); 190 191 if ((items == null) || (items.length ==0)) { 192 String [] args = new String [] { 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 [] args = new String [] { 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 [] args = new String [] { device, "URI not " + CAPABILITIES_SOURCE.toString()}; 212 throw new ProtocolException(ERRMSG_INVALID_CAPABILITIES, args); 213 } 214 } 215 } | Popular Tags |