/**
*
* Copyright 2004 Protique Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
**/
package org.codehaus.activemq.transport;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.codehaus.activemq.ConfigurationException;
import org.codehaus.activemq.broker.BrokerContainer;
import org.codehaus.activemq.util.MapHelper;
import javax.jms.JMSException;
import java.util.HashMap;
import java.util.Map;
/**
* A {@link NetworkConnector} which uses discovery to find remote brokers
* to connect to
*
* @version $Revision: 1.7 $
*/
public class DiscoveryNetworkConnector extends NetworkConnector implements DiscoveryListener {
private static final Log log = LogFactory.getLog(DiscoveryNetworkConnector.class);
private DiscoveryAgent discoveryAgent;
private Map localDetails = new HashMap();
private Map channelMap = new HashMap();
private String remoteUserName;
private String remotePassword;
public DiscoveryNetworkConnector(BrokerContainer brokerContainer) {
super(brokerContainer);
}
public void start() throws JMSException {
DiscoveryAgent discoveryAgent = getBrokerContainer().getDiscoveryAgent();
if (discoveryAgent == null) {
throw new ConfigurationException("Must be configured with a discoveryAgent property");
}
discoveryAgent.setDiscoveryListener(this);
super.start();
}
public void addService(DiscoveryEvent event) {
Map details = event.getServiceDetails();
if (!getLocalBrokerName().equals(details.get("brokerName"))) {
String url = MapHelper.getString(details, "connectURL");
if (url != null) {
addChannel(url, details);
}
}
}
public void removeService(DiscoveryEvent event) {
Map details = event.getServiceDetails();
if (!getLocalBrokerName().equals(details.get("brokerName"))) {
String url = MapHelper.getString(details, "connectURL");
if (url != null) {
removeChannel(url, details);
}
}
}
// Properties
//-------------------------------------------------------------------------
public Map getLocalDetails() {
return localDetails;
}
public void setLocalDetails(Map localDetails) {
this.localDetails = localDetails;
}
public String getRemotePassword() {
return remotePassword;
}
public void setRemotePassword(String remotePassword) {
this.remotePassword = remotePassword;
}
public String getRemoteUserName() {
return remoteUserName;
}
public void setRemoteUserName(String remoteUserName) {
this.remoteUserName = remoteUserName;
}
// Implementation methods
//-------------------------------------------------------------------------
protected synchronized void addChannel(String url, Map details) {
NetworkChannel channel = (NetworkChannel) channelMap.get(url);
if (channel == null) {
channel = createNetworkChannel(url, details);
channel.setUri(url);
log.info(getLocalBrokerName() + ": Adding new NeworkChannel on: " + url + " with details: " + details);
try {
channel.start();
channelMap.put(url, channel);
}
catch (JMSException e) {
log.warn(getLocalBrokerName() + ": Could not start channel: " + channel + ". Reason: " + e, e);
}
}
}
protected synchronized void removeChannel(String url, Map details) {
NetworkChannel channel = (NetworkChannel) channelMap.remove(url);
if (channel != null) {
log.info(getLocalBrokerName() + ": Removing NeworkChannel: " + channel);
try {
channel.stop();
}
catch (JMSException e) {
log.info("Failed to stop channel: " + channel + ". Reason: " + e, e);
}
}
}
protected NetworkChannel createNetworkChannel(String url, Map details) {
NetworkChannel answer = new NetworkChannel(getBrokerContainer(), url);
answer.setRemoteUserName(getRemoteUserName());
answer.setRemotePassword(getRemotePassword());
return answer;
}
protected String getLocalBrokerName() {
return getBrokerContainer().getBroker().getBrokerName();
}
}