View Javadoc
1 /*** 2 * Copyright (c) 2005, CodeStreet LLC. All rights reserved. 3 * <p> 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions are met: 6 * <p> 7 * Redistributions of source code must retain the above copyright notice, this 8 * list of conditions and the following disclaimer. Redistributions in binary 9 * form must reproduce the above copyright notice, this list of conditions and 10 * the following disclaimer in the documentation and/or other materials provided 11 * with the distribution. Neither the name of CodeStreet LLC. nor the names of 12 * its contributors may be used to endorse or promote products derived from this 13 * software without specific prior written permission. 14 * <p> 15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 23 * CONTRACT, STRICT LIABILITY, OR TORT(INCLUDING NEGLIGENCE OR OTHERWISE) 24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 25 * POSSIBILITY OF SUCH DAMAGE. 26 * <p> 27 */ 28 29 package com.codestreet.messageforge; 30 31 import java.util.ArrayList; 32 import java.util.Date; 33 import java.util.Enumeration; 34 import java.util.List; 35 36 import com.tibco.tibrv.TibrvException; 37 import com.tibco.tibrv.TibrvMsg; 38 import com.tibco.tibrv.TibrvMsgField; 39 40 public abstract class ConverterTibrv extends Converter 41 { 42 /*** 43 * Create a <tt>TibrvMsg</tt> from a <tt>RMapMessage</tt>. The 44 * marshalling to TIBCO Rendezvous is done by in three steps: First a 45 * <tt>String</tt> field with the name of the message is set in the 46 * <tt>TibrvMsg</tt>. Next all fields in the <tt>RMapMessage</tt> that 47 * are supported in TIBCO Rendezvous are copied to the <tt>TibrvMsg</tt>. 48 * <p> 49 * 50 * @param source 51 * Source message. 52 * @return <tt>TibrvMsg</tt> 53 * @see RMapMessage 54 * @see RMsg 55 */ 56 public static TibrvMsg marshal(RMapMessage source) 57 throws ConverterException 58 { 59 try 60 { 61 TibrvMsg target = new TibrvMsg(); 62 return marshal(source, target); 63 } 64 catch (Exception ex) 65 { 66 throw new ConverterException(ex); 67 } 68 } 69 70 /*** 71 * Create a <tt>TibrvMsg</tt> from a <tt>RMapMessage</tt>. The 72 * marshalling to TIBCO Rendezvous is done by in three steps: First a 73 * <tt>String</tt> field with the name of the message is set in the 74 * <tt>com.tibco.tibrv.TibrvMsg</tt>. Next all fields in the 75 * <tt>RMapMessage</tt> that are supported in TIBCO Rendezvous are copied 76 * to the <tt>com.tibco.tibrv.TibrvMsg</tt>. 77 * <p> 78 * 79 * @param source 80 * Source message. 81 * @return <tt>TibrvMsg</tt> 82 * @see RMapMessage 83 * @see RMsg 84 */ 85 public static TibrvMsg marshal(RMapMessage source, TibrvMsg target) 86 throws ConverterException 87 { 88 if (source == null) 89 { 90 throw new ConverterException("Supplied source TibrvMsg was null"); 91 } 92 if (target == null) 93 { 94 throw new ConverterException("Supplied target TibrvMsg was null"); 95 } 96 try 97 { 98 // Set message name as a String field 99 target.add(MSG_NAME, source.getName()); 100 101 RFld[] flds = source.getFieldsAsArray(); 102 for (int i = flds.length - 1; i >= 0; --i) 103 { 104 RFldInterface srcFld = flds[i]; 105 RFldType type = srcFld.getType(); 106 String fldName = srcFld.getName(); 107 if (srcFld.isValSet()) 108 { 109 if (srcFld.getType().isComplex()) 110 { 111 if (type == RFldType.MSGOBJ) 112 { 113 RFldMsgObj obj = (RFldMsgObj) srcFld; 114 target.add(fldName, ConverterTibrv.marshal(obj 115 .getValue())); 116 } 117 else if (type == RFldType.BEAN) 118 { 119 target.add(fldName, ConverterBeanAndTibrv 120 .marshal(srcFld.getValueAsObject())); 121 } 122 else if (type == RFldType.BEANLIST) 123 { 124 RFldBeanList beanLst = (RFldBeanList) srcFld; 125 List lst = beanLst.getValue(); 126 127 TibrvMsg outer = new TibrvMsg(); 128 outer.add("length", lst.size()); 129 for (int j = 0; j < lst.size(); ++j) 130 { 131 outer.add(String.valueOf(j), 132 ConverterBeanAndTibrv.marshal(lst 133 .get(j))); 134 } 135 target.add(fldName, outer); 136 } 137 else if (type == RFldType.MSGOBJARRAY) 138 { 139 TibrvMsg outer = new TibrvMsg(); 140 RFldMsgObjArray objA = (RFldMsgObjArray) srcFld; 141 outer.add("length", objA.length()); 142 for (int j = 0; j < objA.length(); ++j) 143 { 144 RMsg elem = objA.getElement(j); 145 outer.add(String.valueOf(j), ConverterTibrv 146 .marshal(elem)); 147 } 148 target.add(fldName, outer); 149 } 150 else if (type == RFldType.MSGOBJHASHTABLE) 151 { 152 TibrvMsg outer = new TibrvMsg(); 153 java.util.Hashtable hash = ((RFldMsgObjHashtable) srcFld) 154 .getValue(); 155 for (Enumeration en = hash.keys(); en 156 .hasMoreElements();) 157 { 158 String key = (String) en.nextElement(); 159 RMsg elem = (RMsg) hash.get(key); 160 outer.add(key, ConverterTibrv.marshal(elem)); 161 } 162 target.add(fldName, outer); 163 } 164 else if (type == RFldType.DATEARRAY) 165 { 166 TibrvMsg outer = new TibrvMsg(); 167 RFldDatetimeArray dtA = (RFldDatetimeArray) srcFld; 168 outer.add("length", dtA.length()); 169 for (int j = 0; j < dtA.length(); ++j) 170 { 171 Date elem = dtA.getElement(j); 172 outer.add(String.valueOf(j), RDateFormat.getInstance().format(elem)); 173 } 174 target.add(fldName, outer); 175 } 176 else if (type == RFldType.STRINGARRAY) 177 { 178 TibrvMsg outer = new TibrvMsg(); 179 RFldStringArray strA = (RFldStringArray) srcFld; 180 outer.add("length", strA.length()); 181 for (int j = 0; j < strA.length(); ++j) 182 { 183 String elem = strA.getElement(j); 184 outer.add(String.valueOf(j), elem); 185 } 186 target.add(fldName, outer); 187 } 188 else if (type == RFldType.STRINGLIST) 189 { 190 TibrvMsg outer = new TibrvMsg(); 191 List strL = ((RFldStringList) srcFld).getValue(); 192 outer.add("length", strL.size()); 193 for (int j = 0; j < strL.size(); ++j) 194 { 195 String elem = (String) strL.get(j); 196 outer.add(String.valueOf(j), elem); 197 } 198 target.add(fldName, outer); 199 } 200 else if (type == RFldType.STRINGHASHTABLE) 201 { 202 TibrvMsg outer = new TibrvMsg(); 203 java.util.Hashtable hash = ((RFldMsgObjHashtable) srcFld) 204 .getValue(); 205 for (Enumeration en = hash.keys(); en 206 .hasMoreElements();) 207 { 208 String key = (String) en.nextElement(); 209 String elem = (String) hash.get(key); 210 outer.add(key, elem); 211 } 212 target.add(fldName, outer); 213 } 214 else if (type == RFldType.DECIMAL) 215 { 216 target.add(fldName, Converter 217 .getBigDecimalFormatter().format( 218 srcFld.getValueAsObject())); 219 } 220 } 221 else 222 { 223 target.add(fldName, srcFld.getValueAsObject()); 224 } 225 } 226 } 227 228 return target; 229 } 230 catch (Exception ex) 231 { 232 throw new ConverterException(ex); 233 } 234 } 235 236 /*** 237 * Create and set a <tt>RMsg</tt> from a <tt>TibrvMsg</tt>. 238 * 239 * @param source 240 * Source message. 241 * @return Unmarshalled message object. 242 * @see #unmarshal(TibrvMsg, RMapMessage) 243 */ 244 public static RMapMessage unmarshal(TibrvMsg source) 245 throws ConverterException 246 { 247 try 248 { 249 // Create message object 250 String msgName = (String) source.get(MSG_NAME); 251 if (msgName == null) 252 throw new ConverterException("Message name field not found: " 253 + MSG_NAME); 254 255 RMapMessage target = Converter.createMsgObject(msgName); 256 257 unmarshal(source, target); 258 259 return target; 260 } 261 catch (Exception ex) 262 { 263 throw new ConverterException(ex); 264 } 265 } 266 267 /*** 268 * Create and set a <tt>RMsg</tt> from a <tt>TibrvMsg</tt>. 269 * 270 * @param source 271 * Source message. 272 * @param target 273 * Target message object. 274 */ 275 public static void unmarshal(TibrvMsg source, RMapMessage target) 276 throws ConverterException 277 { 278 try 279 { 280 tibUnmarshal(source, target); 281 } 282 catch (Exception ex) 283 { 284 throw new ConverterException(ex); 285 } 286 } 287 288 private static void tibUnmarshal(TibrvMsg source, RMapMessage target) 289 throws ConverterException, TibrvException 290 { 291 int numFlds = 0; 292 numFlds = source.getNumFields(); 293 294 for (int fldIdx = 0; fldIdx < numFlds; ++fldIdx) 295 { 296 TibrvMsgField fld = source.getFieldByIndex(fldIdx); 297 String name = fld.name; 298 try 299 { 300 Object val = source.get(name); 301 RFld targetFld = target.getFieldIfExists(name); 302 if (targetFld != null) 303 { 304 // Skip if target field is locked 305 if (targetFld.isLocked()) 306 continue; 307 308 RFldType targetFldType = targetFld.getType(); 309 if (targetFldType == RFldType.DECIMAL) 310 { 311 RFldDecimal realFld = (RFldDecimal) targetFld; 312 realFld.set(source.get(name)); 313 } 314 else if (targetFldType.isComplex()) 315 { 316 setComplex((TibrvMsg) val, targetFld); 317 } 318 else 319 { 320 targetFld.set(val); 321 } 322 } 323 } 324 catch (Exception ex) 325 { 326 throw new ConverterException("Error converting field: " + name, 327 ex); 328 } 329 } 330 } 331 332 private static void setComplex(TibrvMsg source, RFld target) 333 throws ConverterException 334 { 335 try 336 { 337 RFldType type = target.getType(); 338 if (type == RFldType.BEAN) 339 { 340 RFldBean realFld = (RFldBean) target; 341 realFld.set(ConverterBeanAndTibrv.unmarshal(source)); 342 } 343 else if (type == RFldType.BEANLIST) 344 { 345 RFldBeanList realFld = (RFldBeanList) target; 346 int len = source.getInt("length", 0); 347 List lst = new ArrayList(len); 348 for (int i = 0; i < len; ++i) 349 { 350 TibrvMsg val = (TibrvMsg) source.get(String.valueOf(i)); 351 lst.add(ConverterBeanAndTibrv.unmarshal(val)); 352 } 353 realFld.set(lst); 354 } 355 else if (type == RFldType.MSGOBJ) 356 { 357 RFldMsgObj realFld = (RFldMsgObj) target; 358 if (realFld.isValSet()) 359 { 360 realFld.getValue().resetFields(); 361 ConverterTibrv.unmarshal(source, (RMapMessage) target 362 .getValueAsObject()); 363 } 364 else 365 { 366 RMsg msg = (RMsg) Converter.createMsgObject(realFld 367 .getClassObject()); 368 ConverterTibrv.unmarshal(source, msg); 369 realFld.set(msg); 370 } 371 } 372 else if (type == RFldType.MSGOBJARRAY) 373 { 374 RFldMsgObjArray realFld = (RFldMsgObjArray) target; 375 int len = source.getInt("length", 0); 376 RMsg[] elements = new RMsg[len]; 377 for (int i = 0; i < len; ++i) 378 { 379 elements[i] = (RMsg) unmarshal((TibrvMsg) source.get(String 380 .valueOf(i))); 381 } 382 realFld.set(elements); 383 } 384 else if (type == RFldType.MSGOBJHASHTABLE) 385 { 386 RFldMsgObjHashtable realFld = (RFldMsgObjHashtable) target; 387 java.util.Hashtable elements = new java.util.Hashtable(); 388 int numFlds = source.getNumFields(); 389 for (int fldIdx = 0; fldIdx < numFlds; ++fldIdx) 390 { 391 TibrvMsgField fld = source.getFieldByIndex(fldIdx); 392 TibrvMsg elem = (TibrvMsg) fld.data; 393 elements.put(fld.name, ConverterTibrv.unmarshal(elem)); 394 } 395 realFld.set(elements); 396 } 397 else if (type == RFldType.DATEARRAY) 398 { 399 RFldDatetimeArray realFld = (RFldDatetimeArray) target; 400 int len = source.getInt("length", 0); 401 Date[] elements = new Date[len]; 402 for (int i = 0; i < len; ++i) 403 { 404 String sDate = (String) source.get(String.valueOf(i)); 405 elements[i] = RDateFormat.getInstance().parse(sDate); 406 } 407 realFld.set(elements); 408 } 409 else if (type == RFldType.STRINGARRAY) 410 { 411 RFldStringArray realFld = (RFldStringArray) target; 412 int len = source.getInt("length", 0); 413 String[] elements = new String[len]; 414 for (int i = 0; i < len; ++i) 415 { 416 elements[i] = (String) source.get(String.valueOf(i)); 417 } 418 realFld.set(elements); 419 } 420 else if (type == RFldType.STRINGLIST) 421 { 422 RFldStringList realFld = (RFldStringList) target; 423 int len = source.getInt("length", 0); 424 java.util.List elements = new java.util.ArrayList(len); 425 for (int i = 0; i < len; ++i) 426 { 427 elements.add(source.get(String.valueOf(i))); 428 } 429 realFld.set(elements); 430 } 431 else if (type == RFldType.STRINGHASHTABLE) 432 { 433 RFldStringHashtable realFld = (RFldStringHashtable) target; 434 java.util.Hashtable elements = new java.util.Hashtable(); 435 int numFlds = source.getNumFields(); 436 for (int fldIdx = 0; fldIdx < numFlds; ++fldIdx) 437 { 438 TibrvMsgField fld = source.getFieldByIndex(fldIdx); 439 elements.put(fld.name, fld.data); 440 } 441 realFld.set(elements); 442 } 443 else 444 throw new ConverterException("Invalid field type: " 445 + type.toString()); 446 } 447 catch (Exception ex) 448 { 449 throw new ConverterException(ex); 450 } 451 } 452 453 /*** 454 * Name of <tt>String</tt> property with the message name. 455 */ 456 public static final String MSG_NAME = "RMsgName"; 457 458 /*** 459 * Name of <tt>String</tt> field with the XML representation of non-JMS 460 * supported field types. 461 */ 462 public static final String XML_FIELD_NAME = "RMsgXML"; 463 }

This page was automatically generated by Maven