KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jacorb > notification > StructuredEventMessage


1 package org.jacorb.notification;
2
3 /*
4  * JacORB - a free Java ORB
5  *
6  * Copyright (C) 1999-2004 Gerald Brose
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the Free
20  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  *
22  */

23
24 import java.util.Date JavaDoc;
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 JavaDoc;
33 import org.omg.CORBA.AnyHolder JavaDoc;
34 import org.omg.CORBA.TCKind JavaDoc;
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 /**
50  * Adapts a StructuredEvent to the Message Interface.
51  *
52  * @author Alphonse Bendt
53  * @version $Id: StructuredEventMessage.java,v 1.17 2005/04/16 23:00:28 alphonse.bendt Exp $
54  */

55
56 public class StructuredEventMessage extends AbstractMessage
57 {
58     private Any JavaDoc anyValue_;
59
60     private StructuredEvent structuredEventValue_;
61
62     private Property[] typedEventValue_;
63
64     private boolean isTranslationPossible_ = true;
65
66     private String JavaDoc constraintKey_;
67
68     private Date JavaDoc startTime_ = null;
69
70     private Date JavaDoc stopTime_ = null;
71
72     private long timeout_ = 0;
73
74     private boolean isTimeoutSet_;
75
76     private short priority_;
77
78     // //////////////////////////////////////
79

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 JavaDoc 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 JavaDoc();
141                 }
142
143                 if (!structuredEventValue_.filterable_data[0].value.type().kind().equals(
144                         TCKind.tk_string))
145                 {
146                     throw new IllegalArgumentException JavaDoc();
147                 }
148
149                 typedEventValue_ = structuredEventValue_.filterable_data;
150             } catch (Exception JavaDoc e)
151             {
152                 isTranslationPossible_ = false;
153
154                 throw new NoTranslationException(e);
155             }
156         }
157
158         return typedEventValue_;
159     }
160
161     public String JavaDoc getConstraintKey()
162     {
163         return constraintKey_;
164     }
165
166     public EvaluationResult extractFilterableData(EvaluationContext context, ComponentName root,
167             String JavaDoc v) throws EvaluationException
168     {
169         Any JavaDoc _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 JavaDoc v) throws EvaluationException
177     {
178         Any JavaDoc _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 JavaDoc(unixTime(UtcTHelper.extract(props[x].value)));
193             }
194             else if (StopTime.value.equals(props[x].name))
195             {
196                 stopTime_ = new Date JavaDoc(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 JavaDoc value) throws UnsupportedFilterableData
268     {
269         return filter.match_structured(toStructuredEvent(), value);
270     }
271
272     public String JavaDoc toString()
273     {
274         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc("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