org/objectweb/proactive/core/component/adl/bindings/ProActiveBindingCompiler.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.adl.bindings;
00032 
00033 import java.util.HashMap;
00034 import java.util.List;
00035 import java.util.Map;
00036 import java.util.NoSuchElementException;
00037 
00038 import org.objectweb.deployment.scheduling.component.api.InstanceProviderTask;
00039 import org.objectweb.deployment.scheduling.core.api.Task;
00040 import org.objectweb.fractal.adl.ADLException;
00041 import org.objectweb.fractal.adl.TaskMap;
00042 import org.objectweb.fractal.adl.bindings.Binding;
00043 import org.objectweb.fractal.adl.bindings.BindingBuilder;
00044 import org.objectweb.fractal.adl.bindings.BindingCompiler;
00045 import org.objectweb.fractal.adl.bindings.BindingContainer;
00046 import org.objectweb.fractal.adl.components.Component;
00047 import org.objectweb.fractal.adl.components.ComponentContainer;
00048 import org.objectweb.fractal.adl.components.ComponentPair;
00049 import org.objectweb.fractal.adl.interfaces.Interface;
00050 import org.objectweb.fractal.adl.interfaces.InterfaceContainer;
00051 import org.objectweb.fractal.adl.types.TypeInterface;
00052 import org.objectweb.proactive.core.component.adl.types.ProActiveTypeInterface;
00053 
00054 public class ProActiveBindingCompiler extends BindingCompiler {
00055     
00056     
00057     private static Map<String, Integer> multicastItfTaskIndexer = new HashMap<String, Integer>();
00058 
00059         // --------------------------------------------------------------------------
00060           // Implementation of the Compiler interface
00061           // --------------------------------------------------------------------------
00062           
00063           public void compile (
00064             final List path,
00065             final ComponentContainer container,
00066             final TaskMap tasks,
00067             final Map context) throws ADLException 
00068           {
00069             Map subComponents = new HashMap();
00070             subComponents.put("this", container);
00071             Component[] comps = container.getComponents();
00072             for (int i = 0; i < comps.length; i++) {
00073               subComponents.put(comps[i].getName(), comps[i]);
00074             }
00075             
00076             if (container instanceof BindingContainer) {
00077               Binding[] bindings = ((BindingContainer)container).getBindings();
00078               for (int i = 0; i < bindings.length; i++) {
00079                 Binding binding = bindings[i];
00080                 
00081                 String value = binding.getFrom();
00082                 int index = value.indexOf('.');
00083                 Object clientComp = subComponents.get(value.substring(0, index)); 
00084                 String clientItf = value.substring(index + 1);
00085                   
00086                 value = binding.getTo();
00087                 index = value.indexOf('.');
00088                 Object serverComp = subComponents.get(value.substring(0, index)); 
00089                 String serverItf = value.substring(index + 1);
00090                     
00091                 InstanceProviderTask createClientTask = 
00092                   (InstanceProviderTask)tasks.getTask("create", clientComp);
00093                 InstanceProviderTask createServerTask = 
00094                   (InstanceProviderTask)tasks.getTask("create", serverComp);
00095                 
00096                 int type = BindingBuilder.NORMAL_BINDING;
00097                 if (binding.getFrom().startsWith("this.")) {
00098                   type = BindingBuilder.EXPORT_BINDING;
00099                 }
00100                 if (binding.getTo().startsWith("this.")) {
00101                   type = BindingBuilder.IMPORT_BINDING;
00102                 }
00103                 
00104                 try {
00105                     // the task may already exist, in case of a shared component
00106                     // but multicast interfaces are handled specifically
00107                     if ( (BindingBuilder.EXPORT_BINDING == type) 
00108                             && (container instanceof BindingContainer) 
00109                             && (container instanceof InterfaceContainer)) {
00110                         Interface[] itfs = ((InterfaceContainer)container).getInterfaces();
00111                         for (int j = 0; j < itfs.length; j++) {
00112                             TypeInterface itf = (TypeInterface)itfs[j];
00113                             if (clientItf.equals(itf.getName())) {
00114                                 if (ProActiveTypeInterface.MULTICAST_CARDINALITY.equals(itf.getCardinality())) {
00115                                     throw new NoSuchElementException(clientItf);
00116                                 }
00117                             }
00118                         }
00119                         
00120                         
00121                     } else if ((clientComp instanceof BindingContainer)
00122                             && (clientComp instanceof InterfaceContainer)) {
00123                         Interface[] itfs = ((InterfaceContainer)clientComp).getInterfaces();
00124                         for (int j = 0; j < itfs.length; j++) {
00125                             TypeInterface itf = (TypeInterface)itfs[j];
00126                             if (clientItf.equals(itf.getName())) {
00127                                 if (ProActiveTypeInterface.MULTICAST_CARDINALITY.equals(itf.getCardinality())) {
00128                                     throw new NoSuchElementException(clientItf);
00129                                 }
00130                             }
00131                         }
00132                     }
00133                     
00134                     
00135                 
00136                   tasks.getTask("bind" + clientItf, clientComp);
00137               
00138                 } catch (NoSuchElementException e) {
00139                   BindTask bindTask = new BindTask(builder, type, clientItf, serverItf);
00140                   bindTask.setInstanceProviderTask(createClientTask);
00141                   bindTask.setServerInstanceProviderTask(createServerTask);
00142               
00143               // add an index to the task in case of a multicast binding
00144               if ( (BindingBuilder.EXPORT_BINDING == type) 
00145                       && (container instanceof BindingContainer) 
00146                       && (container instanceof InterfaceContainer)) {
00147                   // export binding : use current container
00148                   InterfaceContainer itfContainer = ((InterfaceContainer)container);
00149                   clientItf = setMulticastIndex(clientComp, clientItf, itfContainer);
00150               } else if ((clientComp instanceof BindingContainer)
00151                            && (clientComp instanceof InterfaceContainer)) {
00152                   // normal binding : use client component as container
00153                   InterfaceContainer itfContainer = ((InterfaceContainer)clientComp);
00154                   clientItf = setMulticastIndex(
00155                                               clientComp, clientItf, itfContainer);
00156                   }
00157               
00158                   tasks.addTask("bind" + clientItf, clientComp, bindTask);
00159                   
00160                   if (clientComp != container) {
00161                     Task addTask = tasks.getTask("add", new ComponentPair(
00162                       container, (Component)clientComp));
00163                     bindTask.addPreviousTask(addTask);
00164                   }
00165                   if (serverComp != container) {
00166                     Task addTask = tasks.getTask("add", new ComponentPair(
00167                       container, (Component)serverComp));
00168                     bindTask.addPreviousTask(addTask);
00169                   }
00170                   
00171                   Task startTask = tasks.getTask("start", clientComp);
00172                   startTask.addPreviousTask(bindTask);
00173                 }
00174               }
00175         }
00176           }
00177 
00184     private String setMulticastIndex(Object clientComp, String clientItf, InterfaceContainer itfContainer) {
00185 
00186         Interface[] itfs = itfContainer.getInterfaces();
00187             for (int j = 0; j < itfs.length; j++) {
00188                 TypeInterface itf = (TypeInterface)itfs[j];
00189                 if (clientItf.equals(itf.getName())) {
00190                    if (ProActiveTypeInterface.MULTICAST_CARDINALITY.equals(itf.getCardinality())) {
00191                        // ok, this is a multicast interface => multiple bindings allowed from this interface
00192                        // ==> need to create several tasks
00193                        int multicastIndex = 1;
00194                        if (multicastItfTaskIndexer.containsKey(clientComp+clientItf)) {
00195                            // increment and update
00196                            multicastIndex += multicastItfTaskIndexer.get(clientComp+clientItf);
00197                            multicastItfTaskIndexer.put(clientComp+clientItf, multicastIndex);
00198                        } else {
00199                            // initialize for this interface
00200                            multicastItfTaskIndexer.put(clientComp+clientItf, multicastIndex);
00201                        }
00202                        clientItf = clientItf+"-" + multicastIndex;      
00203                    }
00204                 }
00205             }
00206         return clientItf;
00207     }
00208 
00209 }

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