1 57 58 package org.apache.wsif.base; 59 60 import java.io.ByteArrayInputStream ; 61 import java.io.ByteArrayOutputStream ; 62 import java.io.ObjectOutputStream ; 63 import java.io.Serializable ; 64 import java.lang.reflect.InvocationTargetException ; 65 import java.lang.reflect.Method ; 66 import java.util.HashMap ; 67 import java.util.Iterator ; 68 import java.util.Map ; 69 import javax.wsdl.Message; 70 71 import org.apache.wsif.WSIFConstants; 72 import org.apache.wsif.WSIFException; 73 import org.apache.wsif.WSIFMessage; 74 import org.apache.wsif.logging.Trc; 75 76 92 public class WSIFDefaultMessage implements WSIFMessage { 93 94 private static final long serialVersionUID = 1L; 95 96 protected Map parts; 97 protected String name; 98 protected String style; 99 protected Message msgDefinition; 100 101 public WSIFDefaultMessage() { 102 Trc.entry(this); 103 Trc.exit(); 104 } 105 106 109 public String getName() { 110 Trc.entry(this); 111 Trc.exit(name); 112 return name; 113 } 114 115 118 public void setName(String name) { 119 Trc.entry(this, name); 120 this.name = name; 121 Trc.exit(); 122 } 123 124 127 public String getRepresentationStyle() { 128 Trc.entry(this); 129 Trc.exit(style); 130 return style; 131 } 132 133 136 public void setRepresentationStyle(String rStyle) { 137 Trc.entry(this, rStyle); 138 style = rStyle; 139 Trc.exit(); 140 } 141 142 146 public Iterator getParts() { 147 Trc.entry(this); 148 if (parts == null) { 149 parts = new HashMap (); 150 } 151 Iterator it = parts.values().iterator(); 152 Trc.exit(it); 153 return it; 154 } 155 156 public void setParts(Map sourceParts) { 157 Trc.entry(this, sourceParts); 158 if (parts == null) { 159 parts = new HashMap (); 160 } else { 161 parts.clear(); 162 } 163 164 for (Iterator names = sourceParts.keySet().iterator(); names.hasNext();) { 165 String name = (String ) names.next(); 166 Object part = sourceParts.get(name); 167 parts.put(name, part); 168 } 169 Trc.exit(); 170 } 171 172 public Message getMessageDefinition() { 173 Trc.entry(this); 174 Trc.exit(msgDefinition); 175 return msgDefinition; 176 } 177 178 public void setMessageDefinition(Message msgDef) { 179 Trc.entry(this, msgDefinition); 180 msgDefinition = msgDef; 181 Trc.exit(); 182 } 183 184 public void setObjectPart(String name, Object part) throws WSIFException { 185 Trc.entry(this, name, part); 186 if (parts == null) { 187 parts = new HashMap (); 188 } 189 parts.put(name, part); 190 Trc.exit(); 191 } 192 193 public Object getObjectPart(String name) throws WSIFException { 194 Trc.entry(this, name); 195 if (parts == null) { 196 handleNoPartsException(name, "Object"); 197 Trc.exit(null); 198 return null; 199 } 200 if (parts.get(name) == null) { 201 if (!parts.keySet().contains(name)) { 202 handlePartNotFoundException(name); 203 } 204 Trc.exit(null); 205 return null; 206 } 207 Object o = parts.get(name); 208 Trc.exit(o); 209 return o; 210 } 211 212 public Object getObjectPart(String name, Class sourceClass) 213 throws WSIFException { 214 Trc.entry(this, name, sourceClass); 215 if (parts == null) { 216 handleNoPartsException(name, "Object"); 217 Trc.exit(null); 218 return null; 219 } 220 Object part = parts.get(name); 221 if (part == null) { 222 if (!parts.keySet().contains(name)) { 223 handlePartNotFoundException(name); 224 } 225 Trc.exit(null); 226 return null; 227 } else { 228 if (part.getClass().getName().equals(sourceClass.getName())) { 229 Trc.exit(part); 230 return part; 231 } else { 232 handleSourcedPartNotFoundException(name, sourceClass); 233 Trc.exit(null); 234 return null; 235 } 236 } 237 } 238 239 public byte getBytePart(String name) throws WSIFException { 240 Trc.entry(this, name); 241 if (parts == null) 242 handleNoPartsException(name, "byte"); 243 try { 244 byte b = ((Byte ) parts.get(name)).byteValue(); 245 Trc.exit(b); 246 return b; 247 } catch (NullPointerException ne) { 248 Trc.exception(ne); 249 handlePartNotFoundException(name); 250 Trc.exit(0); 251 return 0; 252 } catch (ClassCastException ce) { 253 Trc.exception(ce); 254 handlePartCastException(name, parts.get(name).getClass().getName(), "Byte"); 255 Trc.exit(0); 256 return 0; 257 } 258 } 259 260 public void setBytePart(String name, byte part) { 261 Trc.entry(this, name, new Byte (part)); 262 if (parts == null) { 263 parts = new HashMap (); 264 } 265 parts.put(name, new Byte (part)); 266 Trc.exit(); 267 } 268 269 public char getCharPart(String name) throws WSIFException { 270 Trc.entry(this, name); 271 if (parts == null) 272 handleNoPartsException(name, "char"); 273 try { 274 char c = ((Character ) parts.get(name)).charValue(); 275 Trc.exit(c); 276 return c; 277 } catch (NullPointerException ne) { 278 Trc.exception(ne); 279 handlePartNotFoundException(name); 280 Trc.exit(0); 281 return 0; 282 } catch (ClassCastException ce) { 283 Trc.exception(ce); 284 handlePartCastException( 285 name, 286 parts.get(name).getClass().getName(), 287 "Character"); 288 Trc.exit(0); 289 return 0; 290 } 291 } 292 293 public void setCharPart(String name, char part) { 294 Trc.entry(this, name, new Character (part)); 295 if (parts == null) { 296 parts = new HashMap (); 297 } 298 parts.put(name, new Character (part)); 299 Trc.exit(); 300 } 301 302 public int getIntPart(String name) throws WSIFException { 303 Trc.entry(this, name); 304 if (parts == null) 305 handleNoPartsException(name, "int"); 306 try { 307 int i = ((Integer ) parts.get(name)).intValue(); 308 Trc.exit(i); 309 return i; 310 } catch (NullPointerException ne) { 311 Trc.exception(ne); 312 handlePartNotFoundException(name); 313 Trc.exit(0); 314 return 0; 315 } catch (ClassCastException ce) { 316 Trc.exception(ce); 317 handlePartCastException(name, parts.get(name).getClass().getName(), "Integer"); 318 Trc.exit(0); 319 return 0; 320 } 321 } 322 323 public void setIntPart(String name, int part) { 324 Trc.entry(this, name, new Integer (part)); 325 if (parts == null) { 326 parts = new HashMap (); 327 } 328 parts.put(name, new Integer (part)); 329 Trc.exit(); 330 } 331 332 public long getLongPart(String name) throws WSIFException { 333 Trc.entry(this, name); 334 if (parts == null) 335 handleNoPartsException(name, "long"); 336 try { 337 long l = ((Long ) parts.get(name)).longValue(); 338 Trc.exit(new Long (l)); 339 return l; 340 } catch (NullPointerException ne) { 341 Trc.exception(ne); 342 handlePartNotFoundException(name); 343 Trc.exit(0); 344 return 0; 345 } catch (ClassCastException ce) { 346 Trc.exception(ce); 347 handlePartCastException(name, parts.get(name).getClass().getName(), "Long"); 348 Trc.exit(0); 349 return 0; 350 } 351 } 352 353 public void setLongPart(String name, long part) { 354 Trc.entry(this, name, new Long (part)); 355 if (parts == null) { 356 parts = new HashMap (); 357 } 358 parts.put(name, new Long (part)); 359 Trc.exit(); 360 } 361 362 public short getShortPart(String name) throws WSIFException { 363 Trc.entry(this, name); 364 if (parts == null) 365 handleNoPartsException(name, "short"); 366 try { 367 short s = ((Short ) parts.get(name)).shortValue(); 368 Trc.exit(s); 369 return s; 370 } catch (NullPointerException ne) { 371 Trc.exception(ne); 372 handlePartNotFoundException(name); 373 Trc.exit(0); 374 return 0; 375 } catch (ClassCastException ce) { 376 Trc.exception(ce); 377 handlePartCastException(name, parts.get(name).getClass().getName(), "Short"); 378 Trc.exit(0); 379 return 0; 380 } 381 } 382 383 public void setShortPart(String name, short part) { 384 Trc.entry(this, name, new Short (part)); 385 if (parts == null) { 386 parts = new HashMap (); 387 } 388 parts.put(name, new Short (part)); 389 Trc.exit(); 390 } 391 392 public float getFloatPart(String name) throws WSIFException { 393 Trc.entry(this, name); 394 if (parts == null) 395 handleNoPartsException(name, "float"); 396 try { 397 float f = ((Float ) parts.get(name)).floatValue(); 398 Trc.exit(new Float (f)); 399 return f; 400 } catch (NullPointerException ne) { 401 Trc.exception(ne); 402 handlePartNotFoundException(name); 403 Trc.exit(0); 404 return 0; 405 } catch (ClassCastException ce) { 406 Trc.exception(ce); 407 handlePartCastException(name, parts.get(name).getClass().getName(), "Float"); 408 Trc.exit(0); 409 return 0; 410 } 411 } 412 413 public void setFloatPart(String name, float part) { 414 Trc.entry(this, name, new Float (part)); 415 if (parts == null) { 416 parts = new HashMap (); 417 } 418 parts.put(name, new Float (part)); 419 Trc.exit(); 420 } 421 422 public double getDoublePart(String name) throws WSIFException { 423 Trc.entry(this, name); 424 if (parts == null) 425 handleNoPartsException(name, "double"); 426 try { 427 double d = ((Double ) parts.get(name)).doubleValue(); 428 Trc.exit(new Double (d)); 429 return d; 430 } catch (NullPointerException ne) { 431 Trc.exception(ne); 432 handlePartNotFoundException(name); 433 Trc.exit(0); 434 return 0; 435 } catch (ClassCastException ce) { 436 Trc.exception(ce); 437 handlePartCastException(name, parts.get(name).getClass().getName(), "Double"); 438 Trc.exit(0); 439 return 0; 440 } 441 } 442 443 public void setDoublePart(String name, double part) { 444 Trc.entry(this, name, new Double (part)); 445 if (parts == null) { 446 parts = new HashMap (); 447 } 448 parts.put(name, new Double (part)); 449 Trc.exit(); 450 } 451 452 public boolean getBooleanPart(String name) throws WSIFException { 453 Trc.entry(this, name); 454 if (parts == null) 455 handleNoPartsException(name, "boolean"); 456 try { 457 boolean b = ((Boolean ) parts.get(name)).booleanValue(); 458 Trc.exit(b); 459 return b; 460 } catch (NullPointerException ne) { 461 Trc.exception(ne); 462 handlePartNotFoundException(name); 463 Trc.exit(false); 464 return false; 465 } catch (ClassCastException ce) { 466 Trc.exception(ce); 467 handlePartCastException(name, parts.get(name).getClass().getName(), "Boolean"); 468 Trc.exit(false); 469 return false; 470 } 471 } 472 473 public void setBooleanPart(String name, boolean part) { 474 Trc.entry(this, name, new Boolean (part)); 475 if (parts == null) { 476 parts = new HashMap (); 477 } 478 parts.put(name, new Boolean (part)); 479 Trc.exit(); 480 } 481 482 public Iterator getPartNames() { 483 Trc.entry(this); 484 if (parts == null) { 485 parts = new HashMap (); 486 } 487 Iterator it = parts.keySet().iterator(); 488 Trc.exit(it); 489 return it; 490 } 491 492 public Object clone() throws CloneNotSupportedException { 493 Trc.entry(this); 494 WSIFDefaultMessage dm = new WSIFDefaultMessage(); 495 dm.setName(this.name); 496 dm.setRepresentationStyle(this.style); 497 dm.setMessageDefinition(this.msgDefinition); 498 Iterator it = getPartNames(); 499 while (it.hasNext()) { 500 String pn = (String ) it.next(); 501 Object po = parts.get(pn); 502 if (po == null) { 503 try { 504 dm.setObjectPart(pn, null); 505 } catch (Exception e) { 506 Trc.exception(e); 507 throw new CloneNotSupportedException ( 508 "Exception thrown whilst cloning part " 509 + pn 510 + ". Message is " 511 + e.getMessage()); 512 } 513 } else if (po instanceof Cloneable ) { 514 Class cls = po.getClass(); 515 try { 516 Method clone = cls.getMethod("clone", null); 517 Object poc = clone.invoke(po, null); 518 dm.setObjectPart(pn, poc); 519 } catch (InvocationTargetException e) { 520 Trc.exception(e); 521 throw new CloneNotSupportedException ( 522 "Exception thrown whilst cloning part " 523 + pn 524 + ". Message is " 525 + e.getTargetException().getMessage()); 526 } catch (Exception e) { 527 Trc.exception(e); 528 throw new CloneNotSupportedException ( 529 "Exception thrown whilst cloning part " 530 + pn 531 + ". Message is " 532 + e.getMessage()); 533 } 534 } else if (po instanceof Serializable ) { 535 try { 536 ByteArrayOutputStream b = new ByteArrayOutputStream (); 537 ObjectOutputStream out = new ObjectOutputStream (b); 538 out.writeObject(po); 539 out.flush(); 540 out.close(); 541 byte[] data = b.toByteArray(); 542 WSIFObjectInputStream in = new WSIFObjectInputStream(new ByteArrayInputStream (data)); 543 Object poc = in.readObject(); 544 in.close(); 545 dm.setObjectPart(pn, poc); 546 } catch (Exception e) { 547 Trc.exception(e); 548 throw new CloneNotSupportedException ( 549 "Exception thrown whilst cloning part " 550 + pn 551 + " by serialization. Message is " 552 + e.getMessage()); 553 } 554 } else { 555 throw new CloneNotSupportedException ("Part " + pn + " cannot be cloned"); 556 } 557 } 558 if (Trc.ON) 559 Trc.exit(dm.deep()); 560 return dm; 561 } 562 563 private void handleNoPartsException(String part, String type) 564 throws WSIFException { 565 throw new WSIFException( 566 "Cannot get " + type + " part '" + part + "'. No parts are set on the message"); 567 } 568 569 private void handlePartNotFoundException(String part) throws WSIFException { 570 throw new WSIFException( 571 "Cannot get part '" + part + "'. Part was not found in message"); 572 } 573 574 private void handleSourcedPartNotFoundException(String part, Class sclass) 575 throws WSIFException { 576 throw new WSIFException( 577 "Cannot get part. Part '" 578 + part 579 + "' with source '" 580 + sclass 581 + "' was not found in message"); 582 } 583 584 private void handlePartCastException(String part, String act, String exp) 585 throws WSIFException { 586 throw new WSIFException( 587 "Cannot get part '" 588 + part 589 + "'. Cannot convert " 590 + "from " 591 + act 592 + " to " 593 + exp); 594 } 595 596 public String toString() { 597 return deep(); 598 } 599 600 public String deep() { 601 String buff = ""; 602 try { 603 buff = new String (super.toString()); 604 buff += " name:" + name; 605 if (parts==null) 606 buff += " parts:null"; 607 else 608 buff += Trc.brief(" parts",parts.values()); 609 } catch (Exception e) { 610 Trc.exceptionInTrace(e); 611 } 612 return buff; 613 } 614 } | Popular Tags |