AMQConnectionURL.java
001 /*
002  *
003  * Licensed to the Apache Software Foundation (ASF) under one
004  * or more contributor license agreements.  See the NOTICE file
005  * distributed with this work for additional information
006  * regarding copyright ownership.  The ASF licenses this file
007  * to you under the Apache License, Version 2.0 (the
008  * "License"); you may not use this file except in compliance
009  * with the License.  You may obtain a copy of the License at
010  *
011  *   http://www.apache.org/licenses/LICENSE-2.0
012  *
013  * Unless required by applicable law or agreed to in writing,
014  * software distributed under the License is distributed on an
015  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
016  * KIND, either express or implied.  See the License for the
017  * specific language governing permissions and limitations
018  * under the License.
019  *
020  */
021 package org.apache.qpid.client;
022 
023 import java.util.HashMap;
024 import java.util.LinkedList;
025 import java.util.List;
026 import java.util.Map;
027 
028 import org.apache.qpid.client.url.URLParser;
029 import org.apache.qpid.framing.AMQShortString;
030 import org.apache.qpid.framing.ProtocolVersion;
031 import org.apache.qpid.jms.BrokerDetails;
032 import org.apache.qpid.jms.ConnectionURL;
033 import org.apache.qpid.url.URLHelper;
034 import org.apache.qpid.url.URLSyntaxException;
035 import org.slf4j.Logger;
036 import org.slf4j.LoggerFactory;
037 
038 public class AMQConnectionURL implements ConnectionURL
039 {
040     private static final Logger _logger = LoggerFactory.getLogger(AMQConnectionURL.class);
041 
042     private String _url;
043     private String _failoverMethod;
044     private Map<String, String> _failoverOptions;
045     private Map<String, String> _options;
046     private List<BrokerDetails> _brokers;
047     private String _clientName;
048     private String _username;
049     private String _password;
050     private String _virtualHost;
051     private AMQShortString _defaultQueueExchangeName;
052     private AMQShortString _defaultTopicExchangeName;
053     private AMQShortString _temporaryTopicExchangeName;
054     private AMQShortString _temporaryQueueExchangeName;
055 
056     public AMQConnectionURL(String fullURLthrows URLSyntaxException
057     {
058         if (fullURL == nullthrow new IllegalArgumentException("URL cannot be null");
059         _url = fullURL;
060         _options = new HashMap<String, String>();
061         _brokers = new LinkedList<BrokerDetails>();
062         _failoverOptions = new HashMap<String, String>();
063         new URLParser(this);
064     }
065 
066     public String getURL()
067     {
068         return _url;
069     }
070 
071     public Map<String,String> getOptions()
072     {
073         return _options;
074     }
075 
076     public String getFailoverMethod()
077     {
078         return _failoverMethod;
079     }
080 
081     public void setFailoverMethod(String failoverMethod)
082     {
083         _failoverMethod = failoverMethod;
084     }
085 
086     public Map<String,String> getFailoverOptions()
087     {
088         return _failoverOptions;
089     }
090 
091     public String getFailoverOption(String key)
092     {
093         return _failoverOptions.get(key);
094     }
095 
096     public void setFailoverOption(String key, String value)
097     {
098         _failoverOptions.put(key, value);
099     }
100 
101     public int getBrokerCount()
102     {
103         return _brokers.size();
104     }
105 
106     public BrokerDetails getBrokerDetails(int index)
107     {
108         if (index < _brokers.size())
109         {
110             return _brokers.get(index);
111         }
112         else
113         {
114             return null;
115         }
116     }
117 
118     public void addBrokerDetails(BrokerDetails broker)
119     {
120         if (!(_brokers.contains(broker)))
121         {
122             _brokers.add(broker);
123         }
124     }
125 
126     public void setBrokerDetails(List<BrokerDetails> brokers)
127     {
128         _brokers = brokers;
129     }
130 
131     public List<BrokerDetails> getAllBrokerDetails()
132     {
133         return _brokers;
134     }
135 
136     public String getClientName()
137     {
138         return _clientName;
139     }
140 
141     public void setClientName(String clientName)
142     {
143         _clientName = clientName;
144     }
145 
146     public String getUsername()
147     {
148         return _username;
149     }
150 
151     public void setUsername(String username)
152     {
153         _username = username;
154     }
155 
156     public String getPassword()
157     {
158         return _password;
159     }
160 
161     public void setPassword(String password)
162     {
163         _password = password;
164     }
165 
166     public String getVirtualHost()
167     {
168         return _virtualHost;
169     }
170 
171     public void setVirtualHost(String virtuaHost)
172     {
173         _virtualHost = virtuaHost;
174     }
175 
176     public String getOption(String key)
177     {
178         return _options.get(key);
179     }
180 
181     public void setOption(String key, String value)
182     {
183         _options.put(key, value);
184     }
185 
186     public AMQShortString getDefaultQueueExchangeName()
187     {
188         return _defaultQueueExchangeName;
189     }
190 
191     public void setDefaultQueueExchangeName(AMQShortString defaultQueueExchangeName)
192     {
193         _defaultQueueExchangeName = defaultQueueExchangeName;
194     }
195 
196     public AMQShortString getDefaultTopicExchangeName()
197     {
198         return _defaultTopicExchangeName;
199     }
200 
201     public void setDefaultTopicExchangeName(AMQShortString defaultTopicExchangeName)
202     {
203         _defaultTopicExchangeName = defaultTopicExchangeName;
204     }
205 
206     public AMQShortString getTemporaryQueueExchangeName()
207     {
208         return _temporaryQueueExchangeName;
209     }
210 
211     public void setTemporaryQueueExchangeName(AMQShortString temporaryQueueExchangeName)
212     {
213         _temporaryQueueExchangeName = temporaryQueueExchangeName;
214     }
215 
216     public AMQShortString getTemporaryTopicExchangeName()
217     {
218         return _temporaryTopicExchangeName;
219     }
220 
221     public void setTemporaryTopicExchangeName(AMQShortString temporaryTopicExchangeName)
222     {
223         _temporaryTopicExchangeName = temporaryTopicExchangeName;
224     }
225 
226     public String toString()
227     {
228         StringBuffer sb = new StringBuffer();
229 
230         sb.append(AMQ_PROTOCOL);
231         sb.append("://");
232 
233         if (_username != null)
234         {
235             sb.append(_username);
236 
237             if (_password != null)
238             {
239                 sb.append(':');
240                 sb.append("********");
241             }
242 
243             sb.append('@');
244         }
245 
246         sb.append(_clientName);
247 
248         sb.append(_virtualHost);
249 
250         sb.append(optionsToString());
251 
252         return sb.toString();
253     }
254 
255     private String optionsToString()
256     {
257         StringBuffer sb = new StringBuffer();
258 
259         sb.append("?" + OPTIONS_BROKERLIST + "='");
260 
261         for (BrokerDetails service : _brokers)
262         {
263             sb.append(service.toString());
264             sb.append(';');
265         }
266 
267         sb.deleteCharAt(sb.length() 1);
268         sb.append("'");
269 
270         if (_failoverMethod != null)
271         {
272             sb.append(URLHelper.DEFAULT_OPTION_SEPERATOR);
273             sb.append(OPTIONS_FAILOVER + "='");
274             sb.append(_failoverMethod);
275             sb.append(URLHelper.printOptions(_failoverOptions));
276             sb.append("'");
277         }
278 
279         return sb.toString();
280     }
281 
282     public static void main(String[] argsthrows URLSyntaxException
283     {
284         String url2 =
285             "amqp://ritchiem:bob@temp/testHost?brokerlist='tcp://localhost:5672;tcp://fancyserver:3000/',failover='roundrobin'";
286         // "amqp://user:pass@clientid/virtualhost?brokerlist='tcp://host:1?option1=\'value\',option2=\'value\';vm://:3?option1=\'value\'',failover='method?option1=\'value\',option2='value''";
287 
288         ConnectionURL connectionurl2 = new AMQConnectionURL(url2);
289 
290         System.out.println(url2);
291         System.out.println(connectionurl2);
292 
293     }
294 }