001: package org.jacorb.notification.util;
002:
003: /*
004: * JacORB - a free Java ORB
005: *
006: * Copyright (C) 1997-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: import java.util.ArrayList;
024: import java.util.Collections;
025: import java.util.HashSet;
026: import java.util.List;
027: import java.util.Set;
028:
029: import org.apache.avalon.framework.configuration.Configuration;
030: import org.jacorb.notification.conf.Attributes;
031: import org.jacorb.notification.conf.Default;
032: import org.omg.CORBA.Any;
033: import org.omg.CORBA.BAD_OPERATION;
034: import org.omg.CosNotification.AnyOrder;
035: import org.omg.CosNotification.BestEffort;
036: import org.omg.CosNotification.ConnectionReliability;
037: import org.omg.CosNotification.DeadlineOrder;
038: import org.omg.CosNotification.DiscardPolicy;
039: import org.omg.CosNotification.EventReliability;
040: import org.omg.CosNotification.FifoOrder;
041: import org.omg.CosNotification.LifoOrder;
042: import org.omg.CosNotification.MaxEventsPerConsumer;
043: import org.omg.CosNotification.MaximumBatchSize;
044: import org.omg.CosNotification.NamedPropertyRangeSeqHolder;
045: import org.omg.CosNotification.OrderPolicy;
046: import org.omg.CosNotification.PacingInterval;
047: import org.omg.CosNotification.Persistent;
048: import org.omg.CosNotification.Priority;
049: import org.omg.CosNotification.PriorityOrder;
050: import org.omg.CosNotification.Property;
051: import org.omg.CosNotification.PropertyError;
052: import org.omg.CosNotification.PropertyRange;
053: import org.omg.CosNotification.QoSError_code;
054: import org.omg.CosNotification.StartTime;
055: import org.omg.CosNotification.StartTimeSupported;
056: import org.omg.CosNotification.StopTime;
057: import org.omg.CosNotification.StopTimeSupported;
058: import org.omg.CosNotification.Timeout;
059: import org.omg.CosNotification.UnsupportedQoS;
060: import org.omg.TimeBase.TimeTHelper;
061:
062: /**
063: * @author Alphonse Bendt
064: * @version $Id: QoSPropertySet.java,v 1.10 2005/10/27 21:36:17 alphonse.bendt Exp $
065: */
066:
067: public class QoSPropertySet extends PropertySet {
068: public static final int CHANNEL_QOS = 0;
069: public static final int ADMIN_QOS = 1;
070: public static final int PROXY_QOS = 2;
071:
072: private Property[] defaultChannelQoS_;
073: private Property[] defaultAdminQoS_;
074:
075: private static final Set sValidChannelQoSNames_;
076: private static final Set sValidAdminQoSNames_;
077: private static final Set sValidProxyQoSNames_;
078:
079: private static final Any connectionReliabilityLow_;
080: private static final Any connectionReliabilityHigh_;
081:
082: private static final Any eventReliabilityLow_;
083: private static final Any eventReliabilityHigh_;
084:
085: private static Any orderPolicyLow_;
086: //private Any orderPolicyDefault_;
087: private static Any orderPolicyHigh_;
088:
089: private static Any discardPolicyLow_;
090: private static Any discardPolicyHigh_;
091:
092: private static Any priorityLow_;
093: private static Any priorityDefault_;
094: private static Any priorityHigh_;
095:
096: private Any maxEventsPerConsumerLow_;
097: private Any maxEventsPerConsumerDefault_;
098: private Any maxEventsPerConsumerHigh_;
099:
100: //private Any timeoutHigh_;
101: private Any timeoutDefault_;
102: //private Any timeoutLow_;
103:
104: private static final Any trueAny;
105: private static final Any falseAny;
106:
107: ////////////////////////////////////////
108:
109: static {
110: trueAny = sORB.create_any();
111: falseAny = sORB.create_any();
112: trueAny.insert_boolean(true);
113: falseAny.insert_boolean(false);
114:
115: //////////////////////////////
116:
117: HashSet _validChannelQoS = new HashSet();
118: _validChannelQoS.add(EventReliability.value);
119: _validChannelQoS.add(ConnectionReliability.value);
120: _validChannelQoS.add(Priority.value);
121: _validChannelQoS.add(Timeout.value);
122: _validChannelQoS.add(StartTimeSupported.value);
123: _validChannelQoS.add(StopTimeSupported.value);
124: _validChannelQoS.add(MaxEventsPerConsumer.value);
125: _validChannelQoS.add(OrderPolicy.value);
126: _validChannelQoS.add(DiscardPolicy.value);
127: _validChannelQoS.add(MaximumBatchSize.value);
128: _validChannelQoS.add(PacingInterval.value);
129:
130: sValidChannelQoSNames_ = Collections
131: .unmodifiableSet(_validChannelQoS);
132:
133: ////////////////////
134:
135: HashSet _adminNames = new HashSet(sValidChannelQoSNames_);
136: _adminNames.remove(EventReliability.value);
137: sValidAdminQoSNames_ = Collections.unmodifiableSet(_adminNames);
138:
139: ////////////////////
140:
141: sValidProxyQoSNames_ = sValidAdminQoSNames_;
142:
143: ////////////////////
144:
145: HashSet _validMessageQoS = new HashSet();
146: _validMessageQoS.add(EventReliability.value);
147: _validMessageQoS.add(Priority.value);
148: _validMessageQoS.add(StartTime.value);
149: _validMessageQoS.add(StopTime.value);
150: _validMessageQoS.add(Timeout.value);
151:
152: ////////////////////
153:
154: connectionReliabilityHigh_ = sORB.create_any();
155: connectionReliabilityHigh_.insert_short(Persistent.value);
156:
157: connectionReliabilityLow_ = sORB.create_any();
158: connectionReliabilityLow_.insert_short(BestEffort.value);
159:
160: ////////////////////
161:
162: eventReliabilityLow_ = sORB.create_any();
163: eventReliabilityLow_.insert_short(BestEffort.value);
164:
165: eventReliabilityHigh_ = sORB.create_any();
166: eventReliabilityHigh_.insert_short(BestEffort.value);
167:
168: ////////////////////
169:
170: orderPolicyLow_ = sORB.create_any();
171: orderPolicyLow_.insert_short(AnyOrder.value);
172:
173: orderPolicyHigh_ = sORB.create_any();
174: orderPolicyHigh_.insert_short(DeadlineOrder.value);
175:
176: ////////////////////
177:
178: discardPolicyLow_ = sORB.create_any();
179: discardPolicyLow_.insert_short(AnyOrder.value);
180:
181: discardPolicyHigh_ = sORB.create_any();
182: discardPolicyHigh_.insert_short(DeadlineOrder.value);
183:
184: ////////////////////
185:
186: priorityLow_ = sORB.create_any();
187: priorityLow_.insert_short(Short.MIN_VALUE);
188:
189: priorityDefault_ = sORB.create_any();
190: priorityDefault_.insert_short((short) 0);
191:
192: priorityHigh_ = sORB.create_any();
193: priorityHigh_.insert_short(Short.MAX_VALUE);
194: }
195:
196: private void configure(Configuration conf) {
197: int _maxEventsPerConsumerDefault = conf.getAttributeAsInteger(
198: Attributes.MAX_EVENTS_PER_CONSUMER,
199: Default.DEFAULT_MAX_EVENTS_PER_CONSUMER);
200:
201: maxEventsPerConsumerDefault_ = sORB.create_any();
202: maxEventsPerConsumerDefault_
203: .insert_long(_maxEventsPerConsumerDefault);
204:
205: maxEventsPerConsumerHigh_ = sORB.create_any();
206: maxEventsPerConsumerLow_ = sORB.create_any();
207:
208: maxEventsPerConsumerLow_.insert_long(0);
209: maxEventsPerConsumerHigh_.insert_long(Integer.MAX_VALUE);
210:
211: ////////////////////
212:
213: timeoutDefault_ = sORB.create_any();
214: TimeTHelper.insert(timeoutDefault_, 0);
215:
216: ////////////////////
217:
218: Any _isStartTimeSupportedDefault = sORB.create_any();
219:
220: boolean _isStartTimeSupported = conf.getAttribute(
221: Attributes.START_TIME_SUPPORTED,
222: Default.DEFAULT_START_TIME_SUPPORTED).equalsIgnoreCase(
223: "on");
224:
225: _isStartTimeSupportedDefault
226: .insert_boolean(_isStartTimeSupported);
227:
228: ////////////////////
229:
230: Any _isStopTimeSupportedDefault = sORB.create_any();
231:
232: boolean _isStopTimeSupported = conf.getAttribute(
233: Attributes.STOP_TIME_SUPPORTED,
234: Default.DEFAULT_STOP_TIME_SUPPORTED).equalsIgnoreCase(
235: "on");
236: _isStopTimeSupportedDefault
237: .insert_boolean(_isStopTimeSupported);
238:
239: ////////////////////
240:
241: int _maxBatchSize = conf.getAttributeAsInteger(
242: Attributes.MAX_BATCH_SIZE,
243: Default.DEFAULT_MAX_BATCH_SIZE);
244:
245: Any _maxBatchSizeDefault = sORB.create_any();
246: _maxBatchSizeDefault.insert_long(_maxBatchSize);
247:
248: ////////////////////
249:
250: defaultChannelQoS_ = new Property[] {
251: new Property(EventReliability.value,
252: eventReliabilityLow_),
253: new Property(ConnectionReliability.value,
254: connectionReliabilityLow_),
255: new Property(Priority.value, priorityDefault_),
256: new Property(MaxEventsPerConsumer.value,
257: maxEventsPerConsumerDefault_),
258: new Property(Timeout.value, timeoutDefault_),
259: new Property(StartTimeSupported.value,
260: _isStartTimeSupportedDefault),
261: new Property(StopTimeSupported.value,
262: _isStartTimeSupportedDefault),
263: new Property(MaximumBatchSize.value,
264: _maxBatchSizeDefault) };
265:
266: defaultAdminQoS_ = new Property[] {
267: new Property(ConnectionReliability.value,
268: connectionReliabilityLow_),
269: new Property(Priority.value, priorityDefault_),
270: new Property(MaxEventsPerConsumer.value,
271: maxEventsPerConsumerDefault_),
272: new Property(Timeout.value, timeoutDefault_),
273: new Property(StartTimeSupported.value,
274: _isStartTimeSupportedDefault),
275: new Property(StopTimeSupported.value,
276: _isStartTimeSupportedDefault),
277: new Property(MaximumBatchSize.value,
278: _maxBatchSizeDefault) };
279: }
280:
281: ////////////////////////////////////////
282:
283: private final Set validNames_;
284:
285: ////////////////////////////////////////
286:
287: public QoSPropertySet(Configuration configuration, int type) {
288: super ();
289:
290: configure(configuration);
291:
292: switch (type) {
293: case CHANNEL_QOS:
294: validNames_ = sValidChannelQoSNames_;
295:
296: set_qos(defaultChannelQoS_);
297: break;
298: case ADMIN_QOS:
299: validNames_ = sValidAdminQoSNames_;
300:
301: set_qos(defaultAdminQoS_);
302: break;
303: case PROXY_QOS:
304: validNames_ = sValidProxyQoSNames_;
305: break;
306: default:
307: throw new IllegalArgumentException("Type " + type
308: + " is invalid");
309: }
310: }
311:
312: ////////////////////////////////////////
313:
314: protected Set getValidNames() {
315: return validNames_;
316: }
317:
318: public void set_qos(Property[] ps) {
319: set_properties(ps);
320: }
321:
322: public Property[] get_qos() {
323: return toArray();
324: }
325:
326: public void validate_qos(Property[] props,
327: NamedPropertyRangeSeqHolder namedPropertyRange)
328: throws UnsupportedQoS {
329: List _errors = new ArrayList();
330:
331: checkPropertyExistence(props, _errors);
332:
333: if (!_errors.isEmpty()) {
334: throw new UnsupportedQoS((PropertyError[]) _errors
335: .toArray(PROPERTY_ERROR_ARRAY_TEMPLATE));
336: }
337:
338: checkPropertyValues(props, _errors);
339:
340: if (!_errors.isEmpty()) {
341: throw new UnsupportedQoS((PropertyError[]) _errors
342: .toArray(PROPERTY_ERROR_ARRAY_TEMPLATE));
343: }
344: }
345:
346: private short checkIsShort(String name, Any value, List errors)
347: throws BAD_OPERATION {
348: try {
349: return value.extract_short();
350: } catch (BAD_OPERATION e) {
351: errors.add(badType(name));
352:
353: throw e;
354: }
355: }
356:
357: private void logError(List errors, QoSError_code error_code,
358: String name, Any value, Any high, Any low) {
359:
360: if (logger_.isInfoEnabled()) {
361: logger_.info("wrong value for Property '" + name + "': "
362: + value);
363: }
364:
365: errors.add(new PropertyError(error_code, name,
366: new PropertyRange(high, low)));
367: }
368:
369: private void checkPropertyValues(Property[] props, List errors) {
370: for (int x = 0; x < props.length; ++x) {
371: final String _name = props[x].name;
372: final Any _value = props[x].value;
373:
374: try {
375: if (ConnectionReliability.value.equals(_name)) {
376:
377: final short _connectionReliability = checkIsShort(
378: _name, _value, errors);
379:
380: switch (_connectionReliability) {
381: case BestEffort.value:
382: // fallthrough
383: case Persistent.value:
384: break;
385: default:
386: logError(errors, QoSError_code.BAD_VALUE,
387: _name, _value,
388: connectionReliabilityLow_,
389: connectionReliabilityHigh_);
390: }
391: } else if (EventReliability.value.equals(_name)) {
392:
393: final short _eventReliability = checkIsShort(_name,
394: _value, errors);
395:
396: switch (_eventReliability) {
397: case BestEffort.value:
398: // fallthrough
399: case Persistent.value:
400: break;
401: default:
402: logError(errors, QoSError_code.BAD_VALUE,
403: _name, _value, eventReliabilityLow_,
404: eventReliabilityHigh_);
405: }
406: } else if (OrderPolicy.value.equals(_name)) {
407:
408: final short _orderPolicy = checkIsShort(_name,
409: _value, errors);
410:
411: switch (_orderPolicy) {
412: case AnyOrder.value:
413: break;
414: case FifoOrder.value:
415: break;
416: case PriorityOrder.value:
417: break;
418: case DeadlineOrder.value:
419: break;
420: default:
421: logError(errors, QoSError_code.BAD_VALUE,
422: _name, _value, orderPolicyLow_,
423: orderPolicyHigh_);
424: }
425: } else if (DiscardPolicy.value.equals(_name)) {
426: final short _discardPolicy = checkIsShort(_name,
427: _value, errors);
428:
429: switch (_discardPolicy) {
430: case AnyOrder.value:
431: break;
432: case FifoOrder.value:
433: break;
434: case LifoOrder.value:
435: break;
436: case PriorityOrder.value:
437: break;
438: case DeadlineOrder.value:
439: break;
440: default:
441: logError(errors, QoSError_code.BAD_VALUE,
442: _name, _value, discardPolicyLow_,
443: discardPolicyHigh_);
444: }
445: }
446: } catch (BAD_OPERATION e) {
447: // Nothing to do. an error has already been added to
448: // List 'errors'.
449: }
450: }
451: }
452: }
|