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.message.WireFormat;
24 import org.codehaus.activemq.transport.reliable.ReliableTransportChannel;
25 import org.codehaus.activemq.util.Callback;
26 import org.codehaus.activemq.util.ExceptionTemplate;
27 import org.codehaus.activemq.util.MapHelper;
28
29 import javax.jms.JMSException;
30 import java.net.URI;
31 import java.net.URISyntaxException;
32 import java.util.List;
33 import java.util.Map;
34
35 /***
36 * A {@link ReliableTransportChannel} which uses a {@link DiscoveryAgent} to discover remote broker
37 * instances and dynamically connect to them.
38 *
39 * @version $Revision: 1.3 $
40 */
41 public class DiscoveryTransportChannel extends ReliableTransportChannel implements DiscoveryListener {
42 private static final Log log = LogFactory.getLog(DiscoveryTransportChannel.class);
43
44
45 private DiscoveryAgent discoveryAgent;
46 private String remoteUserName;
47 private String remotePassword;
48
49
50 public DiscoveryTransportChannel(WireFormat wireFormat, DiscoveryAgent discoveryAgent) {
51 super(wireFormat);
52 this.discoveryAgent = discoveryAgent;
53 }
54
55 public void start() throws JMSException {
56 if (discoveryAgent == null) {
57 throw new ConfigurationException("Must be configured with a discoveryAgent property");
58 }
59
60
61 discoveryAgent.setDiscoveryListener(this);
62 discoveryAgent.start();
63
64 super.start();
65 }
66
67 public void stop() {
68 ExceptionTemplate template = new ExceptionTemplate();
69 template.run(new Callback() {
70 public void execute() throws Throwable {
71 discoveryAgent.stop();
72 }
73 });
74 template.run(new Callback() {
75 public void execute() throws Throwable {
76 DiscoveryTransportChannel.super.stop();
77 }
78 });
79 Throwable e = template.getFirstException();
80 log.warn("Failed to stop the transport channel cleanly due to: " + e, e);
81 }
82
83
84 public synchronized void addService(DiscoveryEvent event) {
85 Map details = event.getServiceDetails();
86 String url = MapHelper.getString(details, "connectURL");
87 if (url != null) {
88 try {
89 URI uri = new URI(url);
90 List urlList = getUris();
91 if (!urlList.contains(uri)) {
92 log.info("Adding new broker connection URL: " + uri + " with details: " + details);
93
94 urlList.add(uri);
95 }
96 }
97 catch (URISyntaxException e) {
98 log.warn("Could not connect to remote URI: " + url + " due to bad URI syntax: " + e, e);
99 }
100 }
101 }
102
103 public synchronized void removeService(DiscoveryEvent event) {
104 Map details = event.getServiceDetails();
105 String url = MapHelper.getString(details, "connectURL");
106 if (url != null) {
107 try {
108 URI uri = new URI(url);
109 synchronized (this) {
110 List urlList = getUris();
111 if (urlList.remove(uri)) {
112 log.info("Removing broker connection URL: " + uri);
113 }
114 }
115 }
116 catch (URISyntaxException e) {
117 log.warn("Could not remove remote URI: " + url + " due to bad URI syntax: " + e, e);
118 }
119 }
120 }
121
122
123
124 public DiscoveryAgent getDiscoveryAgent() {
125 return discoveryAgent;
126 }
127
128 public void setDiscoveryAgent(DiscoveryAgent discoveryAgent) {
129 this.discoveryAgent = discoveryAgent;
130 }
131
132
133 public String getRemotePassword() {
134 return remotePassword;
135 }
136
137 public void setRemotePassword(String remotePassword) {
138 this.remotePassword = remotePassword;
139 }
140
141 public String getRemoteUserName() {
142 return remoteUserName;
143 }
144
145 public void setRemoteUserName(String remoteUserName) {
146 this.remoteUserName = remoteUserName;
147 }
148
149 }