1 31 package org.objectweb.proactive.ext.migration; 32 33 import org.apache.log4j.Logger; 34 35 36 41 public class MigrationStrategyImpl implements java.io.Serializable , MigrationStrategy { 42 43 static Logger logger = Logger.getLogger(MigrationStrategyImpl.class.getName()); 44 45 private java.util.Vector table; 46 private int index; 47 48 49 52 public MigrationStrategyImpl() { 53 super(); 54 table = new java.util.Vector (); 55 index = -1; } 57 58 59 64 65 public MigrationStrategyImpl(String filename) { 66 super(); 67 table = new java.util.Vector (); 68 index = -1; String s; 70 java.io.FileReader f_in = null; 71 try { 72 f_in = new java.io.FileReader (filename); 73 } catch (java.io.FileNotFoundException e) { 74 logger.error("File not Found"); 75 } 76 java.io.BufferedReader _in = new java.io.BufferedReader (f_in); 78 79 try { 82 while (_in.ready()) { 84 s = _in.readLine(); 86 java.util.StringTokenizer tokens = new java.util.StringTokenizer (s, " "); 87 this.add(new NodeDestination(new String (tokens.nextToken()), tokens.nextToken())); 88 } 89 } catch (Exception e) { 91 } 92 93 try { 94 _in.close(); 95 } catch (java.io.IOException e) {} 96 } 97 98 99 102 public void add(Destination r) { 103 table.addElement(r); 104 } 105 106 107 public void add(String nodeURL, String method) { 108 table.addElement(new NodeDestination(nodeURL, method)); 109 } 110 111 115 public void addNext(Destination r) { 116 if (index == -1 || index==table.size()-1) 117 table.addElement(r); 118 else 119 table.add(index+1,r); 120 121 } 122 123 124 public void addNext(String nodeURL, String method) { 125 if (index == -1 || index==table.size()-1) 126 table.addElement(new NodeDestination(nodeURL, method)); 127 else table.add(index+1,new NodeDestination(nodeURL, method)); 128 129 } 130 131 132 public void remove(String nodeURL, String method) { 133 removeFromItinerary(new NodeDestination(nodeURL, method)); 134 } 135 136 137 public void remove(Destination d) { 138 removeFromItinerary(d); 139 } 140 141 private void removeFromItinerary(Destination r) { 143 int i = 0; 145 Destination r2; 146 while (i < table.size()) { 147 r2 = (Destination)table.elementAt(i); 148 if ((r2.getDestination().equals(r.getDestination())) && (r2.getMethodName().equals(r.getMethodName()))) { 149 table.removeElementAt(i); 150 if (i < index) 152 index--; 153 return; 154 } 155 i++; 156 } 157 } 158 159 160 164 public Destination next() { 165 index++; 166 if (index < table.size()) { 167 Destination r = (Destination)table.elementAt(index); 168 return (r); 170 } else { 171 return (null); 173 } 174 } 175 176 177 181 public Destination getCurrentDestination() { 182 if ((index < table.size()) && (index >= 0)) { 183 Destination r = (Destination)table.elementAt(index); 184 return (r); 185 } else { 187 return (null); 188 } 189 } 190 191 192 195 public Destination getNextExcept(String s) { 196 Destination temp; 197 if (s == null) return next(); 198 while ((temp = next()) != null) { 199 if (!temp.getDestination().equals(s)) 200 return temp; 201 } 202 return null; 203 } 204 205 206 public int size() { 207 return table.size(); 208 } 209 210 211 public void decrease() { 212 if (index >= 0) index--; 213 } 214 215 216 public void reset() { 217 index = -1; } 219 220 221 224 public java.util.Vector toVector() { 225 java.util.Vector temp = new java.util.Vector (); 226 for (int i = 0; i < table.size(); i++) { 227 temp.add(((Destination)table.elementAt(i)).getDestination()); 228 } 229 return temp; 230 } 231 } 232 | Popular Tags |