KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.lang.reflect.Method JavaDoc;
34
35 import org.objectweb.proactive.Body;
36 import org.objectweb.proactive.ProActive;
37 import org.objectweb.proactive.core.body.migration.Migratable;
38 import org.objectweb.proactive.core.body.migration.MigrationException;
39 import org.objectweb.proactive.core.event.MigrationEvent;
40 import org.objectweb.proactive.core.event.MigrationEventListener;
41 import org.objectweb.proactive.core.mop.MethodCall;
42
43 public class MigrationStrategyManagerImpl implements MigrationStrategyManager, MigrationEventListener, java.io.Serializable JavaDoc {
44
45   /**
46    * Name of the method to be called when the agent reaches a new site
47    */

48   private String JavaDoc methodOnArrival = null;
49   /**
50    * Name of the method to be called before an agents leaves the current site
51    */

52   private String JavaDoc methodOnDeparture = null;
53   
54   /**
55    * MigrationStrategyImpl for the mobile object
56    */

57   private MigrationStrategy migrationStrategy;
58   
59   /**
60    * Indicates if the object follows an migrationStrategy
61    */

62   private boolean onItinerary;
63   
64   /**
65    * Method used to migrate by default = migrateTo
66    * @see migrateTo
67    */

68   private transient MethodCall migrationMethodCall = null;
69   
70   /**
71    * An indication regarding the migration strategy
72    * Indicates wether we first serve pending requests
73    * before applying the startegy
74    */

75   private boolean fifoFirst;
76
77
78
79   //
80
// -- CONSTRUCTORS -----------------------------------------------
81
//
82

83   public MigrationStrategyManagerImpl() {
84   }
85
86   public MigrationStrategyManagerImpl(Migratable migratableBody) {
87     migratableBody.addMigrationEventListener(this);
88   }
89
90
91   //
92
// -- PUBLIC METHODS -----------------------------------------------
93
//
94

95   
96   //
97
// -- Implements MigrationStrategyManager -----------------------------------------------
98
//
99

100   public void onDeparture(String JavaDoc s) {
101     this.methodOnDeparture = s;
102   }
103
104
105   public void onArrival(String JavaDoc s) {
106     this.methodOnArrival = s;
107   }
108
109
110   public void startStrategy(Body body) throws MigrationException {
111     getMigrationStrategy().reset();
112     fifoFirst = false;
113     onItinerary = true;
114     continueStrategy(body);
115   }
116
117
118   public MigrationStrategy getMigrationStrategy() {
119     if (migrationStrategy == null) {
120       migrationStrategy = new MigrationStrategyImpl();
121     }
122     return migrationStrategy;
123   }
124
125
126   public void setMigrationStrategy(MigrationStrategy s) {
127     migrationStrategy = s;
128   }
129    
130
131   //
132
// -- Implements MigrationEventListener -----------------------------------------------
133
//
134

135   public void migrationAboutToStart(MigrationEvent event) {
136     //System.out.println("MigrationStrategyManagerImpl.migrationAboutToStart");
137
Body body = (Body) event.getSource();
138     try {
139       executeMethodOnDeparture(body);
140     } catch (MigrationException e) {
141       e.printStackTrace();
142     }
143   }
144   
145   public void migrationFinished(MigrationEvent event) {
146     //System.out.println("MigrationStrategyManagerImpl.migrationFinished");
147
}
148   
149   public void migrationExceptionThrown(MigrationEvent event) {
150     //System.out.println("MigrationStrategyManagerImpl.migrationExceptionThrown");
151
MigrationException e = (MigrationException) event.getSource();
152     e.printStackTrace();
153   }
154   
155   public void migratedBodyRestarted(MigrationEvent event) {
156     //System.out.println("MigrationStrategyManagerImpl.migratedBodyRestarted");
157
Body body = (Body) event.getSource();
158     try {
159       executeMethodOnArrival(body);
160       continueStrategy(body);
161     } catch (MigrationException e) {
162       e.printStackTrace();
163     }
164   }
165
166
167
168   //
169
// -- PROTECTED METHODS -----------------------------------------------
170
//
171

172   protected void executeMethodOnDeparture(Body body) throws MigrationException {
173     if (methodOnDeparture == null) return;
174     //NE PAS SUPPRIMER!!!
175
// System.out.println("MigrationManagerImpl: calling onDeparture()");
176
executeMethod(body.getReifiedObject(), methodOnDeparture);
177   }
178   
179
180   protected void executeMethodOnArrival(Body body) throws MigrationException {
181     if (methodOnArrival == null) return;
182     executeMethod(body.getReifiedObject(), methodOnArrival);
183   }
184
185
186   protected void continueStrategy(Body body) throws MigrationException {
187     if (! onItinerary) return;
188     // Method autoExecMethod = reifiedObject.getClass().getMethod(methodOnArrival, null);
189
// MethodCall autoExecMethodCall = org.objectweb.proactive.core.mop.MethodCall.getMethodCall(autoExecMethod, null);
190
Destination r = migrationStrategy.next();
191     if (r == null) {
192       this.onItinerary = false;
193       return;
194     }
195     methodOnArrival = r.getMethodName();
196     ProActive.migrateTo(body, r.getDestination(), fifoFirst);
197     
198   }
199
200
201
202   //
203
// -- PRIVATE METHODS -----------------------------------------------
204
//
205

206   private void executeMethod(Object JavaDoc target, String JavaDoc methodName) throws MigrationException {
207     try {
208       Method JavaDoc m = target.getClass().getMethod(methodName, null);
209       m.invoke(target, null);
210     } catch (NoSuchMethodException JavaDoc e) {
211       throw new MigrationException("Cannot find method "+methodName+" in class "+target.getClass().getName(),e);
212     } catch (IllegalAccessException JavaDoc e) {
213       throw new MigrationException("Cannot access method "+methodName+" in class "+target.getClass().getName(),e);
214     } catch (java.lang.reflect.InvocationTargetException JavaDoc e) {
215       throw new MigrationException("Error while trying to execute method "+methodName,e);
216     }
217   }
218
219 }
220
Popular Tags