KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > hajdbc > balancer > RandomBalancer


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

21 package net.sf.hajdbc.balancer;
22
23 import java.util.ArrayList JavaDoc;
24 import java.util.Collection JavaDoc;
25 import java.util.HashSet JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.Random JavaDoc;
28 import java.util.Set JavaDoc;
29
30 import net.sf.hajdbc.Database;
31
32 /**
33  * @author Paul Ferraro
34  * @since 1.0
35  */

36 public class RandomBalancer extends AbstractBalancer
37 {
38     private Random JavaDoc random = new Random JavaDoc();
39     private Set JavaDoc<Database> databaseSet = new HashSet JavaDoc<Database>();
40     private List JavaDoc<Database> databaseList = new ArrayList JavaDoc<Database>();
41     
42     /**
43      * @see net.sf.hajdbc.balancer.AbstractBalancer#getDatabases()
44      */

45     @Override JavaDoc
46     protected Collection JavaDoc<Database> getDatabases()
47     {
48         return this.databaseSet;
49     }
50
51     /**
52      * @see net.sf.hajdbc.Balancer#add(net.sf.hajdbc.Database)
53      */

54     @Override JavaDoc
55     public synchronized boolean add(Database database)
56     {
57         boolean added = super.add(database);
58         
59         if (added)
60         {
61             int weight = database.getWeight();
62             
63             for (int i = 0; i < weight; ++i)
64             {
65                 this.databaseList.add(database);
66             }
67         }
68         
69         return added;
70     }
71     
72     /**
73      * @see net.sf.hajdbc.Balancer#remove(net.sf.hajdbc.Database)
74      */

75     @Override JavaDoc
76     public synchronized boolean remove(Database database)
77     {
78         boolean removed = super.remove(database);
79         
80         int weight = database.getWeight();
81         
82         if (removed && (weight > 0))
83         {
84             int index = this.databaseList.indexOf(database);
85             
86             this.databaseList.subList(index, index + weight).clear();
87         }
88         
89         return removed;
90     }
91     
92     /**
93      * @see net.sf.hajdbc.Balancer#next()
94      */

95     public synchronized Database next()
96     {
97         if (this.databaseList.isEmpty())
98         {
99             return this.first();
100         }
101         
102         int index = this.random.nextInt(this.databaseList.size());
103         
104         return this.databaseList.get(index);
105     }
106 }
107
Popular Tags