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.region.virtual;
018
019import java.util.Set;
020
021import org.apache.activemq.broker.ConnectionContext;
022import org.apache.activemq.broker.region.BaseDestination;
023import org.apache.activemq.broker.region.Destination;
024import org.apache.activemq.broker.region.DestinationFilter;
025import org.apache.activemq.broker.region.IndirectMessageReference;
026import org.apache.activemq.broker.region.RegionBroker;
027import org.apache.activemq.broker.region.Subscription;
028import org.apache.activemq.broker.region.Topic;
029import org.apache.activemq.command.ActiveMQDestination;
030import org.apache.activemq.command.Message;
031import org.apache.activemq.util.SubscriptionKey;
032
033/**
034 * Creates a mapped Queue that can recover messages from subscription recovery
035 * policy of its Virtual Topic.
036 */
037public class MappedQueueFilter extends DestinationFilter {
038
039    private final ActiveMQDestination virtualDestination;
040
041    public MappedQueueFilter(ActiveMQDestination virtualDestination, Destination destination) {
042        super(destination);
043        this.virtualDestination = virtualDestination;
044    }
045
046    @Override
047    public synchronized void addSubscription(ConnectionContext context, Subscription sub) throws Exception {
048        // recover messages for first consumer only
049        boolean noSubs = getConsumers().isEmpty();
050
051        if (!sub.getActiveMQDestination().isPattern() || sub.getActiveMQDestination().equals(next.getActiveMQDestination())) {
052            super.addSubscription(context, sub);
053
054            if (noSubs && !getConsumers().isEmpty()) {
055                // new subscription added, recover retroactive messages
056                final RegionBroker regionBroker = (RegionBroker) context.getBroker().getAdaptor(RegionBroker.class);
057                final Set<Destination> virtualDests = regionBroker.getDestinations(virtualDestination);
058
059                final ActiveMQDestination newDestination = sub.getActiveMQDestination();
060                final BaseDestination regionDest = getBaseDestination((Destination) regionBroker.getDestinations(newDestination).toArray()[0]);
061
062                for (Destination virtualDest : virtualDests) {
063                    if (virtualDest.getActiveMQDestination().isTopic() &&
064                            (virtualDest.isAlwaysRetroactive() || sub.getConsumerInfo().isRetroactive())) {
065
066                        Topic topic = (Topic) getBaseDestination(virtualDest);
067                        if (topic != null) {
068                            // re-use browse() to get recovered messages
069                            final Message[] messages = topic.getSubscriptionRecoveryPolicy().browse(topic.getActiveMQDestination());
070
071                            // add recovered messages to subscription
072                            for (Message message : messages) {
073                                final Message copy = message.copy();
074                                copy.setOriginalDestination(message.getDestination());
075                                copy.setDestination(newDestination);
076                                copy.setRegionDestination(regionDest);
077                                sub.addRecoveredMessage(context, newDestination.isQueue() ? new IndirectMessageReference(copy) : copy);
078                            }
079                        }
080                    }
081                }
082            }
083        }
084    }
085
086    private BaseDestination getBaseDestination(Destination virtualDest) {
087        if (virtualDest instanceof BaseDestination) {
088            return (BaseDestination) virtualDest;
089        } else if (virtualDest instanceof DestinationFilter) {
090            return ((DestinationFilter) virtualDest).getAdaptor(BaseDestination.class);
091        }
092        return null;
093    }
094
095    @Override
096    public synchronized void removeSubscription(ConnectionContext context, Subscription sub, long lastDeliveredSequenceId) throws Exception {
097        super.removeSubscription(context, sub, lastDeliveredSequenceId);
098    }
099
100    @Override
101    public synchronized void deleteSubscription(ConnectionContext context, SubscriptionKey key) throws Exception {
102        super.deleteSubscription(context, key);
103    }
104
105    @Override
106    public String toString() {
107        return "MappedQueueFilter[" + virtualDestination + ", " + next + "]";
108    }
109}