org/objectweb/proactive/core/component/request/ComponentRequestImpl.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.component.request;
00032 
00033 import java.io.IOException;
00034 import java.io.Serializable;
00035 import java.util.Iterator;
00036 import java.util.List;
00037 import java.util.ListIterator;
00038 
00039 import org.apache.log4j.Logger;
00040 import org.objectweb.fractal.api.NoSuchInterfaceException;
00041 import org.objectweb.fractal.api.control.LifeCycleController;
00042 import org.objectweb.proactive.Body;
00043 import org.objectweb.proactive.core.body.UniversalBody;
00044 import org.objectweb.proactive.core.body.future.FutureResult;
00045 import org.objectweb.proactive.core.body.request.Request;
00046 import org.objectweb.proactive.core.body.request.RequestImpl;
00047 import org.objectweb.proactive.core.body.request.ServeException;
00048 import org.objectweb.proactive.core.component.Constants;
00049 import org.objectweb.proactive.core.component.Fractive;
00050 import org.objectweb.proactive.core.component.ProActiveInterface;
00051 import org.objectweb.proactive.core.component.body.ComponentBody;
00052 import org.objectweb.proactive.core.component.body.ComponentBodyImpl;
00053 import org.objectweb.proactive.core.component.controller.ComponentParametersController;
00054 import org.objectweb.proactive.core.component.identity.ProActiveComponentImpl;
00055 import org.objectweb.proactive.core.component.interception.InputInterceptor;
00056 import org.objectweb.proactive.core.component.representative.ItfID;
00057 import org.objectweb.proactive.core.component.type.ProActiveInterfaceType;
00058 import org.objectweb.proactive.core.mop.MethodCall;
00059 import org.objectweb.proactive.core.mop.MethodCallExecutionFailedException;
00060 import org.objectweb.proactive.core.util.log.Loggers;
00061 import org.objectweb.proactive.core.util.log.ProActiveLogger;
00062 
00063 
00075 public class ComponentRequestImpl extends RequestImpl
00076     implements ComponentRequest, Serializable {
00077     protected static Logger logger = ProActiveLogger.getLogger(Loggers.COMPONENTS_REQUESTS);
00078 
00079     //private int shortcutCounter = 0;
00080     //private Shortcut shortcut;
00081     private final Class declaringClass;
00082     private final Class targetClass;
00083 
00084     // priorities for NF requests (notably when using filters on functional requests) : 
00085     //private short priority=ComponentRequest.STRICT_FIFO_PRIORITY;
00086     public ComponentRequestImpl(MethodCall methodCall, UniversalBody sender,
00087         boolean isOneWay, long nextSequenceID) {
00088         super(methodCall, sender, isOneWay, nextSequenceID);
00089         declaringClass = methodCall.getReifiedMethod().getDeclaringClass();
00090         targetClass = methodCall.getReifiedMethod().getDeclaringClass();
00091     }
00092 
00093     public ComponentRequestImpl(Request request) {
00094         super(request.getMethodCall(), request.getSender(), request.isOneWay(),
00095             request.getSequenceNumber());
00096         declaringClass = methodCall.getReifiedMethod().getDeclaringClass();
00097         targetClass = methodCall.getReifiedMethod().getDeclaringClass();
00098     }
00099 
00105     protected FutureResult serveInternal(Body targetBody)
00106         throws ServeException {
00107         Object result = null;
00108         Throwable exception = null;
00109         
00110         if (logger.isDebugEnabled()) {
00111             try {
00112                 logger.debug("invocation on method [" + methodCall.getName() +
00113                     "] of interface [" +
00114                     methodCall.getComponentMetadata().getComponentInterfaceName() +
00115                     "] on component : [" +
00116                     ((ComponentParametersController) ((ComponentBodyImpl) targetBody)
00117                      .getProActiveComponentImpl().getFcInterface(Constants.COMPONENT_PARAMETERS_CONTROLLER)).getComponentParameters()
00118                      .getName() + "]");
00119             } catch (NoSuchInterfaceException e) {
00120                 e.printStackTrace();
00121             }
00122         }
00123 
00124         try {
00125             if (isControllerRequest()) {
00126                 
00127                 result = ((ProActiveComponentImpl) ((ComponentBodyImpl) targetBody)
00128                           .getProActiveComponentImpl()).getControllerRequestHandler()
00129                           .handleRequest(this);
00130 
00131             } else {
00132                 
00133                 if (((ComponentBodyImpl) targetBody).getProActiveComponentImpl() != null) {
00134                     interceptBeforeInvocation(targetBody);
00135 
00136                     String hierarchical_type = Fractive.getComponentParametersController(((ComponentBodyImpl) targetBody).getProActiveComponentImpl())
00137                                                        .getComponentParameters()
00138                                                        .getHierarchicalType();
00139                     
00140                     // gather: interception managed with non-transformed incoming requests
00141                     ProActiveInterface itf =  (ProActiveInterface)((ComponentBody)targetBody).getProActiveComponentImpl().getFcInterface(methodCall.getComponentMetadata().getComponentInterfaceName());
00142                     ProActiveInterfaceType itfType = (ProActiveInterfaceType)itf.getFcItfType();
00143                     if (itfType.isFcGathercastItf() && (!getMethodCall().getComponentMetadata().getSenderItfID().equals(new ItfID(itfType.getFcItfName(), targetBody.getID())))) {
00144                         // delegate to gather controller, except for self requests
00145                         result = Fractive.getGathercastController(((ComponentBodyImpl) targetBody).getProActiveComponentImpl()).handleRequestOnGatherItf(this);
00146                         
00147                     }
00148 
00149                     // if the component is a composite , forward to functional interface 
00150                     else if (hierarchical_type.equals(Constants.COMPOSITE)) {
00151                         //                                              // forward to functional interface whose name is given as a parameter in the method call
00152                         try {
00153                             if (getShortcut() != null) {
00154                                 // TODO_M allow stopping shortcut here
00155                             }
00156                             // executing on connected server interface
00157                             result = methodCall.execute((ProActiveInterface) (((ComponentBodyImpl) targetBody)
00158                                   .getProActiveComponentImpl())
00159                                   .getFcInterface(methodCall.getComponentMetadata().getComponentInterfaceName()));
00160                         } catch (IllegalArgumentException e) {
00161                             throw new ServeException("could not reify method call : ",
00162                                 e);
00163                         } catch (Throwable e) {
00164                             e.printStackTrace();
00165                             throw new ServeException("could not reify method call : ",
00166                                 e);
00167                         }
00168                     } else {
00169                         // the component is a primitive
00170                         // directly execute the method on the active object
00171                         if (logger.isDebugEnabled()) {
00172                             if (getShortcutLength() > 0) {
00173                                 logger.debug("request has crossed " +
00174                                     (getShortcutLength() - 1) +
00175                                     " membranes before reaching a primitive component");
00176                             }
00177                         }
00178                         result = methodCall.execute(targetBody.getReifiedObject());
00179                     }
00180                     interceptAfterInvocation(targetBody);
00181                 } else {
00182                     throw new ServeException(
00183                         "trying to execute a component method on an object that is not a component");
00184                 }
00185             }
00186         } catch (NoSuchInterfaceException nsie) {
00187             throw new ServeException("cannot serve request : problem accessing a component controller",
00188                 nsie);
00189         } catch (MethodCallExecutionFailedException e) {
00190             throw new ServeException("serve method " +
00191                 methodCall.getReifiedMethod().toString() + " failed", e);
00192         } catch (java.lang.reflect.InvocationTargetException e) {
00193             exception = e.getTargetException();
00194             e.printStackTrace();
00195 
00196             if (isOneWay) {
00197                 throw new ServeException("serve method " +
00198                     methodCall.getReifiedMethod().toString() + " failed",
00199                     exception);
00200             }
00201         }
00202         
00203 
00204         return new FutureResult(result, exception, null);
00205     }
00206 
00207     // intercept and delegate for preprocessing from the inputInterceptors 
00208     private void interceptBeforeInvocation(Body targetBody) {
00209         if (methodCall.getReifiedMethod() != null) {
00210             List inputInterceptors = ((ComponentBodyImpl) targetBody).getProActiveComponentImpl()
00211                                       .getInputInterceptors();
00212             Iterator it = inputInterceptors.iterator();
00213             while (it.hasNext()) {
00214                 try {
00215                     InputInterceptor interceptor = (InputInterceptor) it.next();
00216                     interceptor.beforeInputMethodInvocation(methodCall);
00217                 } catch (NullPointerException e) {
00218                     logger.error("could not intercept invocation : " +
00219                         e.getMessage());
00220                 }
00221             }
00222         }
00223     }
00224 
00225     // intercept and delegate for postprocessing from the inputInterceptors 
00226     private void interceptAfterInvocation(Body targetBody) {
00227         if (methodCall.getReifiedMethod() != null) {
00228             if (((ComponentBodyImpl) targetBody).getProActiveComponentImpl() != null) {
00229                 List interceptors = ((ComponentBodyImpl) targetBody).getProActiveComponentImpl()
00230                                      .getInputInterceptors();
00231 
00232                 // use inputInterceptors in reverse order after invocation
00233                 ListIterator it = interceptors.listIterator();
00234 
00235                 // go to the end of the list first
00236                 while (it.hasNext()) {
00237                     it.next();
00238                 }
00239                 while (it.hasPrevious()) {
00240                     ((InputInterceptor) it.previous()).afterInputMethodInvocation(methodCall);
00241                 }
00242             }
00243         }
00244     }
00245 
00246     /*
00247      * @see org.objectweb.proactive.core.component.request.ComponentRequest#isControllerRequest()
00248      */
00249     public boolean isControllerRequest() {
00250         // according to the Fractal spec v2.0 , section 4.1
00251         return ((methodCall.getComponentMetadata().getComponentInterfaceName() != null) &&
00252         (methodCall.getComponentMetadata().getComponentInterfaceName().endsWith("-controller") ||
00253         methodCall.getComponentMetadata().getComponentInterfaceName().equals("component")));
00254     }
00255 
00256     /*
00257      * @see org.objectweb.proactive.core.component.request.ComponentRequest#isStopFcRequest()
00258      */
00259     public boolean isStopFcRequest() {
00260         return (declaringClass.equals(LifeCycleController.class) &&
00261         "stopFc".equals(getMethodName()));
00262     }
00263 
00264     /*
00265      * @see org.objectweb.proactive.core.component.request.ComponentRequest#isStartFcRequest()
00266      */
00267     public boolean isStartFcRequest() {
00268         return (declaringClass.equals(LifeCycleController.class) &&
00269         "startFc".equals(getMethodName()));
00270     }
00271 
00272     public void notifyReception(UniversalBody bodyReceiver)
00273         throws IOException {
00274         if (getShortcut() != null) {
00275             if (logger.isDebugEnabled()) {
00276                 logger.debug("notifying reception of method " +
00277                     methodCall.getName());
00278             }
00279             Shortcut shortcut = getShortcut();
00280             shortcut.updateDestination(bodyReceiver.getRemoteAdapter());
00281             shortcut.getSender().createShortcut(shortcut);
00282 
00283             ((ComponentBodyImpl) bodyReceiver).keepShortcut(shortcut);
00284         }
00285         super.notifyReception(bodyReceiver);
00286     }
00287 
00288     public void shortcutNotification(UniversalBody sender,
00289         UniversalBody intermediate) {
00290         methodCall.getComponentMetadata().shortcutNotification(sender, intermediate);
00291     }
00292 
00293     public Shortcut getShortcut() {
00294         return methodCall.getComponentMetadata().getShortcut();
00295     }
00296 
00297     public int getShortcutLength() {
00298         return ((getShortcut() == null) ? 0 : getShortcut().length());
00299     }
00300 
00301     /*
00302      * @see org.objectweb.proactive.core.component.request.ComponentRequest#getNFPriority()
00303      */
00304     public short getPriority() {
00305         return methodCall.getComponentMetadata().getPriority();
00306     }
00307 
00308     /*
00309      * @see org.objectweb.proactive.core.component.request.ComponentRequest#getTargetClass()
00310      */
00311     public Class getTargetClass() {
00312         return targetClass;
00313     }
00314 }

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