00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 
00027 
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           
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                     
00106                     
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               
00144               if ( (BindingBuilder.EXPORT_BINDING == type) 
00145                       && (container instanceof BindingContainer) 
00146                       && (container instanceof InterfaceContainer)) {
00147                   
00148                   InterfaceContainer itfContainer = ((InterfaceContainer)container);
00149                   clientItf = setMulticastIndex(clientComp, clientItf, itfContainer);
00150               } else if ((clientComp instanceof BindingContainer)
00151                            && (clientComp instanceof InterfaceContainer)) {
00152                   
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                        
00192                        
00193                        int multicastIndex = 1;
00194                        if (multicastItfTaskIndexer.containsKey(clientComp+clientItf)) {
00195                            
00196                            multicastIndex += multicastItfTaskIndexer.get(clientComp+clientItf);
00197                            multicastItfTaskIndexer.put(clientComp+clientItf, multicastIndex);
00198                        } else {
00199                            
00200                            multicastItfTaskIndexer.put(clientComp+clientItf, multicastIndex);
00201                        }
00202                        clientItf = clientItf+"-" + multicastIndex;      
00203                    }
00204                 }
00205             }
00206         return clientItf;
00207     }
00208 
00209 }