View Javadoc
1 /*** 2 * Copyright 2003, 2004, 2005. CodeStreet LLC. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 7 * 8 * Unless required by applicable law or agreed to in writing, software 9 * distributed under the License is distributed on an "AS IS" BASIS, 10 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 * See the License for the specific language governing permissions and 12 * limitations under the License. 13 */ 14 15 package com.codestreet.selector.rv; 16 17 import java.util.StringTokenizer; 18 19 import com.codestreet.selector.parser.IValueProvider; 20 import com.codestreet.selector.parser.Identifier; 21 import com.codestreet.selector.parser.NumericValue; 22 import com.tibco.tibrv.TibrvException; 23 import com.tibco.tibrv.TibrvMsg; 24 import com.tibco.tibrv.TibrvMsgField; 25 26 /*** 27 * /** Value provider for TIBCO/Rendezvous. This implementation allows values to 28 * be extracted from <tt>Rendezvous</tt> messages using a <tt>dot</tt> 29 * notation. 30 * <p> 31 * For example, <tt>.order.quantity</tt> would access the <tt>quantity</tt> 32 * field of the nested sub-message field <tt>order</tt>, if it exists. If the 33 * field does not exist, it returns <tt>null</tt>. 34 * <p> 35 * The reason for requiring a leading <tt>dot</tt> before field names - 36 * includign top level fields - is to provide comptibility between the 37 * <tt>Rendezvous</tt> and <tt>JMS</tt> value provides. 38 * 39 * @author Jawaid Hakim. 40 * 41 * @author Jawaid Hakim. 42 */ 43 public class ValueProvider implements IValueProvider 44 { 45 /*** 46 * Factory method. 47 * 48 * @param msg 49 * Message to extract values from. 50 * @return Instance. 51 */ 52 public static ValueProvider valueOf(final TibrvMsg msg) 53 { 54 return new ValueProvider(msg); 55 } 56 57 /*** 58 * Ctor. 59 * 60 * @param msg 61 * <tt>Message</tt> from which values will be extracted. 62 * expressions. 63 * <p> 64 * For example, <tt>trade.quantity</tt> would select the 65 * <tt>quantity</tt> field of the nested sub-message field 66 * <tt>trade</tt> if it exists. Otherwise, it selects nothing (<tt>null</tt>). 67 */ 68 private ValueProvider(final TibrvMsg msg) 69 { 70 msg_ = msg; 71 } 72 73 /*** 74 * Get the value for the specified identifier. 75 * 76 * @param identifier 77 * Field identifier. 78 * @param correlation 79 * Application correlation data. May be <tt>null</tt>. 80 * @return Value of the specified identifier. Returns <tt>null</tt> if the 81 * value of the identifier is not found. 82 */ 83 public Object getValue(final Object identifier, final Object correlation) 84 { 85 try 86 { 87 return getValueFromDot(msg_, (Identifier) identifier); 88 } 89 catch (TibrvException ex) 90 { 91 throw new IllegalArgumentException(ex.toString()); 92 } 93 } 94 95 private static Object getValueFromDot(final TibrvMsg msg, final Identifier identifier) 96 throws TibrvException 97 { 98 TibrvMsg workMsg = msg; 99 100 TibrvMsgField fld = null; 101 String id = identifier.getIdentifier(); 102 103 // Skip over leading '.' if any 104 int ind = id.indexOf('.'); 105 if (ind == 0) 106 { 107 id = id.substring(1); 108 ind = id.indexOf('.'); 109 } 110 if (ind < 0) 111 { 112 fld = workMsg.getField(id); 113 } 114 else 115 { 116 for (StringTokenizer strTok = new StringTokenizer(id, ".", false); strTok 117 .hasMoreTokens();) 118 { 119 if (fld != null) 120 workMsg = (TibrvMsg) fld.data; 121 String fldName = strTok.nextToken(); 122 fld = workMsg.getField(fldName); 123 } 124 } 125 126 if (fld != null) 127 { 128 /*** 129 * Wrap numeric values in NumericValue instance 130 */ 131 if ((fld.data instanceof Integer)) 132 return new NumericValue((Integer) fld.data); 133 134 if ((fld.data instanceof Float)) 135 return new NumericValue((Float) fld.data); 136 137 if ((fld.data instanceof Double)) 138 return new NumericValue((Double) fld.data); 139 140 if ((fld.data instanceof Long)) 141 return new NumericValue((Long) fld.data); 142 143 if ((fld.data instanceof Short)) 144 return new NumericValue((Short) fld.data); 145 146 if ((fld.data instanceof Byte)) 147 return new NumericValue((Byte) fld.data); 148 149 return fld.data; 150 } 151 152 return null; 153 } 154 155 private final TibrvMsg msg_; 156 }

This page was automatically generated by Maven