/**
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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.apache.activemq.broker.region.policy;

import org.apache.activemq.broker.region.DurableTopicSubscription;
import org.apache.activemq.broker.region.Subscription;
import org.apache.activemq.command.ActiveMQDestination;
import org.apache.activemq.command.ActiveMQQueue;
import org.apache.activemq.command.ActiveMQTopic;
import org.apache.activemq.command.Message;

/**
 * A {@link DeadLetterStrategy} where each destination has its own individual
 * DLQ using the subject naming hierarchy.
 *
 * @org.apache.xbean.XBean
 *
 */
public class IndividualDeadLetterStrategy extends AbstractDeadLetterStrategy {

    private String topicPrefix = "ActiveMQ.DLQ.Topic.";
    private String queuePrefix = "ActiveMQ.DLQ.Queue.";
    private boolean useQueueForQueueMessages = true;
    private boolean useQueueForTopicMessages = true;
    private boolean destinationPerDurableSubscriber;

    public ActiveMQDestination getDeadLetterQueueFor(Message message, Subscription subscription) {
        if (message.getDestination().isQueue()) {
            return createDestination(message, queuePrefix, useQueueForQueueMessages, subscription);
        } else {
            return createDestination(message, topicPrefix, useQueueForTopicMessages, subscription);
        }
    }

    // Properties
    // -------------------------------------------------------------------------

    public String getQueuePrefix() {
        return queuePrefix;
    }

    /**
     * Sets the prefix to use for all dead letter queues for queue messages
     */
    public void setQueuePrefix(String queuePrefix) {
        this.queuePrefix = queuePrefix;
    }

    public String getTopicPrefix() {
        return topicPrefix;
    }

    /**
     * Sets the prefix to use for all dead letter queues for topic messages
     */
    public void setTopicPrefix(String topicPrefix) {
        this.topicPrefix = topicPrefix;
    }

    public boolean isUseQueueForQueueMessages() {
        return useQueueForQueueMessages;
    }

    /**
     * Sets whether a queue or topic should be used for queue messages sent to a
     * DLQ. The default is to use a Queue
     */
    public void setUseQueueForQueueMessages(boolean useQueueForQueueMessages) {
        this.useQueueForQueueMessages = useQueueForQueueMessages;
    }

    public boolean isUseQueueForTopicMessages() {
        return useQueueForTopicMessages;
    }

    /**
     * Sets whether a queue or topic should be used for topic messages sent to a
     * DLQ. The default is to use a Queue
     */
    public void setUseQueueForTopicMessages(boolean useQueueForTopicMessages) {
        this.useQueueForTopicMessages = useQueueForTopicMessages;
    }

    public boolean isDestinationPerDurableSubscriber() {
        return destinationPerDurableSubscriber;
    }

    /**
     * sets whether durable topic subscriptions are to get individual dead letter destinations.
     * When true, the DLQ is of the form 'topicPrefix.clientId:subscriptionName'
     * The default is false.
     * @param destinationPerDurableSubscriber
     */
    public void setDestinationPerDurableSubscriber(boolean destinationPerDurableSubscriber) {
        this.destinationPerDurableSubscriber = destinationPerDurableSubscriber;
    }

    // Implementation methods
    // -------------------------------------------------------------------------
    protected ActiveMQDestination createDestination(Message message,
                                                    String prefix,
                                                    boolean useQueue,
                                                    Subscription subscription ) {
        String name = null;

        if (message.getRegionDestination() != null
                && message.getRegionDestination().getActiveMQDestination() != null
                && message.getRegionDestination().getActiveMQDestination().getPhysicalName() != null
                && !message.getRegionDestination().getActiveMQDestination().getPhysicalName().isEmpty()){
            name = prefix + message.getRegionDestination().getActiveMQDestination().getPhysicalName();
        } else {
            name = prefix + message.getDestination().getPhysicalName();
        }

        if (destinationPerDurableSubscriber && subscription instanceof DurableTopicSubscription) {
            name += "." + ((DurableTopicSubscription)subscription).getSubscriptionKey();
        }
        if (useQueue) {
            return new ActiveMQQueue(name);
        } else {
            return new ActiveMQTopic(name);
        }
    }
}

@@ -1521,24 +1521,88 @@
 LQ.Queue.%22;%0A
+    private String topicSuffix;%0A    private String queueSuffix;%0A
     private 
@@ -1952,24 +1952,37 @@
 queuePrefix,
+ queueSuffix,
  useQueueFor
@@ -2082,24 +2082,37 @@
 topicPrefix,
+ topicSuffix,
  useQueueFor
@@ -2778,32 +2778,560 @@
 cPrefix;%0A    %7D%0A%0A
+    public String getQueueSuffix() %7B%0A        return queueSuffix;%0A    %7D%0A%0A    /**%0A     * Sets the suffix to use for all dead letter queues for queue messages%0A     */%0A    public void setQueueSuffix(String queueSuffix) %7B%0A        this.queueSuffix = queueSuffix;%0A    %7D%0A%0A    public String getTopicSuffix() %7B%0A        return topicSuffix;%0A    %7D%0A%0A    /**%0A     * Sets the suffix to use for all dead letter queues for topic messages%0A     */%0A    public void setTopicSuffix(String topicSuffix) %7B%0A        this.topicSuffix = topicSuffix;%0A    %7D%0A%0A
     public boole
@@ -4902,16 +4902,83 @@
 prefix,%0A
+                                                    String suffix,%0A
         
@@ -5888,24 +5888,115 @@
 ;%0A        %7D%0A
+%0A        if (suffix != null && !suffix.isEmpty()) %7B%0A            name += suffix;%0A        %7D%0A%0A
         if (
@@ -6129,9 +6129,8 @@
 %0A    %7D%0A%7D
-%0A

fix for: https://issues.apache.org/jira/browse/AMQ-4065