KickJava   Java API By Example, From Geeks To Geeks.

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


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.sql.SQLException JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import java.util.StringTokenizer JavaDoc;
29
30 import org.objectweb.cjdbc.common.exceptions.NoMoreControllerException;
31 import org.objectweb.cjdbc.driver.CjdbcUrl;
32 import org.objectweb.cjdbc.driver.ControllerInfo;
33
34 /**
35  * This class defines a PreferredListConnectPolicy
36  *
37  * @author <a HREF="mailto:emmanuel.cecchet@emicnetworks.com">Emmanuel Cecchet
38  * </a>
39  * @version 1.0
40  */

41 public class PreferredListConnectPolicy extends AbstractControllerConnectPolicy
42 {
43   private int index = -1;
44   private ArrayList JavaDoc preferredControllers;
45   private ArrayList JavaDoc deadPreferredControllers;
46
47   /**
48    * Creates a new <code>PreferredListConnectPolicy</code> object
49    *
50    * @param controllerList list of controller from C-JDBC url
51    * @param retryIntervalInMs Interval in milliseconds before retrying to
52    * re-connect to a controller that has failed
53    * @param preferredControllerList comma separated list of preferred
54    * controllers
55    * @param debugLevel the debug level to use
56    * @see org.objectweb.cjdbc.driver.CjdbcUrl#DEBUG_LEVEL_OFF
57    */

58   public PreferredListConnectPolicy(ControllerInfo[] controllerList,
59       long retryIntervalInMs, String JavaDoc preferredControllerList, int debugLevel)
60   {
61     super(controllerList, retryIntervalInMs, debugLevel);
62
63     // Check the validity of each controller in the list
64
StringTokenizer JavaDoc controllers = new StringTokenizer JavaDoc(preferredControllerList,
65         ",", true);
66     int tokenNumber = controllers.countTokens();
67     preferredControllers = new ArrayList JavaDoc(tokenNumber - 1);
68     deadPreferredControllers = new ArrayList JavaDoc(tokenNumber - 1);
69     int i = 0;
70     String JavaDoc s;
71     boolean lastTokenWasComma = false;
72     while (controllers.hasMoreTokens())
73     {
74       s = controllers.nextToken().trim();
75       if (s.equals(","))
76       {
77         if (lastTokenWasComma || (i == 0) || (i == tokenNumber - 1))
78           // ',' cannot be the first or the last token
79
// another ',' cannot follow a ','
80
throw new RuntimeException JavaDoc(
81               "Syntax error in controller list for preferredController attribute '"
82                   + preferredControllerList + "'");
83         else
84         {
85           lastTokenWasComma = true;
86           continue;
87         }
88       }
89       lastTokenWasComma = false;
90       try
91       {
92         preferredControllers.add(CjdbcUrl.parseController(s));
93       }
94       catch (SQLException JavaDoc e)
95       {
96         throw new RuntimeException JavaDoc("Invalid controller " + s
97             + " in controller list for preferredController attribute");
98       }
99       i++;
100     }
101
102   }
103
104   /**
105    * @see org.objectweb.cjdbc.driver.connectpolicy.AbstractControllerConnectPolicy#removeControllerFromSuspectList(org.objectweb.cjdbc.driver.ControllerInfo)
106    */

107   public synchronized void removeControllerFromSuspectList(
108       ControllerInfo controller)
109   {
110     super.removeControllerFromSuspectList(controller);
111     if (deadPreferredControllers.remove(controller))
112       preferredControllers.add(controller);
113   }
114
115   /**
116    * @see org.objectweb.cjdbc.driver.connectpolicy.AbstractControllerConnectPolicy#suspectControllerOfFailure(org.objectweb.cjdbc.driver.ControllerInfo)
117    */

118   public synchronized void suspectControllerOfFailure(
119       ControllerInfo controllerInfo)
120   {
121     super.suspectControllerOfFailure(controllerInfo);
122     if (preferredControllers.remove(controllerInfo))
123       deadPreferredControllers.add(controllerInfo);
124   }
125
126   /**
127    * @see org.objectweb.cjdbc.driver.connectpolicy.AbstractControllerConnectPolicy#getController()
128    */

129   public synchronized ControllerInfo getController()
130       throws NoMoreControllerException
131   {
132     if (suspectedControllers.size() == controllerList.length)
133       throw new NoMoreControllerException();
134
135     if (preferredControllers.isEmpty())
136     { // Find 1st available controller in round-robin
137
do
138       {
139         index = (index + 1) % controllerList.length;
140       }
141       while (suspectedControllers.contains(controllerList[index]));
142       if (debugLevel == CjdbcUrl.DEBUG_LEVEL_DEBUG)
143         System.out.println("Selected controller[" + index + "]:"
144             + controllerList[index]);
145       return controllerList[index];
146     }
147     else
148     {
149       index = (index + 1) % preferredControllers.size();
150       ControllerInfo controllerInfo = (ControllerInfo) preferredControllers
151           .get(index);
152       if (debugLevel == CjdbcUrl.DEBUG_LEVEL_DEBUG)
153         System.out.println("Selected controller[" + index + "]:"
154             + controllerInfo);
155       return controllerInfo;
156     }
157   }
158
159 }
160
Popular Tags