1
2
3
4
5
6
7
8
9
10
11 package org.mule.util.queue;
12
13 import org.mule.api.MuleContext;
14 import org.mule.api.store.ListableObjectStore;
15 import org.mule.api.store.ObjectStore;
16 import org.mule.api.store.ObjectStoreException;
17
18 import java.io.Serializable;
19 import java.util.HashMap;
20 import java.util.Map;
21
22
23
24
25 public class QueueInfo
26 {
27 private QueueConfiguration config;
28 private String name;
29 private QueueInfoDelegate delegate;
30 private MuleContext muleContext;
31 private boolean delegateCanTake;
32
33 private static Map<Class<? extends ObjectStore>, QueueInfoDelegateFactory> delegateFactories = new HashMap<Class<? extends ObjectStore>, QueueInfoDelegateFactory>();
34
35 public QueueInfo(String name, MuleContext muleContext, QueueConfiguration config)
36 {
37 this.name = name;
38 this.muleContext = muleContext;
39 setConfigAndDelegate(config);
40 }
41
42 public QueueInfo(QueueInfo other)
43 {
44 this(other.name, other.muleContext, other.config);
45 }
46
47 public void setConfig(QueueConfiguration config)
48 {
49 setConfigAndDelegate(config);
50 }
51
52 private void setConfigAndDelegate(QueueConfiguration config)
53 {
54 boolean hadConfig = this.config != null;
55 this.config = config;
56 int capacity = 0;
57 QueueInfoDelegateFactory factory = null;
58 if (config != null)
59 {
60 capacity = config.capacity;
61 factory = delegateFactories.get(config.objectStore.getClass());
62 }
63 if (delegate == null || (config != null && !hadConfig))
64 {
65 this.delegate = factory != null ? factory.createDelegate(this, muleContext) : new DefaultQueueInfoDelegate(capacity);
66 delegateCanTake = this.delegate instanceof TakingQueueInfoDelegate;
67 }
68 }
69
70 @Override
71 public boolean equals(Object obj)
72 {
73 return (obj instanceof QueueInfo && name.equals(((QueueInfo) obj).name));
74 }
75
76 public String getName()
77 {
78 return name;
79 }
80
81
82 @Override
83 public int hashCode()
84 {
85 return name.hashCode();
86 }
87
88 public void putNow(Serializable o)
89 {
90 delegate.putNow(o);
91 }
92
93 public boolean offer(Serializable o, int room, long timeout)
94 throws InterruptedException, ObjectStoreException
95 {
96 return delegate.offer(o, room, timeout);
97 }
98
99 public Serializable poll(long timeout)
100 throws InterruptedException
101 {
102 return delegate.poll(timeout);
103 }
104
105 public Serializable peek()
106 throws InterruptedException
107 {
108 return delegate.peek();
109 }
110
111 public void untake(Serializable item)
112 throws InterruptedException, ObjectStoreException
113 {
114 delegate.untake(item);
115 }
116
117 public int getSize()
118 {
119 return delegate.getSize();
120 }
121
122 public ListableObjectStore<Serializable> getStore()
123 {
124 return config == null ? null : config.objectStore;
125 }
126
127 public static synchronized void registerDelegateFactory(Class<? extends ObjectStore>storeType, QueueInfoDelegateFactory factory)
128 {
129 delegateFactories.put(storeType, factory);
130 }
131
132 public int getCapacity()
133 {
134 return config == null ? null : config.capacity;
135 }
136
137 public boolean canTakeFromStore()
138 {
139 return delegateCanTake;
140 }
141
142 public Serializable takeNextItemFromStore(long timeout) throws InterruptedException
143 {
144 if (canTakeFromStore())
145 {
146 return ((TakingQueueInfoDelegate)delegate).takeFromObjectStore(timeout);
147 }
148
149 throw new UnsupportedOperationException("Method 'takeNextItemFromStore' is not supported for queue " + name);
150 }
151
152 public void writeToObjectStore(Serializable data) throws InterruptedException, ObjectStoreException
153 {
154 if (canTakeFromStore())
155 {
156 ((TakingQueueInfoDelegate)delegate).writeToObjectStore(data);
157 return;
158 }
159
160 throw new UnsupportedOperationException("Method 'writeToObjectStore' is not supported for queue " + name);
161 }
162
163
164
165 public static interface QueueInfoDelegateFactory
166 {
167
168
169
170 QueueInfoDelegate createDelegate(QueueInfo parent, MuleContext muleContext);
171 }
172 }