KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > carol > cmi > Random


1 /*
2  * Copyright (C) 2002-2003, Simon Nieuviarts
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
17  * USA
18  */

19 package org.objectweb.carol.cmi;
20
21 import java.util.Collection JavaDoc;
22 import java.util.Iterator JavaDoc;
23
24 //TODO synchronization ?
25
public class Random extends StubLB {
26     private ClusterStubData csd;
27     private int len;
28     private StubData[] sd;
29
30     /**
31      * Builds a random algorithm on a Collection of StubData objects.
32      * @param c a Collection of StubData objects.
33      */

34     public Random(ClusterStubData csd, Collection JavaDoc c) {
35         this.csd = csd;
36         len = c.size();
37         sd = new StubData[len];
38         Iterator JavaDoc it = c.iterator();
39         for (int i = 0; i < len; i++) {
40             StubData s = (StubData) it.next();
41             sd[i] = s;
42         }
43     }
44
45     private synchronized void ensureCapacity(int minCapacity) {
46         int old = sd.length;
47         if (old >= minCapacity)
48             return;
49         int l = (old * 3) / 2 + 1;
50         if (l < minCapacity)
51             l = minCapacity;
52         StubData[] nsd = new StubData[l];
53         System.arraycopy(sd, 0, nsd, 0, old);
54         sd = nsd;
55     }
56
57     /**
58      * This method must be called only by the ClusterStubData to ensure integrity
59      * between this load balancer and the cluster stub.
60      * @see org.objectweb.carol.cmi.lb.StubLB#add(org.objectweb.carol.cmi.StubData)
61      */

62     void add(StubData sd) {
63         ensureCapacity(len + 1);
64         this.sd[len] = sd;
65         len++;
66     }
67
68     /**
69      * This method must be called only by the ClusterStubData to ensure integrity
70      * between this load balancer and the cluster stub.
71      * @see org.objectweb.carol.cmi.lb.StubLB#remove(org.objectweb.carol.cmi.StubData)
72      */

73     void removeCallback(StubData s) {
74         for (int i = 0; i < len; i++) {
75             if (sd[i] == s) {
76                 len--;
77                 sd[i] = sd[len];
78                 sd[len] = null;
79                 return;
80             }
81         }
82     }
83
84     public synchronized StubData get() throws NoMoreStubException {
85         if (len <= 0) {
86             throw new NoMoreStubException();
87         }
88         int choice = SecureRandom.getInt(len);
89         return sd[choice];
90     }
91
92     public synchronized StubData get(StubLBFilter f) throws NoMoreStubException {
93         int n = SecureRandom.getInt(len);
94         for (int i=0; i<len; i++) {
95             StubData s = sd[n];
96             if (!f.contains(s)) {
97                 return s;
98             }
99             n = (n + 1) % len;
100         }
101         throw new NoMoreStubException();
102     }
103
104     /**
105      * @see org.objectweb.carol.cmi.StubLB#remove(java.rmi.Remote)
106      */

107     public void remove(StubData s) {
108         csd.removeStubData(s);
109     }
110 }
111
Popular Tags