1 4 package org.oddjob.designer.factory; 5 6 import java.io.InputStream ; 7 import java.util.HashMap ; 8 import java.util.Iterator ; 9 import java.util.LinkedHashMap ; 10 import java.util.Map ; 11 12 import org.apache.log4j.Logger; 13 import org.oddjob.arooa.ArooaException; 14 import org.oddjob.arooa.ArooaFactory; 15 import org.oddjob.arooa.ObjectFactory; 16 import org.oddjob.arooa.SimpleObjectFactory; 17 import org.oddjob.arooa.reflect.IntrospectionHelper; 18 import org.oddjob.designer.model.ComponentAction; 19 import org.oddjob.designer.model.DesignComponent; 20 import org.oddjob.designer.model.DesignElementType; 21 22 25 public class DesignFactoryBean { 26 public static Logger logger = Logger.getLogger(DesignFactoryBean.class); 27 28 public static final String UNKNOWN_TAG = "other"; 29 30 31 private final Map components = new HashMap (); 32 33 34 private final SimpleHierarchy top = new SimpleHierarchy(String .class); 35 36 37 private final Map types = new LinkedHashMap (); 38 39 43 public DesignFactoryBean(InputStream in) { 44 load(in); 45 logger.debug("Loaded " + components.size() + " definitions."); 46 } 47 48 53 public void addConfiguredCompDef(ComponentDefinition compdef) { 54 components.put(compdef.getTag(), compdef); 55 if (compdef.getGroup() == null) { 56 top.addLeaf(compdef.getTag()); 57 } 58 else { 59 top.addToHierarchy(compdef.getGroup(), compdef.getTag()); 60 } 61 } 62 63 68 public void addConfiguredTypeDef(TypeDefinition typedef) { 69 types.put(typedef.getTag(), typedef); 70 } 71 72 76 void load(InputStream in) { 77 components.clear(); 78 types.clear(); 79 ArooaFactory af = new ArooaFactory(); 80 af.setObjectToConfigure(this); 81 af.build(in); 82 } 83 84 93 public SimpleHierarchy childActions(final Object subject, final String element) { 94 return top.convert(new HierarchyConversion() { 95 public Object convert(final Object from) { 96 return new ComponentAction() { 97 public String getName() { 98 return (String ) from; 99 } 100 public void perform() { 101 DesignComponent dc = createComponent((String ) from); 102 IntrospectionHelper ih = IntrospectionHelper 103 .getHelper(subject.getClass()); 104 ih.storeComponent(subject, dc, element); 105 } 106 }; 107 } 108 }, ComponentAction.class); 109 } 110 111 119 public DesignComponent createComponent(String name) throws ArooaException { 120 ComponentDefinition compdef = (ComponentDefinition) components.get(name); 121 if (compdef == null) { 122 logger.debug("No definition for [" + name + "]"); 123 return null; 124 } 125 126 String value = compdef.getClassname(); 127 if (value == null) { 128 throw new IllegalStateException ("Component definition for [" 129 + name + "] does not specify a class."); 130 } 131 132 Object o = null; 133 try { 134 o = SimpleObjectFactory.createObjectFromClass(value); 135 } catch (IllegalAccessException e) { 136 throw new ArooaException("Illegal access attempting to create object of class " 137 + value + " for shorthand " + name, e); 138 } catch (InstantiationException e) { 139 throw new ArooaException("InstantiationException attempting to create object of class " 140 + value + " for shorthand " + name, e); 141 } 142 143 DesignComponent dc = (DesignComponent) o; 144 dc.tag(name); 145 dc.name(compdef.getName()); 146 return dc; 147 } 148 149 154 public ObjectFactory componentFactory() { 155 return new ObjectFactory() { 156 public Object createObject(String name) throws ArooaException { 157 return createComponent(name); 158 } 159 }; 160 } 161 162 168 public String [] supportedTypes(Class type) { 169 for (Iterator it = types.values().iterator(); it.hasNext(); ) { 170 TypeDefinition td = (TypeDefinition) it.next(); 171 if (td.getTypeClass().equals(type)) { 172 return supportedTypes(td); 173 } 174 } 175 throw new IllegalArgumentException ("No defintion for [" + type.getName() + "]"); 176 } 177 178 public String [] supportedTypes(TypeDefinition typedef) { 179 String [] supports = typedef.getSupports(); 180 if (supports == null) { 181 return new String [0]; 182 } 183 if (supports.length == 1 && supports[0].equals("all")) { 184 return allTypes(); 185 } 186 return supports; 187 } 188 189 public String [] allTypes() { 190 return (String []) types.keySet().toArray(new String [0]); 191 } 192 193 201 public DesignElementType createType(String name) throws ArooaException { 202 TypeDefinition typedef = (TypeDefinition) types.get(name); 203 if (typedef == null) { 204 logger.debug("No definition for [" + name + "]"); 205 return null; 206 } 207 208 Class type = typedef.getTypeClass(); 209 if (type == null) { 210 throw new IllegalStateException ("Type definition for [" 211 + name + "] does not specify a class."); 212 } 213 214 Object o = null; 215 try { 216 o = type.newInstance(); 217 } catch (IllegalAccessException e) { 218 throw new ArooaException("Illegal access attempting to create object of class " 219 + type+ " for shorthand " + name, e); 220 } catch (InstantiationException e) { 221 throw new ArooaException("InstantiationException attempting to create object of class " 222 + type + " for shorthand " + name, e); 223 } 224 225 DesignElementType de = (DesignElementType) o; 226 de.type(name); 227 de.supportedTypes(supportedTypes(typedef)); 228 return de; 229 } 230 231 public ObjectFactory valueFactory() { 232 return new ObjectFactory() { 233 public Object createObject(String name) throws ArooaException { 234 return createType(name); 235 } 236 }; 237 } 238 239 } 240 | Popular Tags |