1 package org.jacorb.notification; 2 3 23 24 import java.util.Date ; 25 26 import org.jacorb.notification.filter.ComponentName; 27 import org.jacorb.notification.filter.EvaluationContext; 28 import org.jacorb.notification.filter.EvaluationException; 29 import org.jacorb.notification.filter.EvaluationResult; 30 import org.jacorb.notification.interfaces.Message; 31 import org.jacorb.util.Time; 32 import org.omg.CORBA.Any ; 33 import org.omg.CORBA.AnyHolder ; 34 import org.omg.CORBA.TCKind ; 35 import org.omg.CosNotification.Priority; 36 import org.omg.CosNotification.Property; 37 import org.omg.CosNotification.StartTime; 38 import org.omg.CosNotification.StopTime; 39 import org.omg.CosNotification.StructuredEvent; 40 import org.omg.CosNotification.StructuredEventHelper; 41 import org.omg.CosNotification.Timeout; 42 import org.omg.CosNotifyFilter.Filter; 43 import org.omg.CosNotifyFilter.MappingFilter; 44 import org.omg.CosNotifyFilter.UnsupportedFilterableData; 45 import org.omg.TimeBase.TimeTHelper; 46 import org.omg.TimeBase.UtcT; 47 import org.omg.TimeBase.UtcTHelper; 48 49 55 56 public class StructuredEventMessage extends AbstractMessage 57 { 58 private Any anyValue_; 59 60 private StructuredEvent structuredEventValue_; 61 62 private Property[] typedEventValue_; 63 64 private boolean isTranslationPossible_ = true; 65 66 private String constraintKey_; 67 68 private Date startTime_ = null; 69 70 private Date stopTime_ = null; 71 72 private long timeout_ = 0; 73 74 private boolean isTimeoutSet_; 75 76 private short priority_; 77 78 80 public synchronized void setStructuredEvent(StructuredEvent structuredEvent, 81 boolean startTimeSupported, boolean timeOutSupported) 82 { 83 structuredEventValue_ = structuredEvent; 84 85 constraintKey_ = AbstractMessage.calcConstraintKey( 86 structuredEventValue_.header.fixed_header.event_type.domain_name, 87 structuredEventValue_.header.fixed_header.event_type.type_name); 88 89 parseQosSettings(startTimeSupported, timeOutSupported); 90 } 91 92 public synchronized void reset() 93 { 94 super.reset(); 95 96 anyValue_ = null; 97 structuredEventValue_ = null; 98 typedEventValue_ = null; 99 isTranslationPossible_ = true; 100 constraintKey_ = null; 101 startTime_ = null; 102 stopTime_ = null; 103 priority_ = 0; 104 } 105 106 public int getType() 107 { 108 return Message.TYPE_STRUCTURED; 109 } 110 111 public synchronized Any toAny() 112 { 113 if (anyValue_ == null) 114 { 115 anyValue_ = sOrb.create_any(); 116 StructuredEventHelper.insert(anyValue_, structuredEventValue_); 117 } 118 119 return anyValue_; 120 } 121 122 public synchronized StructuredEvent toStructuredEvent() 123 { 124 return structuredEventValue_; 125 } 126 127 public synchronized Property[] toTypedEvent() throws NoTranslationException 128 { 129 if (!isTranslationPossible_) 130 { 131 throw new NoTranslationException(); 132 } 133 134 if (typedEventValue_ == null) 135 { 136 try 137 { 138 if (!structuredEventValue_.filterable_data[0].name.equals("operation")) 139 { 140 throw new IllegalArgumentException (); 141 } 142 143 if (!structuredEventValue_.filterable_data[0].value.type().kind().equals( 144 TCKind.tk_string)) 145 { 146 throw new IllegalArgumentException (); 147 } 148 149 typedEventValue_ = structuredEventValue_.filterable_data; 150 } catch (Exception e) 151 { 152 isTranslationPossible_ = false; 153 154 throw new NoTranslationException(e); 155 } 156 } 157 158 return typedEventValue_; 159 } 160 161 public String getConstraintKey() 162 { 163 return constraintKey_; 164 } 165 166 public EvaluationResult extractFilterableData(EvaluationContext context, ComponentName root, 167 String v) throws EvaluationException 168 { 169 Any _any = context.getDynamicEvaluator().evaluatePropertyList( 170 toStructuredEvent().filterable_data, v); 171 172 return EvaluationResult.fromAny(_any); 173 } 174 175 public EvaluationResult extractVariableHeader(EvaluationContext context, ComponentName root, 176 String v) throws EvaluationException 177 { 178 Any _any = context.getDynamicEvaluator().evaluatePropertyList( 179 toStructuredEvent().header.variable_header, v); 180 181 return EvaluationResult.fromAny(_any); 182 } 183 184 private void parseQosSettings(boolean startTimeSupported, boolean timeoutSupported) 185 { 186 Property[] props = toStructuredEvent().header.variable_header; 187 188 for (int x = 0; x < props.length; ++x) 189 { 190 if (startTimeSupported && StartTime.value.equals(props[x].name)) 191 { 192 startTime_ = new Date (unixTime(UtcTHelper.extract(props[x].value))); 193 } 194 else if (StopTime.value.equals(props[x].name)) 195 { 196 stopTime_ = new Date (unixTime(UtcTHelper.extract(props[x].value))); 197 } 198 else if (timeoutSupported && Timeout.value.equals(props[x].name)) 199 { 200 setTimeout(TimeTHelper.extract(props[x].value)); 201 } 202 else if (Priority.value.equals(props[x].name)) 203 { 204 priority_ = props[x].value.extract_short(); 205 } 206 } 207 } 208 209 private static long unixTime(UtcT corbaTime) 210 { 211 long _unixTime = (corbaTime.time - Time.UNIX_OFFSET) / 10000; 212 213 if (corbaTime.tdf != 0) 214 { 215 _unixTime = _unixTime - (corbaTime.tdf * 60000); 216 } 217 218 return _unixTime; 219 } 220 221 public synchronized boolean hasStartTime() 222 { 223 return startTime_ != null; 224 } 225 226 public long getStartTime() 227 { 228 return startTime_.getTime(); 229 } 230 231 public boolean hasStopTime() 232 { 233 return stopTime_ != null; 234 } 235 236 public synchronized long getStopTime() 237 { 238 return stopTime_.getTime(); 239 } 240 241 public boolean hasTimeout() 242 { 243 return isTimeoutSet_; 244 } 245 246 public long getTimeout() 247 { 248 return timeout_; 249 } 250 251 private void setTimeout(long timeout) 252 { 253 isTimeoutSet_ = true; 254 timeout_ = timeout; 255 } 256 257 public boolean match(Filter filter) throws UnsupportedFilterableData 258 { 259 return filter.match_structured(toStructuredEvent()); 260 } 261 262 public int getPriority() 263 { 264 return priority_; 265 } 266 267 public boolean match(MappingFilter filter, AnyHolder value) throws UnsupportedFilterableData 268 { 269 return filter.match_structured(toStructuredEvent(), value); 270 } 271 272 public String toString() 273 { 274 StringBuffer buffer = new StringBuffer ("StructuredEventMessage [referenced="); 275 buffer.append(referenced_); 276 277 buffer.append(", StructuredEvent="); 278 buffer.append(toStructuredEvent()); 279 buffer.append("]"); 280 281 return buffer.toString(); 282 } 283 } | Popular Tags |