KickJava   Java API By Example, From Geeks To Geeks.

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


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
26 import org.continuent.sequoia.common.exceptions.NoMoreControllerException;
27 import org.continuent.sequoia.driver.ControllerInfo;
28 import org.continuent.sequoia.driver.SequoiaUrl;
29
30 /**
31  * This class defines an OrderedConnectPolicy used when the Sequoia URL has the
32  * following form:
33  * jdbc:sequoia://node1,node2,node3/myDB?preferredController=ordered
34  * <p>
35  * This always direct to the first available controller in the list following
36  * the order of the list. With this example, we first try node1, and if not
37  * available then try to node2 and finally if none are available try node3.
38  *
39  * @author <a HREF="mailto:emmanuel.cecchet@emicnetworks.com">Emmanuel Cecchet
40  * </a>
41  * @version 1.0
42  */

43 public class OrderedConnectPolicy extends AbstractControllerConnectPolicy
44 {
45   private ArrayList JavaDoc availableControllerList;
46
47   /**
48    * Creates a new <code>OrderedConnectPolicy</code> object
49    *
50    * @param controllerList list of controller from Sequoia 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.continuent.sequoia.driver.SequoiaUrl#DEBUG_LEVEL_OFF
55    */

56   public OrderedConnectPolicy(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       availableControllerList.add(controllerList[i]);
64   }
65
66   /**
67    * @see org.continuent.sequoia.driver.connectpolicy.AbstractControllerConnectPolicy#removeControllerFromSuspectList(org.continuent.sequoia.driver.ControllerInfo)
68    */

69   public synchronized void removeControllerFromSuspectList(
70       ControllerInfo controller)
71   {
72     super.removeControllerFromSuspectList(controller);
73     // Rebuild the list in the correct order
74
availableControllerList.clear();
75     for (int i = 0; i < controllerList.length; i++)
76       if (!isSuspectedOfFailure(controllerList[i]))
77         availableControllerList.add(controllerList[i]);
78   }
79
80   /**
81    * @see org.continuent.sequoia.driver.connectpolicy.AbstractControllerConnectPolicy#suspectControllerOfFailure(org.continuent.sequoia.driver.ControllerInfo)
82    */

83   public synchronized void suspectControllerOfFailure(
84       ControllerInfo controllerInfo)
85   {
86     super.suspectControllerOfFailure(controllerInfo);
87     while (availableControllerList.remove(controllerInfo))
88     {
89       // Remove all possible duplicates
90
}
91   }
92
93   /**
94    * @see org.continuent.sequoia.driver.connectpolicy.AbstractControllerConnectPolicy#getController()
95    */

96   public synchronized ControllerInfo getController()
97       throws NoMoreControllerException
98   {
99     int size = availableControllerList.size();
100     if (size == 0)
101       throw new NoMoreControllerException();
102
103     // Take the first available controller
104
ControllerInfo controllerInfo = (ControllerInfo) availableControllerList
105         .get(0);
106     if (debugLevel == SequoiaUrl.DEBUG_LEVEL_DEBUG)
107       System.out.println("Selected controller " + controllerInfo);
108     return controllerInfo;
109   }
110
111 }
112
Popular Tags