1 23 24 package org.objectweb.fractal.adl.implementations; 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.FactoryProviderTask; 31 import org.objectweb.deployment.scheduling.component.lib.AbstractInstanceProviderTask; 32 import org.objectweb.fractal.adl.ADLException; 33 import org.objectweb.fractal.adl.Definition; 34 import org.objectweb.fractal.adl.Node; 35 import org.objectweb.fractal.adl.TaskMap; 36 import org.objectweb.fractal.adl.attributes.AttributesContainer; 37 import org.objectweb.fractal.adl.components.Component; 38 import org.objectweb.fractal.adl.components.ComponentContainer; 39 import org.objectweb.fractal.adl.components.PrimitiveCompiler; 40 import org.objectweb.fractal.api.control.BindingController; 41 42 45 46 public class ImplementationCompiler implements BindingController, PrimitiveCompiler { 47 48 52 53 public final static String BUILDER_BINDING = "builder"; 54 55 58 59 public ImplementationBuilder 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 = (ImplementationBuilder)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 boolean template = context != null && "true".equals(context.get("template")); 99 100 String implementation = null; 101 if (container instanceof ImplementationContainer) { 102 ImplementationContainer ic = (ImplementationContainer)container; 103 Implementation i = ic.getImplementation(); 104 if (i != null) { 105 implementation = i.getClassName(); 106 } 107 } 108 109 String controller = null; 110 if (container instanceof ControllerContainer) { 111 ControllerContainer cc = (ControllerContainer)container; 112 if (cc.getController() != null) { 113 controller = cc.getController().getDescriptor(); 114 } 115 } 116 117 String templateController = null; 118 if (container instanceof TemplateControllerContainer) { 119 TemplateControllerContainer tcc = (TemplateControllerContainer)container; 120 if (tcc.getTemplateController() != null) { 121 templateController = tcc.getTemplateController().getDescriptor(); 122 template = true; 123 } 124 } 125 126 String name = null; 127 if (container instanceof Definition) { 128 name = ((Definition)container).getName(); 129 } else if (container instanceof Component) { 130 name = ((Component)container).getName(); 131 } 132 133 String definition = null; 134 if (container instanceof Definition) { 135 definition = name; 136 } else { 137 definition = (String )((Node)container).astGetDecoration("definition"); 138 } 139 140 boolean attrs = false; 141 if (container instanceof AttributesContainer) { 142 attrs = ((AttributesContainer)container).getAttributes() != null; 143 } 144 Component[] comps = ((ComponentContainer)container).getComponents(); 145 146 try { 147 tasks.getTask("create", container); 149 } catch (NoSuchElementException e) { 150 AbstractInstanceProviderTask createTask; 151 if (comps.length > 0 || implementation == null) { 152 if (implementation != null) { 153 throw new ADLException("Implementation must be empty", (Node)container); 154 } 155 if (controller == null) { 156 controller = "composite"; 157 } 158 if (template) { 159 if (templateController == null) { 160 if (attrs) { 161 templateController = "parametricCompositeTemplate"; 162 } else { 163 templateController = "compositeTemplate"; 164 } 165 } 166 createTask = newCreateTask(path, container, name, definition, templateController, new Object [] { controller, null }, context); 167 } else { 168 createTask = newCreateTask(path, container, name, definition, controller, null, context); 169 } 170 } else { 171 if (controller == null) { 172 controller = "primitive"; 173 } 174 if (template) { 175 if (templateController == null) { 176 if (attrs) { 177 templateController = "parametricPrimitiveTemplate"; 178 } else { 179 templateController = "primitiveTemplate"; 180 } 181 } 182 createTask = newCreateTask(path, container, name, definition, templateController, new Object [] { controller, implementation }, context); 183 } else { 184 createTask = newCreateTask(path, container, name, definition, controller, implementation, context); 185 } 186 } 187 188 FactoryProviderTask typeTask = 189 (FactoryProviderTask)tasks.getTask("type", container); 190 createTask.setFactoryProviderTask(typeTask); 191 192 tasks.addTask("create", container, createTask); 193 } 194 } 195 196 public AbstractInstanceProviderTask newCreateTask ( 197 final List path, 198 final ComponentContainer container, 199 final String name, 200 final String definition, 201 final Object controller, 202 final Object implementation, 203 final Map context) 204 { 205 return new CreateTask(builder, name, definition, controller, implementation); 206 } 207 208 212 static class CreateTask extends AbstractInstanceProviderTask { 213 214 ImplementationBuilder builder; 215 216 String name; 217 218 String definition; 219 220 Object controllerDesc; 221 222 Object contentDesc; 223 224 public CreateTask ( 225 final ImplementationBuilder builder, 226 final String name, 227 final String definition, 228 final Object controllerDesc, 229 final Object contentDesc) 230 { 231 this.builder = builder; 232 this.name = name; 233 this.definition = definition; 234 this.controllerDesc = controllerDesc; 235 this.contentDesc = contentDesc; 236 } 237 238 public void execute (final Object context) throws Exception { 239 if (getInstance() != null) { 240 return; 241 } 242 Object type = getFactoryProviderTask().getFactory(); 243 Object result = builder.createComponent( 244 type, name, definition, controllerDesc, contentDesc, context); 245 setInstance(result); 246 } 247 248 public String toString () { 249 return "T" + System.identityHashCode(this) + 250 "[CreateTask(" + name + "," + controllerDesc + "," + contentDesc + ")]"; 251 } 252 } 253 } 254 | Popular Tags |