1 package com.ubermq.jms.client.unicast; 2 import com.ubermq.jms.client.*; 3 import com.ubermq.jms.client.impl.*; 4 import com.ubermq.kernel.*; 5 import com.ubermq.kernel.chooser.*; 6 import com.ubermq.kernel.overflow.*; 7 import java.net.*; 8 import java.util.*; 9 10 26 public class FailoverConnectionDescriptor 27 implements InternetConnectionDescriptor, 28 IOverflowHandler 29 { 30 private static final org.apache.log4j.Logger log = org.apache.log4j.Logger.getLogger(FailoverConnectionDescriptor.class); 31 32 private Collection connections; 33 private IOverflowHandler delegate; 34 private Chooser selector; 35 36 52 public FailoverConnectionDescriptor(Collection connections, 53 IOverflowHandler handler, 54 Chooser selector) 55 { 56 if (connections == null || 57 handler == null || 58 selector == null) 59 throw new IllegalArgumentException (); 60 61 this.connections = connections; 62 this.delegate = handler; 63 this.selector = selector; 64 } 65 66 87 public static final InternetConnectionDescriptor parseFailoverSpec(String spec, 88 int defaultPort) 89 { 90 List connections = new ArrayList(); 91 92 StringTokenizer st = new StringTokenizer(spec, ","); 93 while(st.hasMoreTokens()) 94 { 95 String url = st.nextToken(); 96 connections.add(new SimpleInternetConnectionDescriptor(url, defaultPort)); 97 } 98 99 log.debug("parsed failover URL as " + connections); 100 101 if (connections.size() == 1) 103 return new SimpleInternetConnectionDescriptor(spec, defaultPort); 104 else 105 return new FailoverConnectionDescriptor(connections, 106 new KeepTrying(connections.size() - 1, 107 0), 108 new RoundRobinChooser()); 109 } 110 111 121 public static final InternetConnectionDescriptor parseFailoverSpec(URI uri, 122 int defaultPort) 123 { 124 String hosts = uri.getSchemeSpecificPart(); 125 int slash = hosts.indexOf('/', 2); if (slash >= 0) 127 hosts = hosts.substring(0, slash); 128 129 return parseFailoverSpec(hosts, defaultPort); 130 } 131 132 public SocketAddress getAddress() 133 { 134 InternetConnectionDescriptor icd = ((InternetConnectionDescriptor)selector.choose(connections)); 136 log.debug("failover choosing " + icd); 137 138 return icd.getAddress(); 139 } 140 141 145 public int overflow() 146 { 147 return delegate.overflow(); 148 } 149 150 156 public IOverflowHandler getRetryHandler() 157 { 158 return new FailoverConnectionDescriptor(connections, 159 delegate.getRetryHandler(), 160 selector); 161 } 162 163 167 public int overflow(IDatagram d) 168 { 169 throw new UnsupportedOperationException (); 170 } 171 172 public String toString() 173 { 174 return "FailoverConnectionDescriptor for " + connections.toString(); 175 } 176 } 177 | Popular Tags |