1 23 24 package org.objectweb.fractal.julia.factory; 25 26 import org.objectweb.fractal.api.Component; 27 import org.objectweb.fractal.api.NoSuchInterfaceException; 28 import org.objectweb.fractal.api.Type; 29 import org.objectweb.fractal.api.factory.GenericFactory; 30 import org.objectweb.fractal.api.factory.InstantiationException; 31 import org.objectweb.fractal.api.type.ComponentType; 32 import org.objectweb.fractal.api.type.InterfaceType; 33 import org.objectweb.fractal.api.type.TypeFactory; 34 35 import org.objectweb.fractal.julia.Controller; 36 import org.objectweb.fractal.julia.InitializationContext; 37 import org.objectweb.fractal.julia.loader.Loader; 38 import org.objectweb.fractal.julia.loader.Tree; 39 40 import java.util.HashMap ; 41 import java.util.List ; 42 import java.util.Map ; 43 44 59 60 public class BasicGenericFactoryMixin implements GenericFactory { 61 62 66 public BasicGenericFactoryMixin () { 67 } 68 69 73 public Component newFcInstance ( 74 final Type type, 75 final Object controllerDesc, 76 final Object contentDesc) throws InstantiationException 77 { 78 Object loader; 79 Object controller; 80 81 if (controllerDesc instanceof Object []) { 82 loader = getFcLoader(((Object [])controllerDesc)[0]); 83 controller = ((Object [])controllerDesc)[1]; 84 } else { 85 loader = getFcLoader(null); 86 controller = controllerDesc; 87 } 88 89 Tree typeTree = getFcTypeDescriptor(type); 91 Tree controllerTree = getFcControllerDescriptor(controller); 92 Tree contentTree = getFcContentDescriptor(contentDesc); 93 Tree componentTree = new Tree(new Tree[] { 94 new Tree("org.objectweb.fractal.julia.asm.ContextClassGenerator"), 95 typeTree, 96 controllerTree, 97 contentTree 98 }); 99 100 Class initializationContextClass; 103 try { 104 initializationContextClass = 105 _this_weaveableL.loadClass(componentTree, loader); 106 } catch (Exception e) { 107 throw new ChainedInstantiationException( 108 e, 109 null, 110 "Cannot load the specific InitializationContext sub class " + 111 "needed to create the component"); 112 } 113 InitializationContext initializationContext; 114 try { 115 initializationContext = 116 (InitializationContext)initializationContextClass.newInstance(); 117 } catch (Exception e) { 118 throw new ChainedInstantiationException( 119 e, 120 null, 121 "Cannot create the specific InitializationContext object " + 122 "needed to create the component"); 123 } 124 125 if (!(contentDesc instanceof String )) { 127 initializationContext.content = contentDesc; 128 } 129 initializationContext.hints = _this_weaveableTF; 130 initializationContext.create(); 131 132 List controllers = initializationContext.controllers; 134 for (int i = 0; i < controllers.size(); ++i) { 135 Object o = controllers.get(i); 136 if (o instanceof Controller) { 137 ((Controller)o).initFcController(initializationContext); 138 } 139 } 140 141 try { 143 Component c = (Component)initializationContext.getInterface("component"); 144 return (Component)c.getFcInterface("component"); 145 } catch (NoSuchInterfaceException e) { 146 throw new ChainedInstantiationException( 147 e, 148 null, 149 "The created component does not provide the Component interface"); 150 } 151 } 152 153 public Object getFcLoader (final Object loader) { 154 return loader; 155 } 156 157 167 168 public Tree getFcTypeDescriptor (final Type type) 169 throws InstantiationException 170 { 171 ComponentType compType; 173 try { 174 compType = (ComponentType)type; 175 } catch (ClassCastException e) { 176 throw new ChainedInstantiationException( 177 e, null, "The component type must be an instance of ComponentType"); 178 } 179 180 InterfaceType[] itfTypes = compType.getFcInterfaceTypes(); 182 Tree[] itfTrees = new Tree[itfTypes.length]; 183 for (int i = 0; i < itfTypes.length; i++) { 184 InterfaceType itfType = itfTypes[i]; 185 String itfName = itfType.getFcItfName(); 186 if (!itfName.equals("attribute-controller")) { 187 if (itfName.equals("component") || itfName.endsWith("-controller")) { 188 throw new ChainedInstantiationException( 189 null, 190 null, 191 "The component type must not contain control interface types"); 192 } 193 } 194 itfTrees[i] = new Tree(new Tree[] { 195 new Tree(itfName), 196 new Tree(itfType.getFcItfSignature()), 197 new Tree(itfType.isFcClientItf() ? "true" : "false"), 198 new Tree(itfType.isFcOptionalItf() ? "true" : "false"), 199 new Tree(itfType.isFcCollectionItf() ? "true" : "false") 200 }); 201 } 202 203 return new Tree(itfTrees); 205 } 206 207 217 218 public Tree getFcControllerDescriptor (final Object controllerDesc) 219 throws InstantiationException 220 { 221 Map definitions = new HashMap (); 222 definitions.put("attributeControllerInterface", new Tree("QUOTE")); 223 definitions.put("interfaceName", new Tree("QUOTE")); 224 try { 225 return _this_weaveableL.evalTree( 226 _this_weaveableL.loadTree((String )controllerDesc), definitions); 227 } catch (Exception e) { 228 throw new ChainedInstantiationException( 229 e, 230 null, 231 "Cannot load the '" + controllerDesc + "' controller descriptor"); 232 } 233 } 234 235 243 244 public Tree getFcContentDescriptor (final Object contentDesc) { 245 if (contentDesc instanceof String ) { 246 return new Tree((String )contentDesc); 247 } else if (contentDesc != null && !(contentDesc instanceof Object [])) { 248 return new Tree(contentDesc.getClass().getName()); 249 } else { 250 return new Tree("EMPTY"); 251 } 252 } 253 254 258 263 264 public Loader _this_weaveableL; 265 266 271 272 public TypeFactory _this_weaveableTF; 273 } 274 | Popular Tags |