1 18 package org.apache.geronimo.interop.rmi.iiop.client; 19 20 import java.util.ArrayList ; 21 import java.util.StringTokenizer ; 22 23 import org.apache.geronimo.interop.SystemException; 24 import org.apache.geronimo.interop.util.ListUtil; 25 26 27 public class HostList { 28 private static ArrayList EMPTY_LIST = new ArrayList (0); 29 30 private int _connectCount; 31 32 private boolean _firstConnectAll = true; 33 34 private int _cacheTimeout = 600; 36 private int _preferredIndex; 37 38 private int _alternateIndex; 39 40 private ArrayList _preferredServers = EMPTY_LIST; 41 42 private ArrayList _alternateServers = EMPTY_LIST; 43 44 public HostList(String hostList) { 45 try { 46 int cycle = -1; 47 for (StringTokenizer st = new StringTokenizer (hostList, ";"); st.hasMoreTokens();) { 48 String token = st.nextToken().trim(); 49 if (token.startsWith("cycle=")) { 50 String value = token.substring(6); 51 cycle = Integer.parseInt(value); 52 } else if (token.startsWith("cacheTimeout=")) { 53 String value = token.substring(6); 54 _cacheTimeout = Integer.parseInt(value); 55 } else if (token.startsWith("preferredServers=")) { 56 String value = token.substring(17); 57 _preferredServers = ListUtil.getCommaSeparatedList(value); 58 } else if (token.startsWith("alternateServers=")) { 59 String value = token.substring(17); 60 _alternateServers = ListUtil.getCommaSeparatedList(value); 61 } 62 } 64 if (cycle < 0) { 65 cycle = 0; } 67 if (_preferredServers.size() > 0) { 68 _preferredIndex = cycle % _preferredServers.size(); 69 } 70 if (_alternateServers.size() > 0) { 71 _alternateIndex = cycle % _alternateServers.size(); 72 } 73 } catch (Exception ex) { 74 throw new SystemException("hostList = " + hostList, ex); 75 } 76 } 77 78 public boolean connectAll() { 79 synchronized (this) { 80 return _connectCount >= 5; } 82 } 83 84 public void countConnect() { 85 synchronized (this) { 86 int n = _connectCount; 87 if (n < Integer.MAX_VALUE) { 88 _connectCount = ++n; 89 } 90 } 91 } 92 93 public int getCacheTimeout() { 94 return _cacheTimeout; 95 } 96 97 public int getPreferredIndex() { 98 synchronized (this) { 99 int nextIndex = _preferredIndex; 100 if (connectAll()) { 101 int n = _preferredServers.size(); 102 if (n > 0) { 103 _preferredIndex = (nextIndex + 1) % n; 104 } 105 } 106 return nextIndex; 107 } 108 } 109 110 public int getAlternateIndex() { 111 synchronized (this) { 112 int nextIndex = _alternateIndex; 113 if (connectAll()) { 114 int n = _alternateServers.size(); 115 if (n > 0) { 116 _alternateIndex = (nextIndex + 1) % n; 117 } 118 } 119 return nextIndex; 120 } 121 } 122 123 public ArrayList getPreferredServers() { 124 return _preferredServers; 125 } 126 127 public ArrayList getAlternateServers() { 128 return _alternateServers; 129 } 130 131 public String toString() { 132 return "HostList:cacheTimeout=" + _cacheTimeout 133 + ";preferredServers=" + ListUtil.formatCommaSeparatedList(_preferredServers) 134 + ";alternateServers=" + ListUtil.formatCommaSeparatedList(_alternateServers); 135 } 136 } 137 | Popular Tags |