1 18 package sync4j.framework.engine.dm; 19 20 import java.net.URLEncoder ; 21 import java.util.*; 22 import java.util.Map ; 23 24 import sync4j.framework.core.*; 25 import sync4j.framework.protocol.ProtocolException; 26 import sync4j.framework.tools.Base64; 27 import sync4j.framework.tools.CommandIdGenerator; 28 29 34 public final class Util { 35 36 38 private static final String MANAGEMENT_COMMANDS 39 = ",Add,Alert,Copy,Delete,Exec,Get,Replace,Atomic,Sequence,"; 40 41 private static final String ALERT_OPTION_MINDT = "MINDT" ; 42 private static final String ALERT_OPTION_MAXDT = "MAXDT" ; 43 private static final String ALERT_OPTION_DR = "DR" ; 44 private static final String ALERT_OPTION_MAXLEN = "MAXLEN"; 45 private static final String ALERT_OPTION_IT = "IT" ; 46 private static final String ALERT_OPTION_ET = "ET" ; 47 48 49 51 59 public static AbstractCommand[] 60 managementOperations2commands(ManagementOperation[] operations , 61 CommandIdGenerator idGenerator) { 62 63 if ((operations == null) || (operations.length == 0)) { 64 return new AbstractCommand[0]; 65 } 66 67 AbstractCommand[] ret = new AbstractCommand[operations.length]; 68 for (int i=0; i<operations.length; ++i) { 69 ret[i] = managementOperation2command( 70 operations[i], idGenerator.next(), idGenerator 71 ); 72 } 73 74 return ret; 75 } 76 77 85 public static AbstractCommand 86 managementOperation2command(ManagementOperation o, CmdID cmdId, CommandIdGenerator idGenerator) { 87 88 if (o instanceof AtomicManagementOperation) { 89 return new Atomic( 90 cmdId, 91 false, null , 93 managementOperations2commands(((AtomicManagementOperation)o).getOperations(), idGenerator) 94 ); 95 } else if (o instanceof SequenceManagementOperation) { 96 return new Sequence( 97 cmdId, 98 false, null , 100 managementOperations2commands(((SequenceManagementOperation)o).getOperations(), idGenerator) 101 ); 102 } else if (o instanceof AddManagementOperation) { 103 return new Add( 104 cmdId, 105 false, null , 107 null , 108 nodes2Items(((AddManagementOperation)o).getNodes(), true) 109 ); 110 } else if (o instanceof CopyManagementOperation) { 111 return null; 113 } else if (o instanceof DeleteManagementOperation) { 114 return new Delete( 115 cmdId, 116 false, false, false, null , 120 null , 121 nodes2Items(((DeleteManagementOperation)o).getNodes(), false) 122 ); 123 } else if (o instanceof ExecManagementOperation) { 124 return new Exec( 125 cmdId, 126 false, null , 128 nodes2Items(((ExecManagementOperation)o).getNodes(), true)[0] 129 ); 130 } else if (o instanceof GetManagementOperation) { 131 return new Get( 132 cmdId, 133 false, null , 135 null , 136 null , 137 nodes2Items(((GetManagementOperation)o).getNodes(), false) 138 ); 139 } else if (o instanceof ReplaceManagementOperation) { 140 return new Replace( 141 cmdId, 142 false, null , 144 null , 145 nodes2Items(((ReplaceManagementOperation)o).getNodes(), true) 146 ); 147 } else if (o instanceof UserAlertManagementOperation) { 148 UserAlertManagementOperation a = (UserAlertManagementOperation)o; 149 150 return new Alert( 151 cmdId, 152 false, 153 null, 154 a.getAlertCode(), 155 alert2Items(a) 156 ); 157 } 158 159 return null; 160 } 161 162 171 public static Item[] nodes2Items(Map nodes, boolean useValue) { 172 if ((nodes == null) || (nodes.size()==0)) { 173 return new Item[0]; 174 } 175 176 Item[] items = new Item[nodes.size()]; 177 178 Object key = null, value = null, treeNodeValue = null; 179 180 String format = null; 181 String type = null; 182 Meta meta = null; 183 ComplexData complexData = null; 184 185 int j = 0; 186 Iterator i = nodes.keySet().iterator(); 187 TreeNode treeNode = null; 188 while(i.hasNext()) { 189 key = i.next(); 190 value = nodes.get(key); 191 meta = null; 192 format = null; 193 type = null; 194 treeNodeValue = null; 195 treeNode = null; 196 complexData = null; 197 198 if (!(value instanceof TreeNode)) { 199 value = new TreeNode((String )key, value); 201 } 202 203 treeNode = (TreeNode)value; 204 205 format = treeNode.getFormat(); 206 key = treeNode.getName(); 207 treeNodeValue = treeNode.getValue(); 208 type = treeNode.getType(); 209 210 if (format == null) { 211 format = TreeNode.FORMAT_DEFAULT_VALUE; 212 } 213 214 if (format.equalsIgnoreCase(TreeNode.FORMAT_BINARY)) { 215 if (treeNodeValue instanceof byte[]) { 216 complexData = new ComplexData(new String (Base64.encode( (byte[])treeNodeValue))); 217 } else { 218 complexData = new ComplexData(new String (Base64.encode(treeNodeValue.toString(). 220 getBytes()))); 221 } 222 } else if (format.equalsIgnoreCase(TreeNode.FORMAT_NODE)) { 223 treeNodeValue = ""; 224 } 225 226 if (useValue && (complexData == null)) { 227 complexData = new ComplexData(String.valueOf(treeNodeValue)); 228 } 229 230 meta = new Meta(); 231 meta.setFormat(format); 232 meta.setType(type); 233 234 items[j++] = new Item( 235 new Target(String.valueOf(key)), 236 null , 237 (useValue) ? meta : null , 238 (useValue) ? complexData : null, 239 false ); 241 } 242 243 return items; 244 } 245 246 284 public static ManagementOperationResult[] 285 operationResults(AbstractCommand[] commands, String dischargedStatus) 286 throws ProtocolException { 287 if ((commands == null) || (commands.length==0)) { 288 return new ManagementOperationResult[0]; 289 } 290 291 TreeMap results = new TreeMap(new CmdIdComparator()); 292 293 298 305 String key = null; 309 Status status = null; 310 ManagementOperationResult s = null; 311 String statusCode = null; 312 for(int i=0; i<commands.length; ++i) { 313 if (!(commands[i] instanceof Status)) { 314 continue; 315 } 316 317 status = (Status)commands[i]; 318 319 if (MANAGEMENT_COMMANDS.indexOf(status.getCmd()) < 0) { 320 continue; 321 } 322 323 statusCode = status.getData().getData(); 324 if (dischargedStatus.indexOf(statusCode) != -1) { 325 continue; 326 } 327 328 key = status.getMsgRef(); 329 key = ((key == null) ? "1" : key) 330 + '-' 331 + status.getCmdRef() 332 + '-' 333 + status.getStatusCode() 334 ; 335 336 s = (ManagementOperationResult)results.get(key); 337 if (s == null) { 338 String cmd = status.getCmd(); 339 s = new ManagementOperationResult(); 340 s.setStatusCode(status.getStatusCode()); 341 s.setCommand(cmd); 342 343 if (Alert.COMMAND_NAME.equals(cmd)) { 344 s.setNodes(nodesFromItems(status)); 345 } else { 346 s.setNodes(nodesFromTargetRefs(status)); 347 } 348 349 results.put(key, s); 350 } 351 } 352 353 Results res = null; 357 for(int i=0; i<commands.length; ++i) { 358 if (!(commands[i] instanceof Results)) { 359 continue; 360 } 361 362 res = (Results)commands[i]; 363 364 key = res.getMsgRef(); 365 key = ((key == null) ? "1" : key) 366 + '-' 367 + res.getCmdRef() 368 + "-200" ; 371 372 s = (ManagementOperationResult)results.get(key); 373 374 if (s == null) { 375 throw new ProtocolException( "Results " 379 + key 380 + " is without corresponding Status" 381 ); 382 } 383 384 s.addNodes(nodesFromItems(res)); 385 } 386 387 ManagementOperationResult[] ret 391 = new ManagementOperationResult[results.size()]; 392 393 int i = 0; 394 Iterator iter = results.values().iterator(); 395 while (iter.hasNext()) { 396 ret[i++] = (ManagementOperationResult)iter.next(); 397 } 398 399 return ret; 400 } 401 402 410 public static Map nodesFromItems(AbstractCommand cmd) { 411 HashMap ret = new HashMap(); 412 413 if (!(cmd instanceof ItemizedCommand)) { 414 return ret; 415 } 416 417 String node = null; 418 Object value = null; 419 420 if (((ItemizedCommand)cmd).getItems() == null) { 421 return ret; 422 } 423 424 int c = 0; 425 Item item = null; 426 Iterator i = ((ItemizedCommand)cmd).getItems().iterator(); 427 String format = null; 428 Data itemData = null; 429 while (i.hasNext()) { 430 item = (Item)i.next(); 431 format = getItemFormat(item); 432 433 node = (item.getSource() == null) 434 ? String.valueOf(++c) 435 : item.getSource().getLocURI(); 436 437 itemData = item.getData(); 438 if (itemData != null) { 439 value = itemData.getData(); 440 441 if (format.equalsIgnoreCase(TreeNode.FORMAT_DEFAULT_VALUE)) { 442 value = String.valueOf(value); 443 } else if (format.equalsIgnoreCase(TreeNode.FORMAT_BINARY)) { 444 value = Base64.decode(String.valueOf(value).getBytes()); 445 } else if (format.equalsIgnoreCase(TreeNode.FORMAT_BOOL)) { 446 value = new Boolean ((String )value); 447 } else if (format.equalsIgnoreCase(TreeNode.FORMAT_INT)) { 448 try { 449 value = new Integer ( (String )value); 450 } catch (NumberFormatException e) { 451 value = new ManagementException( 453 "Node with format int not contains a valid integer value (" + value + 454 ")"); 455 } 456 } 457 458 if (format.equalsIgnoreCase("node")) { 459 value = new TreeNode(item.getSource().getLocURI(), value, format); 460 } 461 } 462 463 ret.put(node, (value == null) ? "" : value); 464 } 465 466 return ret; 467 } 468 469 477 public static Map nodesFromTargetRefs(AbstractCommand cmd) { 478 HashMap ret = new HashMap(); 479 480 if (!(cmd instanceof ResponseCommand)) { 481 return ret; 482 } 483 484 String node = null; 485 486 if (((ResponseCommand)cmd).getTargetRef() == null) { 487 return ret; 488 } 489 490 TargetRef ref = null; 491 Iterator i = ((ResponseCommand)cmd).getTargetRef().iterator(); 492 while (i.hasNext()) { 493 ref = (TargetRef)i.next(); 494 node = ref.getValue(); 495 496 ret.put(node, ""); 497 } 498 499 return ret; 500 } 501 502 511 public static Item[] alert2Items(final UserAlertManagementOperation alert) { 512 513 String [] alerts = alert.getAlerts(); 514 515 Item[] items = new Item[(alerts == null) ? 1 : alerts.length+1]; 520 521 StringBuffer options = new StringBuffer (); 525 526 int i = alert.getMinDisplayTime(); 527 if (i > 0) { 528 options.append(ALERT_OPTION_MINDT).append('=').append(i).append('&'); 529 } 530 i = alert.getMaxDisplayTime(); 531 if (i > 0) { 532 options.append(ALERT_OPTION_MAXDT).append('=').append(i).append('&'); 533 } 534 535 String s = alert.getDefaultResponse(); 536 if (s != null) { 537 try { 538 options.append(ALERT_OPTION_DR).append('=').append(URLEncoder.encode(s, "UTF-8")).append('&'); 539 } catch (java.io.UnsupportedEncodingException e) { 540 options.append(e.getMessage()); 541 } 542 } 543 544 i = alert.getMaxLength(); 545 if (i > 0) { 546 options.append(ALERT_OPTION_MAXLEN).append('=').append(i).append('&'); 547 } 548 549 char c = alert.getInputType(); 550 if (c != ' ') { 551 options.append(ALERT_OPTION_IT).append('=').append(c).append('&'); 552 } 553 554 c = alert.getEchoType(); 555 if (c != ' ') { 556 options.append(ALERT_OPTION_ET).append('=').append(c).append('&'); 557 } 558 if (options.length() > 0) { 559 options.deleteCharAt(options.length() - 1); 560 } 561 562 items[0] = new Item(null, null, null, new ComplexData(options.toString()), false); 567 568 for (i=1; i < items.length; ++i) { 572 items[i] = new Item(null, null, null, new ComplexData(alerts[i-1]), false); 573 } 574 575 return items; 576 } 577 578 580 587 private static String getItemFormat(Item item) { 588 Meta meta = item.getMeta(); 589 if (meta == null) { 590 return TreeNode.FORMAT_DEFAULT_VALUE; 591 } 592 String format = meta.getFormat(); 593 if (format == null) { 594 format = TreeNode.FORMAT_DEFAULT_VALUE; 595 } 596 597 return format; 598 } 599 600 602 609 private static class CmdIdComparator 610 implements Comparator { 611 620 public int compare(Object o1, Object o2) { 621 String value1 = null; 622 String value2 = null; 623 624 if (!((o1 instanceof String ) && (o2 instanceof String ))) { 625 throw new IllegalArgumentException ( "o1 (" 626 + o1 627 + ") and o2 (" 628 + o2 629 + ") must be string!" 630 ); 631 } 632 633 value1 = (String )o1; 634 value2 = (String )o2; 635 636 return extractCmdId(value1) - extractCmdId(value2); 637 } 638 639 650 private int extractCmdId(final String s) 651 throws IllegalArgumentException { 652 if (s == null) { 653 throw new IllegalArgumentException ("s cannot be null"); 654 } 655 656 int p1 = s.indexOf('-'); 657 if ((p1 <= 0) || (p1 == s.length()-1)) { 658 throw new IllegalArgumentException ("s is not in the form {msgref}-{cmdref}-{status}"); 659 } 660 661 int p2 = s.indexOf('-', p1+1); 662 663 if (p2 <= 0) { 664 throw new IllegalArgumentException ("s is not in the form {msgref}-{cmdref}-{status}"); 665 } 666 667 try { 668 return Integer.parseInt(s.substring(p1+1, p2)); 669 } catch (NumberFormatException e) { 670 throw new IllegalArgumentException ("s is not in the form {msgref}-{cmdref}-{status}"); 671 } 672 } 673 } 674 } | Popular Tags |