org/objectweb/proactive/core/body/http/util/messages/ReflectRequest.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.core.body.http.util.messages;
00032 
00033 import java.lang.reflect.Method;
00034 import java.util.ArrayList;
00035 import java.util.HashMap;
00036 
00037 import org.apache.log4j.Logger;
00038 import org.objectweb.proactive.core.body.http.util.HttpMessage;
00039 import org.objectweb.proactive.core.util.log.Loggers;
00040 import org.objectweb.proactive.core.util.log.ProActiveLogger;
00041 
00042 
00046 public abstract class ReflectRequest extends HttpMessage {
00047     private static Logger logger = ProActiveLogger.getLogger(Loggers.HTTP_TRANSPORT);
00048 
00049     public ReflectRequest(String url) {
00050         super(url);
00051     }
00052 
00058     protected static HashMap<String, Object> getHashMapReflect(Class theclass) {
00059         // init the hashmap, that contains all the methods of  ProActiveRuntimeImpl 
00060         // in 'Object' (value) and the name of funtions in key 
00061         // (Warning two functions can t have the same name (for now)) 
00062         Method[] allmethods = theclass.getMethods();
00063         int numberOfMethods = allmethods.length;
00064         HashMap<String, Object> hMapMethods = new HashMap<String, Object>(numberOfMethods);
00065 
00066         for (int i = 0; i < numberOfMethods; i++) {
00067             String methodname = allmethods[i].getName();
00068 
00069             if (hMapMethods.containsKey(methodname)) {
00070                 Object obj = hMapMethods.get(methodname);
00071 
00072                 if (!(obj instanceof ArrayList)) {
00073                     ArrayList<Method> array = new ArrayList<Method>();
00074                     array.add((Method) obj);
00075                     array.add(allmethods[i]);
00076                     hMapMethods.put(methodname, array);
00077                 } else {
00078                     ((ArrayList<Method>) obj).add(allmethods[i]);
00079                     hMapMethods.put(methodname, (ArrayList<Method>) obj);
00080                 }
00081             } else {
00082                 hMapMethods.put(methodname, allmethods[i]);
00083             }
00084         }
00085 
00086         return hMapMethods;
00087     }
00088 
00096     protected Method getProActiveRuntimeMethod(String methodsearch,
00097         ArrayList paramsearch, Object hashobjet) {
00098         Object mret = hashobjet;
00099 
00100         if (mret instanceof ArrayList) {
00101             ArrayList allSameMethod = (ArrayList) ((ArrayList) mret).clone();
00102 
00103             int sameMethodSize = allSameMethod.size();
00104             int paramsearchsize = paramsearch.size();
00105 
00106             for (int i = sameMethodSize - 1; i >= 0; i--) {
00107                 if (((Method) allSameMethod.get(i)).getParameterTypes().length != paramsearchsize) {
00108                     allSameMethod.remove(i);
00109                 }
00110             }
00111 
00112             sameMethodSize = allSameMethod.size();
00113 
00114             if (sameMethodSize == 1) {
00115                 mret = allSameMethod.get(0);
00116             } else {
00117                 Class[] paramtypes = null;
00118                 boolean isgood = true;
00119                 boolean ispossible = true;
00120 
00121                 for (int i = sameMethodSize - 1; i >= 0; i--) {
00122                     paramtypes = ((Method) allSameMethod.get(i)).getParameterTypes();
00123 
00124                     for (int j = 0; j < paramsearchsize; j++) {
00125                         Class<? extends Object> classtest = paramsearch.get(j).getClass();
00126 
00127                         if (paramtypes[j] != classtest) {
00128                             isgood = false;
00129 
00130                             if (classtest.isAssignableFrom(paramtypes[j]) == false) {
00131                                 ispossible = false;
00132 
00133                                 break;
00134                             }
00135                         }
00136                     }
00137 
00138                     if (isgood == true) {
00139                         mret = allSameMethod.get(i);
00140 
00141                         break;
00142                     } else if (ispossible == false) {
00143                         allSameMethod.remove(i);
00144                     }
00145 
00146                     isgood = true;
00147                     ispossible = true;
00148                 }
00149             }
00150 
00151             if (allSameMethod.size() == 1) {
00152                 mret = allSameMethod.get(0);
00153             } else {
00154                 logger.error(
00155                     "----------------------------------------------------------------------------");
00156                 logger.error(
00157                     "----- ERROR : two functions in ProActiveRuntimeImpl can t have the same name");
00158                 logger.error(
00159                     "----- ERROR : and the same type of paramters (Extends Implements)");
00160                 logger.error("----- search   : " + methodsearch + " nb param " +
00161                     paramsearch.size());
00162                 logger.error(
00163                     "----------------------------------------------------------------------------");
00164             }
00165         } else if (mret == null) {
00166             logger.error(
00167                 "----------------------------------------------------------------------------");
00168             logger.error("----- ERROR : no method (invoke) find ");
00169             logger.error("----- search   : " + methodsearch + " nb param " +
00170                 paramsearch.size());
00171             logger.error(
00172                 "----------------------------------------------------------------------------");
00173         }
00174 
00175         return (Method) mret;
00176     }
00177 }

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