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