KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > network > ConduitBridge


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.network;
19
20 import org.apache.activemq.command.ConsumerId;
21 import org.apache.activemq.command.ConsumerInfo;
22 import org.apache.activemq.filter.DestinationFilter;
23 import org.apache.activemq.transport.Transport;
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26
27 import java.io.IOException JavaDoc;
28 import java.util.ArrayList JavaDoc;
29 import java.util.Iterator JavaDoc;
30 import java.util.List JavaDoc;
31
32
33 /**
34  * Consolidates subscriptions
35  *
36  * @version $Revision: 1.1 $
37  */

38 public class ConduitBridge extends DemandForwardingBridge{
39     static final private Log log=LogFactory.getLog(ConduitBridge.class);
40     /**
41      * Constructor
42      * @param localBroker
43      * @param remoteBroker
44      */

45     public ConduitBridge(NetworkBridgeConfiguration configuration,Transport localBroker,Transport remoteBroker){
46         super(configuration,localBroker,remoteBroker);
47     }
48     
49     protected DemandSubscription createDemandSubscription(ConsumerInfo info) throws IOException JavaDoc{
50         
51         if (addToAlreadyInterestedConsumers(info)){
52             return null; //don't want this subscription added
53
}
54         return doCreateDemandSubscription(info);
55     }
56     
57     protected boolean addToAlreadyInterestedConsumers(ConsumerInfo info){
58                 
59         if( info.getSelector()!=null )
60             return false;
61         
62         //search through existing subscriptions and see if we have a match
63
boolean matched = false;
64         DestinationFilter filter=DestinationFilter.parseFilter(info.getDestination());
65         for (Iterator JavaDoc i = subscriptionMapByLocalId.values().iterator(); i.hasNext();){
66             DemandSubscription ds = (DemandSubscription)i.next();
67             if (filter.matches(ds.getLocalInfo().getDestination())){
68                 //add the interest in the subscription
69
//ds.add(ds.getRemoteInfo().getConsumerId());
70
ds.add(info.getConsumerId());
71                 matched = true;
72                 //continue - we want interest to any existing DemandSubscriptions
73
}
74         }
75         return matched;
76     }
77     
78     protected void removeDemandSubscription(ConsumerId id) throws IOException JavaDoc{
79         List JavaDoc tmpList = new ArrayList JavaDoc();
80     
81         for (Iterator JavaDoc i = subscriptionMapByLocalId.values().iterator(); i.hasNext();){
82             DemandSubscription ds = (DemandSubscription)i.next();
83             ds.remove(id);
84             if (ds.isEmpty()){
85                 tmpList.add(ds);
86             }
87         }
88         for (Iterator JavaDoc i = tmpList.iterator(); i.hasNext();){
89             DemandSubscription ds = (DemandSubscription) i.next();
90             subscriptionMapByLocalId.remove(ds.getRemoteInfo().getConsumerId());
91             removeSubscription(ds);
92             if(log.isTraceEnabled())
93                 log.trace("removing sub on "+localBroker+" from "+remoteBrokerName+" : "+ds.getRemoteInfo());
94         }
95        
96     }
97
98 }
99
Popular Tags