KickJava   Java API By Example, From Geeks To Geeks.

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


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.BrokerId;
21 import org.apache.activemq.command.BrokerInfo;
22 import org.apache.activemq.command.Command;
23 import org.apache.activemq.command.ConsumerInfo;
24 import org.apache.activemq.command.Endpoint;
25 import org.apache.activemq.command.NetworkBridgeFilter;
26 import org.apache.activemq.transport.Transport;
27 import org.apache.activemq.util.ServiceSupport;
28
29 import java.io.IOException JavaDoc;
30
31 /**
32  * A demand forwarding bridge which works with multicast style transports where
33  * a single Transport could be communicating with multiple remote brokers
34  *
35  * @org.apache.xbean.XBean
36  *
37  * @version $Revision: 517753 $
38  */

39 public class CompositeDemandForwardingBridge extends DemandForwardingBridgeSupport {
40
41     protected final BrokerId remoteBrokerPath[] = new BrokerId[] { null };
42     protected Object JavaDoc brokerInfoMutex = new Object JavaDoc();
43
44     public CompositeDemandForwardingBridge(NetworkBridgeConfiguration configuration,Transport localBroker, Transport remoteBroker) {
45         super(configuration,localBroker, remoteBroker);
46         remoteBrokerName = remoteBroker.toString();
47         remoteBrokerNameKnownLatch.countDown();
48     }
49
50     protected void serviceRemoteBrokerInfo(Command command) throws IOException JavaDoc {
51         synchronized (brokerInfoMutex) {
52             BrokerInfo remoteBrokerInfo = (BrokerInfo) command;
53             BrokerId remoteBrokerId = remoteBrokerInfo.getBrokerId();
54             
55             // lets associate the incoming endpoint with a broker ID so we can refer to it later
56
Endpoint from = command.getFrom();
57             if (from == null) {
58                 log.warn("Incoming command does not have a from endpoint: " + command);
59             }
60             else {
61                 from.setBrokerInfo(remoteBrokerInfo);
62             }
63             if (localBrokerId != null) {
64                 if (localBrokerId.equals(remoteBrokerId)) {
65                     log.info("Disconnecting loop back connection.");
66                     // waitStarted();
67
ServiceSupport.dispose(this);
68                 }
69             }
70             if (!disposed) {
71                 triggerLocalStartBridge();
72             }
73         }
74     }
75
76     protected void addRemoteBrokerToBrokerPath(ConsumerInfo info) throws IOException JavaDoc {
77         info.setBrokerPath(appendToBrokerPath(info.getBrokerPath(), getFromBrokerId(info)));
78     }
79
80     /**
81      * Returns the broker ID that the command came from
82      */

83     protected BrokerId getFromBrokerId(Command command) throws IOException JavaDoc {
84         BrokerId answer = null;
85         Endpoint from = command.getFrom();
86         if (from == null) {
87             log.warn("Incoming command does not have a from endpoint: " + command);
88         }
89         else {
90             answer = from.getBrokerId();
91         }
92         if (answer != null) {
93             return answer;
94         }
95         else {
96             throw new IOException JavaDoc("No broker ID is available for endpoint: " + from + " from command: " + command);
97         }
98     }
99
100     protected void serviceLocalBrokerInfo(Command command) throws InterruptedException JavaDoc {
101         // TODO is there much we can do here?
102
}
103
104     protected NetworkBridgeFilter createNetworkBridgeFilter(ConsumerInfo info) throws IOException JavaDoc {
105         return new NetworkBridgeFilter(getFromBrokerId(info), configuration.getNetworkTTL());
106     }
107     
108     protected BrokerId[] getRemoteBrokerPath(){
109         return remoteBrokerPath;
110     }
111
112 }
113
Popular Tags