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.inteceptor;
018
019import java.util.HashMap;
020import java.util.Map;
021
022import org.apache.activemq.broker.Broker;
023import org.apache.activemq.broker.BrokerRegistry;
024import org.apache.activemq.broker.BrokerService;
025import org.apache.activemq.broker.MutableBrokerFilter;
026import org.apache.activemq.broker.ProducerBrokerExchange;
027import org.apache.activemq.command.ActiveMQDestination;
028import org.apache.activemq.command.Message;
029import org.slf4j.Logger;
030import org.slf4j.LoggerFactory;
031
032public class MessageInterceptorRegistry {
033    private static final Logger LOG = LoggerFactory.getLogger(MessageInterceptorRegistry.class);
034    private static final MessageInterceptorRegistry INSTANCE = new MessageInterceptorRegistry();
035    private final BrokerService brokerService;
036    private MessageInterceptorFilter filter;
037    private final Map<BrokerService, MessageInterceptorRegistry> messageInterceptorRegistryMap = new HashMap<BrokerService, MessageInterceptorRegistry>();
038
039
040    public static MessageInterceptorRegistry getInstance() {
041        return INSTANCE;
042    }
043
044    public MessageInterceptorRegistry get(String brokerName){
045        BrokerService brokerService = BrokerRegistry.getInstance().lookup(brokerName);
046        return get(brokerService);
047    }
048
049    public synchronized MessageInterceptorRegistry get(BrokerService brokerService){
050        MessageInterceptorRegistry result = messageInterceptorRegistryMap.get(brokerService);
051        if (result == null){
052            result = new MessageInterceptorRegistry(brokerService);
053            messageInterceptorRegistryMap.put(brokerService,result);
054        }
055        return result;
056    }
057
058    private MessageInterceptorRegistry(){
059        this.brokerService=BrokerRegistry.getInstance().findFirst();
060        messageInterceptorRegistryMap.put(brokerService,this);
061    }
062
063    private MessageInterceptorRegistry(BrokerService brokerService) {
064        this.brokerService = brokerService;
065    }
066
067    public MessageInterceptor addMessageInterceptor(String destinationName, MessageInterceptor messageInterceptor) {
068        return getFilter().addMessageInterceptor(destinationName, messageInterceptor);
069    }
070
071    public void removeMessageInterceptor(String destinationName, MessageInterceptor messageInterceptor) {
072        getFilter().removeMessageInterceptor(destinationName, messageInterceptor);
073    }
074
075
076    public MessageInterceptor addMessageInterceptorForQueue(String destinationName, MessageInterceptor messageInterceptor) {
077        return getFilter().addMessageInterceptorForQueue(destinationName, messageInterceptor);
078    }
079
080    public void removeMessageInterceptorForQueue(String destinationName, MessageInterceptor messageInterceptor) {
081        getFilter().addMessageInterceptorForQueue(destinationName, messageInterceptor);
082    }
083
084
085    public MessageInterceptor addMessageInterceptorForTopic(String destinationName, MessageInterceptor messageInterceptor) {
086        return getFilter().addMessageInterceptorForTopic(destinationName, messageInterceptor);
087    }
088
089    public void removeMessageInterceptorForTopic(String destinationName, MessageInterceptor messageInterceptor) {
090        getFilter().removeMessageInterceptorForTopic(destinationName, messageInterceptor);
091    }
092
093    public MessageInterceptor addMessageInterceptor(ActiveMQDestination activeMQDestination, MessageInterceptor messageInterceptor) {
094        return getFilter().addMessageInterceptor(activeMQDestination, messageInterceptor);
095    }
096
097    public void removeMessageInterceptor(ActiveMQDestination activeMQDestination, MessageInterceptor interceptor) {
098        getFilter().removeMessageInterceptor(activeMQDestination, interceptor);
099    }
100
101    /**
102     * Re-inject into the Broker chain
103     */
104
105    public void injectMessage(ProducerBrokerExchange producerExchange, final Message messageSend) throws Exception {
106        getFilter().injectMessage(producerExchange, messageSend);
107    }
108
109
110    private synchronized MessageInterceptorFilter getFilter() {
111        if (filter == null) {
112            try {
113                MutableBrokerFilter mutableBrokerFilter = (MutableBrokerFilter) brokerService.getBroker().getAdaptor(MutableBrokerFilter.class);
114                Broker next = mutableBrokerFilter.getNext();
115                filter = new MessageInterceptorFilter(next);
116                mutableBrokerFilter.setNext(filter);
117            } catch (Exception e) {
118                LOG.error("Failed to create MessageInterceptorFilter", e);
119            }
120        }
121        return filter;
122    }
123}