1 /***
2 *
3 * Copyright 2004 Protique Ltd
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * 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 **/
18 package org.codehaus.activemq.transport;
19
20 import org.apache.commons.logging.Log;
21 import org.apache.commons.logging.LogFactory;
22 import org.codehaus.activemq.ConfigurationException;
23 import org.codehaus.activemq.broker.BrokerContainer;
24 import org.codehaus.activemq.util.MapHelper;
25
26 import javax.jms.JMSException;
27 import java.util.HashMap;
28 import java.util.Map;
29
30 /***
31 * A {@link NetworkConnector} which uses discovery to find remote brokers
32 * to connect to
33 *
34 * @version $Revision: 1.7 $
35 */
36 public class DiscoveryNetworkConnector extends NetworkConnector implements DiscoveryListener {
37 private static final Log log = LogFactory.getLog(DiscoveryNetworkConnector.class);
38
39 private DiscoveryAgent discoveryAgent;
40 private Map localDetails = new HashMap();
41 private Map channelMap = new HashMap();
42 private String remoteUserName;
43 private String remotePassword;
44
45 public DiscoveryNetworkConnector(BrokerContainer brokerContainer) {
46 super(brokerContainer);
47 }
48
49 public void start() throws JMSException {
50 DiscoveryAgent discoveryAgent = getBrokerContainer().getDiscoveryAgent();
51 if (discoveryAgent == null) {
52 throw new ConfigurationException("Must be configured with a discoveryAgent property");
53 }
54
55 discoveryAgent.setDiscoveryListener(this);
56
57 super.start();
58 }
59
60 public void addService(DiscoveryEvent event) {
61 Map details = event.getServiceDetails();
62 if (!getLocalBrokerName().equals(details.get("brokerName"))) {
63 String url = MapHelper.getString(details, "connectURL");
64 if (url != null) {
65 addChannel(url, details);
66 }
67 }
68 }
69
70 public void removeService(DiscoveryEvent event) {
71 Map details = event.getServiceDetails();
72 if (!getLocalBrokerName().equals(details.get("brokerName"))) {
73 String url = MapHelper.getString(details, "connectURL");
74 if (url != null) {
75 removeChannel(url, details);
76 }
77 }
78 }
79
80
81
82 public Map getLocalDetails() {
83 return localDetails;
84 }
85
86 public void setLocalDetails(Map localDetails) {
87 this.localDetails = localDetails;
88 }
89
90 public String getRemotePassword() {
91 return remotePassword;
92 }
93
94 public void setRemotePassword(String remotePassword) {
95 this.remotePassword = remotePassword;
96 }
97
98 public String getRemoteUserName() {
99 return remoteUserName;
100 }
101
102 public void setRemoteUserName(String remoteUserName) {
103 this.remoteUserName = remoteUserName;
104 }
105
106
107
108 protected synchronized void addChannel(String url, Map details) {
109 NetworkChannel channel = (NetworkChannel) channelMap.get(url);
110 if (channel == null) {
111 channel = createNetworkChannel(url, details);
112 channel.setUri(url);
113
114 log.info(getLocalBrokerName() + ": Adding new NeworkChannel on: " + url + " with details: " + details);
115
116 try {
117 channel.start();
118 channelMap.put(url, channel);
119 }
120 catch (JMSException e) {
121 log.warn(getLocalBrokerName() + ": Could not start channel: " + channel + ". Reason: " + e, e);
122 }
123 }
124 }
125
126 protected synchronized void removeChannel(String url, Map details) {
127 NetworkChannel channel = (NetworkChannel) channelMap.remove(url);
128 if (channel != null) {
129 log.info(getLocalBrokerName() + ": Removing NeworkChannel: " + channel);
130 try {
131 channel.stop();
132 }
133 catch (JMSException e) {
134 log.info("Failed to stop channel: " + channel + ". Reason: " + e, e);
135 }
136 }
137 }
138
139 protected NetworkChannel createNetworkChannel(String url, Map details) {
140 NetworkChannel answer = new NetworkChannel(getBrokerContainer(), url);
141 answer.setRemoteUserName(getRemoteUserName());
142 answer.setRemotePassword(getRemotePassword());
143 return answer;
144 }
145
146
147 protected String getLocalBrokerName() {
148 return getBrokerContainer().getBroker().getBrokerName();
149 }
150 }