org/objectweb/proactive/core/body/request/RequestReceiverImpl.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.request;
00032 
00033 import java.io.IOException;
00034 import java.util.ArrayList;
00035 import java.util.Arrays;
00036 import java.util.Hashtable;
00037 import java.util.Iterator;
00038 import java.util.List;
00039 
00040 import org.apache.log4j.Logger;
00041 import org.objectweb.proactive.Body;
00042 import org.objectweb.proactive.core.body.ft.protocols.FTManager;
00043 import org.objectweb.proactive.core.util.log.Loggers;
00044 import org.objectweb.proactive.core.util.log.ProActiveLogger;
00045 
00046 
00047 public class RequestReceiverImpl implements RequestReceiver,
00048     java.io.Serializable {
00049     public static Logger logger = ProActiveLogger.getLogger(Loggers.REQUESTS);
00050     private static final String ANY_PARAMETERS = "any-parameters";
00051 
00052     //private java.util.Vector immediateServices;
00053     // refactored : keys are method names, and values are arrays of parameters types
00054     // map of immediate services (method names +lists of method parameters)
00055     private java.util.Map<String, Object> immediateServices;
00056 
00057     public RequestReceiverImpl() {
00058         immediateServices = new Hashtable<String, Object>(2);
00059         immediateServices.put("toString", ANY_PARAMETERS);
00060         immediateServices.put("hashCode", ANY_PARAMETERS);
00061         immediateServices.put("_terminateAOImmediately", ANY_PARAMETERS);
00062     }
00063 
00064     public int receiveRequest(Request request, Body bodyReceiver)
00065         throws java.io.IOException {
00066         try {
00067             if (immediateExecution(request)) {
00068                 if (logger.isDebugEnabled()) {
00069                     logger.debug("immediately serving " +
00070                         request.getMethodName());
00071                 }
00072                 bodyReceiver.serve(request);
00073                 if (logger.isDebugEnabled()) {
00074                     logger.debug("end of service for " +
00075                         request.getMethodName());
00076                 }
00077 
00078                 //Dummy value for immediate services...
00079                 return FTManager.IMMEDIATE_SERVICE;
00080             } else {
00081                 request.notifyReception(bodyReceiver);
00082                 return bodyReceiver.getRequestQueue().add(request);
00083             }
00084         } catch (Exception e) {
00085             e.printStackTrace();
00086             return 0;
00087         }
00088     }
00089 
00090     private boolean immediateExecution(Request request) {
00091         if ((request == null) || (request.getMethodCall() == null) ||
00092                 (request.getMethodCall().getReifiedMethod() == null)) {
00093             return false;
00094         } else {
00095             String methodName = request.getMethodName();
00096             if (immediateServices.containsKey(methodName)) {
00097                 if (ANY_PARAMETERS.equals(immediateServices.get(methodName))) {
00098                     // method was registered using method name only
00099                     return true;
00100                 } else {
00101                     Iterator it = ((List) immediateServices.get(methodName)).iterator();
00102                     while (it.hasNext()) {
00103                         Class[] next = (Class[]) it.next();
00104                         if (Arrays.equals(next,
00105                                     request.getMethodCall().getReifiedMethod()
00106                                                .getParameterTypes())) {
00107                             return true;
00108                         }
00109                     }
00110 
00111                     // not found
00112                     return false;
00113                 }
00114             } else {
00115                 return false;
00116             }
00117         }
00118     }
00119 
00120     public void setImmediateService(String methodName) {
00121         this.immediateServices.put(methodName, ANY_PARAMETERS);
00122     }
00123 
00124     public void removeImmediateService(String methodName,
00125         Class[] parametersTypes) throws IOException {
00126         if (immediateServices.containsKey(methodName)) {
00127             if (!ANY_PARAMETERS.equals(immediateServices.get(methodName))) {
00128                 List<Class[]> list = (List<Class[]>) immediateServices.get(methodName);
00129                 List<Class[]> elementsToRemove = new ArrayList<Class[]>(list.size());
00130                 Iterator<Class[]> it = list.iterator();
00131                 while (it.hasNext()) {
00132                     Class[] element = it.next();
00133                     if (Arrays.equals(element, parametersTypes)) {
00134                         // cannot modify a list while iterating over it => keep reference of 
00135                         // the elements to remove
00136                         elementsToRemove.add(element);
00137                     }
00138                 }
00139                 it = elementsToRemove.iterator();
00140                 while (it.hasNext()) {
00141                     list.remove(it.next());
00142                 }
00143             } else {
00144                 immediateServices.remove(methodName);
00145             }
00146         } else {
00147             // methodName not registered
00148         }
00149     }
00150 
00151     public void setImmediateService(String methodName, Class[] parametersTypes)
00152         throws IOException {
00153         if (immediateServices.containsKey(methodName)) {
00154             if (ANY_PARAMETERS.equals(immediateServices.get(methodName))) {
00155                 // there is already a filter on all methods with that name, whatever the parameters
00156                 return;
00157             } else {
00158                 ((List<Class[]>) immediateServices.get(methodName)).add(parametersTypes);
00159             }
00160         } else {
00161             List<Class[]> list = new ArrayList<Class[]>();
00162             list.add(parametersTypes);
00163             immediateServices.put(methodName, list);
00164         }
00165     }
00166 }

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