001: package org.jacorb.notification;
002:
003: /*
004: * JacORB - a free Java ORB
005: *
006: * Copyright (C) 1999-2004 Gerald Brose
007: *
008: * This library is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU Library General Public
010: * License as published by the Free Software Foundation; either
011: * version 2 of the License, or (at your option) any later version.
012: *
013: * This library is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
016: * Library General Public License for more details.
017: *
018: * You should have received a copy of the GNU Library General Public
019: * License along with this library; if not, write to the Free
020: * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
021: *
022: */
023:
024: import java.util.Date;
025:
026: import org.jacorb.notification.filter.ComponentName;
027: import org.jacorb.notification.filter.EvaluationContext;
028: import org.jacorb.notification.filter.EvaluationException;
029: import org.jacorb.notification.filter.EvaluationResult;
030: import org.jacorb.notification.interfaces.Message;
031: import org.jacorb.util.Time;
032: import org.omg.CORBA.Any;
033: import org.omg.CORBA.AnyHolder;
034: import org.omg.CORBA.ORB;
035: import org.omg.CORBA.TCKind;
036: import org.omg.CosNotification.Priority;
037: import org.omg.CosNotification.Property;
038: import org.omg.CosNotification.StartTime;
039: import org.omg.CosNotification.StopTime;
040: import org.omg.CosNotification.StructuredEvent;
041: import org.omg.CosNotification.StructuredEventHelper;
042: import org.omg.CosNotification.Timeout;
043: import org.omg.CosNotifyFilter.Filter;
044: import org.omg.CosNotifyFilter.MappingFilter;
045: import org.omg.CosNotifyFilter.UnsupportedFilterableData;
046: import org.omg.TimeBase.TimeTHelper;
047: import org.omg.TimeBase.UtcT;
048: import org.omg.TimeBase.UtcTHelper;
049:
050: /**
051: * Adapts a StructuredEvent to the Message Interface.
052: *
053: * @author Alphonse Bendt
054: * @version $Id: StructuredEventMessage.java,v 1.23 2006/07/03 12:51:42 alphonse.bendt Exp $
055: */
056:
057: public class StructuredEventMessage extends AbstractMessage {
058: private Any anyValue_;
059:
060: private StructuredEvent structuredEventValue_;
061:
062: private Property[] typedEventValue_;
063:
064: private String constraintKey_;
065:
066: private Date startTime_ = null;
067:
068: private Date stopTime_ = null;
069:
070: private long timeout_ = 0;
071:
072: private boolean isTimeoutSet_;
073:
074: private short priority_;
075:
076: private NoTranslationException translationException_ = null;
077:
078: private final ORB orb;
079:
080: // //////////////////////////////////////
081:
082: public StructuredEventMessage(ORB orb) {
083: this .orb = orb;
084: }
085:
086: public synchronized void setStructuredEvent(
087: StructuredEvent structuredEvent,
088: boolean startTimeSupported, boolean stopTimeSupported) {
089: structuredEventValue_ = structuredEvent;
090:
091: constraintKey_ = AbstractMessage
092: .calcConstraintKey(
093: structuredEventValue_.header.fixed_header.event_type.domain_name,
094: structuredEventValue_.header.fixed_header.event_type.type_name);
095:
096: parseQosSettings(startTimeSupported, stopTimeSupported);
097: }
098:
099: public void doReset() {
100: anyValue_ = null;
101: structuredEventValue_ = null;
102: typedEventValue_ = null;
103: constraintKey_ = null;
104: startTime_ = null;
105: stopTime_ = null;
106: priority_ = 0;
107: translationException_ = null;
108: }
109:
110: public int getType() {
111: return Message.TYPE_STRUCTURED;
112: }
113:
114: public synchronized Any toAny() {
115: if (anyValue_ == null) {
116: anyValue_ = orb.create_any();
117: StructuredEventHelper.insert(anyValue_,
118: structuredEventValue_);
119: }
120:
121: return anyValue_;
122: }
123:
124: public synchronized StructuredEvent toStructuredEvent() {
125: return structuredEventValue_;
126: }
127:
128: public synchronized Property[] toTypedEvent()
129: throws NoTranslationException {
130: if (translationException_ != null) {
131: throw translationException_;
132: }
133:
134: if (typedEventValue_ == null) {
135: try {
136: if (!structuredEventValue_.filterable_data[0].name
137: .equals("operation")) {
138: throw new IllegalArgumentException();
139: }
140:
141: if (!structuredEventValue_.filterable_data[0].value
142: .type().kind().equals(TCKind.tk_string)) {
143: throw new IllegalArgumentException();
144: }
145:
146: typedEventValue_ = structuredEventValue_.filterable_data;
147: } catch (Exception e) {
148: translationException_ = new NoTranslationException(e);
149: throw translationException_;
150: }
151: }
152:
153: return typedEventValue_;
154: }
155:
156: public synchronized String getConstraintKey() {
157: return constraintKey_;
158: }
159:
160: public EvaluationResult extractFilterableData(
161: EvaluationContext context, ComponentName root, String name)
162: throws EvaluationException {
163: Any _any = context.getETCLEvaluator().evaluatePropertyList(
164: toStructuredEvent().filterable_data, name);
165:
166: return EvaluationResult.fromAny(_any);
167: }
168:
169: public EvaluationResult extractVariableHeader(
170: EvaluationContext context, ComponentName root, String name)
171: throws EvaluationException {
172: Any _any = context.getETCLEvaluator().evaluatePropertyList(
173: toStructuredEvent().header.variable_header, name);
174:
175: return EvaluationResult.fromAny(_any);
176: }
177:
178: private synchronized void parseQosSettings(
179: boolean startTimeSupported, boolean stopTimeSupported) {
180: final Property[] props = toStructuredEvent().header.variable_header;
181:
182: for (int x = 0; x < props.length; ++x) {
183: if (startTimeSupported
184: && StartTime.value.equals(props[x].name)) {
185: startTime_ = new Date(unixTime(UtcTHelper
186: .extract(props[x].value)));
187: } else if (stopTimeSupported
188: && StopTime.value.equals(props[x].name)) {
189: stopTime_ = new Date(unixTime(UtcTHelper
190: .extract(props[x].value)));
191: } else if (stopTimeSupported
192: && Timeout.value.equals(props[x].name)) {
193: setTimeout(TimeTHelper.extract(props[x].value) / 10000);
194: } else if (Priority.value.equals(props[x].name)) {
195: priority_ = props[x].value.extract_short();
196: }
197: }
198: }
199:
200: private static long unixTime(UtcT corbaTime) {
201: long _unixTime = (corbaTime.time - Time.UNIX_OFFSET) / 10000;
202:
203: if (corbaTime.tdf != 0) {
204: _unixTime = _unixTime - (corbaTime.tdf * 60000);
205: }
206:
207: return _unixTime;
208: }
209:
210: public synchronized boolean hasStartTime() {
211: return startTime_ != null;
212: }
213:
214: public synchronized long getStartTime() {
215: return startTime_.getTime();
216: }
217:
218: public synchronized boolean hasStopTime() {
219: return stopTime_ != null;
220: }
221:
222: public synchronized long getStopTime() {
223: return stopTime_.getTime();
224: }
225:
226: public synchronized boolean hasTimeout() {
227: return isTimeoutSet_;
228: }
229:
230: public synchronized long getTimeout() {
231: return timeout_;
232: }
233:
234: private synchronized void setTimeout(long timeout) {
235: isTimeoutSet_ = true;
236: timeout_ = timeout;
237: }
238:
239: public boolean match(Filter filter)
240: throws UnsupportedFilterableData {
241: return filter.match_structured(toStructuredEvent());
242: }
243:
244: public synchronized int getPriority() {
245: return priority_;
246: }
247:
248: public boolean match(MappingFilter filter, AnyHolder value)
249: throws UnsupportedFilterableData {
250: return filter.match_structured(toStructuredEvent(), value);
251: }
252:
253: public String toString() {
254: StringBuffer buffer = new StringBuffer(
255: "StructuredEventMessage [referenced=");
256: buffer.append(referenced_);
257:
258: buffer.append(", StructuredEvent=");
259: buffer.append(toStructuredEvent());
260: buffer.append("]");
261:
262: return buffer.toString();
263: }
264: }
|