org/objectweb/proactive/core/component/controller/AbstractRequestHandler.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.controller;
00032 
00033 import java.io.Serializable;
00034 import java.lang.reflect.InvocationTargetException;
00035 
00036 import org.objectweb.fractal.api.Component;
00037 import org.objectweb.fractal.api.NoSuchInterfaceException;
00038 import org.objectweb.fractal.api.control.AttributeController;
00039 import org.objectweb.proactive.core.ProActiveRuntimeException;
00040 import org.objectweb.proactive.core.component.Constants;
00041 import org.objectweb.proactive.core.component.Fractive;
00042 import org.objectweb.proactive.core.component.identity.ProActiveComponent;
00043 import org.objectweb.proactive.core.component.request.ComponentRequest;
00044 import org.objectweb.proactive.core.mop.MethodCallExecutionFailedException;
00045 
00046 
00056 public abstract class AbstractRequestHandler implements RequestHandler,
00057     Serializable {
00058     private RequestHandler nextHandler = null;
00059     protected ProActiveComponent owner;
00060 
00061     /*
00062      * @see org.objectweb.proactive.core.component.controller.RequestHandler#nextHandler()
00063      */
00064     public RequestHandler nextHandler() {
00065         return nextHandler;
00066     }
00067 
00068     /*
00069      * @see org.objectweb.proactive.core.component.controller.RequestHandler#setNextHandler(org.objectweb.proactive.core.component.controller.RequestHandler)
00070      */
00071     public void setNextHandler(RequestHandler handler) {
00072         nextHandler = handler;
00073     }
00074 
00075     public Object handleRequest(ComponentRequest request)
00076         throws MethodCallExecutionFailedException, InvocationTargetException {
00077         if (request.getMethodCall().getComponentMetadata()
00078                        .getComponentInterfaceName().equals(getFcItfName()) &&
00079                 request.getTargetClass().isAssignableFrom(this.getClass())) {
00080             return request.getMethodCall().execute(this);
00081         }
00082         if (nextHandler() != null) {
00083             return nextHandler().handleRequest(request);
00084         }
00085 
00086         // special case for attribute controllers for primitive components
00087         if (AttributeController.class.isAssignableFrom(request.getTargetClass()) &&
00088                 isPrimitive()) {
00089             // delegate to implementation
00090             return request.getMethodCall()
00091                           .execute(((ProActiveComponent) getFcItfOwner()).getReferenceOnBaseObject());
00092         }
00093         throw new MethodCallExecutionFailedException(
00094             "cannot find a suitable handler for this request");
00095     }
00096 
00097     abstract public String getFcItfName();
00098 
00099     public Component getFcItfOwner() {
00100         return owner;
00101     }
00102 
00103     protected String getHierarchicalType() {
00104         try {
00105             return Fractive.getComponentParametersController((ProActiveComponent) getFcItfOwner())
00106                            .getComponentParameters().getHierarchicalType();
00107         } catch (NoSuchInterfaceException e) {
00108             throw new ProActiveRuntimeException(
00109                 "There is no component parameters controller for this component");
00110         }
00111     }
00112 
00113     protected boolean isPrimitive() {
00114         return Constants.PRIMITIVE.equals(getHierarchicalType());
00115     }
00116 
00117     protected boolean isComposite() {
00118         return Constants.COMPOSITE.equals(getHierarchicalType());
00119     }
00120 }

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