KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jacorb > notification > util > QoSPropertySet


1 package org.jacorb.notification.util;
2
3 /*
4  * JacORB - a free Java ORB
5  *
6  * Copyright (C) 1997-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 import java.util.ArrayList JavaDoc;
24 import java.util.Collections JavaDoc;
25 import java.util.HashSet JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.Set JavaDoc;
28
29 import org.apache.avalon.framework.configuration.Configuration;
30 import org.jacorb.notification.conf.Attributes;
31 import org.jacorb.notification.conf.Default;
32 import org.omg.CORBA.Any JavaDoc;
33 import org.omg.CORBA.BAD_OPERATION JavaDoc;
34 import org.omg.CosNotification.AnyOrder;
35 import org.omg.CosNotification.BestEffort;
36 import org.omg.CosNotification.ConnectionReliability;
37 import org.omg.CosNotification.DeadlineOrder;
38 import org.omg.CosNotification.DiscardPolicy;
39 import org.omg.CosNotification.EventReliability;
40 import org.omg.CosNotification.FifoOrder;
41 import org.omg.CosNotification.LifoOrder;
42 import org.omg.CosNotification.MaxEventsPerConsumer;
43 import org.omg.CosNotification.MaximumBatchSize;
44 import org.omg.CosNotification.NamedPropertyRangeSeqHolder;
45 import org.omg.CosNotification.OrderPolicy;
46 import org.omg.CosNotification.PacingInterval;
47 import org.omg.CosNotification.Persistent;
48 import org.omg.CosNotification.Priority;
49 import org.omg.CosNotification.PriorityOrder;
50 import org.omg.CosNotification.Property;
51 import org.omg.CosNotification.PropertyError;
52 import org.omg.CosNotification.PropertyRange;
53 import org.omg.CosNotification.QoSError_code;
54 import org.omg.CosNotification.StartTime;
55 import org.omg.CosNotification.StartTimeSupported;
56 import org.omg.CosNotification.StopTime;
57 import org.omg.CosNotification.StopTimeSupported;
58 import org.omg.CosNotification.Timeout;
59 import org.omg.CosNotification.UnsupportedQoS;
60 import org.omg.TimeBase.TimeTHelper;
61
62 /**
63  * @author Alphonse Bendt
64  * @version $Id: QoSPropertySet.java,v 1.8 2005/04/10 14:30:31 alphonse.bendt Exp $
65  */

66
67 public class QoSPropertySet extends PropertySet
68 {
69     public static final int CHANNEL_QOS = 0;
70     public static final int ADMIN_QOS = 1;
71     public static final int PROXY_QOS = 2;
72
73     private Property[] defaultChannelQoS_;
74     private Property[] defaultAdminQoS_;
75
76     private static final Set JavaDoc sValidChannelQoSNames_;
77     private static final Set JavaDoc sValidAdminQoSNames_;
78     private static final Set JavaDoc sValidProxyQoSNames_;
79     private static final Set JavaDoc sValidMessageQoSNames_;
80
81     private static final Any JavaDoc connectionReliabilityLow_;
82     private static final Any JavaDoc connectionReliabilityHigh_;
83
84     private static final Any JavaDoc eventReliabilityLow_;
85     private static final Any JavaDoc eventReliabilityHigh_;
86
87     private static Any JavaDoc orderPolicyLow_;
88     //private Any orderPolicyDefault_;
89
private static Any JavaDoc orderPolicyHigh_;
90
91     private static Any JavaDoc discardPolicyLow_;
92     private static Any JavaDoc discardPolicyHigh_;
93
94     private static Any JavaDoc priorityLow_;
95     private static Any JavaDoc priorityDefault_;
96     private static Any JavaDoc priorityHigh_;
97
98     private Any JavaDoc maxEventsPerConsumerLow_;
99     private Any JavaDoc maxEventsPerConsumerDefault_;
100     private Any JavaDoc maxEventsPerConsumerHigh_;
101
102     //private Any timeoutHigh_;
103
private Any JavaDoc timeoutDefault_;
104     //private Any timeoutLow_;
105

106     private static final Any JavaDoc trueAny;
107     private static final Any JavaDoc falseAny;
108     
109     ////////////////////////////////////////
110

111     static {
112         trueAny = sORB.create_any();
113         falseAny = sORB.create_any();
114         trueAny.insert_boolean(true);
115         falseAny.insert_boolean(false);
116
117         //////////////////////////////
118

119         HashSet JavaDoc _validChannelQoS = new HashSet JavaDoc();
120         _validChannelQoS.add(EventReliability.value);
121         _validChannelQoS.add(ConnectionReliability.value);
122         _validChannelQoS.add(Priority.value);
123         _validChannelQoS.add(Timeout.value);
124         _validChannelQoS.add(StartTimeSupported.value);
125         _validChannelQoS.add(StopTimeSupported.value);
126         _validChannelQoS.add(MaxEventsPerConsumer.value);
127         _validChannelQoS.add(OrderPolicy.value);
128         _validChannelQoS.add(DiscardPolicy.value);
129         _validChannelQoS.add(MaximumBatchSize.value);
130         _validChannelQoS.add(PacingInterval.value);
131
132         sValidChannelQoSNames_ = Collections.unmodifiableSet(_validChannelQoS);
133         
134         ////////////////////
135

136         HashSet JavaDoc _adminNames = new HashSet JavaDoc(sValidChannelQoSNames_);
137         _adminNames.remove(EventReliability.value);
138         sValidAdminQoSNames_ = Collections.unmodifiableSet(_adminNames);
139     
140         ////////////////////
141

142         sValidProxyQoSNames_ = sValidAdminQoSNames_;
143
144         ////////////////////
145

146         HashSet JavaDoc _validMessageQoS = new HashSet JavaDoc();
147         _validMessageQoS.add(EventReliability.value);
148         _validMessageQoS.add(Priority.value);
149         _validMessageQoS.add(StartTime.value);
150         _validMessageQoS.add(StopTime.value);
151         _validMessageQoS.add(Timeout.value);
152
153         sValidMessageQoSNames_ = Collections.unmodifiableSet(_validMessageQoS);
154         
155         ////////////////////
156

157         connectionReliabilityHigh_ = sORB.create_any();
158         connectionReliabilityHigh_.insert_short(Persistent.value);
159
160         connectionReliabilityLow_ = sORB.create_any();
161         connectionReliabilityLow_.insert_short(BestEffort.value);
162
163         ////////////////////
164

165         eventReliabilityLow_ = sORB.create_any();
166         eventReliabilityLow_.insert_short(BestEffort.value);
167
168         eventReliabilityHigh_ = sORB.create_any();
169         eventReliabilityHigh_.insert_short(BestEffort.value);
170
171         ////////////////////
172

173         orderPolicyLow_ = sORB.create_any();
174         orderPolicyLow_.insert_short(AnyOrder.value);
175
176         orderPolicyHigh_ = sORB.create_any();
177         orderPolicyHigh_.insert_short(DeadlineOrder.value);
178
179         ////////////////////
180

181         discardPolicyLow_ = sORB.create_any();
182         discardPolicyLow_.insert_short(AnyOrder.value);
183
184         discardPolicyHigh_ = sORB.create_any();
185         discardPolicyHigh_.insert_short(DeadlineOrder.value);
186
187         ////////////////////
188

189         priorityLow_ = sORB.create_any();
190         priorityLow_.insert_short(Short.MIN_VALUE);
191
192         priorityDefault_ = sORB.create_any();
193         priorityDefault_.insert_short((short)0);
194
195         priorityHigh_ = sORB.create_any();
196         priorityHigh_.insert_short(Short.MAX_VALUE);
197     }
198
199
200     private void configure (Configuration conf)
201     {
202         int _maxEventsPerConsumerDefault =
203             conf.getAttributeAsInteger( Attributes.MAX_EVENTS_PER_CONSUMER,
204                                         Default.DEFAULT_MAX_EVENTS_PER_CONSUMER );
205
206         maxEventsPerConsumerDefault_ = sORB.create_any();
207         maxEventsPerConsumerDefault_.insert_long(_maxEventsPerConsumerDefault);
208
209         maxEventsPerConsumerHigh_ = sORB.create_any();
210         maxEventsPerConsumerLow_ = sORB.create_any();
211
212         maxEventsPerConsumerLow_.insert_long(0);
213         maxEventsPerConsumerHigh_.insert_long(Integer.MAX_VALUE);
214
215         ////////////////////
216

217         timeoutDefault_ = sORB.create_any();
218         TimeTHelper.insert(timeoutDefault_, 0);
219
220         ////////////////////
221

222         Any JavaDoc _isStartTimeSupportedDefault = sORB.create_any();
223
224         boolean _isStartTimeSupported =
225             conf.getAttribute(Attributes.START_TIME_SUPPORTED,
226                               Default.DEFAULT_START_TIME_SUPPORTED).
227             equalsIgnoreCase("on");
228
229         _isStartTimeSupportedDefault.insert_boolean(_isStartTimeSupported);
230
231         ////////////////////
232

233         Any JavaDoc _isStopTimeSupportedDefault = sORB.create_any();
234
235         boolean _isStopTimeSupported =
236             conf.getAttribute(Attributes.STOP_TIME_SUPPORTED,
237                               Default.DEFAULT_STOP_TIME_SUPPORTED).
238             equalsIgnoreCase("on");
239         _isStopTimeSupportedDefault.insert_boolean(_isStopTimeSupported);
240
241         ////////////////////
242

243         int _maxBatchSize =
244             conf.getAttributeAsInteger(Attributes.MAX_BATCH_SIZE,
245                                        Default.DEFAULT_MAX_BATCH_SIZE);
246
247         Any JavaDoc _maxBatchSizeDefault = sORB.create_any();
248         _maxBatchSizeDefault.insert_long(_maxBatchSize);
249
250         ////////////////////
251

252         defaultChannelQoS_ = new Property[] {
253             new Property(EventReliability.value, eventReliabilityLow_),
254             new Property(ConnectionReliability.value,
255                          connectionReliabilityLow_),
256             new Property(Priority.value, priorityDefault_),
257             new Property(MaxEventsPerConsumer.value,
258                          maxEventsPerConsumerDefault_),
259             new Property(Timeout.value, timeoutDefault_),
260             new Property(StartTimeSupported.value,
261                          _isStartTimeSupportedDefault),
262             new Property(StopTimeSupported.value,
263                          _isStartTimeSupportedDefault),
264             new Property(MaximumBatchSize.value,
265                          _maxBatchSizeDefault)
266         };
267
268         defaultAdminQoS_ = new Property[] {
269             new Property(ConnectionReliability.value,
270                          connectionReliabilityLow_),
271             new Property(Priority.value, priorityDefault_),
272             new Property(MaxEventsPerConsumer.value,
273                          maxEventsPerConsumerDefault_),
274             new Property(Timeout.value, timeoutDefault_),
275             new Property(StartTimeSupported.value,
276                          _isStartTimeSupportedDefault),
277             new Property(StopTimeSupported.value,
278                          _isStartTimeSupportedDefault),
279             new Property(MaximumBatchSize.value, _maxBatchSizeDefault)
280         };
281     }
282
283     ////////////////////////////////////////
284

285     private final Set JavaDoc validNames_;
286
287     ////////////////////////////////////////
288

289     public QoSPropertySet(Configuration configuration, int type)
290     {
291         super();
292
293         configure(configuration);
294         
295         switch (type)
296             {
297             case CHANNEL_QOS:
298                 validNames_ = sValidChannelQoSNames_;
299
300                 set_qos(defaultChannelQoS_);
301                 break;
302             case ADMIN_QOS:
303                 validNames_ = sValidAdminQoSNames_;
304
305                 set_qos(defaultAdminQoS_);
306                 break;
307             case PROXY_QOS:
308                 validNames_ = sValidProxyQoSNames_;
309                 break;
310             default:
311                 throw new IllegalArgumentException JavaDoc("Type " + type + " is invalid");
312             }
313     }
314
315     ////////////////////////////////////////
316

317     protected Set JavaDoc getValidNames()
318     {
319         return validNames_;
320     }
321
322
323     public void set_qos(Property[] ps)
324     {
325         set_properties(ps);
326     }
327
328
329     public Property[] get_qos()
330     {
331         return toArray();
332     }
333
334
335     public void validate_qos(Property[] props,
336                              NamedPropertyRangeSeqHolder namedPropertyRange)
337         throws UnsupportedQoS
338     {
339         List JavaDoc _errors = new ArrayList JavaDoc();
340
341         checkPropertyExistence(props, _errors);
342
343         if (!_errors.isEmpty()) {
344             throw new UnsupportedQoS((PropertyError[])_errors.toArray(PROPERTY_ERROR_ARRAY_TEMPLATE));
345         }
346
347         checkPropertyValues(props, _errors);
348
349         if (!_errors.isEmpty()) {
350             throw new UnsupportedQoS((PropertyError[])_errors.toArray(PROPERTY_ERROR_ARRAY_TEMPLATE));
351         }
352     }
353
354
355     private short checkIsShort(String JavaDoc name, Any JavaDoc value, List JavaDoc errors) throws BAD_OPERATION JavaDoc {
356         try {
357             return value.extract_short();
358         } catch (BAD_OPERATION JavaDoc e) {
359             errors.add(badType(name));
360
361             throw e;
362         }
363     }
364
365
366     private void logError(List JavaDoc errors,
367                           QoSError_code error_code,
368                           String JavaDoc name,
369                           Any JavaDoc value,
370                           Any JavaDoc high,
371                           Any JavaDoc low) {
372
373          if (logger_.isInfoEnabled()) {
374              logger_.info("wrong value for Property '" +name + "': " + value);
375          }
376
377         errors.add(new PropertyError(error_code,
378                                      name,
379                                      new PropertyRange(high,
380                                                        low)));
381     }
382
383
384     private void checkPropertyValues(Property[] ps, List JavaDoc errors)
385     {
386         for (int x = 0; x < ps.length; ++x) {
387             String JavaDoc _propertyName = ps[x].name;
388             Any JavaDoc _value = ps[x].value;
389
390             try {
391                 if (ConnectionReliability.value.equals(_propertyName)) {
392
393                     short _connectionReliability =
394                         checkIsShort(_propertyName, _value, errors);
395
396                     switch (_connectionReliability)
397                         {
398                         case BestEffort.value:
399                             // fallthrough
400
case Persistent.value:
401                             break;
402                         default:
403                             logError(errors,
404                                      QoSError_code.BAD_VALUE,
405                                      _propertyName,
406                                      _value,
407                                      connectionReliabilityLow_,
408                                      connectionReliabilityHigh_ );
409                         }
410                 } else if (EventReliability.value.equals(_propertyName)) {
411
412                     short _eReliability = checkIsShort(_propertyName, _value, errors);
413
414                     switch (_eReliability)
415                         {
416                         case BestEffort.value:
417                             // fallthrough
418
case Persistent.value:
419                             break;
420                         default:
421                             logError(errors,
422                                      QoSError_code.BAD_VALUE,
423                                      _propertyName,
424                                      _value,
425                                      eventReliabilityLow_,
426                                      eventReliabilityHigh_);
427                         }
428                 } else if (OrderPolicy.value.equals(_propertyName)) {
429
430                     short _oPolicy = checkIsShort(_propertyName, _value, errors);
431
432                     switch (_oPolicy)
433                         {
434                         case AnyOrder.value:
435                             break;
436                         case FifoOrder.value:
437                             break;
438                         case PriorityOrder.value:
439                             break;
440                         case DeadlineOrder.value:
441                             break;
442                         default:
443                             logError(errors,
444                                      QoSError_code.BAD_VALUE,
445                                      _propertyName,
446                                      _value,
447                                      orderPolicyLow_,
448                                      orderPolicyHigh_);
449                         }
450                 } else if (DiscardPolicy.value.equals(_propertyName)) {
451                     short _dPolicy = checkIsShort(_propertyName, _value, errors);
452
453                     switch (_dPolicy)
454                         {
455                         case AnyOrder.value:
456                             break;
457                         case FifoOrder.value:
458                             break;
459                         case LifoOrder.value:
460                             break;
461                         case PriorityOrder.value:
462                             break;
463                         case DeadlineOrder.value:
464                             break;
465                         default:
466                             logError(errors,
467                                      QoSError_code.BAD_VALUE,
468                                      _propertyName,
469                                      _value,
470                                      discardPolicyLow_,
471                                      discardPolicyHigh_);
472                         }
473                 }
474             } catch (BAD_OPERATION JavaDoc e) {
475                 // Nothing to do. a error has already been added to
476
// List 'errors'.
477
}
478         }
479     }
480 }
481
Popular Tags