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