KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jacorb > notification > filter > impl > DefaultETCLEvaluator


1 package org.jacorb.notification.filter.impl;
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 org.apache.avalon.framework.configuration.Configuration;
25 import org.apache.avalon.framework.logger.Logger;
26 import org.jacorb.notification.filter.ETCLEvaluator;
27 import org.jacorb.notification.filter.EvaluationContext;
28 import org.jacorb.notification.filter.EvaluationException;
29 import org.jacorb.notification.filter.EvaluationResult;
30 import org.omg.CORBA.Any JavaDoc;
31 import org.omg.CORBA.ORB JavaDoc;
32 import org.omg.CORBA.TCKind JavaDoc;
33 import org.omg.CORBA.TypeCode JavaDoc;
34 import org.omg.CORBA.TypeCodePackage.BadKind JavaDoc;
35 import org.omg.CORBA.TypeCodePackage.Bounds JavaDoc;
36 import org.omg.CosNotification.Property;
37 import org.omg.DynamicAny.DynAny JavaDoc;
38 import org.omg.DynamicAny.DynAnyFactory JavaDoc;
39 import org.omg.DynamicAny.DynSequence JavaDoc;
40 import org.omg.DynamicAny.DynSequenceHelper JavaDoc;
41 import org.omg.DynamicAny.DynStruct JavaDoc;
42 import org.omg.DynamicAny.DynStructHelper JavaDoc;
43 import org.omg.DynamicAny.DynUnion JavaDoc;
44 import org.omg.DynamicAny.DynUnionHelper JavaDoc;
45 import org.omg.DynamicAny.DynAnyFactoryPackage.InconsistentTypeCode JavaDoc;
46 import org.omg.DynamicAny.DynAnyPackage.InvalidValue JavaDoc;
47 import org.omg.DynamicAny.DynAnyPackage.TypeMismatch JavaDoc;
48
49 /**
50  * Provide the Basic operations needed to evaluate filter expressions on Anys.
51  *
52  * @author Alphonse Bendt
53  * @version $Id: DefaultETCLEvaluator.java,v 1.2 2005/05/01 21:52:09 alphonse.bendt Exp $
54  */

55
56 public class DefaultETCLEvaluator implements ETCLEvaluator
57 {
58     private static final String JavaDoc NAME = "name";
59
60     private static final String JavaDoc VALUE = "value";
61
62     ////////////////////////////////////////
63

64     private final Logger logger_;
65
66     private final DynAnyFactory JavaDoc dynAnyFactory_;
67
68     private static final ORB JavaDoc orb_ = ORB.init();
69
70     private static final Any JavaDoc TRUE_ANY = orb_.create_any();
71
72     private static final Any JavaDoc FALSE_ANY = orb_.create_any();
73
74     static
75     {
76         TRUE_ANY.insert_boolean(true);
77
78         FALSE_ANY.insert_boolean(false);
79     }
80
81     ////////////////////////////////////////
82

83     public DefaultETCLEvaluator(Configuration config, DynAnyFactory JavaDoc dynAnyFactory)
84     {
85         logger_ = ((org.jacorb.config.Configuration) config).getNamedLogger(getClass().getName());
86
87         dynAnyFactory_ = dynAnyFactory;
88     }
89
90     ////////////////////////////////////////
91

92     public boolean hasDefaultDiscriminator(Any JavaDoc any) throws EvaluationException
93     {
94         try
95         {
96             return (any.type().default_index() != -1);
97         } catch (BadKind JavaDoc e)
98         {
99             throw newEvaluationException(e);
100         }
101     }
102
103     public Any JavaDoc evaluateExistIdentifier(Any JavaDoc value, String JavaDoc identifier) throws EvaluationException
104     {
105         try
106         {
107             evaluateIdentifier(value, identifier);
108
109             return TRUE_ANY;
110         } catch (EvaluationException e)
111         {
112             return FALSE_ANY;
113         }
114     }
115
116     public Any JavaDoc evaluateTypeName(Any JavaDoc value) throws EvaluationException
117     {
118         try
119         {
120             TypeCode JavaDoc _tc = value.type();
121             Any JavaDoc _ret = orb_.create_any();
122             _ret.insert_string(_tc.name());
123
124             return _ret;
125         } catch (BadKind JavaDoc e)
126         {
127             throw newEvaluationException(e);
128         }
129     }
130
131     public Any JavaDoc evaluateRepositoryId(Any JavaDoc value) throws EvaluationException
132     {
133         try
134         {
135             TypeCode JavaDoc _tc = value.type();
136             Any JavaDoc _ret = orb_.create_any();
137             _ret.insert_string(_tc.id());
138
139             return _ret;
140         } catch (BadKind JavaDoc e)
141         {
142             throw newEvaluationException(e);
143         }
144     }
145
146     public Any JavaDoc evaluateListLength(Any JavaDoc value) throws EvaluationException
147     {
148         final int _length;
149
150         switch (value.type().kind().value()) {
151         case TCKind._tk_array:
152             DynAny JavaDoc _dynAny = toDynAny(value);
153             _length = _dynAny.component_count();
154             break;
155
156         case TCKind._tk_sequence:
157             DynSequence JavaDoc _dynSequence = toDynSequence(value);
158             _length = _dynSequence.get_length();
159             break;
160
161         default:
162             throw new EvaluationException("Neither array nor sequence");
163         }
164
165         Any JavaDoc _any = orb_.create_any();
166         _any.insert_long(_length);
167
168         return _any;
169     }
170
171     private String JavaDoc getDefaultUnionMemberName(TypeCode JavaDoc unionTypeCode) throws EvaluationException
172     {
173         try
174         {
175             int _defaultIndex = unionTypeCode.default_index();
176
177             if (_defaultIndex != -1)
178             {
179                 return unionTypeCode.member_name(_defaultIndex);
180             }
181         } catch (BadKind JavaDoc e)
182         {
183             throw newEvaluationException(e);
184         } catch (Bounds JavaDoc e)
185         {
186             throw newEvaluationException(e);
187         }
188
189         throw new EvaluationException();
190     }
191
192     private String JavaDoc getUnionMemberNameFromDiscriminator(TypeCode JavaDoc unionTypeCode, int discriminator)
193             throws EvaluationException
194     {
195         try
196         {
197             Any JavaDoc _any = orb_.create_any();
198
199             switch (unionTypeCode.discriminator_type().kind().value()) {
200
201             case TCKind._tk_long:
202                 _any.insert_long(discriminator);
203                 break;
204
205             case TCKind._tk_ulong:
206                 _any.insert_ulong(discriminator);
207                 break;
208
209             case TCKind._tk_short:
210                 _any.insert_short((short) discriminator);
211                 break;
212
213             case TCKind._tk_double:
214                 _any.insert_double(discriminator);
215                 break;
216
217             case TCKind._tk_ushort:
218                 _any.insert_ushort((short) discriminator);
219                 break;
220             }
221
222             int _memberCount = unionTypeCode.member_count();
223
224             try
225             {
226                 for (int _x = 0; _x < _memberCount; _x++)
227                 {
228                     if (_any.equal(unionTypeCode.member_label(_x)))
229                     {
230                         return unionTypeCode.member_name(_x);
231                     }
232                 }
233             } catch (Bounds JavaDoc b)
234             {
235                 // this should never happen as _x should be always < _memberCount.
236
throw new RuntimeException JavaDoc();
237             }
238
239         } catch (BadKind JavaDoc e)
240         {
241             throw newEvaluationException(e);
242         }
243
244         throw new EvaluationException();
245     }
246
247     public Any JavaDoc evaluateUnion(Any JavaDoc value) throws EvaluationException
248     {
249         String JavaDoc _defaultMemberName = getDefaultUnionMemberName(value.type());
250
251         return evaluateIdentifier(value, _defaultMemberName);
252     }
253
254     public Any JavaDoc evaluateUnion(Any JavaDoc value, int position) throws EvaluationException
255     {
256         final DynUnion JavaDoc _dynUnion = toDynUnion(value);
257
258         _dynUnion.seek(0);
259
260         if (logger_.isDebugEnabled())
261         {
262             logger_.debug("extract idx: " + position + " from Union " + _dynUnion.type());
263         }
264
265         String JavaDoc _discrimName = getUnionMemberNameFromDiscriminator(value.type(), position);
266
267         return evaluateIdentifier(_dynUnion, _discrimName);
268     }
269
270     public Any JavaDoc evaluatePropertyList(Property[] list, String JavaDoc name)
271     {
272         if (logger_.isDebugEnabled())
273         {
274             logger_.debug("evaluatePropertyList " + list);
275             logger_.debug("list length: " + list.length);
276         }
277
278         for (int x = 0; x < list.length; ++x)
279         {
280             if (logger_.isDebugEnabled())
281             {
282                 logger_.debug(x + ": " + list[x].name + " => " + list[x].value);
283             }
284
285             if (name.equals(list[x].name))
286             {
287                 return list[x].value;
288             }
289         }
290
291         return null;
292     }
293
294     public Any JavaDoc evaluateNamedValueList(Any JavaDoc any, String JavaDoc name) throws EvaluationException
295     {
296         try
297         {
298             if (logger_.isDebugEnabled())
299             {
300                 logger_.debug("evaluateNamedValueList(" + any + ", " + name + ")");
301             }
302
303             final DynAny JavaDoc _dynAny = toDynAny(any);
304             final int _count = _dynAny.component_count();
305
306             DynAny JavaDoc _cursor;
307             Any JavaDoc _ret = null;
308
309             _dynAny.rewind();
310
311             if (logger_.isDebugEnabled())
312             {
313                 logger_.debug("Entries: " + _count);
314             }
315
316             for (int _x = 0; _x < _count; _x++)
317             {
318                 _dynAny.seek(_x);
319                 _cursor = _dynAny.current_component();
320                 _ret = evaluateNamedValue(_cursor, name);
321
322                 if (_ret != null)
323                 {
324                     break;
325                 }
326             }
327
328             return _ret;
329         } catch (TypeMismatch JavaDoc e)
330         {
331             throw newEvaluationException(e);
332         }
333     }
334
335     private Any JavaDoc evaluateNamedValue(DynAny JavaDoc any, String JavaDoc name) throws EvaluationException
336     {
337         if (logger_.isDebugEnabled())
338         {
339             logger_.debug("evaluate assoc " + name + " on a Any of type: " + any.type());
340         }
341
342         Any JavaDoc _ret = null;
343
344         String JavaDoc _anyName = evaluateIdentifier(any, NAME).extract_string();
345
346         if (logger_.isDebugEnabled())
347         {
348             logger_.debug("test if " + name + " == " + _anyName);
349         }
350
351         if (name.equals(_anyName))
352         {
353             logger_.debug("YES");
354             _ret = evaluateIdentifier(any, VALUE);
355         }
356
357         return _ret;
358     }
359
360     public Any JavaDoc evaluateArrayIndex(Any JavaDoc any, int index) throws EvaluationException
361     {
362         try
363         {
364             if (logger_.isDebugEnabled())
365             {
366                 logger_.debug("evaluate array idx " + index + " on a Any of type: " + any.type());
367             }
368
369             DynAny JavaDoc _dynAny = toDynAny(any);
370             DynAny JavaDoc _cursor;
371
372             _dynAny.rewind();
373             _dynAny.seek(index);
374             _cursor = _dynAny.current_component();
375
376             if (logger_.isDebugEnabled())
377             {
378                 logger_.debug("evaluation result is of type: " + _cursor.type());
379             }
380
381             return _cursor.to_any();
382         } catch (TypeMismatch JavaDoc e)
383         {
384             throw newEvaluationException(e);
385         }
386     }
387
388     private Any JavaDoc evaluateIdentifier(DynAny JavaDoc any, int position) throws EvaluationException
389     {
390         try
391         {
392             final DynAny JavaDoc _result;
393
394             switch (any.type().kind().value()) {
395
396             case TCKind._tk_struct:
397                 any.seek(position);
398                 _result = any.current_component();
399                 break;
400
401             default:
402                 throw new EvaluationException("attempt to access member on non-struct");
403             }
404
405             return _result.to_any();
406         } catch (TypeMismatch JavaDoc e)
407         {
408             throw newEvaluationException(e);
409         }
410     }
411
412     public Any JavaDoc evaluateIdentifier(Any JavaDoc any, int position) throws EvaluationException
413     {
414         if (logger_.isDebugEnabled())
415         {
416             logger_.debug("evaluate idx " + position + " on Any");
417         }
418
419         DynAny JavaDoc _dynAny = toDynAny(any);
420
421         return evaluateIdentifier(_dynAny, position);
422     }
423
424     public Any JavaDoc evaluateDiscriminator(Any JavaDoc any) throws EvaluationException
425     {
426         switch (any.type().kind().value()) {
427         case TCKind._tk_union:
428             DynUnion JavaDoc _dynUnion = toDynUnion(any);
429             return _dynUnion.get_discriminator().to_any();
430
431         default:
432             throw new EvaluationException("any does not contain member _d");
433         }
434     }
435
436     public EvaluationResult evaluateElementInSequence(EvaluationContext context,
437             EvaluationResult element, Any JavaDoc sequence) throws EvaluationException
438     {
439         try
440         {
441             final DynSequence JavaDoc _dynSequence = DynSequenceHelper.narrow(toDynAny(sequence));
442             DynAny JavaDoc _currentComponent;
443
444             _dynSequence.rewind();
445
446             while (true)
447             {
448                 _currentComponent = _dynSequence.current_component();
449
450                 EvaluationResult _r = EvaluationResult.fromAny(_currentComponent.to_any());
451
452                 if (element.compareTo(_r) == 0)
453                 {
454                     return EvaluationResult.BOOL_TRUE;
455                 }
456
457                 if (!_dynSequence.next())
458                 {
459                     return EvaluationResult.BOOL_FALSE;
460                 }
461             }
462         } catch (TypeMismatch JavaDoc e)
463         {
464             throw newEvaluationException(e);
465         }
466     }
467
468     /**
469      * expensive
470      */

471     public Any JavaDoc evaluateIdentifier(Any JavaDoc any, String JavaDoc identifier) throws EvaluationException
472     {
473         // expensive call
474
DynAny JavaDoc _dynAny = toDynAny(any);
475
476         // expensive call
477
return evaluateIdentifier(_dynAny, identifier);
478     }
479
480     private Any JavaDoc evaluateIdentifier(DynAny JavaDoc any, String JavaDoc identifier) throws EvaluationException
481     {
482         try
483         {
484             String JavaDoc _strippedIdentifier = stripBackslash(identifier);
485
486             if (logger_.isDebugEnabled())
487             {
488                 logger_.debug("evaluate " + _strippedIdentifier + " on Any");
489             }
490
491             DynAny JavaDoc _cursor = any;
492
493             switch (any.type().kind().value()) {
494
495             case TCKind._tk_struct:
496
497                 logger_.debug("Any is a struct");
498
499                 final DynStruct JavaDoc _dynStruct = DynStructHelper.narrow(any);
500                 String JavaDoc _currentName;
501
502                 _dynStruct.rewind();
503
504                 while (true)
505                 {
506                     _currentName = _dynStruct.current_member_name();
507
508                     if (logger_.isDebugEnabled())
509                     {
510                         logger_.debug(" => " + _currentName);
511                     }
512
513                     if (_currentName.equals(_strippedIdentifier))
514                     {
515                         // expensive operation
516
_cursor = _dynStruct.current_component();
517                         break;
518                     }
519
520                     boolean _hasNext = _dynStruct.next();
521
522                     if (!_hasNext)
523                     {
524                         throw new EvaluationException("struct has no member " + _strippedIdentifier);
525                     }
526                 }
527
528                 break;
529
530             case TCKind._tk_union:
531
532                 if (logger_.isDebugEnabled())
533                 {
534                     logger_.debug("Any is a Union");
535                 }
536
537                 DynUnion JavaDoc _dynUnion = toDynUnion(any);
538
539                 if (_dynUnion.member_name().equals(_strippedIdentifier))
540                 {
541                     _cursor = _dynUnion.member();
542                 }
543                 else
544                 {
545                     if (logger_.isDebugEnabled())
546                     {
547                         logger_.debug(_dynUnion.member_name() + " != " + _strippedIdentifier);
548                     }
549
550                     throw new EvaluationException("member " + _strippedIdentifier
551                             + " is not active on struct");
552                 }
553
554                 break;
555
556             case TCKind._tk_any:
557                 logger_.debug("encapsulated any");
558
559                 return evaluateIdentifier(any.get_any(), _strippedIdentifier);
560
561             default:
562                 logger_.debug("unknown " + any.type());
563
564                 return null;
565             }
566
567             if (logger_.isDebugEnabled())
568             {
569                 logger_.debug("Result: " + _cursor);
570             }
571
572             if (_cursor != null && logger_.isDebugEnabled())
573             {
574                 logger_.debug("evaluation result is of type: " + _cursor.type());
575             }
576
577             if (_cursor == null)
578             {
579                 logger_.debug("Member not found");
580
581                 throw new EvaluationException("member not found");
582             }
583
584             return _cursor.to_any();
585         } catch (InvalidValue JavaDoc e)
586         {
587             throw newEvaluationException(e);
588         } catch (TypeMismatch JavaDoc e)
589         {
590             throw newEvaluationException(e);
591         }
592     }
593
594     ////////////////////////////////////////
595

596     private DynAny JavaDoc toDynAny(Any JavaDoc any) throws EvaluationException
597     {
598         try
599         {
600             return dynAnyFactory_.create_dyn_any(any);
601         } catch (InconsistentTypeCode JavaDoc e)
602         {
603             throw newEvaluationException(e);
604         }
605     }
606
607     private DynUnion JavaDoc toDynUnion(Any JavaDoc any) throws EvaluationException
608     {
609         return DynUnionHelper.narrow(toDynAny(any));
610     }
611
612     private DynUnion JavaDoc toDynUnion(DynAny JavaDoc dynAny)
613     {
614         return DynUnionHelper.narrow(dynAny);
615     }
616
617     private DynSequence JavaDoc toDynSequence(Any JavaDoc any) throws EvaluationException
618     {
619         return DynSequenceHelper.narrow(toDynAny(any));
620     }
621
622     private static String JavaDoc stripBackslash(String JavaDoc identifier)
623     {
624         StringBuffer JavaDoc _buffer = new StringBuffer JavaDoc();
625         int _length = identifier.length();
626
627         for (int _x = 0; _x < _length; _x++)
628         {
629             if (identifier.charAt(_x) != '\\')
630             {
631                 _buffer.append(identifier.charAt(_x));
632             }
633         }
634
635         return _buffer.toString();
636     }
637
638     private static EvaluationException newEvaluationException(Exception JavaDoc e)
639     {
640         return new EvaluationException(e.getMessage());
641     }
642 }
Popular Tags