KickJava   Java API By Example, From Geeks To Geeks.

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


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.HashSet 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 AbstractControllerConnectPolicy used by the driver to
32  * choose a controller to connect to.
33  *
34  * @author <a HREF="mailto:emmanuel.cecchet@emicnetworks.com">Emmanuel Cecchet
35  * </a>
36  * @version 1.0
37  */

38 public abstract class AbstractControllerConnectPolicy
39 {
40   protected ControllerInfo[] controllerList;
41   protected HashSet JavaDoc suspectedControllers;
42   private long retryIntervalInMs;
43   private ControllerPingThread controllerPingThread = null;
44   protected int debugLevel = SequoiaUrl.DEBUG_LEVEL_OFF;
45
46   /**
47    * Creates a new <code>AbstractControllerConnectPolicy</code> object
48    *
49    * @param controllerList the controller list on which the policy applies
50    * @param retryIntervalInMs Interval in milliseconds before retrying to
51    * re-connect to a controller that has failed
52    * @param debugLevel the debug level to use
53    * @see org.continuent.sequoia.driver.SequoiaUrl#DEBUG_LEVEL_OFF
54    */

55   public AbstractControllerConnectPolicy(ControllerInfo[] controllerList,
56       long retryIntervalInMs, int debugLevel)
57   {
58     if (controllerList == null)
59       throw new NullPointerException JavaDoc(
60           "Invalid null controller list in connect policy constructor");
61     if (controllerList.length == 0)
62       throw new RuntimeException JavaDoc(
63           "Invalid empty controller list in connect policy constructor");
64     this.controllerList = controllerList;
65     this.suspectedControllers = new HashSet JavaDoc(controllerList.length);
66     this.retryIntervalInMs = retryIntervalInMs;
67     this.debugLevel = debugLevel;
68   }
69
70   /**
71    * Terminate the controller ping thread if any and cleanup the suspected
72    * controller list.
73    *
74    * @see java.lang.Object#finalize()
75    */

76   protected void finalize() throws Throwable JavaDoc
77   {
78     super.finalize();
79     // Kill controller ping thread
80
suspectedControllers.clear();
81     if (controllerPingThread != null)
82       synchronized (controllerPingThread)
83       {
84         controllerPingThread.notify();
85       }
86   }
87
88   /**
89    * Get a controller using the implementation specific policy
90    *
91    * @return <code>ControllerInfo</code> of the selected controller
92    * @throws NoMoreControllerException if no controller in the controller list
93    * is reachable
94    */

95   public abstract ControllerInfo getController()
96       throws NoMoreControllerException;
97
98   /**
99    * Returns the controllerList value.
100    *
101    * @return Returns the controllerList.
102    */

103   public ControllerInfo[] getControllerList()
104   {
105     return controllerList;
106   }
107
108   /**
109    * Returns the suspectedControllers value.
110    *
111    * @return Returns the suspectedControllers.
112    */

113   public HashSet JavaDoc getSuspectedControllers()
114   {
115     return suspectedControllers;
116   }
117
118   /**
119    * Returns true if the specified controller is suspected of failure.
120    *
121    * @param controllerInfo the controller to check
122    * @return true if the controller is in the suspect list
123    */

124   public boolean isSuspectedOfFailure(ControllerInfo controllerInfo)
125   {
126     synchronized (suspectedControllers)
127     {
128       return suspectedControllers.contains(controllerInfo);
129     }
130   }
131
132   /**
133    * Sets the controllerList value.
134    *
135    * @param controllerList The controllerList to set.
136    */

137   public void setControllerList(ControllerInfo[] controllerList)
138   {
139     this.controllerList = controllerList;
140   }
141
142   /**
143    * Add the controller to the list of suspects.
144    *
145    * @param controllerInfo the controller suspected of failure
146    */

147   public synchronized void suspectControllerOfFailure(
148       ControllerInfo controllerInfo)
149   {
150     synchronized (suspectedControllers)
151     {
152       // Check that the controllerInfo is correct and add it to the list
153
for (int i = 0; i < controllerList.length; i++)
154       {
155         ControllerInfo controller = controllerList[i];
156         if (controller.equals(controllerInfo))
157         {
158           if (debugLevel >= SequoiaUrl.DEBUG_LEVEL_INFO)
159             System.out.println("Controller " + controllerInfo
160                 + " is now suspected of failure");
161           suspectedControllers.add(controllerInfo);
162           // If no controller ping thread has been created then we create a new
163
// one. As we are in the synchronized block, we are sure that the
164
// thread cannot die now but if it is already dead, then we have to
165
// create a new one (restart is not possible since the thread might
166
// not be completely dead yet).
167
// @see ControllerPingThread
168
if ((controllerPingThread == null)
169               || (controllerPingThread.isTerminated()))
170           {
171             controllerPingThread = new ControllerPingThread(this,
172                 retryIntervalInMs, debugLevel);
173             controllerPingThread.start();
174           }
175           return;
176         }
177       }
178     }
179   }
180
181   /**
182    * Remove the specified controller from the list of suspect controllers
183    *
184    * @param controller the controller to remove from the list
185    */

186   public void removeControllerFromSuspectList(ControllerInfo controller)
187   {
188     synchronized (suspectedControllers)
189     {
190       if (debugLevel >= SequoiaUrl.DEBUG_LEVEL_INFO)
191         System.out.println("Controller " + controller
192             + " is removed from suspect list");
193       suspectedControllers.remove(controller);
194     }
195   }
196
197 }
198
Popular Tags