KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > proactive > ext > implicit > ImplicitService


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.ext.implicit;
32
33 import org.objectweb.proactive.core.UniqueID;
34 import org.objectweb.proactive.core.body.request.BlockingRequestQueueImpl;
35 import org.objectweb.proactive.core.body.request.Request;
36 import org.objectweb.proactive.core.mop.MOP;
37
38 public class ImplicitService extends BlockingRequestQueueImpl implements Implicit {
39
40   /**
41    * The Hashtable for caching (shortcut/Methods) for this reified object
42    */

43   transient protected java.util.Hashtable JavaDoc shortcuts;
44   
45   /**
46    * The Hashtable for caching (name of class / shortcuts table) associations
47    */

48   public final static java.util.Hashtable JavaDoc shortcutsTables = new java.util.Hashtable JavaDoc();
49
50   
51   /**
52    * A hashtable to hold 'forbid' declarations
53    */

54   protected java.util.Hashtable JavaDoc sync;
55
56   public ImplicitService(UniqueID id) {
57     super(id);
58     sync = new java.util.Hashtable JavaDoc();
59   }
60
61
62   /**
63    * Associates a shortcut with a blocking condition.
64    *
65    * The blocking condition is a method that return a boolean.
66    * @throws InvalidAssociateDeclaration if one of the following is met : <UL>
67    * <LI>There is more than one method with name <code>condition</code>
68    * <LI>There is no method with name <code>condition</code>
69    * <LI>There is a method with name condition but it does not return a boolean
70    * <LI>There is a method with name condition but it takes arguments
71    * </UL>
72    */

73   public void forbid(String JavaDoc shortcut, String JavaDoc condition) throws InvalidAssociateDeclaration {
74     java.lang.reflect.Method JavaDoc cond;
75     System.out.println("ImplicitBody: forbid() for " + shortcut);
76
77     if (this.shortcuts == null) {
78       //System.out.println("this.shortcuts = " + this.shortcuts);
79
this.setShortcutsTable();
80     }
81
82     // System.out.println("this.shortcuts = " + this.shortcuts);
83

84     if (!(this.shortcuts.containsKey(shortcut))) {
85       throw new InvalidAssociateDeclaration("No shortcut " + shortcut + " defined");
86     }
87
88     //try {
89
//cond = body.getReifiedObject().getClass().getMethod(condition, argstype);
90
cond = null;
91     //} catch (NoSuchMethodException e) {
92
// throw new InvalidAssociateDeclaration("No method with name " + condition + " and no args");
93
//}
94

95     if (!(cond.getReturnType().equals(java.lang.Boolean.TYPE))) {
96       throw new InvalidAssociateDeclaration("Method " + condition + " does not return a boolean as expected");
97     }
98
99     // The declaration is ok
100
this.sync.put(shortcut, cond);
101     //System.out.println ("New association : "+shortcut+" with condition "+cond.getName());
102

103     return;
104   }
105
106
107   public void run() {
108     /*
109     boolean test = true; // If something fails, do not serve the request !
110     int index;
111
112     System.out.println("ImplicitBody: run()");
113  
114     //locateLiveRoutine("org.objectweb.proactive.Implicit");
115     //launchLive(); // Executes the live(...) routine (reading associations)
116
117     while (body.isActive()) {
118       synchronized (this) {
119         if (isEmpty()) {
120           //System.out.println("XXXXXXXXXXXXXXXXXXWaiting for request");
121           blockingRemoveOldest(); //No requests in line
122           //System.out.println("XXXXXXXXXXXXXXXXXXWaiting over");
123         } else {
124           Request request = getOldestReadyRequest();
125           if (request != null) {
126             serve(request);
127           } else {
128             //System.out.println("XXXXXXXXXXXXXXXXXXWaiting for new request");
129             waitForNewRequest();
130           }
131         }
132       }
133     }*/

134   }
135
136
137   public synchronized Request getOldestReadyRequest() {
138     java.util.Iterator JavaDoc iterator = iterator();
139     while (iterator.hasNext()) {
140       Request r = (Request) iterator.next();
141       java.lang.reflect.Method JavaDoc target = null; //r.getMethodCall().getReifiedMethod();
142
// Find if there is a shortcut for this method
143
String JavaDoc methodName;
144       if (this.shortcuts.contains(target)) {
145         methodName = this.getShortcut(target);
146       } else {
147         methodName = target.getName();
148       }
149       //Is there a blocking condition for this method ?
150
java.lang.reflect.Method JavaDoc cond = (java.lang.reflect.Method JavaDoc) this.sync.get(methodName);
151       if (cond != null) {
152         //System.out.println("Testing condition");
153
boolean test = this.testCondition(cond);
154         //System.out.println("Testing condition on method " + s + " result is " + test);
155
if (test) {
156           return r;
157         }
158       }
159     }
160     return null;
161   }
162
163
164   private boolean testCondition(java.lang.reflect.Method JavaDoc cond) {
165     boolean result = true;
166     Object JavaDoc[] args = new Object JavaDoc[0];
167
168     System.out.println("ImplicitBody: testingCondition() on method" + cond);
169
170     try {
171       result = false; // ((Boolean)cond.invoke(body.getReifiedObject(), args)).booleanValue();
172
} catch (Exception JavaDoc e) {
173       System.err.println("Exception during invocation of method " + cond.getName());
174       e.printStackTrace();
175     }
176     return result;
177   }
178
179
180   public synchronized void serveOldestThatIsNot(String JavaDoc s) {
181     /*
182     java.util.Iterator li = iterator();
183     Request r;
184     while (li.hasNext()) {
185       r = (Request)li.next();
186       if (!s.equals(r.getMethodName())) {
187         serve(r);
188         return;
189       }
190     }
191     */

192   }
193
194
195
196   /**
197    * Dumps the shortcut table to System.out
198    */

199   public void dumpShortcutsTable() {
200     java.util.Enumeration JavaDoc en;
201     String JavaDoc currentshc;
202     java.lang.reflect.Method JavaDoc currentmethod;
203     en = this.shortcuts.keys();
204     System.out.println("--- Dump of the shortcuts table ---");
205     while (en.hasMoreElements()) {
206       currentshc = (String JavaDoc)en.nextElement();
207       currentmethod = (java.lang.reflect.Method JavaDoc)this.shortcuts.get(currentshc);
208       System.out.println(currentshc + "/" + currentmethod.toString());
209     }
210   }
211
212
213   public String JavaDoc getShortcut(java.lang.reflect.Method JavaDoc m) {
214     java.util.Enumeration JavaDoc en;
215     String JavaDoc currentshc;
216     java.lang.reflect.Method JavaDoc currentmethod;
217
218     en = this.shortcuts.keys();
219
220     while (en.hasMoreElements()) {
221       currentshc = (String JavaDoc)en.nextElement();
222       currentmethod = (java.lang.reflect.Method JavaDoc)this.shortcuts.get(currentshc);
223
224       if (currentmethod.equals(m))
225         return currentshc;
226     }
227     return null;
228   }
229
230
231   public void addShortcut(String JavaDoc shortcut, String JavaDoc name, String JavaDoc[] argumentstype) {
232     int index = 0, size;
233     Class JavaDoc[] args;
234     java.lang.reflect.Method JavaDoc met = null;
235
236     // If the shortcut is already a key, do nothing
237
if (this.shortcuts.containsKey(shortcut)) {
238       System.err.println("Shortcut '" + shortcut + "' already exists.");
239       return;
240     }
241
242     size = argumentstype.length;
243     args = new Class JavaDoc[size];
244
245     // Let's find a method with the given arguments
246
try {
247       for (index = 0; index < size; index++) {
248         // Do not deal with primitive types for now
249
args[index] = MOP.forName(argumentstype[index]);
250       }
251       met = null; //body.getReifiedObject().getClass().getMethod(name, args);
252
} catch (ClassNotFoundException JavaDoc e) {
253       System.err.println("Class not found : " + argumentstype[index]);
254       return;
255     }// catch (NoSuchMethodException e) {
256
// System.err.println("No Such Method");
257
// e.printStackTrace();
258
// return;
259
//}
260
this.shortcuts.put(shortcut, met);
261   }
262
263
264   /**
265    * Fills the shortcuts table with all obvious entries.
266    *
267    * An entry is obvious when there is only one method with the that name.
268    */

269
270   public void fillShortcutsTable() {
271     java.lang.reflect.Method JavaDoc[] mets;
272     java.lang.reflect.Method JavaDoc currentmethod;
273     String JavaDoc currentshc;
274     int size, index, index2;
275
276     mets = null;// body.getReifiedObject().getClass().getMethods();
277
size = mets.length;
278
279     for (index = 0; index < size; index++) {
280       currentmethod = mets[index];
281       currentshc = currentmethod.getName();
282
283       //if (body.isOverloaded(currentmethod)) {
284
// If there is another method with the same simple name, do nothing
285
//} else {
286
// put it in hashtable
287
// this.shortcuts.put(currentshc, currentmethod);
288
//}
289
}
290   }
291
292
293   public void setShortcutsTable() {
294     // if there already exists one hashtable for this class, use it
295
// else create it
296

297     String JavaDoc reifiedObjectClassName;
298
299     //this.shortcuts = (java.util.Hashtable)shortcutsTables.get(body.getReifiedObject().getClass());
300

301     if (this.shortcuts == null) {
302       this.shortcuts = new java.util.Hashtable JavaDoc();
303       this.fillShortcutsTable();
304       // On enregistre cette nouvelle table aupres du gestionnaire des tables
305
//shortcutsTables.put(body.getReifiedObject().getClass(), this.shortcuts);
306
} else {
307       // Nothing to do
308
}
309   }
310   
311   
312   /**
313    * Tests if there is another method with the same simple name in the class
314    */

315   public static boolean isOverloaded(java.lang.reflect.Method JavaDoc met, Class JavaDoc reifiedObjectClass) {
316     int n = 0;
317     java.lang.reflect.Method JavaDoc[] mets = reifiedObjectClass.getMethods();
318     int size = mets.length;
319     for (int index = 0; index < size; index++) {
320       java.lang.reflect.Method JavaDoc currentmethod = mets[index];
321       String JavaDoc currentshc = currentmethod.getName();
322       if (currentshc.equals(met.getName()))
323         n++;
324     }
325     return (n > 1);
326   }
327
328
329 }
330
Popular Tags