org/objectweb/proactive/core/component/representative/FunctionalInterfaceProxyImpl.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.representative;
00032 
00033 import java.io.Serializable;
00034 import java.lang.reflect.Field;
00035 
00036 import org.apache.log4j.Logger;
00037 import org.objectweb.proactive.core.ProActiveRuntimeException;
00038 import org.objectweb.proactive.core.body.LocalBodyStore;
00039 import org.objectweb.proactive.core.body.UniversalBody;
00040 import org.objectweb.proactive.core.body.proxy.UniversalBodyProxy;
00041 import org.objectweb.proactive.core.mop.MethodCall;
00042 import org.objectweb.proactive.core.mop.Proxy;
00043 import org.objectweb.proactive.core.util.log.Loggers;
00044 import org.objectweb.proactive.core.util.log.ProActiveLogger;
00045 
00046 
00070 public class FunctionalInterfaceProxyImpl implements FunctionalInterfaceProxy,
00071     Serializable {
00072     protected transient final static Logger logger = ProActiveLogger.getLogger(Loggers.COMPONENTS_REQUESTS);
00073     private static Field universalBodyField;
00074     private static Field bodyIDField;
00075     Proxy bodyProxyDelegatee = null;
00076     Proxy nonShortcutProxy = null;
00077     String fcItfName; // name of the functional interface
00078 
00079     static {
00080         try {
00081             universalBodyField = UniversalBodyProxy.class.getDeclaredField(
00082                     "universalBody");
00083             universalBodyField.setAccessible(true);
00084             bodyIDField = UniversalBodyProxy.class.getSuperclass()
00085                                                   .getDeclaredField("bodyID");
00086             bodyIDField.setAccessible(true);
00087         } catch (NoSuchFieldException e) {
00088             logger.error(e.getMessage());
00089         }
00090     }
00091 
00092     private FunctionalInterfaceProxyImpl() {
00093     }
00094 
00095     public FunctionalInterfaceProxyImpl(Proxy bodyProxyDelegatee,
00096         String fcItfName) {
00097         nonShortcutProxy = this.bodyProxyDelegatee = bodyProxyDelegatee;
00098         this.fcItfName = fcItfName;
00099     }
00100 
00101     private void changeRefOnBody(UniversalBody body) {
00102         try {
00103             if (logger.isDebugEnabled()) {
00104                 logger.debug("changing reference on body");
00105             }
00106             UniversalBodyProxy proxy = (UniversalBodyProxy) UniversalBodyProxy.class.newInstance();
00107             universalBodyField.set(proxy, body);
00108             bodyIDField.set(proxy, body.getID());
00109             bodyProxyDelegatee = proxy;
00110         } catch (InstantiationException e) {
00111             throw new ProActiveRuntimeException(e);
00112         } catch (IllegalAccessException e) {
00113             throw new ProActiveRuntimeException(e);
00114         }
00115     }
00116 
00117     public Object reify(MethodCall c) throws Throwable {
00118         // check shortcut by asking source body (LocalBodyStore.currentThreadBody)
00119         // if shortcut : change ref on Body
00120         UniversalBody newDestinationBody = LocalBodyStore.getInstance()
00121                                                          .getCurrentThreadBody()
00122                                                          .getShortcutTargetBody(new ItfID(
00123                     c.getComponentMetadata().getComponentInterfaceName(),
00124                     ((UniversalBodyProxy) bodyProxyDelegatee).getBody().getID()));
00125         if (newDestinationBody != null) {
00126             changeRefOnBody(newDestinationBody);
00127         }
00128         return bodyProxyDelegatee.reify(c);
00129     }
00130 
00131     public void setBodyProxy(Proxy proxy) {
00132         bodyProxyDelegatee = proxy;
00133     }
00134 
00135     public Proxy getBodyProxy() {
00136         return bodyProxyDelegatee;
00137     }
00138 
00139  
00140 }

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