KickJava   Java API By Example, From Geeks To Geeks.

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


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
29 import org.objectweb.cjdbc.controller.backend.DatabaseBackend;
30
31 /**
32  * Chooses the number of nodes nodes for error checking using a round-robin
33  * algorithm.
34  *
35  * @author <a HREF="mailto:Emmanuel.Cecchet@inria.fr">Emmanuel Cecchet</a>
36  * @version 1.0
37  */

38 public class ErrorCheckingRoundRobin extends ErrorCheckingPolicy
39 {
40   /** Round-robin index. */
41   private int index = 0;
42
43   /**
44    * Creates a new <code>ErrorCheckingRoundRobin</code> instance.
45    *
46    * @param numberOfNodes number of nodes
47    */

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

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

102   public String JavaDoc getInformation()
103   {
104     return "Error checking using "
105       + nbOfNodes
106       + " nodes choosen using a round-robin algorithm";
107   }
108 }
109
Popular Tags