org/objectweb/proactive/core/component/group/ProxyForComponentInterfaceGroup.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.group;
00032 
00033 import java.lang.reflect.InvocationTargetException;
00034 import java.lang.reflect.Method;
00035 import java.lang.reflect.ParameterizedType;
00036 import java.util.Map;
00037 import java.util.Vector;
00038 
00039 import org.objectweb.fractal.api.Component;
00040 import org.objectweb.fractal.api.Interface;
00041 import org.objectweb.proactive.Body;
00042 import org.objectweb.proactive.ProActive;
00043 import org.objectweb.proactive.core.ProActiveRuntimeException;
00044 import org.objectweb.proactive.core.body.LocalBodyStore;
00045 import org.objectweb.proactive.core.component.collectiveitfs.MulticastHelper;
00046 import org.objectweb.proactive.core.component.exceptions.ParameterDispatchException;
00047 import org.objectweb.proactive.core.component.identity.ProActiveComponent;
00048 import org.objectweb.proactive.core.component.type.ProActiveInterfaceType;
00049 import org.objectweb.proactive.core.component.type.ProActiveInterfaceTypeImpl;
00050 import org.objectweb.proactive.core.group.ExceptionListException;
00051 import org.objectweb.proactive.core.group.ProActiveComponentGroup;
00052 import org.objectweb.proactive.core.group.ProcessForAsyncCall;
00053 import org.objectweb.proactive.core.group.ProcessForOneWayCall;
00054 import org.objectweb.proactive.core.group.ProxyForGroup;
00055 import org.objectweb.proactive.core.mop.ClassNotReifiableException;
00056 import org.objectweb.proactive.core.mop.ConstructionOfReifiedObjectFailedException;
00057 import org.objectweb.proactive.core.mop.ConstructorCall;
00058 import org.objectweb.proactive.core.mop.MOP;
00059 import org.objectweb.proactive.core.mop.MethodCall;
00060 import org.objectweb.proactive.core.mop.StubObject;
00061 
00062 
00069 public class ProxyForComponentInterfaceGroup extends ProxyForGroup {
00070     protected ProActiveInterfaceType interfaceType;
00071     protected Class itfSignatureClass = null;
00072     protected ProActiveComponent owner;
00073     protected ProxyForComponentInterfaceGroup delegatee = null;
00074 
00075     public ProxyForComponentInterfaceGroup()
00076         throws ConstructionOfReifiedObjectFailedException {
00077         super();
00078         className = Interface.class.getName();
00079     }
00080 
00081     public ProxyForComponentInterfaceGroup(ConstructorCall c, Object[] p)
00082         throws ConstructionOfReifiedObjectFailedException {
00083         super(c, p);
00084         className = Interface.class.getName();
00085     }
00086 
00087     public ProxyForComponentInterfaceGroup(String nameOfClass)
00088         throws ConstructionOfReifiedObjectFailedException {
00089         this();
00090         className = Interface.class.getName();
00091     }
00092 
00096     public ProActiveInterfaceType getInterfaceType() {
00097         return interfaceType;
00098     }
00099 
00100     /*
00101      * @see org.objectweb.proactive.core.group.ProxyForGroup#reify(org.objectweb.proactive.core.mop.MethodCall)
00102      */
00103     @Override
00104     public synchronized Object reify(MethodCall mc)
00105         throws InvocationTargetException {
00106         if (delegatee != null) {
00107             // check
00108             if (itfSignatureClass.equals(mc.getReifiedMethod()
00109                                                .getDeclaringClass())) {
00110                 // nothing to do
00111             } else if (mc.getReifiedMethod().getDeclaringClass()
00112                              .isAssignableFrom(itfSignatureClass)) {
00113                 // need to adapt method call
00114                 Method adaptedMethod;
00115                 try {
00116                     // TODO optimize (avoid reflective calls!)
00117                     adaptedMethod = itfSignatureClass.getMethod(mc.getReifiedMethod()
00118                                                                   .getName(),
00119                             mc.getReifiedMethod().getParameterTypes());
00120                 } catch (SecurityException e) {
00121                     throw new InvocationTargetException(e,
00122                         "could not adapt client interface to multicast server interface " +
00123                         interfaceType.getFcItfName());
00124                 } catch (NoSuchMethodException e) {
00125                     throw new InvocationTargetException(e,
00126                         "could not adapt client interface to multicast server interface " +
00127                         interfaceType.getFcItfName());
00128                 }
00129                 mc = MethodCall.getComponentMethodCall(adaptedMethod,
00130                         mc.getEffectiveArguments(), mc.getGenericTypesMapping(),
00131                         mc.getComponentMetadata().getComponentInterfaceName(),
00132                         mc.getComponentMetadata().getSenderItfID(),
00133                         mc.getComponentMetadata().getPriority());
00134             } else {
00135                 throw new InvocationTargetException(null,
00136                     "method " + mc.getName() + " defined in " +
00137                     mc.getReifiedMethod().getDeclaringClass() +
00138                     " cannot be invoked on " + itfSignatureClass.getName());
00139             }
00140         }
00141 
00142         return super.reify(mc);
00143     }
00144 
00145     /*
00146      * @see org.objectweb.proactive.core.group.Group#getGroupByType()
00147      */
00148     public Object getGroupByType() {
00149         try {
00150             Interface result = ProActiveComponentGroup.newComponentInterfaceGroup(interfaceType,
00151                     owner);
00152 
00153             ProxyForComponentInterfaceGroup proxy = (ProxyForComponentInterfaceGroup) ((StubObject) result).getProxy();
00154             proxy.memberList = this.memberList;
00155             proxy.className = this.className;
00156             proxy.interfaceType = this.interfaceType;
00157             proxy.owner = this.owner;
00158             proxy.proxyForGroupID = this.proxyForGroupID;
00159             proxy.waited = this.waited;
00160             return result;
00161         } catch (ClassNotReifiableException e) {
00162             e.printStackTrace();
00163             return null;
00164         } catch (ClassNotFoundException e) {
00165             e.printStackTrace();
00166             return null;
00167         }
00168     }
00169 
00170     /*
00171      * @see org.objectweb.proactive.core.group.ProxyForGroup#asynchronousCallOnGroup(org.objectweb.proactive.core.mop.MethodCall)
00172      */
00173     @Override
00174     protected Object asynchronousCallOnGroup(MethodCall mc)
00175         throws InvocationTargetException {
00176         if (((ProActiveInterfaceTypeImpl) interfaceType).isFcCollective()) {
00177             if (delegatee != null) {
00178                 Object result;
00179                 Body body = ProActive.getBodyOnThis();
00180 
00181                 // Creates a stub + ProxyForGroup for representing the result
00182                 try {
00183                     Object[] paramProxy = new Object[0];
00184 
00185                     // create a result group of the type of the adapted mc
00186                     if (!(mc.getReifiedMethod().getGenericReturnType() instanceof ParameterizedType)) {
00187                         throw new ProActiveRuntimeException(
00188                             "all methods in multicast interfaces must return parameterized lists, " +
00189                             "which is not the case for method " +
00190                             mc.getReifiedMethod().toString());
00191                     }
00192 
00193                     Class returnTypeForGroup = (Class) ((ParameterizedType) mc.getReifiedMethod()
00194                                                                               .getGenericReturnType()).getActualTypeArguments()[0];
00195                     result = MOP.newInstance(returnTypeForGroup.getName(),
00196                                 null, null, ProxyForGroup.class.getName(), paramProxy);
00197                     ((ProxyForGroup) ((StubObject) result).getProxy()).setClassName(returnTypeForGroup.getName());
00198                 } catch (Exception e) {
00199                     e.printStackTrace();
00200                     return null;
00201                 }
00202 
00203                 Map<MethodCall, Integer> generatedMethodCalls;
00204 
00205                 try {
00206                     generatedMethodCalls = MulticastHelper
00207                                                    .generateMethodCallsForMulticastDelegatee(owner, mc,
00208                             delegatee);
00209                 } catch (ParameterDispatchException e) {
00210                     throw new InvocationTargetException(e,
00211                         "cannot dispatch invocation parameters for method " +
00212                         mc.getReifiedMethod().toString() +
00213                         " from collective interface " +
00214                         interfaceType.getFcItfName());
00215                 }
00216 
00217                 // Init the lists of result with null value to permit the "set(index)" operation
00218                 Vector memberListOfResultGroup = ((ProxyForGroup) ((StubObject) result).getProxy()).getMemberList();
00219 
00220                 // there are as many results expected as there are method invocations
00221                 for (int i = 0; i < generatedMethodCalls.size(); i++) {
00222                     memberListOfResultGroup.add(null);
00223                 }
00224 
00225                 for (MethodCall currentMc : generatedMethodCalls.keySet()) {
00226                     // delegate invocations
00227                     this.threadpool.addAJob(new ProcessForAsyncCall(delegatee,
00228                             delegatee.memberList, memberListOfResultGroup,
00229                             generatedMethodCalls.get(currentMc), currentMc, body));
00230                 }
00231 
00232                 LocalBodyStore.getInstance().setCurrentThreadBody(body);
00233                 return result;
00234             } else {
00235                 Thread.dumpStack();
00236                 return null;
00237             }
00238         } else {
00239             return super.asynchronousCallOnGroup(mc);
00240         }
00241     }
00242 
00243     /*
00244      * @see org.objectweb.proactive.core.group.ProxyForGroup#oneWayCallOnGroup(org.objectweb.proactive.core.mop.MethodCall, org.objectweb.proactive.core.group.ExceptionListException)
00245      */
00246     @Override
00247     protected void oneWayCallOnGroup(MethodCall mc,
00248         ExceptionListException exceptionList) throws InvocationTargetException {
00249         if (((ProActiveInterfaceTypeImpl) interfaceType).isFcCollective() &&
00250                 (delegatee != null)) {
00251             // 2. generate adapted method calls depending on nb members and parameters distribution
00252             // each method call is assigned a given member index
00253             Body body = ProActive.getBodyOnThis();
00254 
00255             Map<MethodCall, Integer> generatedMethodCalls;
00256 
00257             try {
00258                 generatedMethodCalls = MulticastHelper
00259                                                .generateMethodCallsForMulticastDelegatee(owner, mc,
00260                         delegatee);
00261             } catch (ParameterDispatchException e) {
00262                 throw new InvocationTargetException(e,
00263                     "cannot dispatch invocation parameters for method " +
00264                     mc.getReifiedMethod().toString() +
00265                     " from collective interface " +
00266                     interfaceType.getFcItfName());
00267             }
00268 
00269             for (MethodCall currentMc : generatedMethodCalls.keySet()) {
00270                 // delegate invocations
00271                 this.threadpool.addAJob(new ProcessForOneWayCall(delegatee,
00272                         delegatee.memberList,
00273                         generatedMethodCalls.get(currentMc), currentMc, body,
00274                         exceptionList));
00275             }
00276 
00277             LocalBodyStore.getInstance().setCurrentThreadBody(body);
00278         }
00279 
00280         // TODO Auto-generated method stub
00281         super.oneWayCallOnGroup(mc, exceptionList);
00282     }
00283 
00288     public void setDelegatee(ProxyForComponentInterfaceGroup delegatee) {
00289         this.delegatee = delegatee;
00290     }
00291 
00296     public ProxyForComponentInterfaceGroup getDelegatee() {
00297         return delegatee;
00298     }
00299 
00300     /*
00301      * @see org.objectweb.proactive.core.group.ProxyForGroup#size()
00302      */
00303     @Override
00304     public int size() {
00305         if (getDelegatee() != null) {
00306             return getDelegatee().size();
00307         }
00308         return super.size();
00309     }
00310 
00314     public Component getOwner() {
00315         return owner;
00316     }
00317 
00321     public void setOwner(Component owner) {
00322         this.owner = (ProActiveComponent)owner;
00323     }
00324 
00328     public void setInterfaceType(ProActiveInterfaceType interfaceType) {
00329         this.interfaceType = interfaceType;
00330         try {
00331             itfSignatureClass = Class.forName(interfaceType.getFcItfSignature());
00332         } catch (ClassNotFoundException e) {
00333             throw new ProActiveRuntimeException("cannot find Java interface " +
00334                 interfaceType.getFcItfSignature() +
00335                 " defined in interface named " + interfaceType.getFcItfName(), e);
00336         }
00337     }
00338 }

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