KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > cjdbc > driver > connectpolicy > RandomConnectPolicy


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

23
24 package org.objectweb.cjdbc.driver.connectpolicy;
25
26 import java.util.ArrayList JavaDoc;
27 import java.util.Random JavaDoc;
28
29 import org.objectweb.cjdbc.common.exceptions.NoMoreControllerException;
30 import org.objectweb.cjdbc.driver.CjdbcUrl;
31 import org.objectweb.cjdbc.driver.ControllerInfo;
32
33 /**
34  * This class defines a RandomConnectPolicy used when the C-JDBC URL has the
35  * following form:
36  * jdbc:cjdbc://node1,node2,node3/myDB?preferredController=random
37  *
38  * @author <a HREF="mailto:emmanuel.cecchet@emicnetworks.com">Emmanuel Cecchet
39  * </a>
40  * @version 1.0
41  */

42 public class RandomConnectPolicy extends AbstractControllerConnectPolicy
43 {
44   private Random JavaDoc rand;
45   private ArrayList JavaDoc availableControllerList;
46
47   /**
48    * Creates a new <code>RandomConnectPolicy</code> object
49    *
50    * @param controllerList list of controller from C-JDBC url
51    * @param retryIntervalInMs Interval in milliseconds before retrying to
52    * re-connect to a controller that has failed
53    * @param debugLevel the debug level to use
54    * @see org.objectweb.cjdbc.driver.CjdbcUrl#DEBUG_LEVEL_OFF
55    */

56   public RandomConnectPolicy(ControllerInfo[] controllerList,
57       long retryIntervalInMs, int debugLevel)
58   {
59     super(controllerList, retryIntervalInMs, debugLevel);
60     // Clone the list of controllers
61
availableControllerList = new ArrayList JavaDoc(controllerList.length);
62     for (int i = 0; i < controllerList.length; i++)
63     {
64       if (controllerList[i] == null)
65         throw new RuntimeException JavaDoc(
66             "Invalid null controller in list while instanciating RandomConnectPolicy");
67       availableControllerList.add(controllerList[i]);
68     }
69     rand = new Random JavaDoc(System.currentTimeMillis());
70   }
71
72   /**
73    * @see org.objectweb.cjdbc.driver.connectpolicy.AbstractControllerConnectPolicy#removeControllerFromSuspectList(org.objectweb.cjdbc.driver.ControllerInfo)
74    */

75   public void removeControllerFromSuspectList(ControllerInfo controller)
76   {
77     super.removeControllerFromSuspectList(controller);
78     availableControllerList.add(controller);
79   }
80
81   /**
82    * @see org.objectweb.cjdbc.driver.connectpolicy.AbstractControllerConnectPolicy#suspectControllerOfFailure(org.objectweb.cjdbc.driver.ControllerInfo)
83    */

84   public synchronized void suspectControllerOfFailure(
85       ControllerInfo controllerInfo)
86   {
87     super.suspectControllerOfFailure(controllerInfo);
88     availableControllerList.remove(controllerInfo);
89   }
90
91   /**
92    * @see org.objectweb.cjdbc.driver.connectpolicy.AbstractControllerConnectPolicy#getController()
93    */

94   public synchronized ControllerInfo getController()
95       throws NoMoreControllerException
96   {
97     int size = availableControllerList.size();
98     if (size == 0)
99       throw new NoMoreControllerException();
100
101     ControllerInfo controllerInfo = (ControllerInfo) availableControllerList
102         .get(rand.nextInt(size));
103     if (debugLevel == CjdbcUrl.DEBUG_LEVEL_DEBUG)
104       System.out.println("Selected controller " + controllerInfo);
105     return controllerInfo;
106   }
107
108 }
109
Popular Tags