KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jacorb > notification > queue > EventQueueFactory


1 package org.jacorb.notification.queue;
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.Collections JavaDoc;
25 import java.util.HashMap JavaDoc;
26 import java.util.Map JavaDoc;
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 /**
44  * @author Alphonse Bendt
45  * @version $Id: EventQueueFactory.java,v 1.11 2005/02/14 00:11:10 alphonse.bendt Exp $
46  */

47
48 public class EventQueueFactory implements Configurable
49 {
50     private static final short UNKNOWN_POLICY = Short.MIN_VALUE;
51
52     private static final Map JavaDoc mapOrderPolicyNameToValue;
53
54     private static final Map JavaDoc mapDiscardPolicyNameToValue;
55
56     private String JavaDoc orderPolicy_;
57
58     private String JavaDoc discardPolicy_;
59
60     static
61     {
62         Map JavaDoc orderMap = new HashMap JavaDoc();
63         
64         orderMap.put("anyorder", new Short JavaDoc(AnyOrder.value));
65         orderMap.put("fifoorder", new Short JavaDoc(FifoOrder.value));
66         orderMap.put("priorityorder", new Short JavaDoc(PriorityOrder.value));
67         orderMap.put("deadlineorder", new Short JavaDoc(DeadlineOrder.value));
68
69         mapOrderPolicyNameToValue = Collections.unmodifiableMap(orderMap);
70         
71         
72         Map JavaDoc discardMap = new HashMap JavaDoc();
73         
74         discardMap.put("anyorder", new Short JavaDoc(AnyOrder.value));
75         discardMap.put("fifoorder", new Short JavaDoc(FifoOrder.value));
76         discardMap.put("lifoorder", new Short JavaDoc(LifoOrder.value));
77         discardMap.put("priorityorder", new Short JavaDoc(PriorityOrder.value));
78         discardMap.put("deadlineorder", new Short JavaDoc(DeadlineOrder.value));
79         
80         mapDiscardPolicyNameToValue = Collections.unmodifiableMap(discardMap);
81     }
82
83     ////////////////////////////////////////
84

85     public EventQueueFactory(Configuration config) throws ConfigurationException
86     {
87         configure(config);
88     }
89
90     ////////////////////////////////////////
91

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 JavaDoc e)
101         {
102             throw new ConfigurationException("Invalid Policy", e);
103         }
104     }
105
106     private void setDiscardPolicy(String JavaDoc s)
107     {
108         String JavaDoc policy = s.toLowerCase().trim();
109
110         if (mapDiscardPolicyNameToValue.containsKey(policy))
111         {
112             discardPolicy_ = policy;
113         }
114         else
115         {
116             throw new IllegalArgumentException JavaDoc("Invalid DiscardPolicy: " + s);
117         }
118     }
119
120     private void setOrderPolicy(String JavaDoc s)
121     {
122         String JavaDoc policy = s.toLowerCase().trim();
123
124         if (mapOrderPolicyNameToValue.containsKey(policy))
125         {
126             orderPolicy_ = policy;
127         }
128         else
129         {
130             throw new IllegalArgumentException JavaDoc("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 JavaDoc 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         // fallthrough. will default to FifoOrder
164

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 JavaDoc("Discardpolicy: " + discardPolicy_
183                     + "DiscardPolicyValue: " + shortDiscardPolicy + " unknown");
184         }
185
186         final AbstractBoundedEventQueue queue;
187         switch (shortOrderPolicy) {
188         case AnyOrder.value:
189         // fallthrough. will default to FifoOrder
190

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 JavaDoc("Orderpolicy: " + orderPolicy_
205                     + " OrderPolicyValue: " + shortOrderPolicy + " unknown");
206         }
207
208         return new BasicMessageQueueAdapter(queue);
209     }
210
211     private static short orderPolicyNameToValue(String JavaDoc orderPolicyName)
212     {
213         if (mapOrderPolicyNameToValue.containsKey(orderPolicyName.toLowerCase()))
214         {
215             return ((Short JavaDoc) mapOrderPolicyNameToValue.get(orderPolicyName)).shortValue();
216         }
217         return UNKNOWN_POLICY;
218     }
219
220     private static short discardPolicyNameToValue(String JavaDoc discardPolicyName)
221     {
222         if (mapDiscardPolicyNameToValue.containsKey(discardPolicyName.toLowerCase()))
223         {
224             return ((Short JavaDoc) mapDiscardPolicyNameToValue.get(discardPolicyName)).shortValue();
225         }
226         return UNKNOWN_POLICY;
227     }
228 }
Popular Tags