1 31 package org.objectweb.proactive.core.body.migration; 32 33 import org.apache.log4j.Logger; 34 35 import org.objectweb.proactive.core.UniqueID; 36 import org.objectweb.proactive.core.body.MetaObjectFactory; 37 import org.objectweb.proactive.core.body.UniversalBody; 38 import org.objectweb.proactive.core.component.body.ComponentBodyImpl; 39 import org.objectweb.proactive.core.event.MigrationEventListener; 40 import org.objectweb.proactive.core.node.Node; 41 42 43 public class MigratableBody extends ComponentBodyImpl implements Migratable, 44 java.io.Serializable { 45 protected static Logger logger = Logger.getLogger(MigratableBody.class.getName()); 46 47 51 52 protected MigrationManager migrationManager; 53 54 55 protected transient boolean hasJustMigrated; 56 57 public MigratableBody() { 61 } 62 63 public MigratableBody(Object reifiedObject, String nodeURL, 64 MetaObjectFactory factory, String jobID) { 65 super(reifiedObject, nodeURL, factory, jobID); 66 this.migrationManager = factory.newMigrationManagerFactory() 67 .newMigrationManager(); 68 } 69 70 public UniversalBody migrateTo(Node node) throws MigrationException { 77 return internalMigrateTo(node, false); 78 } 79 80 public UniversalBody cloneTo(Node node) throws MigrationException { 81 return internalMigrateTo(node, true); 82 } 83 84 public void addMigrationEventListener(MigrationEventListener listener) { 85 if (migrationManager != null) { 86 migrationManager.addMigrationEventListener(listener); 87 } 88 } 89 90 public void removeMigrationEventListener(MigrationEventListener listener) { 91 if (migrationManager != null) { 92 migrationManager.removeMigrationEventListener(listener); 93 } 94 } 95 96 100 103 protected void activityStarted() { 104 super.activityStarted(); 105 106 if (logger.isDebugEnabled()) { 107 logger.debug("Body run on node " + nodeURL + " migration=" + 108 hasJustMigrated); 109 } 110 if (hasJustMigrated) { 111 if (migrationManager != null) { 112 migrationManager.startingAfterMigration(this); 113 } 114 hasJustMigrated = false; 115 } 116 } 117 118 protected UniversalBody internalMigrateTo(Node node, boolean byCopy) 122 throws MigrationException { 123 if (!isAlive()) { 124 throw new MigrationException( 125 "Attempt to migrate a dead body that has been terminated"); 126 } 127 128 if (!isActive()) { 129 throw new MigrationException("Attempt to migrate a non active body"); 130 } 131 132 node = migrationManager.checkNode(node); 134 135 String saveNodeURL = nodeURL; 137 nodeURL = node.getNodeInformation().getURL(); 138 139 blockCommunication(); 141 142 UniqueID savedID = bodyID; 144 145 if (byCopy) { 146 bodyID = null; 149 } 150 151 UniversalBody migratedBody = null; 153 try { 154 migratedBody = migrationManager.migrateTo(node, this); 155 } catch (MigrationException e) { 156 nodeURL = saveNodeURL; 157 bodyID = savedID; 158 localBodyStrategy.getFuturePool().unsetMigrationTag(); 159 acceptCommunication(); 160 throw e; 161 } 162 if (!byCopy) { 163 changeBodyAfterMigration(migratedBody); 164 } else { 165 bodyID = savedID; 166 nodeURL = saveNodeURL; 167 } 168 acceptCommunication(); 169 170 return migratedBody; 171 } 172 173 protected void changeBodyAfterMigration(UniversalBody migratedBody) { 174 requestReceiver = migrationManager.createRequestReceiver(migratedBody, 176 requestReceiver); 177 replyReceiver = migrationManager.createReplyReceiver(migratedBody, 178 replyReceiver); 179 activityStopped(); 180 migrationManager = null; 181 182 hasJustMigrated = true; 184 } 185 186 private void writeObject(java.io.ObjectOutputStream out) 190 throws java.io.IOException { 191 if (logger.isDebugEnabled()) { 192 logger.debug("stream = " + out); 193 } 194 out.defaultWriteObject(); 195 } 196 197 private void readObject(java.io.ObjectInputStream in) 198 throws java.io.IOException , ClassNotFoundException { 199 if (logger.isDebugEnabled()) { 200 logger.debug("stream = " + in); 201 } 202 in.defaultReadObject(); 203 hasJustMigrated = true; 204 } 205 } 206 | Popular Tags |