KickJava   Java API By Example, From Geeks To Geeks.

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


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.HashSet JavaDoc;
27
28 import org.objectweb.cjdbc.common.exceptions.NoMoreControllerException;
29 import org.objectweb.cjdbc.driver.CjdbcUrl;
30 import org.objectweb.cjdbc.driver.ControllerInfo;
31
32 /**
33  * This class defines an AbstractControllerConnectPolicy used by the driver to
34  * choose a controller to connect to.
35  *
36  * @author <a HREF="mailto:emmanuel.cecchet@emicnetworks.com">Emmanuel Cecchet
37  * </a>
38  * @version 1.0
39  */

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

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

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

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

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

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

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

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

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

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