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.plugin;
018
019import java.util.ArrayList;
020import java.util.StringTokenizer;
021import java.util.regex.Pattern;
022
023import org.apache.activemq.broker.Broker;
024import org.apache.activemq.broker.BrokerPlugin;
025import org.slf4j.Logger;
026import org.slf4j.LoggerFactory;
027
028/**
029 * @author Filip Hanik
030 * @org.apache.xbean.XBean element="discardingDLQBrokerPlugin"
031 * @version 1.0
032 */
033public class DiscardingDLQBrokerPlugin implements BrokerPlugin {
034    public DiscardingDLQBrokerPlugin() {
035    }
036
037    public static Logger log = LoggerFactory.getLogger(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        cb.setReportInterval(getReportInterval());
062        return cb;
063    }
064
065    public boolean isDropAll() {
066        return dropAll;
067    }
068
069    public boolean isDropTemporaryQueues() {
070        return dropTemporaryQueues;
071    }
072
073    public boolean isDropTemporaryTopics() {
074        return dropTemporaryTopics;
075    }
076
077    public String getDropOnly() {
078        return dropOnly;
079    }
080
081    public int getReportInterval() {
082        return reportInterval;
083    }
084
085    public void setDropTemporaryTopics(boolean dropTemporaryTopics) {
086        this.dropTemporaryTopics = dropTemporaryTopics;
087    }
088
089    public void setDropTemporaryQueues(boolean dropTemporaryQueues) {
090        this.dropTemporaryQueues = dropTemporaryQueues;
091    }
092
093    public void setDropAll(boolean dropAll) {
094        this.dropAll = dropAll;
095    }
096
097    public void setDropOnly(String dropOnly) {
098        this.dropOnly = dropOnly;
099    }
100
101    public void setReportInterval(int reportInterval) {
102        this.reportInterval = reportInterval;
103    }
104
105    public Pattern[] getDestFilter() {
106        if (getDropOnly()==null) return null;
107        ArrayList<Pattern> list = new ArrayList<Pattern>();
108        StringTokenizer t = new StringTokenizer(getDropOnly()," ");
109        while (t.hasMoreTokens()) {
110            String s = t.nextToken();
111            if (s!=null && s.trim().length()>0) list.add(Pattern.compile(s));
112        }
113        if (list.size()==0) return null;
114        return list.toArray(new Pattern[0]);
115    }
116}