1 23 24 package org.objectweb.fractal.adl.attributes; 25 26 import java.util.List ; 27 import java.util.Map ; 28 import java.util.NoSuchElementException ; 29 30 import org.objectweb.deployment.scheduling.core.api.Task; 31 import org.objectweb.deployment.scheduling.component.api.InstanceProviderTask; 32 import org.objectweb.deployment.scheduling.component.lib.AbstractAttributeSetterTask; 33 import org.objectweb.fractal.adl.ADLException; 34 import org.objectweb.fractal.adl.TaskMap; 35 import org.objectweb.fractal.adl.components.ComponentContainer; 36 import org.objectweb.fractal.adl.components.PrimitiveCompiler; 37 import org.objectweb.fractal.api.control.BindingController; 38 39 42 43 public class AttributeCompiler implements BindingController, PrimitiveCompiler { 44 45 49 50 public final static String BUILDER_BINDING = "builder"; 51 52 55 56 public AttributeBuilder builder; 57 58 62 public String [] listFc() { 63 return new String [] { BUILDER_BINDING }; 64 } 65 66 public Object lookupFc (final String itf) { 67 if (itf.equals(BUILDER_BINDING)) { 68 return builder; 69 } 70 return null; 71 } 72 73 public void bindFc (final String itf, final Object value) { 74 if (itf.equals(BUILDER_BINDING)) { 75 builder = (AttributeBuilder)value; 76 } 77 } 78 79 public void unbindFc (final String itf) { 80 if (itf.equals(BUILDER_BINDING)) { 81 builder = null; 82 } 83 } 84 85 89 public void compile ( 90 final List path, 91 final ComponentContainer container, 92 final TaskMap tasks, 93 final Map context) throws ADLException 94 { 95 if (container instanceof AttributesContainer) { 96 Attributes attributes = ((AttributesContainer)container).getAttributes(); 97 if (attributes != null) { 98 InstanceProviderTask createTask = 99 (InstanceProviderTask)tasks.getTask("create", container); 100 101 Task startTask = tasks.getTask("start", container); 102 103 Attribute[] attrs = attributes.getAttributes(); 104 for (int i = 0; i < attrs.length; ++i) { 105 try { 106 tasks.getTask("attr" + attrs[i].getName(), container); 108 } catch (NoSuchElementException e) { 109 AttributeTask t = new AttributeTask( 110 builder, 111 attributes.getSignature(), 112 attrs[i].getName(), 113 attrs[i].getValue()); 114 t.setInstanceProviderTask(createTask); 115 116 startTask.addPreviousTask(t); 117 118 tasks.addTask("attr" + attrs[i].getName(), container, t); 119 } 120 } 121 } 122 } 123 } 124 125 129 static class AttributeTask extends AbstractAttributeSetterTask { 130 131 private AttributeBuilder builder; 132 133 private String attributeController; 134 135 private String name; 136 137 public AttributeTask ( 138 final AttributeBuilder builder, 139 final String attributeController, 140 final String name, 141 final String value) 142 { 143 this.builder = builder; 144 this.attributeController = attributeController; 145 this.name = name; 146 setValue(value); 147 } 148 149 public void execute (final Object context) throws Exception { 150 Object component = getInstanceProviderTask().getInstance(); 151 builder.setAttribute( 152 component, 153 attributeController, 154 name, 155 (String )getValue(), 156 context); 157 } 158 159 public String toString () { 160 return "T" + System.identityHashCode(this) + 161 "[AttributeTask(" + name + "," + getValue() + ")]"; 162 } 163 } 164 } 165 | Popular Tags |