001/**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.activemq.broker.region;
018
019import java.io.IOException;
020import java.util.List;
021import java.util.Set;
022
023import org.apache.activemq.broker.Broker;
024import org.apache.activemq.broker.ConnectionContext;
025import org.apache.activemq.broker.ProducerBrokerExchange;
026import org.apache.activemq.broker.region.policy.DeadLetterStrategy;
027import org.apache.activemq.broker.region.policy.SlowConsumerStrategy;
028import org.apache.activemq.command.ActiveMQDestination;
029import org.apache.activemq.command.Message;
030import org.apache.activemq.command.MessageAck;
031import org.apache.activemq.command.MessageDispatchNotification;
032import org.apache.activemq.command.ProducerInfo;
033import org.apache.activemq.store.MessageStore;
034import org.apache.activemq.usage.MemoryUsage;
035import org.apache.activemq.usage.TempUsage;
036import org.apache.activemq.usage.Usage;
037import org.apache.activemq.util.SubscriptionKey;
038
039/**
040 *
041 *
042 */
043public class DestinationFilter implements Destination {
044
045    protected final Destination next;
046
047    public DestinationFilter(Destination next) {
048        this.next = next;
049    }
050
051    @Override
052    public void acknowledge(ConnectionContext context, Subscription sub, MessageAck ack, MessageReference node) throws IOException {
053        next.acknowledge(context, sub, ack, node);
054    }
055
056    @Override
057    public void addSubscription(ConnectionContext context, Subscription sub) throws Exception {
058        next.addSubscription(context, sub);
059    }
060
061    @Override
062    public Message[] browse() {
063        return next.browse();
064    }
065
066    @Override
067    public void dispose(ConnectionContext context) throws IOException {
068        next.dispose(context);
069    }
070
071    @Override
072    public boolean isDisposed() {
073        return next.isDisposed();
074    }
075
076    @Override
077    public void gc() {
078        next.gc();
079    }
080
081    @Override
082    public void markForGC(long timeStamp) {
083        next.markForGC(timeStamp);
084    }
085
086    @Override
087    public boolean canGC() {
088        return next.canGC();
089    }
090
091    @Override
092    public long getInactiveTimeoutBeforeGC() {
093        return next.getInactiveTimeoutBeforeGC();
094    }
095
096    @Override
097    public ActiveMQDestination getActiveMQDestination() {
098        return next.getActiveMQDestination();
099    }
100
101    @Override
102    public DeadLetterStrategy getDeadLetterStrategy() {
103        return next.getDeadLetterStrategy();
104    }
105
106    @Override
107    public DestinationStatistics getDestinationStatistics() {
108        return next.getDestinationStatistics();
109    }
110
111    @Override
112    public String getName() {
113        return next.getName();
114    }
115
116    @Override
117    public MemoryUsage getMemoryUsage() {
118        return next.getMemoryUsage();
119    }
120
121    @Override
122    public void setMemoryUsage(MemoryUsage memoryUsage) {
123        next.setMemoryUsage(memoryUsage);
124    }
125
126    @Override
127    public TempUsage getTempUsage() {
128        return next.getTempUsage();
129    }
130
131    @Override
132    public void removeSubscription(ConnectionContext context, Subscription sub, long lastDeliveredSequenceId) throws Exception {
133        next.removeSubscription(context, sub, lastDeliveredSequenceId);
134    }
135
136    @Override
137    public void send(ProducerBrokerExchange context, Message messageSend) throws Exception {
138        next.send(context, messageSend);
139    }
140
141    @Override
142    public void start() throws Exception {
143        next.start();
144    }
145
146    @Override
147    public void stop() throws Exception {
148        next.stop();
149    }
150
151    @Override
152    public List<Subscription> getConsumers() {
153        return next.getConsumers();
154    }
155
156    /**
157     * Sends a message to the given destination which may be a wildcard
158     *
159     * @param context broker context
160     * @param message message to send
161     * @param destination possibly wildcard destination to send the message to
162     * @throws Exception on error
163     */
164    protected void send(ProducerBrokerExchange context, Message message, ActiveMQDestination destination) throws Exception {
165        Broker broker = context.getConnectionContext().getBroker();
166        Set<Destination> destinations = broker.getDestinations(destination);
167
168        for (Destination dest : destinations) {
169            dest.send(context, message.copy());
170        }
171    }
172
173    @Override
174    public MessageStore getMessageStore() {
175        return next.getMessageStore();
176    }
177
178    @Override
179    public boolean isProducerFlowControl() {
180        return next.isProducerFlowControl();
181    }
182
183    @Override
184    public void setProducerFlowControl(boolean value) {
185        next.setProducerFlowControl(value);
186    }
187
188    @Override
189    public boolean isAlwaysRetroactive() {
190        return next.isAlwaysRetroactive();
191    }
192
193    @Override
194    public void setAlwaysRetroactive(boolean value) {
195        next.setAlwaysRetroactive(value);
196    }
197
198    @Override
199    public void setBlockedProducerWarningInterval(long blockedProducerWarningInterval) {
200        next.setBlockedProducerWarningInterval(blockedProducerWarningInterval);
201    }
202
203    @Override
204    public long getBlockedProducerWarningInterval() {
205        return next.getBlockedProducerWarningInterval();
206    }
207
208    @Override
209    public void addProducer(ConnectionContext context, ProducerInfo info) throws Exception {
210        next.addProducer(context, info);
211    }
212
213    @Override
214    public void removeProducer(ConnectionContext context, ProducerInfo info) throws Exception {
215        next.removeProducer(context, info);
216    }
217
218    @Override
219    public int getMaxAuditDepth() {
220        return next.getMaxAuditDepth();
221    }
222
223    @Override
224    public int getMaxProducersToAudit() {
225        return next.getMaxProducersToAudit();
226    }
227
228    @Override
229    public boolean isEnableAudit() {
230        return next.isEnableAudit();
231    }
232
233    @Override
234    public void setEnableAudit(boolean enableAudit) {
235        next.setEnableAudit(enableAudit);
236    }
237
238    @Override
239    public void setMaxAuditDepth(int maxAuditDepth) {
240        next.setMaxAuditDepth(maxAuditDepth);
241    }
242
243    @Override
244    public void setMaxProducersToAudit(int maxProducersToAudit) {
245        next.setMaxProducersToAudit(maxProducersToAudit);
246    }
247
248    @Override
249    public boolean isActive() {
250        return next.isActive();
251    }
252
253    @Override
254    public int getMaxPageSize() {
255        return next.getMaxPageSize();
256    }
257
258    @Override
259    public void setMaxPageSize(int maxPageSize) {
260        next.setMaxPageSize(maxPageSize);
261    }
262
263    @Override
264    public boolean isUseCache() {
265        return next.isUseCache();
266    }
267
268    @Override
269    public void setUseCache(boolean useCache) {
270        next.setUseCache(useCache);
271    }
272
273    @Override
274    public int getMinimumMessageSize() {
275        return next.getMinimumMessageSize();
276    }
277
278    @Override
279    public void setMinimumMessageSize(int minimumMessageSize) {
280        next.setMinimumMessageSize(minimumMessageSize);
281    }
282
283    @Override
284    public void wakeup() {
285        next.wakeup();
286    }
287
288    @Override
289    public boolean isLazyDispatch() {
290        return next.isLazyDispatch();
291    }
292
293    @Override
294    public void setLazyDispatch(boolean value) {
295        next.setLazyDispatch(value);
296    }
297
298    public void messageExpired(ConnectionContext context, PrefetchSubscription prefetchSubscription, MessageReference node) {
299        next.messageExpired(context, prefetchSubscription, node);
300    }
301
302    @Override
303    public boolean iterate() {
304        return next.iterate();
305    }
306
307    @Override
308    public void fastProducer(ConnectionContext context, ProducerInfo producerInfo) {
309        next.fastProducer(context, producerInfo);
310    }
311
312    @Override
313    public void isFull(ConnectionContext context, Usage<?> usage) {
314        next.isFull(context, usage);
315    }
316
317    @Override
318    public void messageConsumed(ConnectionContext context, MessageReference messageReference) {
319        next.messageConsumed(context, messageReference);
320    }
321
322    @Override
323    public void messageDelivered(ConnectionContext context, MessageReference messageReference) {
324        next.messageDelivered(context, messageReference);
325    }
326
327    @Override
328    public void messageDiscarded(ConnectionContext context, Subscription sub, MessageReference messageReference) {
329        next.messageDiscarded(context, sub, messageReference);
330    }
331
332    @Override
333    public void slowConsumer(ConnectionContext context, Subscription subs) {
334        next.slowConsumer(context, subs);
335    }
336
337    @Override
338    public void messageExpired(ConnectionContext context, Subscription subs, MessageReference node) {
339        next.messageExpired(context, subs, node);
340    }
341
342    @Override
343    public int getMaxBrowsePageSize() {
344        return next.getMaxBrowsePageSize();
345    }
346
347    @Override
348    public void setMaxBrowsePageSize(int maxPageSize) {
349        next.setMaxBrowsePageSize(maxPageSize);
350    }
351
352    @Override
353    public void processDispatchNotification(MessageDispatchNotification messageDispatchNotification) throws Exception {
354        next.processDispatchNotification(messageDispatchNotification);
355    }
356
357    @Override
358    public int getCursorMemoryHighWaterMark() {
359        return next.getCursorMemoryHighWaterMark();
360    }
361
362    @Override
363    public void setCursorMemoryHighWaterMark(int cursorMemoryHighWaterMark) {
364        next.setCursorMemoryHighWaterMark(cursorMemoryHighWaterMark);
365    }
366
367    @Override
368    public boolean isPrioritizedMessages() {
369        return next.isPrioritizedMessages();
370    }
371
372    @Override
373    public SlowConsumerStrategy getSlowConsumerStrategy() {
374        return next.getSlowConsumerStrategy();
375    }
376
377    @Override
378    public boolean isDoOptimzeMessageStorage() {
379        return next.isDoOptimzeMessageStorage();
380    }
381
382    @Override
383    public void setDoOptimzeMessageStorage(boolean doOptimzeMessageStorage) {
384        next.setDoOptimzeMessageStorage(doOptimzeMessageStorage);
385    }
386
387    @Override
388    public void clearPendingMessages() {
389        next.clearPendingMessages();
390    }
391
392    @Override
393    public void duplicateFromStore(Message message, Subscription subscription) {
394        next.duplicateFromStore(message, subscription);
395    }
396
397    public void deleteSubscription(ConnectionContext context, SubscriptionKey key) throws Exception {
398        if (next instanceof DestinationFilter) {
399            DestinationFilter filter = (DestinationFilter) next;
400            filter.deleteSubscription(context, key);
401        } else if (next instanceof Topic) {
402            Topic topic = (Topic)next;
403            topic.deleteSubscription(context, key);
404        }
405    }
406
407    public Destination getNext() {
408        return next;
409    }
410
411    public <T> T getAdaptor(Class <? extends T> clazz) {
412        if (clazz.isInstance(this)) {
413            return clazz.cast(this);
414        } else if (next != null && clazz.isInstance(next)) {
415            return clazz.cast(next);
416        } else if (next instanceof DestinationFilter) {
417            return ((DestinationFilter)next).getAdaptor(clazz);
418        }
419        return null;
420    }
421}