1 31 package org.objectweb.proactive.ext.migration; 32 33 import java.lang.reflect.Method ; 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 { 44 45 48 private String methodOnArrival = null; 49 52 private String methodOnDeparture = null; 53 54 57 private MigrationStrategy migrationStrategy; 58 59 62 private boolean onItinerary; 63 64 68 private transient MethodCall migrationMethodCall = null; 69 70 75 private boolean fifoFirst; 76 77 78 79 83 public MigrationStrategyManagerImpl() { 84 } 85 86 public MigrationStrategyManagerImpl(Migratable migratableBody) { 87 migratableBody.addMigrationEventListener(this); 88 } 89 90 91 95 96 100 public void onDeparture(String s) { 101 this.methodOnDeparture = s; 102 } 103 104 105 public void onArrival(String 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 135 public void migrationAboutToStart(MigrationEvent event) { 136 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 } 148 149 public void migrationExceptionThrown(MigrationEvent event) { 150 MigrationException e = (MigrationException) event.getSource(); 152 e.printStackTrace(); 153 } 154 155 public void migratedBodyRestarted(MigrationEvent event) { 156 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 172 protected void executeMethodOnDeparture(Body body) throws MigrationException { 173 if (methodOnDeparture == null) return; 174 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 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 206 private void executeMethod(Object target, String methodName) throws MigrationException { 207 try { 208 Method m = target.getClass().getMethod(methodName, null); 209 m.invoke(target, null); 210 } catch (NoSuchMethodException e) { 211 throw new MigrationException("Cannot find method "+methodName+" in class "+target.getClass().getName(),e); 212 } catch (IllegalAccessException e) { 213 throw new MigrationException("Cannot access method "+methodName+" in class "+target.getClass().getName(),e); 214 } catch (java.lang.reflect.InvocationTargetException e) { 215 throw new MigrationException("Error while trying to execute method "+methodName,e); 216 } 217 } 218 219 } 220 | Popular Tags |