Source code: org/activemq/transport/composite/CompositeTransportChannelFactory.java
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.activemq.transport.composite;
19
20 import java.net.URI;
21 import java.net.URISyntaxException;
22 import java.util.ArrayList;
23 import java.util.List;
24 import java.util.StringTokenizer;
25
26 import javax.jms.JMSException;
27
28 import org.activemq.io.WireFormat;
29 import org.activemq.transport.TransportChannel;
30 import org.activemq.transport.TransportChannelFactorySupport;
31 import org.activemq.util.JMSExceptionHelper;
32
33 /**
34 * A Composite implementation of a TransportChannelFactory
35 *
36 * @version $Revision: 1.1.1.1 $
37 */
38 public class CompositeTransportChannelFactory extends TransportChannelFactorySupport {
39 private static String separator = ",";
40
41 public TransportChannel create(WireFormat wireFormat, URI remoteLocation) throws JMSException {
42 try {
43 List uris = new ArrayList();
44 String text = parseURIs(uris, remoteLocation);
45 uris = randomizeURIs(uris);
46 TransportChannel channel = new CompositeTransportChannel(wireFormat, uris);
47
48 return populateProperties(channel, text);
49 }
50 catch (URISyntaxException e) {
51 throw JMSExceptionHelper.newJMSException("Can't parse list of URIs for: " + remoteLocation + ". Reason: " + e, e);
52 }
53 }
54
55 public TransportChannel create(WireFormat wireFormat, URI remoteLocation, URI localLocation) throws JMSException {
56 return create(wireFormat, remoteLocation);
57 }
58
59 public boolean requiresEmbeddedBroker() {
60 return false;
61 }
62
63 public static String parseURIs(List uris, URI uri) throws URISyntaxException {
64 String answer = "";
65 String text = uri.getSchemeSpecificPart();
66 if (text.startsWith("(")) {
67 // lets find the end of the string
68 int count = 1;
69 int size = text.length();
70 for (int i = 1; i < size; i++) {
71 char ch = text.charAt(i);
72 if (ch == '(') {
73 count++;
74 }
75 else if (ch == ')') {
76 if (--count == 0) {
77 // we're at the end
78 answer = text.substring(i + 1);
79 text = text.substring(1, i);
80 break;
81 }
82 }
83 }
84 }
85 // else URI is not a group
86 else {
87 answer = uri.getQuery();
88 }
89
90 StringTokenizer iter = new StringTokenizer(text, separator);
91 while (iter.hasMoreTokens()) {
92 String child = stripLeadingSlashes(iter.nextToken().trim());
93 uris.add(new URI(child));
94 }
95 return answer;
96 }
97
98 protected static String stripLeadingSlashes(String text) {
99 int idx = 0;
100 while (idx < text.length() && text.charAt(idx) == '/') {
101 idx++;
102 }
103 if (idx > 0) {
104 return text.substring(idx);
105 }
106 return text;
107 }
108
109
110 protected List randomizeURIs(List uris) {
111 return uris;
112 }
113 }