KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > proactive > core > body > migration > MigratableBody


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.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 JavaDoc {
45     protected static Logger logger = Logger.getLogger(MigratableBody.class.getName());
46
47     //
48
// -- PROTECTED MEMBERS -----------------------------------------------
49
//
50

51     /** The object responsible for the migration */
52     protected MigrationManager migrationManager;
53
54     /** signal that the body has just migrated */
55     protected transient boolean hasJustMigrated;
56
57     //
58
// -- CONSTRUCTORS -----------------------------------------------
59
//
60
public MigratableBody() {
61     }
62
63     public MigratableBody(Object JavaDoc reifiedObject, String JavaDoc nodeURL,
64         MetaObjectFactory factory, String JavaDoc jobID) {
65         super(reifiedObject, nodeURL, factory, jobID);
66         this.migrationManager = factory.newMigrationManagerFactory()
67                                        .newMigrationManager();
68     }
69
70     //
71
// -- PUBLIC METHODS -----------------------------------------------
72
//
73
//
74
// -- implements Migratable -----------------------------------------------
75
//
76
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     //
97
// -- PROTECTED METHODS -----------------------------------------------
98
//
99

100     /**
101      * Signals that the activity of this body, managed by the active thread has just started.
102      */

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     //
119
// -- PRIVATE METHODS -----------------------------------------------
120
//
121
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         // check node with Manager
133
node = migrationManager.checkNode(node);
134
135         // get the name of the node
136
String JavaDoc saveNodeURL = nodeURL;
137         nodeURL = node.getNodeInformation().getURL();
138
139         // stop accepting communication
140
blockCommunication();
141
142         // save the id
143
UniqueID savedID = bodyID;
144
145         if (byCopy) {
146             // if moving by copy we have to create a new unique ID
147
// the bodyID will be automatically recreate when deserialized
148
bodyID = null;
149         }
150
151         // try to migrate
152
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         // cleanup after migration
175
requestReceiver = migrationManager.createRequestReceiver(migratedBody,
176                 requestReceiver);
177         replyReceiver = migrationManager.createReplyReceiver(migratedBody,
178                 replyReceiver);
179         activityStopped();
180         migrationManager = null;
181
182         // signal that this body (remaining after migration) has just migrated
183
hasJustMigrated = true;
184     }
185
186     //
187
// -- SERIALIZATION METHODS -----------------------------------------------
188
//
189
private void writeObject(java.io.ObjectOutputStream JavaDoc out)
190         throws java.io.IOException JavaDoc {
191         if (logger.isDebugEnabled()) {
192             logger.debug("stream = " + out);
193         }
194         out.defaultWriteObject();
195     }
196
197     private void readObject(java.io.ObjectInputStream JavaDoc in)
198         throws java.io.IOException JavaDoc, ClassNotFoundException JavaDoc {
199         if (logger.isDebugEnabled()) {
200             logger.debug("stream = " + in);
201         }
202         in.defaultReadObject();
203         hasJustMigrated = true;
204     }
205 }
206
Popular Tags