KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > continuent > sequoia > driver > connectpolicy > RandomConnectPolicy


1 /**
2  * Sequoia: Database clustering technology.
3  * Copyright (C) 2005 Emic Networks.
4  * Contact: sequoia@continuent.org
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * Initial developer(s): Emmanuel Cecchet.
19  * Contributor(s): ______________________.
20  */

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

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

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

73   public synchronized void removeControllerFromSuspectList(ControllerInfo controller)
74   {
75     super.removeControllerFromSuspectList(controller);
76     availableControllerList.add(controller);
77   }
78
79   /**
80    * @see org.continuent.sequoia.driver.connectpolicy.AbstractControllerConnectPolicy#suspectControllerOfFailure(org.continuent.sequoia.driver.ControllerInfo)
81    */

82   public synchronized void suspectControllerOfFailure(
83       ControllerInfo controllerInfo)
84   {
85     super.suspectControllerOfFailure(controllerInfo);
86     while (availableControllerList.remove(controllerInfo))
87     { // Remove all possible duplicates
88
}
89   }
90
91   /**
92    * @see org.continuent.sequoia.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 == SequoiaUrl.DEBUG_LEVEL_DEBUG)
104       System.out.println("Selected controller " + controllerInfo);
105     return controllerInfo;
106   }
107
108 }
109
Popular Tags