KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > cjdbc > controller > loadbalancer > policies > errorchecking > ErrorCheckingRandom


1 /**
2  * C-JDBC: Clustered JDBC.
3  * Copyright (C) 2002-2004 French National Institute For Research In Computer
4  * Science And Control (INRIA).
5  * Contact: c-jdbc@objectweb.org
6  *
7  * This library is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as published by the
9  * Free Software Foundation; either version 2.1 of the License, or any later
10  * version.
11  *
12  * This library is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
15  * for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this library; if not, write to the Free Software Foundation,
19  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
20  *
21  * Initial developer(s): Emmanuel Cecchet.
22  * Contributor(s): _______________________
23  */

24
25 package org.objectweb.cjdbc.controller.loadbalancer.policies.errorchecking;
26
27 import java.util.ArrayList JavaDoc;
28 import java.util.Random JavaDoc;
29
30 import org.objectweb.cjdbc.controller.backend.DatabaseBackend;
31
32 /**
33  * Chooses numberOfNodes nodes randomly for error checking.
34  *
35  * @author <a HREF="mailto:Emmanuel.Cecchet@inria.fr">Emmanuel Cecchet</a>
36  * @version 1.0
37  */

38 public class ErrorCheckingRandom extends ErrorCheckingPolicy
39 {
40   private Random JavaDoc random = new Random JavaDoc();
41
42   /**
43    * Creates a new <code>ErrorCheckingRandom</code> instance.
44    *
45    * @param numberOfNodes number of nodes to use to check for errors on a query
46    */

47   public ErrorCheckingRandom(int numberOfNodes)
48   {
49     super(ErrorCheckingPolicy.RANDOM, numberOfNodes);
50   }
51
52   /**
53    * @see org.objectweb.cjdbc.controller.loadbalancer.policies.errorchecking.ErrorCheckingPolicy#getBackends(ArrayList)
54    */

55   public ArrayList JavaDoc getBackends(ArrayList JavaDoc backends)
56     throws ErrorCheckingException
57   {
58     int size = backends.size();
59
60     if (nbOfNodes == size)
61       return backends;
62     else if (nbOfNodes > size)
63       throw new ErrorCheckingException(
64         "Asking for more backends ("
65           + nbOfNodes
66           + ") than available ("
67           + size
68           + ")");
69
70     ArrayList JavaDoc result = new ArrayList JavaDoc(nbOfNodes);
71     ArrayList JavaDoc clonedList = new ArrayList JavaDoc(size);
72     for (int i = 0; i < size; i++)
73     { // Take all enabled backends
74
DatabaseBackend db = (DatabaseBackend) backends.get(i);
75       if (db.isReadEnabled() || db.isWriteEnabled())
76         clonedList.add(db);
77     }
78
79     int clonedSize = clonedList.size();
80
81     if (nbOfNodes == clonedSize)
82       return backends;
83     else if (nbOfNodes > clonedSize)
84       throw new ErrorCheckingException(
85         "Asking for more backends ("
86           + nbOfNodes
87           + ") than available ("
88           + clonedSize
89           + ")");
90
91     // Pickup the nodes randomly
92
for (int i = 0; i < nbOfNodes; i++)
93       result.add(clonedList.remove(random.nextInt(clonedSize)));
94
95     return result;
96   }
97
98   /**
99    * @see org.objectweb.cjdbc.controller.loadbalancer.policies.errorchecking.ErrorCheckingPolicy#getInformation()
100    */

101   public String JavaDoc getInformation()
102   {
103     return "Error checking using " + nbOfNodes + " nodes choosen randomly";
104   }
105 }
106
Popular Tags