KickJava   Java API By Example, From Geeks To Geeks.

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


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
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 OrderedConnectPolicy used when the C-JDBC URL has the
34  * following form:
35  * jdbc:cjdbc://node1,node2,node3/myDB?preferredController=ordered
36  * <p>
37  * This always direct to the first available controller in the list following
38  * the order of the list. With this example, we first try node1, and if not
39  * available then try to node2 and finally if none are available try node3.
40  *
41  * @author <a HREF="mailto:emmanuel.cecchet@emicnetworks.com">Emmanuel Cecchet
42  * </a>
43  * @version 1.0
44  */

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

58   public OrderedConnectPolicy(ControllerInfo[] controllerList,
59       long retryIntervalInMs, int debugLevel)
60   {
61     super(controllerList, retryIntervalInMs, debugLevel);
62     // Clone the list of controllers
63
availableControllerList = new ArrayList JavaDoc(controllerList.length);
64     for (int i = 0; i < controllerList.length; i++)
65       availableControllerList.add(controllerList[i]);
66   }
67
68   /**
69    * @see org.objectweb.cjdbc.driver.connectpolicy.AbstractControllerConnectPolicy#removeControllerFromSuspectList(org.objectweb.cjdbc.driver.ControllerInfo)
70    */

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

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

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