001 /** 002 * 003 * Copyright 2004 Protique Ltd 004 * 005 * Licensed under the Apache License, Version 2.0 (the "License"); 006 * you may not use this file except in compliance with the License. 007 * You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 * 017 **/ 018 package org.activemq.transport.composite; 019 020 import java.net.URI; 021 import java.net.URISyntaxException; 022 import java.util.ArrayList; 023 import java.util.List; 024 import java.util.StringTokenizer; 025 026 import javax.jms.JMSException; 027 028 import org.activemq.io.WireFormat; 029 import org.activemq.transport.TransportChannel; 030 import org.activemq.transport.TransportChannelFactorySupport; 031 import org.activemq.util.JMSExceptionHelper; 032 033 /** 034 * A Composite implementation of a TransportChannelFactory 035 * 036 * @version $Revision: 1.1.1.1 $ 037 */ 038 public class CompositeTransportChannelFactory extends TransportChannelFactorySupport { 039 private static String separator = ","; 040 041 public TransportChannel create(WireFormat wireFormat, URI remoteLocation) throws JMSException { 042 try { 043 List uris = new ArrayList(); 044 String text = parseURIs(uris, remoteLocation); 045 uris = randomizeURIs(uris); 046 TransportChannel channel = new CompositeTransportChannel(wireFormat, uris); 047 048 return populateProperties(channel, text); 049 } 050 catch (URISyntaxException e) { 051 throw JMSExceptionHelper.newJMSException("Can't parse list of URIs for: " + remoteLocation + ". Reason: " + e, e); 052 } 053 } 054 055 public TransportChannel create(WireFormat wireFormat, URI remoteLocation, URI localLocation) throws JMSException { 056 return create(wireFormat, remoteLocation); 057 } 058 059 public boolean requiresEmbeddedBroker() { 060 return false; 061 } 062 063 public static String parseURIs(List uris, URI uri) throws URISyntaxException { 064 String answer = ""; 065 String text = uri.getSchemeSpecificPart(); 066 if (text.startsWith("(")) { 067 // lets find the end of the string 068 int count = 1; 069 int size = text.length(); 070 for (int i = 1; i < size; i++) { 071 char ch = text.charAt(i); 072 if (ch == '(') { 073 count++; 074 } 075 else if (ch == ')') { 076 if (--count == 0) { 077 // we're at the end 078 answer = text.substring(i + 1); 079 text = text.substring(1, i); 080 break; 081 } 082 } 083 } 084 } 085 // else URI is not a group 086 else { 087 answer = uri.getQuery(); 088 } 089 090 StringTokenizer iter = new StringTokenizer(text, separator); 091 while (iter.hasMoreTokens()) { 092 String child = stripLeadingSlashes(iter.nextToken().trim()); 093 uris.add(new URI(child)); 094 } 095 return answer; 096 } 097 098 protected static String stripLeadingSlashes(String text) { 099 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 }