KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > proactive > ext > migration > MigrationStrategyImpl


1 /*
2 * ################################################################
3 *
4 * ProActive: The Java(TM) library for Parallel, Distributed,
5 * Concurrent computing with Security and Mobility
6 *
7 * Copyright (C) 1997-2002 INRIA/University of Nice-Sophia Antipolis
8 * Contact: proactive-support@inria.fr
9 *
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or any later version.
14 *
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
23 * USA
24 *
25 * Initial developer(s): The ProActive Team
26 * http://www.inria.fr/oasis/ProActive/contacts.html
27 * Contributor(s):
28 *
29 * ################################################################
30 */

31 package org.objectweb.proactive.ext.migration;
32
33 import org.apache.log4j.Logger;
34
35
36 /**
37  * MigrationStrategyImpl contains destination. It allows us to program an Agent to follow an itinary
38  * so that it can perform automaticly some method upon arrival on a site.
39  * An migrationStrategy is instanciated by the user.
40  */

41 public class MigrationStrategyImpl implements java.io.Serializable JavaDoc, MigrationStrategy {
42     
43     static Logger logger = Logger.getLogger(MigrationStrategyImpl.class.getName());
44
45   private java.util.Vector JavaDoc table;
46   private int index;
47
48
49   /**
50    * Creates an empty MigrationStrategyImpl
51    */

52   public MigrationStrategyImpl() {
53     super();
54     table = new java.util.Vector JavaDoc();
55     index = -1; //Negative value for first execution of getNextDestination
56
}
57
58
59   /**
60    * Creates an itinary using a text-file.
61    * Should implement verification of the existence of the methods to avoid
62    * problems at run-time.
63    */

64
65   public MigrationStrategyImpl(String JavaDoc filename) {
66     super();
67     table = new java.util.Vector JavaDoc();
68     index = -1; //Negative value for first execution of getNextDestination
69
String JavaDoc s;
70     java.io.FileReader JavaDoc f_in = null;
71     try {
72       f_in = new java.io.FileReader JavaDoc(filename);
73     } catch (java.io.FileNotFoundException JavaDoc e) {
74       logger.error("File not Found");
75     }
76     // on ouvre un "lecteur" sur ce fichier
77
java.io.BufferedReader JavaDoc _in = new java.io.BufferedReader JavaDoc(f_in);
78
79     // on lit a partir de ce fichier
80
// NB : a priori on ne sait pas combien de lignes on va lire !!
81
try {
82       // tant qu'il y a quelque chose a lire
83
while (_in.ready()) {
84         // on le lit
85
s = _in.readLine();
86         java.util.StringTokenizer JavaDoc tokens = new java.util.StringTokenizer JavaDoc(s, " ");
87         this.add(new NodeDestination(new String JavaDoc(tokens.nextToken()), tokens.nextToken()));
88       }
89     }// catch (IOException e) {}
90
catch (Exception JavaDoc e) {
91     }
92
93     try {
94       _in.close();
95     } catch (java.io.IOException JavaDoc e) {}
96   }
97
98
99   /**
100    * Adds a Destination to an itinary
101    */

102   public void add(Destination r) {
103     table.addElement(r);
104   }
105
106
107   public void add(String JavaDoc nodeURL, String JavaDoc method) {
108     table.addElement(new NodeDestination(nodeURL, method));
109   }
110
111   /**
112   * Adds a Destination for the next migration
113   *
114   */

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 JavaDoc nodeURL, String JavaDoc 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 JavaDoc nodeURL, String JavaDoc method) {
133     removeFromItinerary(new NodeDestination(nodeURL, method));
134   }
135
136
137   public void remove(Destination d) {
138     removeFromItinerary(d);
139   }
140
141   //Maybe we should return something
142
private void removeFromItinerary(Destination r) {
143     //System.out.println("MigrationStrategyImpl: removeFromItinerary() the result is " + table.removeElement(r));
144
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         //we have removed an element before the index , so we shift the index
151
if (i < index)
152           index--;
153         return;
154       }
155       i++;
156     }
157   }
158
159
160   /**
161    * Returns the next destination on the list and increase index by one.
162    * If there is no more destination, then return null
163    */

164   public Destination next() {
165     index++;
166     if (index < table.size()) {
167       Destination r = (Destination)table.elementAt(index);
168       //index++;
169
return (r);
170     } else {
171       //System.out.println("MigrationStrategyImpl: next() no next destination found");
172
return (null);
173     }
174   }
175
176
177   /**
178    * Returns the current Destination.
179    * Returns null if nothing is available
180    */

181   public Destination getCurrentDestination() {
182     if ((index < table.size()) && (index >= 0)) {
183       Destination r = (Destination)table.elementAt(index);
184       return (r);
185     } else //should never happens
186
{
187       return (null);
188     }
189   }
190
191
192   /**
193    * Returns the next destination in the migrationStrategy which is not s
194    */

195   public Destination getNextExcept(String JavaDoc 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; //Negative value for first execution of getNextDestination
218
}
219
220
221   /**
222    * Return a java.util.Vector made of strings representing the destinations
223    */

224   public java.util.Vector JavaDoc toVector() {
225     java.util.Vector JavaDoc temp = new java.util.Vector JavaDoc();
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