KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > proactive > core > body > ActiveBody


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;
32
33
34 /**
35  * This class is the default implementation of the Body interface.
36  * An implementation of the Body interface, which lets the reified object
37  * explicitely manage the queue of pending requests through its live() routine.
38  *
39  * @author ProActive Team
40  * @version 1.0, 2001/10/23
41  * @since ProActive 0.9
42  * @see org.objectweb.proactive.Body
43  * @see AbstractBody
44  * @see org.objectweb.proactive.core.body.migration.MigratableBody
45  *
46  */

47 import org.apache.log4j.Logger;
48 import org.objectweb.proactive.Active;
49 import org.objectweb.proactive.Body;
50 import org.objectweb.proactive.EndActive;
51 import org.objectweb.proactive.InitActive;
52 import org.objectweb.proactive.RunActive;
53 import org.objectweb.proactive.core.body.migration.MigratableBody;
54 import org.objectweb.proactive.core.mop.ConstructorCall;
55 import org.objectweb.proactive.core.mop.ConstructorCallExecutionFailedException;
56
57
58 public class ActiveBody extends MigratableBody implements Runnable JavaDoc,
59     java.io.Serializable JavaDoc {
60     protected static Logger logger = Logger.getLogger(ActiveBody.class.getName());
61
62     //
63
// -- STATIC MEMBERS -----------------------------------------------
64
//
65
//
66
// -- PROTECTED MEMBERS -----------------------------------------------
67
//
68
//
69
// -- PRIVATE MEMBERS -----------------------------------------------
70
//
71
private transient InitActive initActive; // used only once when active object is started first time
72
private RunActive runActive;
73     private EndActive endActive;
74
75     //
76
// -- CONSTRUCTORS -----------------------------------------------
77
//
78

79     /**
80      * Doesn't build anything, just for having one no-arg constructor
81      */

82     public ActiveBody() {
83     }
84
85     /**
86      * Build the body object, then fires its service thread
87      */

88     public ActiveBody(ConstructorCall c, String JavaDoc nodeURL, Active activity,
89         MetaObjectFactory factory, String JavaDoc jobID)
90         throws java.lang.reflect.InvocationTargetException JavaDoc,
91             ConstructorCallExecutionFailedException {
92         // Creates the reified object
93
super(c.execute(), nodeURL, factory, jobID);
94
95         // InitActive
96
Object JavaDoc reifiedObject = localBodyStrategy.getReifiedObject();
97         if ((activity != null) && activity instanceof InitActive) {
98             initActive = (InitActive) activity;
99         } else if (reifiedObject instanceof InitActive) {
100             initActive = (InitActive) reifiedObject;
101         }
102
103         // RunActive
104
if ((activity != null) && activity instanceof RunActive) {
105             runActive = (RunActive) activity;
106         } else if (reifiedObject instanceof RunActive) {
107             runActive = (RunActive) reifiedObject;
108         } else {
109             runActive = new FIFORunActive();
110         }
111
112         // EndActive
113
if ((activity != null) && activity instanceof EndActive) {
114             endActive = (EndActive) activity;
115         } else if (reifiedObject instanceof EndActive) {
116             endActive = (EndActive) reifiedObject;
117         } else {
118             endActive = null;
119         }
120
121         startBody();
122     }
123
124     //
125
// -- PUBLIC METHODS -----------------------------------------------
126
//
127
//
128
// -- implements Runnable -----------------------------------------------
129
//
130

131     /**
132      * The method executed by the active thread that will eventually launch the live
133      * method of the active object of the default live method of this body.
134      */

135     public void run() {
136         activityStarted();
137         // execute the initialization if needed. Only once
138
if (initActive != null) {
139             initActive.initActivity(this);
140             initActive = null; // we won't do it again
141
}
142
143         // run the activity of the body
144
try {
145             runActive.runActivity(this);
146             // the body terminate its activity
147
if (isActive()) {
148                 // serve remaining requests if non dead
149
while (!(localBodyStrategy.getRequestQueue().isEmpty())) {
150                     serve(localBodyStrategy.getRequestQueue().removeOldest());
151                 }
152             }
153         } catch (Exception JavaDoc e) {
154             
155             logger.error("Exception occured in runActivity method of body " +
156                     toString() + ". Now terminating the body");
157            
158             e.printStackTrace();
159             terminate();
160         } finally {
161             if (isActive()) {
162                 activityStopped();
163             }
164
165             // execute the end of activity if not after migration
166
if ((!hasJustMigrated) && (endActive != null)) {
167                 endActive.endActivity(this);
168             }
169         }
170     }
171
172     //
173
// -- PROTECTED METHODS -----------------------------------------------
174
//
175

176     /**
177      * Creates the active thread and start it using this runnable body.
178      */

179     protected void startBody() {
180         if (logger.isDebugEnabled()) {
181             logger.debug("Starting Body");
182         }
183         Thread JavaDoc t = new Thread JavaDoc(this,
184                 shortClassName(getName()) + " on " + getNodeURL());
185         t.start();
186     }
187
188     /**
189      * Signals that the activity of this body, managed by the active thread has just stopped.
190      */

191     protected void activityStopped() {
192         super.activityStopped();
193         runActive = null;
194     }
195
196     //
197
// -- PRIVATE METHODS -----------------------------------------------
198
//
199
private static String JavaDoc shortClassName(String JavaDoc fqn) {
200         int n = fqn.lastIndexOf('.');
201         if ((n == -1) || (n == (fqn.length() - 1))) {
202             return fqn;
203         }
204         return fqn.substring(n + 1);
205     }
206
207     private void writeObject(java.io.ObjectOutputStream JavaDoc out)
208         throws java.io.IOException JavaDoc {
209         if (logger.isDebugEnabled()) {
210             logger.debug("out = " + out);
211         }
212         out.defaultWriteObject();
213     }
214
215     private void readObject(java.io.ObjectInputStream JavaDoc in)
216         throws java.io.IOException JavaDoc, ClassNotFoundException JavaDoc {
217         if (logger.isDebugEnabled()) {
218             logger.debug("in = " + in);
219         }
220         in.defaultReadObject();
221         startBody();
222     }
223
224     //
225
// -- INNER CLASSES -----------------------------------------------
226
//
227
private class FIFORunActive implements RunActive, java.io.Serializable JavaDoc {
228         public void runActivity(Body body) {
229             while (isActive()) {
230                 serve(localBodyStrategy.getRequestQueue().blockingRemoveOldest());
231             }
232         }
233     }
234 }
235
Popular Tags