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.util;
018
019import java.util.HashSet;
020import java.util.List;
021import java.util.Map;
022import java.util.Set;
023
024import org.apache.activemq.advisory.AdvisoryBroker;
025import org.apache.activemq.advisory.AdvisorySupport;
026import org.apache.activemq.broker.BrokerService;
027import org.apache.activemq.broker.region.DurableTopicSubscription;
028import org.apache.activemq.broker.region.RegionBroker;
029import org.apache.activemq.broker.region.Subscription;
030import org.apache.activemq.broker.region.TopicRegion;
031import org.apache.activemq.command.ActiveMQDestination;
032import org.apache.activemq.command.BrokerSubscriptionInfo;
033import org.apache.activemq.command.ConsumerInfo;
034import org.apache.activemq.filter.DestinationFilter;
035import org.apache.activemq.network.NetworkBridgeConfiguration;
036import org.slf4j.Logger;
037import org.slf4j.LoggerFactory;
038
039public class NetworkBridgeUtils {
040
041    private static final Logger LOG = LoggerFactory.getLogger(NetworkBridgeUtils.class);
042
043    /**
044     * Generate the BrokerSubscriptionInfo which is used to tell the broker on the other
045     * side of the network bridge which NC durable subscriptions are still needed for demand.
046     * @param brokerService
047     * @param config
048     * @return
049     */
050    public static BrokerSubscriptionInfo getBrokerSubscriptionInfo(final BrokerService brokerService,
051            final NetworkBridgeConfiguration config) {
052
053        RegionBroker regionBroker = (RegionBroker) brokerService.getRegionBroker();
054        TopicRegion topicRegion = (TopicRegion) regionBroker.getTopicRegion();
055        Set<ConsumerInfo> subscriptionInfos = new HashSet<>();
056
057        //Add all durable subscriptions to the set that match the network config
058        //which currently is just the dynamicallyIncludedDestinations list
059        for (SubscriptionKey key : topicRegion.getDurableSubscriptions().keySet()) {
060            DurableTopicSubscription sub = topicRegion.getDurableSubscriptions().get(key);
061            if (sub != null && NetworkBridgeUtils.matchesNetworkConfig(config, sub.getConsumerInfo().getDestination())) {
062                ConsumerInfo ci = sub.getConsumerInfo().copy();
063                ci.setClientId(key.getClientId());
064                subscriptionInfos.add(ci);
065            }
066        }
067
068        //We also need to iterate over all normal subscriptions and check if they are part of
069        //any dynamicallyIncludedDestination that is configured with forceDurable to be true
070        //over the network bridge.  If forceDurable is true then we want to add the consumer to the set
071        for (Subscription sub : topicRegion.getSubscriptions().values()) {
072            if (sub != null && NetworkBridgeUtils.isForcedDurable(sub.getConsumerInfo(),
073                    config.getDynamicallyIncludedDestinations())) {
074                subscriptionInfos.add(sub.getConsumerInfo().copy());
075            }
076        }
077
078        try {
079            //Lastly, if isUseVirtualDestSubs is configured on this broker (to fire advisories) and
080            //configured on the network connector (to listen to advisories) then also add any virtual
081            //dest subscription to the set if forceDurable is true for its destination
082            AdvisoryBroker ab = (AdvisoryBroker) brokerService.getBroker().getAdaptor(AdvisoryBroker.class);
083            if (ab != null && brokerService.isUseVirtualDestSubs() && config.isUseVirtualDestSubs()) {
084                for (ConsumerInfo info : ab.getVirtualDestinationConsumers().keySet()) {
085                    if (NetworkBridgeUtils.isForcedDurable(info, config.getDynamicallyIncludedDestinations())) {
086                        subscriptionInfos.add(info.copy());
087                    }
088                }
089            }
090        } catch (Exception e) {
091            LOG.warn("Error processing virtualDestinationSubs for BrokerSubscriptionInfo");
092            LOG.debug("Error processing virtualDestinationSubs for BrokerSubscriptionInfo", e);
093        }
094        BrokerSubscriptionInfo bsi = new BrokerSubscriptionInfo(brokerService.getBrokerName());
095        bsi.setSubscriptionInfos(subscriptionInfos.toArray(new ConsumerInfo[0]));
096        return bsi;
097    }
098
099    public static boolean isForcedDurable(final ConsumerInfo info,
100            final List<ActiveMQDestination> dynamicallyIncludedDestinations) {
101        return dynamicallyIncludedDestinations != null
102                ? isForcedDurable(info,
103                        dynamicallyIncludedDestinations.toArray(new ActiveMQDestination[0]), null) : false;
104    }
105
106    public static boolean isForcedDurable(final ConsumerInfo info,
107            final ActiveMQDestination[] dynamicallyIncludedDestinations,
108            final ActiveMQDestination[] staticallyIncludedDestinations) {
109
110        if (info.isDurable() || info.getDestination().isQueue()) {
111            return false;
112        }
113
114        ActiveMQDestination destination = info.getDestination();
115        if (AdvisorySupport.isAdvisoryTopic(destination) || destination.isTemporary() ||
116                destination.isQueue()) {
117            return false;
118        }
119
120        ActiveMQDestination matching = findMatchingDestination(dynamicallyIncludedDestinations, destination);
121        if (matching != null) {
122            return isDestForcedDurable(matching);
123        }
124        matching = findMatchingDestination(staticallyIncludedDestinations, destination);
125        if (matching != null) {
126            return isDestForcedDurable(matching);
127        }
128        return false;
129    }
130
131    public static boolean matchesNetworkConfig(final NetworkBridgeConfiguration config,
132            ActiveMQDestination destination) {
133        List<ActiveMQDestination> includedDests = config.getDynamicallyIncludedDestinations();
134        if (includedDests != null && includedDests.size() > 0) {
135            for (ActiveMQDestination dest : includedDests) {
136                DestinationFilter inclusionFilter = DestinationFilter.parseFilter(dest);
137                if (dest != null && inclusionFilter.matches(destination) && dest.getDestinationType() == destination.getDestinationType()) {
138                    return true;
139                }
140            }
141        }
142
143        return false;
144    }
145
146    public static boolean matchesDestinations(ActiveMQDestination[] dests, final ActiveMQDestination destination) {
147        if (dests != null && dests.length > 0) {
148            for (ActiveMQDestination dest : dests) {
149                DestinationFilter inclusionFilter = DestinationFilter.parseFilter(dest);
150                if (dest != null && inclusionFilter.matches(destination) && dest.getDestinationType() == destination.getDestinationType()) {
151                    return true;
152                }
153            }
154        }
155
156        return false;
157    }
158
159    public static ActiveMQDestination findMatchingDestination(ActiveMQDestination[] dests, ActiveMQDestination destination) {
160        if (dests != null && dests.length > 0) {
161            for (ActiveMQDestination dest : dests) {
162                DestinationFilter inclusionFilter = DestinationFilter.parseFilter(dest);
163                if (dest != null && inclusionFilter.matches(destination) && dest.getDestinationType() == destination.getDestinationType()) {
164                    return dest;
165                }
166            }
167        }
168
169        return null;
170    }
171
172    public static boolean isDestForcedDurable(final ActiveMQDestination destination) {
173        boolean isForceDurable = false;
174        if (destination != null) {
175            final Map<String, String> options = destination.getOptions();
176
177            if (options != null) {
178                isForceDurable = (boolean) TypeConversionSupport.convert(options.get("forceDurable"), boolean.class);
179            }
180        }
181
182        return isForceDurable;
183    }
184}