KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > broker > region > virtual > VirtualDestinationInterceptor


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.activemq.broker.region.virtual;
18
19 import java.util.ArrayList JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import java.util.List JavaDoc;
22 import java.util.Set JavaDoc;
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 /**
32  * Implements <a
33  * HREF="http://activemq.apache.org/virtual-destinations.html">Virtual
34  * Topics</a>.
35  *
36  * @org.apache.xbean.XBean
37  *
38  * @version $Revision: 516444 $
39  */

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 JavaDoc virtualDestinations = destinationMap.get(destination.getActiveMQDestination());
47         List JavaDoc destinations = new ArrayList JavaDoc();
48         for (Iterator JavaDoc 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                 // should rarely be used but here just in case
59
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 JavaDoc destinations) {
79         return new DestinationFilter(destination) {
80             public void send(ProducerBrokerExchange context, Message messageSend) throws Exception JavaDoc {
81                 for (Iterator JavaDoc 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