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.network;
018
019import java.io.IOException;
020import java.util.ArrayList;
021import java.util.List;
022
023import org.apache.activemq.command.ConsumerId;
024import org.apache.activemq.command.ConsumerInfo;
025import org.apache.activemq.command.SubscriptionInfo;
026import org.apache.activemq.filter.DestinationFilter;
027import org.apache.activemq.transport.Transport;
028import org.slf4j.Logger;
029import org.slf4j.LoggerFactory;
030
031/**
032 * Consolidates subscriptions
033 */
034public class ConduitBridge extends DemandForwardingBridge {
035    private static final Logger LOG = LoggerFactory.getLogger(ConduitBridge.class);
036
037    /**
038     * Constructor
039     *
040     * @param localBroker
041     * @param remoteBroker
042     */
043    public ConduitBridge(NetworkBridgeConfiguration configuration, Transport localBroker, Transport remoteBroker) {
044        super(configuration, localBroker, remoteBroker);
045    }
046
047    @Override
048    protected DemandSubscription createDemandSubscription(ConsumerInfo info) throws IOException {
049        if (addToAlreadyInterestedConsumers(info, false)) {
050            return null; // don't want this subscription added
051        }
052        //add our original id to ourselves
053        info.addNetworkConsumerId(info.getConsumerId());
054        info.setSelector(null);
055        return doCreateDemandSubscription(info);
056    }
057
058    protected boolean addToAlreadyInterestedConsumers(ConsumerInfo info, boolean isForcedDurable) {
059        // search through existing subscriptions and see if we have a match
060        if (info.isNetworkSubscription()) {
061            return false;
062        }
063        boolean matched = false;
064
065        for (DemandSubscription ds : subscriptionMapByLocalId.values()) {
066            DestinationFilter filter = DestinationFilter.parseFilter(ds.getLocalInfo().getDestination());
067            if (canConduit(ds) && filter.matches(info.getDestination())) {
068                LOG.debug("{} {} with ids {} matched (add interest) {}", new Object[]{
069                        configuration.getBrokerName(), info, info.getNetworkConsumerIds(), ds
070                });
071                // add the interest in the subscription
072                if (!info.isDurable()) {
073                    ds.add(info.getConsumerId());
074                    if (isForcedDurable) {
075                        forcedDurableRemoteId.add(info.getConsumerId());
076                        ds.addForcedDurableConsumer(info.getConsumerId());
077                    }
078                } else {
079                    ds.getDurableRemoteSubs().add(new SubscriptionInfo(info.getClientId(), info.getSubscriptionName()));
080                }
081                matched = true;
082                // continue - we want interest to any existing DemandSubscriptions
083            }
084        }
085        return matched;
086    }
087
088    // we want to conduit statically included consumers which are local networkSubs
089    // but we don't want to conduit remote network subs i.e. (proxy proxy) consumers
090    private boolean canConduit(DemandSubscription ds) {
091        return ds.isStaticallyIncluded() || !ds.getRemoteInfo().isNetworkSubscription();
092    }
093
094    @Override
095    protected void removeDemandSubscription(ConsumerId id) throws IOException {
096        List<DemandSubscription> tmpList = new ArrayList<DemandSubscription>();
097
098        for (DemandSubscription ds : subscriptionMapByLocalId.values()) {
099            if (ds.remove(id)) {
100                LOG.debug("{} on {} from {} removed interest for: {} from {}", new Object[]{
101                        configuration.getBrokerName(), localBroker, remoteBrokerName, id, ds
102                });
103            }
104            if (ds.isEmpty()) {
105                tmpList.add(ds);
106            }
107        }
108
109        for (DemandSubscription ds : tmpList) {
110            removeSubscription(ds);
111            LOG.debug("{} on {} from {} removed {}", new Object[]{
112                    configuration.getBrokerName(), localBroker, remoteBrokerName, ds
113            });
114        }
115    }
116}