org/objectweb/proactive/ext/implicit/ImplicitService.java

00001 /* 
00002  * ################################################################
00003  * 
00004  * ProActive: The Java(TM) library for Parallel, Distributed, 
00005  *            Concurrent computing with Security and Mobility
00006  * 
00007  * Copyright (C) 1997-2007 INRIA/University of Nice-Sophia Antipolis
00008  * Contact: proactive@objectweb.org
00009  * 
00010  * This library is free software; you can redistribute it and/or
00011  * modify it under the terms of the GNU Lesser General Public
00012  * License as published by the Free Software Foundation; either
00013  * version 2.1 of the License, or any later version.
00014  *  
00015  * This library is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018  * Lesser General Public License for more details.
00019  * 
00020  * You should have received a copy of the GNU Lesser General Public
00021  * License along with this library; if not, write to the Free Software
00022  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
00023  * USA
00024  *  
00025  *  Initial developer(s):               The ProActive Team
00026  *                        http://www.inria.fr/oasis/ProActive/contacts.html
00027  *  Contributor(s): 
00028  * 
00029  * ################################################################
00030  */ 
00031 package org.objectweb.proactive.ext.implicit;
00032 
00033 import org.objectweb.proactive.core.UniqueID;
00034 import org.objectweb.proactive.core.body.request.BlockingRequestQueueImpl;
00035 import org.objectweb.proactive.core.body.request.Request;
00036 import org.objectweb.proactive.core.mop.MOP;
00037 
00038 
00039 public class ImplicitService extends BlockingRequestQueueImpl
00040     implements Implicit {
00041 
00045     transient protected java.util.Hashtable shortcuts;
00046 
00050     public final static java.util.Hashtable shortcutsTables = new java.util.Hashtable();
00051 
00055     protected java.util.Hashtable sync;
00056 
00057     public ImplicitService(UniqueID id) {
00058         super(id);
00059         sync = new java.util.Hashtable();
00060     }
00061 
00073     public void forbid(String shortcut, String condition)
00074         throws InvalidAssociateDeclaration {
00075         java.lang.reflect.Method cond;
00076         System.out.println("ImplicitBody: forbid() for " + shortcut);
00077 
00078         if (this.shortcuts == null) {
00079             //System.out.println("this.shortcuts = " + this.shortcuts);
00080             this.setShortcutsTable();
00081         }
00082 
00083         //      System.out.println("this.shortcuts = " + this.shortcuts);
00084         if (!(this.shortcuts.containsKey(shortcut))) {
00085             throw new InvalidAssociateDeclaration("No shortcut " + shortcut +
00086                 " defined");
00087         }
00088 
00089         //try {
00090         //cond = body.getReifiedObject().getClass().getMethod(condition, argstype);
00091         cond = null;
00092         //} catch (NoSuchMethodException e) {
00093         //  throw new InvalidAssociateDeclaration("No method with name " + condition + " and no args");
00094         //}
00095         if (!(cond.getReturnType().equals(java.lang.Boolean.TYPE))) {
00096             throw new InvalidAssociateDeclaration("Method " + condition +
00097                 " does not return a boolean as expected");
00098         }
00099 
00100         // The declaration is ok
00101         this.sync.put(shortcut, cond);
00102         //System.out.println ("New association : "+shortcut+" with condition "+cond.getName());
00103         return;
00104     }
00105 
00106     public void run() {
00107 
00108         /*
00109            boolean test = true; // If something fails, do not serve the request !
00110            int index;
00111            System.out.println("ImplicitBody: run()");
00112 
00113            //locateLiveRoutine("org.objectweb.proactive.Implicit");
00114            //launchLive();  // Executes the live(...) routine (reading associations)
00115            while (body.isActive()) {
00116              synchronized (this) {
00117                if (isEmpty()) {
00118                  //System.out.println("XXXXXXXXXXXXXXXXXXWaiting for request");
00119                  blockingRemoveOldest(); //No requests in line
00120                  //System.out.println("XXXXXXXXXXXXXXXXXXWaiting over");
00121                } else {
00122                  Request request = getOldestReadyRequest();
00123                  if (request != null) {
00124                    serve(request);
00125                  } else {
00126                    //System.out.println("XXXXXXXXXXXXXXXXXXWaiting for new request");
00127                    waitForNewRequest();
00128                  }
00129                }
00130              }
00131            }*/
00132     }
00133 
00134     public synchronized Request getOldestReadyRequest() {
00135         java.util.Iterator iterator = iterator();
00136         while (iterator.hasNext()) {
00137             Request r = (Request) iterator.next();
00138             java.lang.reflect.Method target = null; //r.getMethodCall().getReifiedMethod();
00139 
00140             // Find if there is a shortcut for this method
00141             String methodName;
00142             if (this.shortcuts.contains(target)) {
00143                 methodName = this.getShortcut(target);
00144             } else {
00145                 methodName = target.getName();
00146             }
00147 
00148             //Is there a blocking condition for this method ?
00149             java.lang.reflect.Method cond = (java.lang.reflect.Method) this.sync.get(methodName);
00150             if (cond != null) {
00151                 //System.out.println("Testing condition");
00152                 boolean test = this.testCondition(cond);
00153 
00154                 //System.out.println("Testing condition on method " +  s + "  result is " + test);
00155                 if (test) {
00156                     return r;
00157                 }
00158             }
00159         }
00160         return null;
00161     }
00162 
00163     private boolean testCondition(java.lang.reflect.Method cond) {
00164         boolean result = true;
00165         Object[] args = new Object[0];
00166 
00167         System.out.println("ImplicitBody: testingCondition() on method" + cond);
00168 
00169         try {
00170             result = false; // ((Boolean)cond.invoke(body.getReifiedObject(), args)).booleanValue();
00171         } catch (Exception e) {
00172             System.err.println("Exception during invocation of method " +
00173                 cond.getName());
00174             e.printStackTrace();
00175         }
00176         return result;
00177     }
00178 
00179     public synchronized void serveOldestThatIsNot(String s) {
00180 
00181         /*
00182            java.util.Iterator li = iterator();
00183            Request r;
00184            while (li.hasNext()) {
00185              r = (Request)li.next();
00186              if (!s.equals(r.getMethodName())) {
00187                serve(r);
00188                return;
00189              }
00190            }
00191          */
00192     }
00193 
00197     public void dumpShortcutsTable() {
00198         java.util.Enumeration en;
00199         String currentshc;
00200         java.lang.reflect.Method currentmethod;
00201         en = this.shortcuts.keys();
00202         System.out.println("--- Dump of the shortcuts table ---");
00203         while (en.hasMoreElements()) {
00204             currentshc = (String) en.nextElement();
00205             currentmethod = (java.lang.reflect.Method) this.shortcuts.get(currentshc);
00206             System.out.println(currentshc + "/" + currentmethod.toString());
00207         }
00208     }
00209 
00210     public String getShortcut(java.lang.reflect.Method m) {
00211         java.util.Enumeration en;
00212         String currentshc;
00213         java.lang.reflect.Method currentmethod;
00214 
00215         en = this.shortcuts.keys();
00216 
00217         while (en.hasMoreElements()) {
00218             currentshc = (String) en.nextElement();
00219             currentmethod = (java.lang.reflect.Method) this.shortcuts.get(currentshc);
00220 
00221             if (currentmethod.equals(m)) {
00222                 return currentshc;
00223             }
00224         }
00225         return null;
00226     }
00227 
00228     public void addShortcut(String shortcut, String name, String[] argumentstype) {
00229         int index = 0;
00230         int size;
00231         Class[] args;
00232         java.lang.reflect.Method met = null;
00233 
00234         // If the shortcut is already a key, do nothing
00235         if (this.shortcuts.containsKey(shortcut)) {
00236             System.err.println("Shortcut '" + shortcut + "' already exists.");
00237             return;
00238         }
00239 
00240         size = argumentstype.length;
00241         args = new Class[size];
00242 
00243         // Let's find a method with the given arguments
00244         try {
00245             for (index = 0; index < size; index++) {
00246                 // Do not deal with primitive types for now
00247                 args[index] = MOP.forName(argumentstype[index]);
00248             }
00249             met = null; //body.getReifiedObject().getClass().getMethod(name, args);
00250         } catch (ClassNotFoundException e) {
00251             System.err.println("Class not found : " + argumentstype[index]);
00252             return;
00253         } // catch (NoSuchMethodException e) {
00254 
00255         //  System.err.println("No Such Method");
00256         //  e.printStackTrace();
00257         //  return;
00258         //}
00259         this.shortcuts.put(shortcut, met);
00260     }
00261 
00267     public void fillShortcutsTable() {
00268         java.lang.reflect.Method[] mets;
00269         java.lang.reflect.Method currentmethod;
00270         String currentshc;
00271         int size;
00272         int index;
00273         int index2;
00274 
00275         mets = null; // body.getReifiedObject().getClass().getMethods();
00276         size = mets.length;
00277 
00278         for (index = 0; index < size; index++) {
00279             currentmethod = mets[index];
00280             currentshc = currentmethod.getName();
00281 
00282             //if (body.isOverloaded(currentmethod)) {
00283             // If there is another method with the same simple name, do nothing
00284             //} else {
00285             // put it in hashtable
00286             // this.shortcuts.put(currentshc, currentmethod);
00287             //}
00288         }
00289     }
00290 
00291     public void setShortcutsTable() {
00292         // if there already exists one hashtable for this class, use it
00293         // else create it 
00294         String reifiedObjectClassName;
00295 
00296         //this.shortcuts = (java.util.Hashtable)shortcutsTables.get(body.getReifiedObject().getClass());
00297         if (this.shortcuts == null) {
00298             this.shortcuts = new java.util.Hashtable();
00299             this.fillShortcutsTable();
00300             // On enregistre cette nouvelle table aupres du gestionnaire des tables
00301             //shortcutsTables.put(body.getReifiedObject().getClass(), this.shortcuts);
00302         } else {
00303             // Nothing to do
00304         }
00305     }
00306 
00310     public static boolean isOverloaded(java.lang.reflect.Method met,
00311         Class reifiedObjectClass) {
00312         int n = 0;
00313         java.lang.reflect.Method[] mets = reifiedObjectClass.getMethods();
00314         int size = mets.length;
00315         for (int index = 0; index < size; index++) {
00316             java.lang.reflect.Method currentmethod = mets[index];
00317             String currentshc = currentmethod.getName();
00318             if (currentshc.equals(met.getName())) {
00319                 n++;
00320             }
00321         }
00322         return (n > 1);
00323     }
00324 }

Generated on Mon Jan 22 15:16:10 2007 for ProActive by  doxygen 1.5.1