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