1 17 package org.apache.activemq.broker.region.virtual; 18 19 import java.util.ArrayList ; 20 import java.util.Iterator ; 21 import java.util.List ; 22 import java.util.Set ; 23 24 import org.apache.activemq.broker.ProducerBrokerExchange; 25 import org.apache.activemq.broker.region.Destination; 26 import org.apache.activemq.broker.region.DestinationFilter; 27 import org.apache.activemq.broker.region.DestinationInterceptor; 28 import org.apache.activemq.command.Message; 29 import org.apache.activemq.filter.DestinationMap; 30 31 40 public class VirtualDestinationInterceptor implements DestinationInterceptor { 41 42 private DestinationMap destinationMap = new DestinationMap(); 43 private VirtualDestination[] virtualDestinations; 44 45 public Destination intercept(Destination destination) { 46 Set virtualDestinations = destinationMap.get(destination.getActiveMQDestination()); 47 List destinations = new ArrayList (); 48 for (Iterator iter = virtualDestinations.iterator(); iter.hasNext();) { 49 VirtualDestination virtualDestination = (VirtualDestination) iter.next(); 50 Destination newNestination = virtualDestination.intercept(destination); 51 destinations.add(newNestination); 52 } 53 if (!destinations.isEmpty()) { 54 if (destinations.size() == 1) { 55 return (Destination) destinations.get(0); 56 } 57 else { 58 return createCompositeDestination(destination, destinations); 60 } 61 } 62 return destination; 63 } 64 65 public VirtualDestination[] getVirtualDestinations() { 66 return virtualDestinations; 67 } 68 69 public void setVirtualDestinations(VirtualDestination[] virtualDestinations) { 70 destinationMap = new DestinationMap(); 71 this.virtualDestinations = virtualDestinations; 72 for (int i = 0; i < virtualDestinations.length; i++) { 73 VirtualDestination virtualDestination = virtualDestinations[i]; 74 destinationMap.put(virtualDestination.getVirtualDestination(), virtualDestination); 75 } 76 } 77 78 protected Destination createCompositeDestination(Destination destination, final List destinations) { 79 return new DestinationFilter(destination) { 80 public void send(ProducerBrokerExchange context, Message messageSend) throws Exception { 81 for (Iterator iter = destinations.iterator(); iter.hasNext();) { 82 Destination destination = (Destination) iter.next(); 83 destination.send(context, messageSend); 84 } 85 } 86 }; 87 } 88 89 } 90 | Popular Tags |