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
00026
00027
00028
00029
00030
00031 package org.objectweb.proactive.ext.migration;
00032
00033 import java.lang.reflect.Method;
00034
00035 import org.objectweb.proactive.Body;
00036 import org.objectweb.proactive.ProActive;
00037 import org.objectweb.proactive.core.body.migration.Migratable;
00038 import org.objectweb.proactive.core.body.migration.MigrationException;
00039 import org.objectweb.proactive.core.event.MigrationEvent;
00040 import org.objectweb.proactive.core.event.MigrationEventListener;
00041 import org.objectweb.proactive.core.mop.MethodCall;
00042
00043
00044 public class MigrationStrategyManagerImpl implements MigrationStrategyManager,
00045 MigrationEventListener, java.io.Serializable {
00046
00050 private String methodOnArrival = null;
00051
00055 private String methodOnDeparture = null;
00056
00060 private MigrationStrategy migrationStrategy;
00061
00065 private boolean onItinerary;
00066
00071 private transient MethodCall migrationMethodCall = null;
00072
00078 private boolean fifoFirst;
00079
00080
00081
00082
00083 public MigrationStrategyManagerImpl() {
00084 }
00085
00086 public MigrationStrategyManagerImpl(Migratable migratableBody) {
00087 migratableBody.addMigrationEventListener(this);
00088 }
00089
00090
00091
00092
00093
00094
00095
00096 public void onDeparture(String s) {
00097 this.methodOnDeparture = s;
00098 }
00099
00100 public void onArrival(String s) {
00101 this.methodOnArrival = s;
00102 }
00103
00104 public void startStrategy(Body body) throws MigrationException {
00105 getMigrationStrategy().reset();
00106 fifoFirst = false;
00107 onItinerary = true;
00108 continueStrategy(body);
00109 }
00110
00111 public MigrationStrategy getMigrationStrategy() {
00112 if (migrationStrategy == null) {
00113 migrationStrategy = new MigrationStrategyImpl();
00114 }
00115 return migrationStrategy;
00116 }
00117
00118 public void setMigrationStrategy(MigrationStrategy s) {
00119 migrationStrategy = s;
00120 }
00121
00122
00123
00124
00125 public void migrationAboutToStart(MigrationEvent event) {
00126
00127 Body body = (Body) event.getSource();
00128 try {
00129 executeMethodOnDeparture(body);
00130 } catch (MigrationException e) {
00131 e.printStackTrace();
00132 }
00133 }
00134
00135 public void migrationFinished(MigrationEvent event) {
00136
00137 }
00138
00139 public void migrationExceptionThrown(MigrationEvent event) {
00140
00141 MigrationException e = (MigrationException) event.getSource();
00142 e.printStackTrace();
00143 }
00144
00145 public void migratedBodyRestarted(MigrationEvent event) {
00146
00147 Body body = (Body) event.getSource();
00148 try {
00149 executeMethodOnArrival(body);
00150 continueStrategy(body);
00151 } catch (MigrationException e) {
00152 e.printStackTrace();
00153 }
00154 }
00155
00156
00157
00158
00159 protected void executeMethodOnDeparture(Body body)
00160 throws MigrationException {
00161 if (methodOnDeparture == null) {
00162 return;
00163 }
00164
00165
00166
00167 executeMethod(body.getReifiedObject(), methodOnDeparture);
00168 }
00169
00170 protected void executeMethodOnArrival(Body body) throws MigrationException {
00171 if (methodOnArrival == null) {
00172 return;
00173 }
00174 executeMethod(body.getReifiedObject(), methodOnArrival);
00175 }
00176
00177 protected void continueStrategy(Body body) throws MigrationException {
00178 if (!onItinerary) {
00179 return;
00180 }
00181
00182
00183
00184 Destination r = migrationStrategy.next();
00185 if (r == null) {
00186 this.onItinerary = false;
00187 return;
00188 }
00189 methodOnArrival = r.getMethodName();
00190 ProActive.migrateTo(body, r.getDestination(), fifoFirst);
00191 }
00192
00193
00194
00195
00196 private void executeMethod(Object target, String methodName)
00197 throws MigrationException {
00198 try {
00199 Method m = target.getClass().getMethod(methodName, (Class[]) null);
00200 m.invoke(target, (Object[]) null);
00201 } catch (NoSuchMethodException e) {
00202 throw new MigrationException("Cannot find method " + methodName +
00203 " in class " + target.getClass().getName(), e);
00204 } catch (IllegalAccessException e) {
00205 throw new MigrationException("Cannot access method " + methodName +
00206 " in class " + target.getClass().getName(), e);
00207 } catch (java.lang.reflect.InvocationTargetException e) {
00208 throw new MigrationException(
00209 "Error while trying to execute method " + methodName, e);
00210 }
00211 }
00212 }