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.ArrayList;
020import java.util.Iterator;
021import java.util.List;
022import java.util.Set;
023
024import org.apache.activemq.broker.Broker;
025import org.apache.activemq.broker.ConnectionContext;
026import org.apache.activemq.broker.ProducerBrokerExchange;
027import org.apache.activemq.broker.region.Destination;
028import org.apache.activemq.broker.region.DestinationFilter;
029import org.apache.activemq.broker.region.DestinationInterceptor;
030import org.apache.activemq.command.ActiveMQDestination;
031import org.apache.activemq.command.Message;
032import org.apache.activemq.filter.DestinationMap;
033
034/**
035 * Implements <a
036 * href="http://activemq.apache.org/virtual-destinations.html">Virtual Topics</a>.
037 * 
038 * @org.apache.xbean.XBean
039 * 
040 */
041public class VirtualDestinationInterceptor implements DestinationInterceptor {
042
043    private DestinationMap destinationMap = new DestinationMap();
044    private VirtualDestination[] virtualDestinations;
045
046    public Destination intercept(Destination destination) {
047        Set matchingDestinations = destinationMap.get(destination.getActiveMQDestination());
048        List<Destination> destinations = new ArrayList<Destination>();
049        for (Iterator iter = matchingDestinations.iterator(); iter.hasNext();) {
050            VirtualDestination virtualDestination = (VirtualDestination)iter.next();
051            Destination newDestination = virtualDestination.intercept(destination);
052            destinations.add(newDestination);
053        }
054        if (!destinations.isEmpty()) {
055            if (destinations.size() == 1) {
056                return destinations.get(0);
057            } else {
058                // should rarely be used but here just in case
059                return createCompositeDestination(destination, destinations);
060            }
061        }
062        return destination;
063    }
064    
065
066    public synchronized void create(Broker broker, ConnectionContext context, ActiveMQDestination destination) throws Exception {
067        for (VirtualDestination virt: virtualDestinations) {
068            virt.create(broker, context, destination);
069        }
070    }
071
072    public synchronized void remove(Destination destination) {     
073    }
074
075    public VirtualDestination[] getVirtualDestinations() {
076        return virtualDestinations;
077    }
078
079    public void setVirtualDestinations(VirtualDestination[] virtualDestinations) {
080        destinationMap = new DestinationMap();
081        this.virtualDestinations = virtualDestinations;
082        for (int i = 0; i < virtualDestinations.length; i++) {
083            VirtualDestination virtualDestination = virtualDestinations[i];
084            destinationMap.put(virtualDestination.getVirtualDestination(), virtualDestination);
085        }
086    }
087
088    protected Destination createCompositeDestination(Destination destination, final List<Destination> destinations) {
089        return new DestinationFilter(destination) {
090            public void send(ProducerBrokerExchange context, Message messageSend) throws Exception {
091                for (Iterator<Destination> iter = destinations.iterator(); iter.hasNext();) {
092                    Destination destination = iter.next();
093                    destination.send(context, messageSend);
094                }
095            }
096        };
097    }
098
099}