org/objectweb/proactive/core/component/representative/ProActiveComponentRepresentativeImpl.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.Constructor;
00035 import java.lang.reflect.TypeVariable;
00036 import java.util.HashMap;
00037 import java.util.Iterator;
00038 import java.util.Map;
00039 
00040 import org.apache.log4j.Logger;
00041 import org.objectweb.fractal.api.Component;
00042 import org.objectweb.fractal.api.Interface;
00043 import org.objectweb.fractal.api.NoSuchInterfaceException;
00044 import org.objectweb.fractal.api.Type;
00045 import org.objectweb.fractal.api.control.BindingController;
00046 import org.objectweb.fractal.api.control.ContentController;
00047 import org.objectweb.fractal.api.type.ComponentType;
00048 import org.objectweb.fractal.api.type.InterfaceType;
00049 import org.objectweb.proactive.core.ProActiveRuntimeException;
00050 import org.objectweb.proactive.core.UniqueID;
00051 import org.objectweb.proactive.core.body.proxy.UniversalBodyProxy;
00052 import org.objectweb.proactive.core.component.ComponentParameters;
00053 import org.objectweb.proactive.core.component.Constants;
00054 import org.objectweb.proactive.core.component.Fractive;
00055 import org.objectweb.proactive.core.component.ProActiveInterface;
00056 import org.objectweb.proactive.core.component.Utils;
00057 import org.objectweb.proactive.core.component.config.ComponentConfigurationHandler;
00058 import org.objectweb.proactive.core.component.controller.AbstractProActiveController;
00059 import org.objectweb.proactive.core.component.controller.ComponentParametersController;
00060 import org.objectweb.proactive.core.component.gen.RepresentativeInterfaceClassGenerator;
00061 import org.objectweb.proactive.core.component.identity.ProActiveComponent;
00062 import org.objectweb.proactive.core.component.identity.ProActiveComponentImpl;
00063 import org.objectweb.proactive.core.component.request.ComponentRequest;
00064 import org.objectweb.proactive.core.component.type.ProActiveInterfaceType;
00065 import org.objectweb.proactive.core.group.ProxyForGroup;
00066 import org.objectweb.proactive.core.mop.MethodCall;
00067 import org.objectweb.proactive.core.mop.Proxy;
00068 import org.objectweb.proactive.core.mop.StubObject;
00069 import org.objectweb.proactive.core.util.log.Loggers;
00070 import org.objectweb.proactive.core.util.log.ProActiveLogger;
00071 
00072 
00092 public class ProActiveComponentRepresentativeImpl
00093     implements ProActiveComponentRepresentative, Serializable {
00094     private static Logger logger = ProActiveLogger.getLogger(Loggers.COMPONENTS);
00095 
00096     private Map fcInterfaceReferences;
00097     private Map nfInterfaceReferences;
00098     private Proxy proxy;
00099 
00100     private ComponentType componentType = null; // immutable
00101     private StubObject stubOnBaseObject = null;
00102     private String hierarchicalType = null;
00103     private String currentControllerInterface = null;
00104     private boolean useShortcuts;
00105 
00106     public ProActiveComponentRepresentativeImpl(ComponentType componentType,
00107         String hierarchicalType, String controllersConfigFileLocation) {
00108         this.componentType = componentType;
00109         useShortcuts = ("true".equals(System.getProperty(
00110                     "proactive.components.use_shortcuts")));
00111         this.hierarchicalType = hierarchicalType;
00112         addControllers(componentType, controllersConfigFileLocation);
00113 
00114         // add functional interfaces
00115         // functional interfaces are proxies on the corresponding meta-objects
00116         addFunctionalInterfaces(componentType);
00117     }
00118 
00122     private void addFunctionalInterfaces(ComponentType componentType) {
00123         fcInterfaceReferences = new HashMap(componentType.getFcInterfaceTypes().length);
00124         InterfaceType[] interface_types = componentType.getFcInterfaceTypes();
00125         try {
00126             for (int j = 0; j < interface_types.length; j++) {
00127                 if (!interface_types[j].isFcCollectionItf()) {
00128                         
00129                         // itfs members of collection itfs are dynamically generated
00130                 Interface interface_reference = RepresentativeInterfaceClassGenerator.instance()
00131                                                                                      .generateFunctionalInterface(interface_types[j].getFcItfName(),
00132                         this, (ProActiveInterfaceType)interface_types[j]);
00133 
00134                 // all calls are to be reified
00135                 fcInterfaceReferences.put(interface_reference.getFcItfName(),
00136                     interface_reference);
00137                 } 
00138                 
00139             }
00140         } catch (Exception e) {
00141             e.printStackTrace();
00142             throw new RuntimeException("cannot create interface references : " +
00143                 e.getMessage());
00144         }
00145     }
00146 
00147     private void addControllers(ComponentType componentType,
00148         String controllersConfigFileLocation) {
00149         if (controllersConfigFileLocation == null) {
00150             return;
00151         }
00152         ComponentConfigurationHandler componentConfiguration = ProActiveComponentImpl.loadControllerConfiguration(controllersConfigFileLocation);
00153         Map controllersConfiguration = componentConfiguration.getControllers();
00154 
00155         addControllers(componentType, controllersConfiguration);
00156     }
00157 
00158     private void addControllers(ComponentType componentType,
00159         Map controllersConfiguration) {
00160         // create the interface references tables
00161         // the size is the addition of :  
00162         // - 1 for the current ItfRef (that is at the same time a binding controller, lifecycle controller,
00163         // content controller and name controller
00164         // - the number of client functional interfaces
00165         // - the number of server functional interfaces
00166         //ArrayList interface_references_list = new ArrayList(1 +componentType.getFcInterfaceTypes().length+controllersConfiguration.size());
00167         nfInterfaceReferences = new HashMap(1 +
00168                 controllersConfiguration.size());
00169 
00170         int i = 0;
00171 
00172         // add controllers
00173         //Enumeration controllersInterfaces = controllersConfiguration.propertyNames();
00174         Iterator iteratorOnControllers = controllersConfiguration.keySet()
00175                                                                  .iterator();
00176         Class controllerClass = null;
00177         AbstractProActiveController currentController;
00178         ProActiveInterface currentInterface = null;
00179         Class controllerItf;
00180         while (iteratorOnControllers.hasNext()) {
00181             String controllerItfName = (String) iteratorOnControllers.next();
00182             try {
00183                 controllerItf = Class.forName(controllerItfName);
00184                 controllerClass = Class.forName((String) controllersConfiguration.get(
00185                             controllerItf.getName()));
00186                 Constructor controllerClassConstructor = controllerClass.getConstructor(new Class[] {
00187                             Component.class
00188                         });
00189                 currentController = (AbstractProActiveController) controllerClassConstructor.newInstance(new Object[] {
00190                             this
00191                         });
00192                 currentInterface = RepresentativeInterfaceClassGenerator.instance()
00193                                                                         .generateControllerInterface(currentController.getFcItfName(),
00194                         this, (ProActiveInterfaceType) currentController.getFcItfType());
00195                 ((StubObject)currentInterface).setProxy(proxy);
00196             } catch (Exception e) {
00197                 logger.error("could not create controller " +
00198                     controllersConfiguration.get(controllerItfName) + " : " +
00199                     e.getMessage());
00200                 continue;
00201             }
00202 
00203             if (BindingController.class.isAssignableFrom(controllerClass)) {
00204                 if ((hierarchicalType.equals(Constants.PRIMITIVE) &&
00205                         (Fractive.getClientInterfaceTypes(componentType).length == 0))) {
00206                     //bindingController = null;
00207                     if (logger.isDebugEnabled()) {
00208                         logger.debug(
00209                             "user component class of this component does not have any client interface. It will have no BindingController");
00210                     }
00211                     continue;
00212                 }
00213             }
00214             if (ContentController.class.isAssignableFrom(controllerClass)) {
00215                 if (Constants.PRIMITIVE.equals(hierarchicalType)) {
00216                     // no content controller here
00217                     continue;
00218                 }
00219             }
00220             if (currentInterface != null) {
00221                 nfInterfaceReferences.put(currentController.getFcItfName(),
00222                     currentInterface);
00223 
00224                 i++;
00225             }
00226         }
00227     }
00228 
00229     protected Object reifyCall(String className, String methodName,
00230         Class[] parameterTypes, Object[] effectiveParameters, short priority) {
00231         try {
00232             return proxy.reify((MethodCall) MethodCall.getComponentMethodCall(
00233                     Class.forName(className).getDeclaredMethod(methodName,
00234                         parameterTypes), effectiveParameters, null, (String) null, null,
00235                     priority));
00236 
00237             // functional interface name is null
00238         } catch (NoSuchMethodException e) {
00239             throw new ProActiveRuntimeException(e.toString());
00240         } catch (ClassNotFoundException e) {
00241             throw new ProActiveRuntimeException(e.toString());
00242         } catch (Throwable e) {
00243             throw new ProActiveRuntimeException(e.toString());
00244         }
00245     }
00246 
00247   
00248     /*
00249      *implements  org.objectweb.fractal.api.Component#getFcInterface(String)}
00250      */
00251     public Object getFcInterface(String interfaceName)
00252         throws NoSuchInterfaceException {
00253         if (interfaceName.endsWith("-controller") &&
00254                 !("attribute-controller".equals(interfaceName))) {
00255             if (nfInterfaceReferences == null) {
00256                 // retreive the configuration by calling directly the mandatory component parameters controller itf
00257                 ComponentParameters params = (ComponentParameters) reifyCall(ComponentParametersController.class.getName(),
00258                         "getComponentParameters", new Class[] {  },
00259                         new Object[] {  }, ComponentRequest.STRICT_FIFO_PRIORITY);
00260                 hierarchicalType = params.getHierarchicalType();
00261                 addControllers(componentType,
00262                     params.getControllerDescription().getControllersSignatures());
00263             }
00264             if (nfInterfaceReferences.containsKey(interfaceName)) {
00265                 return nfInterfaceReferences.get(interfaceName);
00266             } else {                    
00267                 throw new NoSuchInterfaceException(interfaceName);
00268             }
00269         }
00270 
00271         if (fcInterfaceReferences.containsKey(interfaceName)) {
00272                 return fcInterfaceReferences.get(interfaceName);
00273         } else {
00274                 // maybe the member of a collection itf?
00275                 InterfaceType itfType = Utils.getItfType(interfaceName, this);
00276                 if (itfType !=null && itfType.isFcCollectionItf()) {
00277                         
00278                         try {
00279 //                                      // generate the corresponding interface locally
00280                                         Interface interface_reference = RepresentativeInterfaceClassGenerator.instance()
00281                                                 .generateFunctionalInterface(interfaceName,
00282                                                                 this, (ProActiveInterfaceType)itfType);
00283 
00284                                         ((StubObject)interface_reference).setProxy(proxy);
00285                                         // keep it in the list of functional interfaces
00286                                         fcInterfaceReferences.put(interfaceName,interface_reference);
00287                                         return interface_reference;
00288                                  
00289                                 } catch (SecurityException e) {
00290                                         // TODO Auto-generated catch block
00291                                         e.printStackTrace();
00292                                 } catch (Throwable e) {
00293                                         e.printStackTrace();
00294                                 }
00295                 }
00296         }
00297                 
00298         
00299 
00300         throw new NoSuchInterfaceException(interfaceName);
00301     }
00302 
00303     /*
00304      *implements  org.objectweb.fractal.api.Component#getFcInterfaces()}
00305      */
00306     public Object[] getFcInterfaces() {
00307         Interface[] nfInterfaces = (Interface[]) (nfInterfaceReferences.values()
00308                                                                        .toArray(new Interface[nfInterfaceReferences.size()]));
00309         Interface[] fcInterfaces = (Interface[]) (fcInterfaceReferences.values()
00310                                                                        .toArray(new Interface[fcInterfaceReferences.size()]));
00311         Interface[] result = new Interface[nfInterfaces.length +
00312             fcInterfaces.length];
00313         System.arraycopy(nfInterfaces, 0, result, 0, nfInterfaces.length);
00314         System.arraycopy(fcInterfaces, 0, result, nfInterfaces.length,
00315             fcInterfaces.length);
00316         return result;
00317     }
00318 
00319     /*
00320      *implements  org.objectweb.fractal.api.Component#getFcType()}
00321      */
00322     public Type getFcType() {
00323         return componentType;
00324     }
00325 
00326     /*
00327      *implements  org.objectweb.proactive.core.mop.StubObject#getProxy()}
00328      */
00329     public Proxy getProxy() {
00330         return proxy;
00331     }
00332 
00333     /*
00334      *implements  org.objectweb.proactive.core.mop.StubObject#setProxy(Proxy)}
00335      */
00336     public void setProxy(Proxy proxy) {
00337         // sets proxy for non functional interfaces
00338         this.proxy = proxy;
00339         // sets the same proxy for all interfaces of this component
00340         Object[] interfaces = getFcInterfaces();
00341         ProActiveInterface[] interface_references = new ProActiveInterface[interfaces.length];
00342         for (int i = 0; i < interfaces.length; i++) {
00343             interface_references[i] = (ProActiveInterface) interfaces[i];
00344         }
00345         for (int i = 0; i < interface_references.length; i++) {
00346             if (useShortcuts) {
00347                 // adds an intermediate FunctionalInterfaceProxy for functional interfaces, to manage shortcutting
00348                 ((StubObject) interface_references[i]).setProxy(new FunctionalInterfaceProxyImpl(
00349                         proxy, interface_references[i].getFcItfName()));
00350             } else {
00351                 try {
00352                     ((StubObject) interface_references[i]).setProxy(proxy);
00353                 } catch (RuntimeException e) {
00354                     logger.error(e.getMessage());
00355                     throw new ProActiveRuntimeException(e);
00356                 }
00357             }
00358         }
00359     }
00360 
00365     public boolean equals(Object component) {
00366         Object result = reifyCall(Object.class.getName(), "equals",
00367                 new Class[] { Object.class }, new Object[] { component },
00368                 ComponentRequest.STRICT_FIFO_PRIORITY);
00369         return ((Boolean) result).booleanValue();
00370     }
00371 
00372     public int hashCode() {
00373         // reified as a standard invocation (not a component one)
00374         Object result;
00375                 try {
00376                         result = proxy.reify((MethodCall) MethodCall.getMethodCall(
00377                                  Class.forName(Object.class.getName()).getDeclaredMethod("hashCode",
00378                                      new Class[] {}), new Object[] {}, (Map<TypeVariable, Class>)null));
00379                 return ((Integer) result).intValue();
00380                 } catch (SecurityException e) {
00381                         throw new ProActiveRuntimeException(e.toString());
00382                 } catch (NoSuchMethodException e) {
00383                         throw new ProActiveRuntimeException(e.toString());
00384                 } catch (ClassNotFoundException e) {
00385                         throw new ProActiveRuntimeException(e.toString());
00386                 } catch (Throwable e) {
00387                         throw new ProActiveRuntimeException(e.toString());
00388                 }
00389 
00390     }
00391     
00392 
00396     public UniqueID getID() {
00397         if (!(getProxy() instanceof ProxyForGroup)) {
00398             return ((UniversalBodyProxy) getProxy()).getBodyID();
00399         } else {
00400             return null;
00401         }
00402     }
00403 
00404     /*
00405      * implements org.objectweb.proactive.core.component.identity.ProActiveComponent#getReferenceOnBaseObject()}
00406      */
00407     public Object getReferenceOnBaseObject() {
00408         logger.error(
00409             "getReferenceOnBaseObject() method is not available in component representatives");
00410         return null;
00411     }
00412 
00413     /*
00414      * implements org.objectweb.proactive.core.component.identity.ProActiveComponent#getRepresentativeOnThis()}
00415      */
00416     public ProActiveComponent getRepresentativeOnThis() {
00417         return this;
00418     }
00419 
00420     /*
00421      * @see org.objectweb.proactive.core.component.representative.ProActiveComponentRepresentative#getStubOnReifiedObject()
00422      */
00423     public StubObject getStubOnBaseObject() {
00424         return stubOnBaseObject;
00425     }
00426 
00427     /*
00428      * @see org.objectweb.proactive.core.component.representative.ProActiveComponentRepresentative#setStubOnReifiedObject(org.objectweb.proactive.core.mop.StubObject)
00429      */
00430     public void setStubOnBaseObject(StubObject stub) {
00431         stubOnBaseObject = stub;
00432     }
00433 
00434     private boolean isPrimitive() {
00435         return Constants.PRIMITIVE.equals(hierarchicalType);
00436     }
00437 
00438         public void _terminateAO(Proxy proxy) {
00439 
00440         }
00441 
00442         public void _terminateAOImmediatly(Proxy proxy) {
00443         }
00444         
00445 }

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