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;
018
019import java.net.URI;
020import java.util.Map;
021import java.util.Set;
022import java.util.concurrent.ThreadPoolExecutor;
023import java.util.concurrent.atomic.AtomicReference;
024
025import org.apache.activemq.broker.region.Destination;
026import org.apache.activemq.broker.region.MessageReference;
027import org.apache.activemq.broker.region.Subscription;
028import org.apache.activemq.broker.region.virtual.VirtualDestination;
029import org.apache.activemq.command.ActiveMQDestination;
030import org.apache.activemq.command.BrokerId;
031import org.apache.activemq.command.BrokerInfo;
032import org.apache.activemq.command.ConnectionInfo;
033import org.apache.activemq.command.ConsumerControl;
034import org.apache.activemq.command.ConsumerInfo;
035import org.apache.activemq.command.DestinationInfo;
036import org.apache.activemq.command.Message;
037import org.apache.activemq.command.MessageAck;
038import org.apache.activemq.command.MessageDispatch;
039import org.apache.activemq.command.MessageDispatchNotification;
040import org.apache.activemq.command.MessagePull;
041import org.apache.activemq.command.ProducerInfo;
042import org.apache.activemq.command.RemoveSubscriptionInfo;
043import org.apache.activemq.command.Response;
044import org.apache.activemq.command.SessionInfo;
045import org.apache.activemq.command.TransactionId;
046import org.apache.activemq.store.PListStore;
047import org.apache.activemq.thread.Scheduler;
048import org.apache.activemq.usage.Usage;
049
050/**
051 * Like a BrokerFilter but it allows you to switch the getNext().broker. This
052 * has more overhead than a BrokerFilter since access to the getNext().broker
053 * has to synchronized since it is mutable
054 *
055 *
056 */
057public class MutableBrokerFilter implements Broker {
058
059    protected AtomicReference<Broker> next = new AtomicReference<Broker>();
060
061    public MutableBrokerFilter(Broker next) {
062        this.next.set(next);
063    }
064
065    @Override
066    public Broker getAdaptor(Class type) {
067        if (type.isInstance(this)) {
068            return this;
069        }
070        return next.get().getAdaptor(type);
071    }
072
073    public Broker getNext() {
074        return next.get();
075    }
076
077    public void setNext(Broker next) {
078        this.next.set(next);
079    }
080
081    @Override
082    public Map<ActiveMQDestination, Destination> getDestinationMap() {
083        return getNext().getDestinationMap();
084    }
085
086    @Override
087    public Map<ActiveMQDestination, Destination> getDestinationMap(ActiveMQDestination destination) {
088        return getNext().getDestinationMap(destination);
089    }
090
091    @Override
092    public Set getDestinations(ActiveMQDestination destination) {
093        return getNext().getDestinations(destination);
094    }
095
096    @Override
097    public void acknowledge(ConsumerBrokerExchange consumerExchange, MessageAck ack) throws Exception {
098        getNext().acknowledge(consumerExchange, ack);
099    }
100
101    @Override
102    public void addConnection(ConnectionContext context, ConnectionInfo info) throws Exception {
103        getNext().addConnection(context, info);
104    }
105
106    @Override
107    public Subscription addConsumer(ConnectionContext context, ConsumerInfo info) throws Exception {
108        return getNext().addConsumer(context, info);
109    }
110
111    @Override
112    public void addProducer(ConnectionContext context, ProducerInfo info) throws Exception {
113        getNext().addProducer(context, info);
114    }
115
116    @Override
117    public void commitTransaction(ConnectionContext context, TransactionId xid, boolean onePhase) throws Exception {
118        getNext().commitTransaction(context, xid, onePhase);
119    }
120
121    @Override
122    public void removeSubscription(ConnectionContext context, RemoveSubscriptionInfo info) throws Exception {
123        getNext().removeSubscription(context, info);
124    }
125
126    @Override
127    public TransactionId[] getPreparedTransactions(ConnectionContext context) throws Exception {
128        return getNext().getPreparedTransactions(context);
129    }
130
131    @Override
132    public int prepareTransaction(ConnectionContext context, TransactionId xid) throws Exception {
133        return getNext().prepareTransaction(context, xid);
134    }
135
136    @Override
137    public void removeConnection(ConnectionContext context, ConnectionInfo info, Throwable error) throws Exception {
138        getNext().removeConnection(context, info, error);
139    }
140
141    @Override
142    public void removeConsumer(ConnectionContext context, ConsumerInfo info) throws Exception {
143        getNext().removeConsumer(context, info);
144    }
145
146    @Override
147    public void removeProducer(ConnectionContext context, ProducerInfo info) throws Exception {
148        getNext().removeProducer(context, info);
149    }
150
151    @Override
152    public void rollbackTransaction(ConnectionContext context, TransactionId xid) throws Exception {
153        getNext().rollbackTransaction(context, xid);
154    }
155
156    @Override
157    public void send(ProducerBrokerExchange producerExchange, Message messageSend) throws Exception {
158        getNext().send(producerExchange, messageSend);
159    }
160
161    @Override
162    public void beginTransaction(ConnectionContext context, TransactionId xid) throws Exception {
163        getNext().beginTransaction(context, xid);
164    }
165
166    @Override
167    public void forgetTransaction(ConnectionContext context, TransactionId transactionId) throws Exception {
168        getNext().forgetTransaction(context, transactionId);
169    }
170
171    @Override
172    public Connection[] getClients() throws Exception {
173        return getNext().getClients();
174    }
175
176    @Override
177    public Destination addDestination(ConnectionContext context, ActiveMQDestination destination,boolean createIfTemporary) throws Exception {
178        return getNext().addDestination(context, destination,createIfTemporary);
179    }
180
181    @Override
182    public void removeDestination(ConnectionContext context, ActiveMQDestination destination, long timeout) throws Exception {
183        getNext().removeDestination(context, destination, timeout);
184    }
185
186    @Override
187    public ActiveMQDestination[] getDestinations() throws Exception {
188        return getNext().getDestinations();
189    }
190
191    @Override
192    public void start() throws Exception {
193        getNext().start();
194    }
195
196    @Override
197    public void stop() throws Exception {
198        getNext().stop();
199    }
200
201    @Override
202    public void addSession(ConnectionContext context, SessionInfo info) throws Exception {
203        getNext().addSession(context, info);
204    }
205
206    @Override
207    public void removeSession(ConnectionContext context, SessionInfo info) throws Exception {
208        getNext().removeSession(context, info);
209    }
210
211    @Override
212    public BrokerId getBrokerId() {
213        return getNext().getBrokerId();
214    }
215
216    @Override
217    public String getBrokerName() {
218        return getNext().getBrokerName();
219    }
220
221    @Override
222    public void gc() {
223        getNext().gc();
224    }
225
226    @Override
227    public void addBroker(Connection connection, BrokerInfo info) {
228        getNext().addBroker(connection, info);
229    }
230
231    @Override
232    public void removeBroker(Connection connection, BrokerInfo info) {
233        getNext().removeBroker(connection, info);
234    }
235
236    @Override
237    public BrokerInfo[] getPeerBrokerInfos() {
238        return getNext().getPeerBrokerInfos();
239    }
240
241    @Override
242    public void preProcessDispatch(MessageDispatch messageDispatch) {
243        getNext().preProcessDispatch(messageDispatch);
244    }
245
246    @Override
247    public void postProcessDispatch(MessageDispatch messageDispatch) {
248        getNext().postProcessDispatch(messageDispatch);
249    }
250
251    @Override
252    public void processDispatchNotification(MessageDispatchNotification messageDispatchNotification) throws Exception {
253        getNext().processDispatchNotification(messageDispatchNotification);
254    }
255
256    @Override
257    public boolean isStopped() {
258        return getNext().isStopped();
259    }
260
261    @Override
262    public Set<ActiveMQDestination> getDurableDestinations() {
263        return getNext().getDurableDestinations();
264    }
265
266    @Override
267    public void addDestinationInfo(ConnectionContext context, DestinationInfo info) throws Exception {
268        getNext().addDestinationInfo(context, info);
269
270    }
271
272    @Override
273    public void removeDestinationInfo(ConnectionContext context, DestinationInfo info) throws Exception {
274        getNext().removeDestinationInfo(context, info);
275
276    }
277
278    @Override
279    public boolean isFaultTolerantConfiguration() {
280        return getNext().isFaultTolerantConfiguration();
281    }
282
283    @Override
284    public ConnectionContext getAdminConnectionContext() {
285        return getNext().getAdminConnectionContext();
286    }
287
288    @Override
289    public void setAdminConnectionContext(ConnectionContext adminConnectionContext) {
290        getNext().setAdminConnectionContext(adminConnectionContext);
291    }
292
293    @Override
294    public Response messagePull(ConnectionContext context, MessagePull pull) throws Exception {
295        return getNext().messagePull(context, pull);
296    }
297
298    @Override
299    public PListStore getTempDataStore() {
300        return getNext().getTempDataStore();
301    }
302
303    @Override
304    public URI getVmConnectorURI() {
305        return getNext().getVmConnectorURI();
306    }
307
308    @Override
309    public void brokerServiceStarted() {
310        getNext().brokerServiceStarted();
311    }
312
313    @Override
314    public BrokerService getBrokerService() {
315        return getNext().getBrokerService();
316    }
317
318    @Override
319    public boolean isExpired(MessageReference messageReference) {
320        return getNext().isExpired(messageReference);
321    }
322
323    @Override
324    public void messageExpired(ConnectionContext context, MessageReference message, Subscription subscription) {
325        getNext().messageExpired(context, message, subscription);
326    }
327
328    @Override
329    public boolean sendToDeadLetterQueue(ConnectionContext context, MessageReference messageReference,
330                                         Subscription subscription, Throwable poisonCause) {
331        return getNext().sendToDeadLetterQueue(context, messageReference, subscription, poisonCause);
332    }
333
334    @Override
335    public Broker getRoot() {
336        return getNext().getRoot();
337    }
338
339    @Override
340    public long getBrokerSequenceId() {
341        return getNext().getBrokerSequenceId();
342    }
343
344    @Override
345    public void fastProducer(ConnectionContext context,ProducerInfo producerInfo,ActiveMQDestination destination) {
346        getNext().fastProducer(context, producerInfo, destination);
347    }
348
349    @Override
350    public void isFull(ConnectionContext context,Destination destination, Usage usage) {
351        getNext().isFull(context,destination, usage);
352    }
353
354    @Override
355    public void messageConsumed(ConnectionContext context,MessageReference messageReference) {
356        getNext().messageConsumed(context, messageReference);
357    }
358
359    @Override
360    public void messageDelivered(ConnectionContext context,MessageReference messageReference) {
361        getNext().messageDelivered(context, messageReference);
362    }
363
364    @Override
365    public void messageDiscarded(ConnectionContext context, Subscription sub, MessageReference messageReference) {
366        getNext().messageDiscarded(context, sub, messageReference);
367    }
368
369    @Override
370    public void slowConsumer(ConnectionContext context, Destination dest, Subscription subs) {
371        getNext().slowConsumer(context, dest,subs);
372    }
373
374    @Override
375    public void virtualDestinationAdded(ConnectionContext context,
376            VirtualDestination virtualDestination) {
377        getNext().virtualDestinationAdded(context, virtualDestination);
378    }
379
380    @Override
381    public void virtualDestinationRemoved(ConnectionContext context,
382            VirtualDestination virtualDestination) {
383        getNext().virtualDestinationRemoved(context, virtualDestination);
384    }
385
386    @Override
387    public void nowMasterBroker() {
388       getNext().nowMasterBroker();
389    }
390
391    @Override
392    public void processConsumerControl(ConsumerBrokerExchange consumerExchange,
393            ConsumerControl control) {
394        getNext().processConsumerControl(consumerExchange, control);
395    }
396
397    @Override
398    public void reapplyInterceptor() {
399        getNext().reapplyInterceptor();
400    }
401
402    @Override
403    public Scheduler getScheduler() {
404       return getNext().getScheduler();
405    }
406
407    @Override
408    public ThreadPoolExecutor getExecutor() {
409       return getNext().getExecutor();
410    }
411
412    @Override
413    public void networkBridgeStarted(BrokerInfo brokerInfo, boolean createdByDuplex, String remoteIp) {
414        getNext().networkBridgeStarted(brokerInfo, createdByDuplex, remoteIp);
415    }
416
417    @Override
418    public void networkBridgeStopped(BrokerInfo brokerInfo) {
419        getNext().networkBridgeStopped(brokerInfo);
420    }
421}