KickJava   Java API By Example, From Geeks To Geeks.

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


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

18 package org.apache.activemq.broker.region.virtual;
19
20 import java.util.Collection JavaDoc;
21 import java.util.Iterator JavaDoc;
22
23 import org.apache.activemq.broker.ProducerBrokerExchange;
24 import org.apache.activemq.broker.region.Destination;
25 import org.apache.activemq.broker.region.DestinationFilter;
26 import org.apache.activemq.command.ActiveMQDestination;
27 import org.apache.activemq.command.Message;
28 import org.apache.activemq.filter.MessageEvaluationContext;
29
30 /**
31  * Represents a composite {@link Destination} where send()s are replicated to
32  * each Destination instance.
33  *
34  * @version $Revision: 516444 $
35  */

36 public class CompositeDestinationInterceptor extends DestinationFilter {
37
38     private Collection JavaDoc forwardDestinations;
39     private boolean forwardOnly;
40     private boolean copyMessage;
41
42     public CompositeDestinationInterceptor(Destination next, Collection JavaDoc forwardDestinations, boolean forwardOnly, boolean copyMessage) {
43         super(next);
44         this.forwardDestinations = forwardDestinations;
45         this.forwardOnly = forwardOnly;
46         this.copyMessage = copyMessage;
47     }
48
49     public void send(ProducerBrokerExchange context, Message message) throws Exception JavaDoc {
50         MessageEvaluationContext messageContext = null;
51
52         for (Iterator JavaDoc iter = forwardDestinations.iterator(); iter.hasNext();) {
53             ActiveMQDestination destination = null;
54             Object JavaDoc value = iter.next();
55
56             if (value instanceof FilteredDestination) {
57                 FilteredDestination filteredDestination = (FilteredDestination) value;
58                 if (messageContext == null) {
59                     messageContext = new MessageEvaluationContext();
60                     messageContext.setMessageReference(message);
61                 }
62                 messageContext.setDestination(filteredDestination.getDestination());
63                 if (filteredDestination.matches(messageContext)) {
64                     destination = filteredDestination.getDestination();
65                 }
66             }
67             else if (value instanceof ActiveMQDestination) {
68                 destination = (ActiveMQDestination) value;
69             }
70             if (destination == null) {
71                 continue;
72             }
73
74             if (copyMessage) {
75                 message = message.copy();
76                 message.setDestination(destination);
77             }
78
79             send(context, message, destination);
80         }
81         if (!forwardOnly) {
82             super.send(context, message);
83         }
84     }
85 }
86
Popular Tags