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 19 package org.codehaus.activemq.transport.reliable; 20 import java.net.URI; 21 import java.net.URISyntaxException; 22 import javax.jms.JMSException; 23 import org.codehaus.activemq.message.WireFormat; 24 import org.codehaus.activemq.transport.TransportChannel; 25 import org.codehaus.activemq.transport.composite.CompositeTransportChannelFactory; 26 import org.codehaus.activemq.util.JMSExceptionHelper; 27 28 /*** 29 * A Reliable implementation of a TransportChannelFactory 30 * 31 * @version $Revision: 1.1 $ 32 */ 33 public class ReliableTransportChannelFactory extends CompositeTransportChannelFactory { 34 ; 35 36 /*** 37 * Create a TransportChannel 38 * 39 * @param wireFormat - the on-the-wire marshaller 40 * @param remoteLocation - location to bind to 41 * @return a reliable transport channel 42 * @throws JMSException if an error occurs 43 */ 44 public TransportChannel create(WireFormat wireFormat, URI remoteLocation) throws JMSException { 45 try { 46 return new ReliableTransportChannel(wireFormat, parseURIs(remoteLocation)); 47 } 48 catch (URISyntaxException e) { 49 throw JMSExceptionHelper.newJMSException("Can't parse list of URIs for: " + remoteLocation + ". Reason: " 50 + e, e); 51 } 52 } 53 54 /*** 55 * @param uris - the URIs to randomize 56 * @return randomized array 57 */ 58 protected URI[] randomizeURIs(URI[] uris) { 59 URI[] result = uris; 60 if (uris != null && uris.length > 1) { 61 result = new URI[uris.length]; 62 SMLCGRandom random = new SMLCGRandom(); 63 int startIndex = (int) (random.nextDouble() * (uris.length + 1)); 64 int count = 0; 65 for (int i = startIndex;i < uris.length;i++) { 66 result[count++] = uris[i]; 67 } 68 for (int i = 0;i < startIndex;i++) { 69 result[count++] = uris[i]; 70 } 71 } 72 return result; 73 } 74 }