1 package org.jacorb.notification.queue; 2 3 23 24 import java.util.Collections ; 25 import java.util.HashMap ; 26 import java.util.Map ; 27 28 import org.apache.avalon.framework.configuration.Configurable; 29 import org.apache.avalon.framework.configuration.Configuration; 30 import org.apache.avalon.framework.configuration.ConfigurationException; 31 import org.jacorb.notification.conf.Attributes; 32 import org.jacorb.notification.conf.Default; 33 import org.jacorb.notification.util.QoSPropertySet; 34 import org.omg.CosNotification.AnyOrder; 35 import org.omg.CosNotification.DeadlineOrder; 36 import org.omg.CosNotification.DiscardPolicy; 37 import org.omg.CosNotification.FifoOrder; 38 import org.omg.CosNotification.LifoOrder; 39 import org.omg.CosNotification.MaxEventsPerConsumer; 40 import org.omg.CosNotification.OrderPolicy; 41 import org.omg.CosNotification.PriorityOrder; 42 43 47 48 public class EventQueueFactory implements Configurable 49 { 50 private static final short UNKNOWN_POLICY = Short.MIN_VALUE; 51 52 private static final Map mapOrderPolicyNameToValue; 53 54 private static final Map mapDiscardPolicyNameToValue; 55 56 private String orderPolicy_; 57 58 private String discardPolicy_; 59 60 static 61 { 62 Map orderMap = new HashMap (); 63 64 orderMap.put("anyorder", new Short (AnyOrder.value)); 65 orderMap.put("fifoorder", new Short (FifoOrder.value)); 66 orderMap.put("priorityorder", new Short (PriorityOrder.value)); 67 orderMap.put("deadlineorder", new Short (DeadlineOrder.value)); 68 69 mapOrderPolicyNameToValue = Collections.unmodifiableMap(orderMap); 70 71 72 Map discardMap = new HashMap (); 73 74 discardMap.put("anyorder", new Short (AnyOrder.value)); 75 discardMap.put("fifoorder", new Short (FifoOrder.value)); 76 discardMap.put("lifoorder", new Short (LifoOrder.value)); 77 discardMap.put("priorityorder", new Short (PriorityOrder.value)); 78 discardMap.put("deadlineorder", new Short (DeadlineOrder.value)); 79 80 mapDiscardPolicyNameToValue = Collections.unmodifiableMap(discardMap); 81 } 82 83 85 public EventQueueFactory(Configuration config) throws ConfigurationException 86 { 87 configure(config); 88 } 89 90 92 public void configure(Configuration conf) throws ConfigurationException 93 { 94 try 95 { 96 setOrderPolicy(conf.getAttribute(Attributes.ORDER_POLICY, Default.DEFAULT_ORDER_POLICY)); 97 98 setDiscardPolicy(conf.getAttribute(Attributes.DISCARD_POLICY, 99 Default.DEFAULT_DISCARD_POLICY)); 100 } catch (IllegalArgumentException e) 101 { 102 throw new ConfigurationException("Invalid Policy", e); 103 } 104 } 105 106 private void setDiscardPolicy(String s) 107 { 108 String policy = s.toLowerCase().trim(); 109 110 if (mapDiscardPolicyNameToValue.containsKey(policy)) 111 { 112 discardPolicy_ = policy; 113 } 114 else 115 { 116 throw new IllegalArgumentException ("Invalid DiscardPolicy: " + s); 117 } 118 } 119 120 private void setOrderPolicy(String s) 121 { 122 String policy = s.toLowerCase().trim(); 123 124 if (mapOrderPolicyNameToValue.containsKey(policy)) 125 { 126 orderPolicy_ = policy; 127 } 128 else 129 { 130 throw new IllegalArgumentException ("Invalid OrderPolicy: " + s); 131 } 132 } 133 134 public MessageQueueAdapter newMessageQueue(QoSPropertySet qosProperties) 135 { 136 short shortOrderPolicy = orderPolicyNameToValue(orderPolicy_); 137 138 short shortDiscardPolicy = discardPolicyNameToValue(discardPolicy_); 139 140 int maxEventsPerConsumer; 141 142 try 143 { 144 maxEventsPerConsumer = qosProperties.get(MaxEventsPerConsumer.value).extract_long(); 145 } catch (Exception e) 146 { 147 maxEventsPerConsumer = Default.DEFAULT_MAX_EVENTS_PER_CONSUMER; 148 } 149 150 if (qosProperties.containsKey(OrderPolicy.value)) 151 { 152 shortOrderPolicy = qosProperties.get(OrderPolicy.value).extract_short(); 153 } 154 155 if (qosProperties.containsKey(DiscardPolicy.value)) 156 { 157 shortDiscardPolicy = qosProperties.get(DiscardPolicy.value).extract_short(); 158 } 159 160 final EventQueueOverflowStrategy _overflowStrategy; 161 switch (shortDiscardPolicy) { 162 case AnyOrder.value: 163 165 case FifoOrder.value: 166 _overflowStrategy = EventQueueOverflowStrategy.FIFO; 167 break; 168 169 case LifoOrder.value: 170 _overflowStrategy = EventQueueOverflowStrategy.LIFO; 171 break; 172 173 case PriorityOrder.value: 174 _overflowStrategy = EventQueueOverflowStrategy.LEAST_PRIORITY; 175 break; 176 177 case DeadlineOrder.value: 178 _overflowStrategy = EventQueueOverflowStrategy.EARLIEST_TIMEOUT; 179 break; 180 181 default: 182 throw new IllegalArgumentException ("Discardpolicy: " + discardPolicy_ 183 + "DiscardPolicyValue: " + shortDiscardPolicy + " unknown"); 184 } 185 186 final AbstractBoundedEventQueue queue; 187 switch (shortOrderPolicy) { 188 case AnyOrder.value: 189 191 case FifoOrder.value: 192 queue = new BoundedFifoEventQueue(maxEventsPerConsumer, _overflowStrategy); 193 break; 194 195 case PriorityOrder.value: 196 queue = new BoundedPriorityEventQueue(maxEventsPerConsumer, _overflowStrategy); 197 break; 198 199 case DeadlineOrder.value: 200 queue = new BoundedDeadlineEventQueue(maxEventsPerConsumer, _overflowStrategy); 201 break; 202 203 default: 204 throw new IllegalArgumentException ("Orderpolicy: " + orderPolicy_ 205 + " OrderPolicyValue: " + shortOrderPolicy + " unknown"); 206 } 207 208 return new BasicMessageQueueAdapter(queue); 209 } 210 211 private static short orderPolicyNameToValue(String orderPolicyName) 212 { 213 if (mapOrderPolicyNameToValue.containsKey(orderPolicyName.toLowerCase())) 214 { 215 return ((Short ) mapOrderPolicyNameToValue.get(orderPolicyName)).shortValue(); 216 } 217 return UNKNOWN_POLICY; 218 } 219 220 private static short discardPolicyNameToValue(String discardPolicyName) 221 { 222 if (mapDiscardPolicyNameToValue.containsKey(discardPolicyName.toLowerCase())) 223 { 224 return ((Short ) mapDiscardPolicyNameToValue.get(discardPolicyName)).shortValue(); 225 } 226 return UNKNOWN_POLICY; 227 } 228 } | Popular Tags |