1 23 24 package org.objectweb.fractal.adl.bindings; 25 26 import java.util.HashMap ; 27 import java.util.List ; 28 import java.util.Map ; 29 import java.util.NoSuchElementException ; 30 31 import org.objectweb.deployment.scheduling.core.api.Task; 32 import org.objectweb.deployment.scheduling.component.api.InstanceProviderTask; 33 import org.objectweb.deployment.scheduling.component.lib.AbstractRequireInstanceProviderTask; 34 import org.objectweb.fractal.adl.ADLException; 35 import org.objectweb.fractal.adl.TaskMap; 36 import org.objectweb.fractal.adl.components.Component; 37 import org.objectweb.fractal.adl.components.ComponentContainer; 38 import org.objectweb.fractal.adl.components.ComponentPair; 39 import org.objectweb.fractal.adl.components.PrimitiveCompiler; 40 import org.objectweb.fractal.api.control.BindingController; 41 42 45 46 public class BindingCompiler implements BindingController, PrimitiveCompiler { 47 48 52 53 public final static String BUILDER_BINDING = "builder"; 54 55 58 59 public BindingBuilder builder; 60 61 65 public String [] listFc() { 66 return new String [] { BUILDER_BINDING }; 67 } 68 69 public Object lookupFc (final String itf) { 70 if (itf.equals(BUILDER_BINDING)) { 71 return builder; 72 } 73 return null; 74 } 75 76 public void bindFc (final String itf, final Object value) { 77 if (itf.equals(BUILDER_BINDING)) { 78 builder = (BindingBuilder)value; 79 } 80 } 81 82 public void unbindFc (final String itf) { 83 if (itf.equals(BUILDER_BINDING)) { 84 builder = null; 85 } 86 } 87 88 92 public void compile ( 93 final List path, 94 final ComponentContainer container, 95 final TaskMap tasks, 96 final Map context) throws ADLException 97 { 98 Map subComponents = new HashMap (); 99 subComponents.put("this", container); 100 Component[] comps = container.getComponents(); 101 for (int i = 0; i < comps.length; i++) { 102 subComponents.put(comps[i].getName(), comps[i]); 103 } 104 105 if (container instanceof BindingContainer) { 106 Binding[] bindings = ((BindingContainer)container).getBindings(); 107 for (int i = 0; i < bindings.length; i++) { 108 Binding binding = bindings[i]; 109 110 String value = binding.getFrom(); 111 int index = value.indexOf('.'); 112 Object clientComp = subComponents.get(value.substring(0, index)); 113 String clientItf = value.substring(index + 1); 114 115 value = binding.getTo(); 116 index = value.indexOf('.'); 117 Object serverComp = subComponents.get(value.substring(0, index)); 118 String serverItf = value.substring(index + 1); 119 120 InstanceProviderTask createClientTask = 121 (InstanceProviderTask)tasks.getTask("create", clientComp); 122 InstanceProviderTask createServerTask = 123 (InstanceProviderTask)tasks.getTask("create", serverComp); 124 125 int type = BindingBuilder.NORMAL_BINDING; 126 if (binding.getFrom().startsWith("this.")) { 127 type = BindingBuilder.EXPORT_BINDING; 128 } 129 if (binding.getTo().startsWith("this.")) { 130 type = BindingBuilder.IMPORT_BINDING; 131 } 132 133 try { 134 tasks.getTask("bind" + clientItf, clientComp); 136 } catch (NoSuchElementException e) { 137 BindTask bindTask = new BindTask(builder, type, clientItf, serverItf); 138 bindTask.setInstanceProviderTask(createClientTask); 139 bindTask.setServerInstanceProviderTask(createServerTask); 140 141 tasks.addTask("bind" + clientItf, clientComp, bindTask); 142 143 if (clientComp != container) { 144 Task addTask = tasks.getTask("add", new ComponentPair( 145 container, (Component)clientComp)); 146 bindTask.addPreviousTask(addTask); 147 } 148 if (serverComp != container) { 149 Task addTask = tasks.getTask("add", new ComponentPair( 150 container, (Component)serverComp)); 151 bindTask.addPreviousTask(addTask); 152 } 153 154 Task startTask = tasks.getTask("start", clientComp); 155 startTask.addPreviousTask(bindTask); 156 } 157 } 158 } 159 } 160 161 165 static class BindTask extends AbstractRequireInstanceProviderTask { 166 167 private InstanceProviderTask serverInstanceProviderTask; 168 169 private BindingBuilder builder; 170 171 private int type; 172 173 private String clientItf; 174 175 private String serverItf; 176 177 public BindTask ( 178 final BindingBuilder builder, 179 final int type, 180 final String clientItf, 181 final String serverItf) 182 { 183 this.builder = builder; 184 this.type = type; 185 this.clientItf = clientItf; 186 this.serverItf = serverItf; 187 } 188 189 public InstanceProviderTask getServerInstanceProviderTask () { 190 return serverInstanceProviderTask; 191 } 192 193 public void setServerInstanceProviderTask (final InstanceProviderTask task) { 194 if (serverInstanceProviderTask != null) { 195 removePreviousTask(serverInstanceProviderTask); 196 } 197 serverInstanceProviderTask = task; 198 if (serverInstanceProviderTask != null) { 199 addPreviousTask(serverInstanceProviderTask); 200 } 201 } 202 203 public void execute (final Object context) throws Exception { 204 Object client = getInstanceProviderTask().getInstance(); 205 Object server = getServerInstanceProviderTask().getInstance(); 206 builder.bindComponent(type, client, clientItf, server, serverItf, context); 207 } 208 209 public Object getResult () { 210 return null; 211 } 212 213 public void setResult (final Object result) { 214 } 215 216 public String toString () { 217 return "T" + System.identityHashCode(this) + 218 "[BindTask(" + clientItf + "," + serverItf + ")]"; 219 } 220 } 221 } 222 | Popular Tags |