KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > interop > rmi > iiop > client > HostList


1 /**
2  *
3  * Copyright 2004-2005 The Apache Software Foundation
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  *
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package org.apache.geronimo.interop.rmi.iiop.client;
19
20 import java.util.ArrayList JavaDoc;
21 import java.util.StringTokenizer JavaDoc;
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 JavaDoc EMPTY_LIST = new ArrayList JavaDoc(0);
29
30     private int _connectCount;
31
32     private boolean _firstConnectAll = true;
33
34     private int _cacheTimeout = 600; // 600 seconds = 10 minutes
35

36     private int _preferredIndex;
37
38     private int _alternateIndex;
39
40     private ArrayList JavaDoc _preferredServers = EMPTY_LIST;
41
42     private ArrayList JavaDoc _alternateServers = EMPTY_LIST;
43
44     public HostList(String JavaDoc hostList) {
45         try {
46             int cycle = -1;
47             for (StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(hostList, ";"); st.hasMoreTokens();) {
48                 String JavaDoc token = st.nextToken().trim();
49                 if (token.startsWith("cycle=")) {
50                     String JavaDoc value = token.substring(6);
51                     cycle = Integer.parseInt(value);
52                 } else if (token.startsWith("cacheTimeout=")) {
53                     String JavaDoc value = token.substring(6);
54                     _cacheTimeout = Integer.parseInt(value);
55                 } else if (token.startsWith("preferredServers=")) {
56                     String JavaDoc value = token.substring(17);
57                     _preferredServers = ListUtil.getCommaSeparatedList(value);
58                 } else if (token.startsWith("alternateServers=")) {
59                     String JavaDoc value = token.substring(17);
60                     _alternateServers = ListUtil.getCommaSeparatedList(value);
61                 }
62                 // Otherwise ignore for forwards compabitility
63
}
64             if (cycle < 0) {
65                 cycle = 0; //FastRandom.getSharedInstance().nextInt(0, 999999999);
66
}
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 JavaDoc ex) {
74             throw new SystemException("hostList = " + hostList, ex);
75         }
76     }
77
78     public boolean connectAll() {
79         synchronized (this) {
80             return _connectCount >= 5; // TODO: make this configurable
81
}
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 JavaDoc getPreferredServers() {
124         return _preferredServers;
125     }
126
127     public ArrayList JavaDoc getAlternateServers() {
128         return _alternateServers;
129     }
130
131     public String JavaDoc toString() {
132         return "HostList:cacheTimeout=" + _cacheTimeout
133                + ";preferredServers=" + ListUtil.formatCommaSeparatedList(_preferredServers)
134                + ";alternateServers=" + ListUtil.formatCommaSeparatedList(_alternateServers);
135     }
136 }
137
Popular Tags