1 23 24 package org.objectweb.fractal.adl.components; 25 26 import java.util.List ; 27 import java.util.Map ; 28 import java.util.NoSuchElementException ; 29 30 import org.objectweb.deployment.scheduling.component.api.InstanceProviderTask; 31 import org.objectweb.deployment.scheduling.component.lib.AbstractRequireInstanceProviderTask; 32 import org.objectweb.deployment.scheduling.component.lib.AbstractInitializationTask; 33 import org.objectweb.fractal.adl.ADLException; 34 import org.objectweb.fractal.adl.TaskMap; 35 import org.objectweb.fractal.api.control.BindingController; 36 37 40 41 public class PrimitiveComponentCompiler implements BindingController, PrimitiveCompiler { 42 43 47 48 public final static String BUILDER_BINDING = "builder"; 49 50 53 54 public ComponentBuilder builder; 55 56 60 public String [] listFc() { 61 return new String [] { BUILDER_BINDING }; 62 } 63 64 public Object lookupFc (final String itf) { 65 if (itf.equals(BUILDER_BINDING)) { 66 return builder; 67 } 68 return null; 69 } 70 71 public void bindFc (final String itf, final Object value) { 72 if (itf.equals(BUILDER_BINDING)) { 73 builder = (ComponentBuilder)value; 74 } 75 } 76 77 public void unbindFc (final String itf) { 78 if (itf.equals(BUILDER_BINDING)) { 79 builder = null; 80 } 81 } 82 83 87 public void compile ( 88 final List path, 89 final ComponentContainer container, 90 final TaskMap tasks, 91 final Map context) throws ADLException 92 { 93 InstanceProviderTask createTask = 94 (InstanceProviderTask)tasks.getTask("create", container); 95 96 StartTask startTask = new StartTask(builder); 97 startTask.setInstanceProviderTask(createTask); 98 tasks.addTask("start", container, startTask); 99 100 Component[] comps = ((ComponentContainer)container).getComponents(); 101 for (int i = 0; i < comps.length; i++) { 102 InstanceProviderTask createSubComponentTask = 103 (InstanceProviderTask)tasks.getTask("create", comps[i]); 104 105 ComponentPair pair = new ComponentPair(container, comps[i]); 106 try { 107 tasks.getTask("add", pair); 109 } catch (NoSuchElementException e) { 110 AddTask addTask = new AddTask(builder, comps[i].getName()); 111 addTask.setInstanceProviderTask(createTask); 112 addTask.setSubInstanceProviderTask(createSubComponentTask); 113 tasks.addTask("add", pair, addTask); 114 } 115 116 } 119 } 120 121 125 static class AddTask extends AbstractRequireInstanceProviderTask { 126 127 private ComponentBuilder builder; 128 129 private InstanceProviderTask subInstanceProviderTask; 130 131 private String name; 132 133 public AddTask (final ComponentBuilder builder, final String name) { 134 this.builder = builder; 135 this.name = name; 136 } 137 138 public InstanceProviderTask getSubInstanceProviderTask () { 139 return subInstanceProviderTask; 140 } 141 142 public void setSubInstanceProviderTask (final InstanceProviderTask task) { 143 if (subInstanceProviderTask != null) { 144 removePreviousTask(subInstanceProviderTask); 145 } 146 subInstanceProviderTask = task; 147 if (subInstanceProviderTask != null) { 148 addPreviousTask(subInstanceProviderTask); 149 } 150 } 151 152 public void execute (final Object context) throws Exception { 153 Object parent = getInstanceProviderTask().getInstance(); 154 Object child = getSubInstanceProviderTask().getInstance(); 155 builder.addComponent(parent, child, name, context); 156 } 157 158 public Object getResult () { 159 return null; 160 } 161 162 public void setResult (Object result) { 163 } 164 165 public String toString () { 166 return "T" + System.identityHashCode(this) + "[AddTask(" + name + ")]"; 167 } 168 } 169 170 static class StartTask extends AbstractInitializationTask { 171 172 private ComponentBuilder builder; 173 174 public StartTask (final ComponentBuilder builder) { 175 this.builder = builder; 176 } 177 178 public void execute (final Object context) throws Exception { 179 builder.startComponent(getInstanceProviderTask().getInstance(), context); 180 } 181 182 public String toString () { 183 return "T" + System.identityHashCode(this) + "[StartTask()]"; 184 } 185 } 186 } 187 | Popular Tags |