KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > protoactive > core > body > metaobject > BodyMetaObject


1 /***
2  * Julia: France Telecom's implementation of the Fractal API
3  * Copyright (C) 2001-2002 France Telecom R&D
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  * Contact: Eric.Bruneton@rd.francetelecom.com
20  *
21  * Author: Eric Bruneton
22  */

23
24 package protoactive.core.body.metaobject;
25
26 import org.objectweb.fractal.api.control.LifeCycleController;
27 import org.objectweb.fractal.api.Fractal;
28 import org.objectweb.fractal.api.Component;
29
30 import org.objectweb.fractal.julia.loader.Loader;
31 import org.objectweb.fractal.julia.loader.Tree;
32
33 import protoactive.core.body.future.LocalFuture;
34 import protoactive.core.mop.MetaObject;
35
36 import java.lang.reflect.Method JavaDoc;
37 import java.util.List JavaDoc;
38 import java.util.ArrayList JavaDoc;
39
40 /**
41  * A meta object to handle reified method calls in an asynchronous way. This
42  * meta object is a controller object that also implements the
43  * LifeCycleController interface.
44  */

45
46 public class BodyMetaObject
47   implements LifeCycleController, MetaObject, Runnable JavaDoc
48 {
49
50   private List JavaDoc requests;
51
52   private boolean running;
53
54   private boolean requestStop;
55
56   /**
57    * Creates a future object to wait for the reply of the given call, and
58    * push the reified method call and its associated future in the request
59    * list. Returns the future object immediately.
60    *
61    * @param itf the name of the Fractal interface that is called.
62    * @param target the base object.
63    * @param m the method that is called.
64    * @param args the method call arguments.
65    * @return the result of the method call, as a future object.
66    */

67
68   public Object JavaDoc handleMethodCall (
69     final String JavaDoc itf,
70     final Object JavaDoc target,
71     final Method JavaDoc m,
72     final Object JavaDoc[] args)
73   {
74     Request r = new Request();
75     r.target = target;
76     r.m = m;
77     r.args = args;
78
79     Class JavaDoc c = m.getReturnType();
80     if (c == Void.TYPE) {
81       r.future = null;
82     } else {
83       try {
84         Component boot = Fractal.getBootstrapComponent();
85         Loader loader = (Loader)boot.getFcInterface("loader");
86         r.future = (LocalFuture)loader.newObject(
87           new Tree(new Tree[] {
88             new Tree(new Tree[] {
89               new Tree("protoactive.core.body.future.FutureClassGenerator"),
90               new Tree("protoactive.core.body.future.LocalFuture"),
91               new Tree(new Tree[] { new Tree(c.getName()) })
92             })
93           }), null
94         );
95       } catch (Exception JavaDoc e) {
96         e.printStackTrace();
97         throw new Error JavaDoc();
98       }
99     }
100
101     // put request into request list
102
synchronized (this) {
103       if (requests == null) {
104        requests = new ArrayList JavaDoc();
105       }
106       requests.add(r);
107       notifyAll();
108     }
109
110     // return the future object
111
return r.future;
112   }
113
114   public boolean isReflectedCall () {
115     Class JavaDoc[] stack = Helper.INSTANCE.getClassContext();
116     return
117       stack.length > 3 &&
118       stack[3].getName().equals("protoactive.core.body.metaobject.Request");
119   }
120
121   public String JavaDoc getFcState () {
122     return (running ? STARTED : STOPPED);
123   }
124
125   public void startFc () {
126     synchronized (this) {
127       if (!running) {
128         // if not already running, start a thread to handle requests
129
running = true;
130         requestStop = false;
131         new Thread JavaDoc(this).start();
132       }
133     }
134   }
135
136   public void stopFc () {
137     synchronized (this) {
138       // ask the service thread to stop
139
requestStop = true;
140       notifyAll();
141       // wait until is effectively stopped
142
while (running) {
143         try {
144           wait();
145         } catch (InterruptedException JavaDoc e) {
146         }
147       }
148     }
149   }
150
151   /**
152    * Code of the thread that handles requests in FIFO order. This thread
153    * gets a request from the request list, executes the corresponding method
154    * call, and puts the result in the associated future object. It then
155    * gets another request, and so on until the 'requestStop' flag is true. It
156    * then sets the 'running' flag to 'false' and stops itself.
157    */

158
159   public void run () {
160     while (true) {
161       // get a request from request list
162
Request r;
163       synchronized (this) {
164         // block until there is at least one request in the queue,
165
// or requestStop is true
166
while ((requests == null || requests.size() == 0) && !requestStop) {
167           try {
168             wait();
169           } catch (InterruptedException JavaDoc e) {
170           }
171         }
172         if (!requestStop) {
173           r = (Request)requests.remove(0);
174         } else {
175           // we are asked to stop, so we stop ourselves, and notify others that
176
// we have stopped ourselves.
177
running = false;
178           notifyAll();
179           return;
180         }
181       }
182       // respond to the request
183
System.err.println(
184         Thread.currentThread() + " HANDLE REQUEST " + r);
185       Object JavaDoc reply = r.invoke();
186       if (r.future != null) {
187         r.future.receiveReply(reply);
188       }
189     }
190   }
191
192   public static class Helper extends SecurityManager JavaDoc {
193
194     public final static Helper INSTANCE = new Helper();
195
196     public Class JavaDoc[] getClassContext() {
197       return super.getClassContext();
198     }
199   }
200 }
201
Popular Tags