00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 package org.objectweb.cjdbc.controller.loadbalancer.policies.createtable;
00026
00027 import java.util.ArrayList;
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038 public class CreateTableRoundRobin extends CreateTableRule
00039 {
00040 private int index = 0;
00041
00042
00043
00044
00045 public CreateTableRoundRobin()
00046 {
00047 super(CreateTablePolicy.ROUND_ROBIN);
00048 }
00049
00050
00051
00052
00053
00054
00055 public CreateTableRoundRobin(ArrayList backendList)
00056 {
00057 super(CreateTablePolicy.ROUND_ROBIN, backendList);
00058 }
00059
00060
00061
00062
00063 public ArrayList getBackends(ArrayList backends) throws CreateTableException
00064 {
00065 if (nbOfNodes == 0)
00066 return null;
00067
00068 ArrayList clonedList = super.getBackends(backends);
00069
00070 int clonedSize = clonedList.size();
00071
00072 if (nbOfNodes == clonedSize)
00073 return clonedList;
00074 else if (nbOfNodes > clonedSize)
00075 throw new CreateTableException(
00076 "Asking for more backends ("
00077 + nbOfNodes
00078 + ") than available ("
00079 + clonedSize
00080 + ")");
00081
00082 ArrayList result = new ArrayList(nbOfNodes);
00083
00084 synchronized (this)
00085 {
00086 for (int i = 0; i < nbOfNodes; i++)
00087 {
00088 index = (index + 1) % clonedSize;
00089 if (index - i < 0)
00090 result.add(clonedList.remove(0));
00091 else
00092 result.add(clonedList.remove(index - i));
00093 }
00094 }
00095
00096 return result;
00097 }
00098
00099
00100
00101
00102 public String getInformation()
00103 {
00104 String s;
00105 if (tableName == null)
00106 s = "Default rule create table on ";
00107 else
00108 s = "Rule for table " + tableName + " create table on ";
00109
00110 return s + nbOfNodes + " node(s) in round-robin from " + backendList;
00111 }
00112 }