/** 
 * 
 * Copyright 2004 Protique Ltd
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 * See the License for the specific language governing permissions and 
 * limitations under the License. 
 * 
 **/

package org.activemq.service;
import javax.jms.JMSException;
import org.apache.commons.logging.*;
import org.activemq.broker.Broker;
import org.activemq.message.ActiveMQDestination;
import org.activemq.message.ActiveMQMessage;

/**
 * Determines how messages are stored in a dead letter queue
 * 
 * @version $Revision: 1.1.1.1 $
 */
public class DeadLetterPolicy {
    /**
     * Prefix used by dead letter queues
     */
    public static final String DEAD_LETTER_PREFIX = "org.activemq.deadletter.";
    private static final String DEFAULT_DEAD_LETTER_NAME = "DLQ";
    private static final Log log = LogFactory.getLog(DeadLetterPolicy.class);
    private Broker broker;
    private String deadLetterPrefix = DEAD_LETTER_PREFIX;
    private String deadLetterName = DEFAULT_DEAD_LETTER_NAME;
    private boolean deadLetterEnabled = true;
    private boolean deadLetterPerDestinationName = true;
    private boolean storeNonPersistentMessages = true;
    private boolean noTopicConsumerEnabled = true;

   
    /**
     * Construct a dead letter policy
     * 
     * @param broker
     */
    public DeadLetterPolicy(Broker broker) {
        this.broker = broker;
    }

    /**
     * Default constructor
     */
    public DeadLetterPolicy() {
    }

    /**
     * @return Returns the broker.
     */
    public Broker getBroker() {
        return broker;
    }

    /**
     * @param broker The broker to set.
     */
    public void setBroker(Broker broker) {
        this.broker = broker;
    }

    /**
     * @return Returns the deadLetterEnabled.
     */
    public boolean isDeadLetterEnabled() {
        return deadLetterEnabled;
    }

    /**
     * @param deadLetterEnabled The deadLetterEnabled to set.
     */
    public void setDeadLetterEnabled(boolean deadLetterEnabled) {
        this.deadLetterEnabled = deadLetterEnabled;
    }

    /**
     * @return Returns the deadLetterPerDestinationName.
     */
    public boolean isDeadLetterPerDestinationName() {
        return deadLetterPerDestinationName;
    }

    /**
     * @param deadLetterPerDestinationName The deadLetterPerDestinationName to set.
     */
    public void setDeadLetterPerDestinationName(boolean deadLetterPerDestinationName) {
        this.deadLetterPerDestinationName = deadLetterPerDestinationName;
    }

    /**
     * @return Returns the deadLetterName.
     */
    public String getDeadLetterName() {
        return deadLetterName;
    }

    /**
     * @param deadLetterName The deadLetterName to set.
     */
    public void setDeadLetterName(String deadLetterName) {
        this.deadLetterName = deadLetterName;
    }

    /**
     * @return Returns the deadLetterPrefix.
     */
    public String getDeadLetterPrefix() {
        return deadLetterPrefix;
    }

    /**
     * @param deadLetterPrefix The deadLetterPrefix to set.
     */
    public void setDeadLetterPrefix(String deadLetterPrefix) {
        this.deadLetterPrefix = deadLetterPrefix;
    }

    /**
     * @return Returns the storeNonPersistentMessages.
     */
    public boolean isStoreNonPersistentMessages() {
        return storeNonPersistentMessages;
    }

    /**
     * @param storeNonPersistentMessages The storeNonPersistentMessages to set.
     */
    public void setStoreNonPersistentMessages(boolean storeNonPersistentMessages) {
        this.storeNonPersistentMessages = storeNonPersistentMessages;
    }
    
    /**
     * @return Returns the noTopicConsumerEnabled.
     */
    public boolean isNoTopicConsumerEnabled() {
        return noTopicConsumerEnabled;
    }
    /**
     * @param noTopicConsumerEnabled The noTopicConsumerEnabled to set.
     */
    public void setNoTopicConsumerEnabled(boolean noTopicConsumerEnabled) {
        this.noTopicConsumerEnabled = noTopicConsumerEnabled;
    }
    
    /**
     * Get the name of the DLQ from the destination provided
     * @param destination
     * @return the name of the DLQ for this Destination
     */
    public String getDeadLetterNameFromDestination(ActiveMQDestination destination){
        String deadLetterName = deadLetterPrefix;
        if (deadLetterPerDestinationName) {
            deadLetterName += destination.getPhysicalName();
        }
        else {
            deadLetterName += deadLetterName;
        }
        return deadLetterName;
    }

    /**
     * Send a message to a dead letter queue
     * 
     * @param message
     * @throws JMSException
     */
    public void sendToDeadLetter(ActiveMQMessage message) throws JMSException {
        if (deadLetterEnabled && message != null && (message.isPersistent() || storeNonPersistentMessages)) {
            if (broker != null) {
                String dlqName = getDeadLetterNameFromDestination(message.getJMSActiveMQDestination());
                message.setDispatchedFromDLQ(true);
                broker.sendToDeadLetterQueue(dlqName, message);
                if (log.isDebugEnabled()) log.debug("Passed message: " + message + " to DLQ: " + dlqName);
            }
            else {
                log.warn("Broker is not initialized - cannot add to DLQ: " + message);
            }
        }else if (log.isDebugEnabled()){
            log.debug("DLQ not storing message: " + message);
        }
    }
}