KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gcc > rmi > iiop > client > HostList


1 /*
2  * Copyright 2004 The Apache Software Foundation or its licensors, as
3  * applicable.
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
14  * implied.
15  *
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */

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; // 600 seconds = 10 minutes
34

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                 // Otherwise ignore for forwards compabitility
72
}
73             if (cycle < 0)
74             {
75                 cycle = 0; //FastRandom.getSharedInstance().nextInt(0, 999999999);
76
}
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; // TODO: make this configurable
97
}
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