KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > cjdbc > controller > loadbalancer > policies > WaitForCompletionPolicy


1 /**
2  * C-JDBC: Clustered JDBC.
3  * Copyright (C) 2002-2004 French National Institute For Research In Computer
4  * Science And Control (INRIA).
5  * Contact: c-jdbc@objectweb.org
6  *
7  * This library is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as published by the
9  * Free Software Foundation; either version 2.1 of the License, or any later
10  * version.
11  *
12  * This library is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
15  * for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this library; if not, write to the Free Software Foundation,
19  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
20  *
21  * Initial developer(s): Emmanuel Cecchet.
22  * Contributor(s): ______________________________________.
23  */

24
25 package org.objectweb.cjdbc.controller.loadbalancer.policies;
26
27 import org.objectweb.cjdbc.common.xml.DatabasesXmlTags;
28 /**
29  * Defines the policy to adopt before returning a result to the client.
30  *
31  * @author <a HREF="mailto:Emmanuel.Cecchet@inria.fr">Emmanuel Cecchet</a>
32  * @version 1.0
33  */

34 public class WaitForCompletionPolicy
35 {
36   /** Return as soon as one node has completed the request. */
37   public static final int FIRST = 0;
38
39   /**
40    * Return as soon as a majority (n/2+1) of nodes has completed the request.
41    */

42   public static final int MAJORITY = 1;
43
44   /** Wait for all nodes to complete the request before returning the result. */
45   public static final int ALL = 2;
46
47   /** Policy (default is {@link #FIRST}). */
48   private int policy = FIRST;
49
50   /**
51    * Returns the policy.
52    *
53    * @return an <code>int</code> value
54    */

55   public int getPolicy()
56   {
57     return policy;
58   }
59
60   /**
61    * Sets the policy.
62    *
63    * @param policy the policy to set
64    */

65   public void setPolicy(int policy)
66   {
67     this.policy = policy;
68   }
69
70   /**
71    * Gives information about the current policy.
72    *
73    * @return a <code>String</code> value
74    */

75   public String JavaDoc getInformation()
76   {
77     switch (policy)
78     {
79       case FIRST :
80         return "return when first node completes";
81       case MAJORITY :
82         return "return when a majority of nodes completes";
83       case ALL :
84         return "return when all nodes have completed";
85       default :
86         return "unknown policy";
87     }
88   }
89
90   /**
91    * Returns this wait policy in xml format.
92    *
93    * @return xml formatted string
94    */

95   public String JavaDoc getXml()
96   {
97     StringBuffer JavaDoc info = new StringBuffer JavaDoc();
98     info.append(
99       "<"
100         + DatabasesXmlTags.ELT_WaitForCompletion
101         + " "
102         + DatabasesXmlTags.ATT_policy
103         + "=\"");
104     switch (policy)
105     {
106       case FIRST :
107         info.append(DatabasesXmlTags.VAL_first);
108         break;
109       case ALL :
110         info.append(DatabasesXmlTags.VAL_all);
111         break;
112       case MAJORITY :
113         info.append(DatabasesXmlTags.VAL_majority);
114         break;
115       default :
116         }
117     info.append("\"/>");
118     return info.toString();
119   }
120 }
121
Popular Tags