org/objectweb/proactive/ext/migration/MigrationStrategyManagerImpl.java

00001 /* 
00002  * ################################################################
00003  * 
00004  * ProActive: The Java(TM) library for Parallel, Distributed, 
00005  *            Concurrent computing with Security and Mobility
00006  * 
00007  * Copyright (C) 1997-2007 INRIA/University of Nice-Sophia Antipolis
00008  * Contact: proactive@objectweb.org
00009  * 
00010  * This library is free software; you can redistribute it and/or
00011  * modify it under the terms of the GNU Lesser General Public
00012  * License as published by the Free Software Foundation; either
00013  * version 2.1 of the License, or any later version.
00014  *  
00015  * This library is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018  * Lesser General Public License for more details.
00019  * 
00020  * You should have received a copy of the GNU Lesser General Public
00021  * License along with this library; if not, write to the Free Software
00022  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
00023  * USA
00024  *  
00025  *  Initial developer(s):               The ProActive Team
00026  *                        http://www.inria.fr/oasis/ProActive/contacts.html
00027  *  Contributor(s): 
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     // -- CONSTRUCTORS -----------------------------------------------
00082     //
00083     public MigrationStrategyManagerImpl() {
00084     }
00085 
00086     public MigrationStrategyManagerImpl(Migratable migratableBody) {
00087         migratableBody.addMigrationEventListener(this);
00088     }
00089 
00090     //
00091     // -- PUBLIC METHODS -----------------------------------------------
00092     //
00093     //
00094     // -- Implements MigrationStrategyManager -----------------------------------------------
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     // -- Implements MigrationEventListener -----------------------------------------------
00124     //
00125     public void migrationAboutToStart(MigrationEvent event) {
00126         //System.out.println("MigrationStrategyManagerImpl.migrationAboutToStart");
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         //System.out.println("MigrationStrategyManagerImpl.migrationFinished");
00137     }
00138 
00139     public void migrationExceptionThrown(MigrationEvent event) {
00140         //System.out.println("MigrationStrategyManagerImpl.migrationExceptionThrown");
00141         MigrationException e = (MigrationException) event.getSource();
00142         e.printStackTrace();
00143     }
00144 
00145     public void migratedBodyRestarted(MigrationEvent event) {
00146         //System.out.println("MigrationStrategyManagerImpl.migratedBodyRestarted");
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     // -- PROTECTED METHODS -----------------------------------------------
00158     //
00159     protected void executeMethodOnDeparture(Body body)
00160         throws MigrationException {
00161         if (methodOnDeparture == null) {
00162             return;
00163         }
00164 
00165         //NE PAS SUPPRIMER!!!
00166         //      System.out.println("MigrationManagerImpl: calling onDeparture()");
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         //      Method autoExecMethod = reifiedObject.getClass().getMethod(methodOnArrival, null);
00183         //      MethodCall autoExecMethodCall = org.objectweb.proactive.core.mop.MethodCall.getMethodCall(autoExecMethod, null);
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     // -- PRIVATE METHODS -----------------------------------------------
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 }

Generated on Mon Jan 22 15:16:10 2007 for ProActive by  doxygen 1.5.1