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     */
017    package org.apache.activemq.plugin;
018    
019    import java.util.ArrayList;
020    import java.util.StringTokenizer;
021    import java.util.regex.Pattern;
022    
023    import org.apache.activemq.broker.Broker;
024    import org.apache.activemq.broker.BrokerPlugin;
025    import org.apache.commons.logging.Log;
026    import org.apache.commons.logging.LogFactory;
027    
028    /**
029     * @author Filip Hanik
030     * @org.apache.xbean.XBean element="discardingDLQBrokerPlugin"
031     * @version 1.0
032     */
033    public class DiscardingDLQBrokerPlugin implements BrokerPlugin {
034        public DiscardingDLQBrokerPlugin() {
035        }
036    
037        public static Log log = LogFactory.getLog(DiscardingDLQBrokerPlugin.class);
038        private boolean dropTemporaryTopics = true;
039        private boolean dropTemporaryQueues = true;
040        private boolean dropAll = true;
041        private String dropOnly;
042        private int reportInterval = 1000;
043    
044        /**
045         * Installs the plugin into the interceptor chain of the broker, returning the new intercepted broker to use.
046         * @param broker Broker
047         * @throws Exception
048         * @return Broker
049         * @todo Implement this org.apache.activemq.broker.BrokerPlugin method
050         */
051        public Broker installPlugin(Broker broker) throws Exception {
052            log.info("Installing Discarding Dead Letter Queue broker plugin[dropAll="+isDropAll()+
053                     "; dropTemporaryTopics="+isDropTemporaryTopics()+"; dropTemporaryQueues="+
054                     isDropTemporaryQueues()+"; dropOnly="+getDropOnly()+"; reportInterval="+
055                     getReportInterval()+"]");
056            DiscardingDLQBroker cb = new DiscardingDLQBroker(broker);
057            cb.setDropAll(isDropAll());
058            cb.setDropTemporaryQueues(isDropTemporaryQueues());
059            cb.setDropTemporaryTopics(isDropTemporaryTopics());
060            cb.setDestFilter(getDestFilter());
061            return cb;
062        }
063    
064        public boolean isDropAll() {
065            return dropAll;
066        }
067    
068        public boolean isDropTemporaryQueues() {
069            return dropTemporaryQueues;
070        }
071    
072        public boolean isDropTemporaryTopics() {
073            return dropTemporaryTopics;
074        }
075    
076        public String getDropOnly() {
077            return dropOnly;
078        }
079    
080        public int getReportInterval() {
081            return reportInterval;
082        }
083    
084        public void setDropTemporaryTopics(boolean dropTemporaryTopics) {
085            this.dropTemporaryTopics = dropTemporaryTopics;
086        }
087    
088        public void setDropTemporaryQueues(boolean dropTemporaryQueues) {
089            this.dropTemporaryQueues = dropTemporaryQueues;
090        }
091    
092        public void setDropAll(boolean dropAll) {
093            this.dropAll = dropAll;
094        }
095    
096        public void setDropOnly(String dropOnly) {
097            this.dropOnly = dropOnly;
098        }
099    
100        public void setReportInterval(int reportInterval) {
101            this.reportInterval = reportInterval;
102        }
103    
104        public Pattern[] getDestFilter() {
105            if (getDropOnly()==null) return null;
106            ArrayList<Pattern> list = new ArrayList<Pattern>();
107            StringTokenizer t = new StringTokenizer(getDropOnly()," ");
108            while (t.hasMoreTokens()) {
109                String s = t.nextToken();
110                if (s!=null && s.trim().length()>0) list.add(Pattern.compile(s));
111            }
112            if (list.size()==0) return null;
113            return list.toArray(new Pattern[0]);
114        }
115    }