KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > oddjob > designer > factory > DesignFactoryBean


1 /*
2  * (c) Rob Gordon 2005.
3  */

4 package org.oddjob.designer.factory;
5
6 import java.io.InputStream JavaDoc;
7 import java.util.HashMap JavaDoc;
8 import java.util.Iterator JavaDoc;
9 import java.util.LinkedHashMap JavaDoc;
10 import java.util.Map JavaDoc;
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 /**
23  * A factory which can create DesignComponents and DesignElements.
24  */

25 public class DesignFactoryBean {
26     public static Logger logger = Logger.getLogger(DesignFactoryBean.class);
27     
28     public static final String JavaDoc UNKNOWN_TAG = "other";
29     
30     /** Map of component name to component definition */
31     private final Map JavaDoc /*<String, ComponentDefinition> */components = new HashMap JavaDoc();
32     
33     /** Hierarchy of component groups */
34     private final SimpleHierarchy top = new SimpleHierarchy(String JavaDoc.class);
35     
36     /** Map of value type name to value type definition */
37     private final Map JavaDoc /*<String, TypeDefinition> */types = new LinkedHashMap JavaDoc();
38     
39     /**
40      * Constructor. Loads the factory.
41      *
42      */

43     public DesignFactoryBean(InputStream JavaDoc in) {
44         load(in);
45         logger.debug("Loaded " + components.size() + " definitions.");
46     }
47
48     /**
49      * Called during parsing of the XML with each ComponentDefinition.
50      *
51      * @param compdef A ComponentDefinition.
52      */

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     /**
64      * Called during parsing of the XML with each TypeDefinition.
65      *
66      * @param compdef A TypeDefinition.
67      */

68     public void addConfiguredTypeDef(TypeDefinition typedef) {
69         types.put(typedef.getTag(), typedef);
70     }
71     
72     /**
73      * Loads the factory.
74      *
75      */

76     void load(InputStream JavaDoc in) {
77         components.clear();
78         types.clear();
79         ArooaFactory af = new ArooaFactory();
80         af.setObjectToConfigure(this);
81         af.build(in);
82     }
83
84     /**
85      * Returns a hierarchy of ComponentAction objects which can be used in a
86      * hierarchical menu structure for creating and adding components.
87      *
88      * @param subject The object the new component will be added to.
89      * @param element The element name the child appears under.
90      *
91      * @return A SimpleHierarchy of ComponentAction objects.
92      */

93     public SimpleHierarchy childActions(final Object JavaDoc subject, final String JavaDoc element) {
94         return top.convert(new HierarchyConversion() {
95             public Object JavaDoc convert(final Object JavaDoc from) {
96                 return new ComponentAction() {
97                     public String JavaDoc getName() {
98                         return (String JavaDoc) from;
99                     }
100                     public void perform() {
101                         DesignComponent dc = createComponent((String JavaDoc) from);
102                         IntrospectionHelper ih = IntrospectionHelper
103                                 .getHelper(subject.getClass());
104                         ih.storeComponent(subject, dc, element);
105                     }
106                 };
107             }
108         }, ComponentAction.class);
109     }
110     
111     /**
112      * Create a component for the given name.
113      *
114      * @param name The name of the tag identifiying the component.
115      * @return A created component. Never null.
116      *
117      * @throws ArooaException If the component can't be created.
118      */

119     public DesignComponent createComponent(String JavaDoc 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 JavaDoc value = compdef.getClassname();
127         if (value == null) {
128             throw new IllegalStateException JavaDoc("Component definition for ["
129                     + name + "] does not specify a class.");
130         }
131         
132         Object JavaDoc o = null;
133         try {
134             o = SimpleObjectFactory.createObjectFromClass(value);
135         } catch (IllegalAccessException JavaDoc e) {
136             throw new ArooaException("Illegal access attempting to create object of class "
137                 + value + " for shorthand " + name, e);
138         } catch (InstantiationException JavaDoc 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     /**
150      * Creates a ObjectFactory which creates components.
151      *
152      * @return An ObjectFactory.
153      */

154     public ObjectFactory componentFactory() {
155         return new ObjectFactory() {
156             public Object JavaDoc createObject(String JavaDoc name) throws ArooaException {
157                 return createComponent(name);
158             }
159         };
160     }
161
162     /**
163      * Return supported child type for a given type.
164      *
165      * @param type The type.
166      * @return A list of supported types.
167      */

168     public String JavaDoc[] supportedTypes(Class JavaDoc type) {
169         for (Iterator JavaDoc 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 JavaDoc("No defintion for [" + type.getName() + "]");
176     }
177
178     public String JavaDoc[] supportedTypes(TypeDefinition typedef) {
179         String JavaDoc [] supports = typedef.getSupports();
180         if (supports == null) {
181             return new String JavaDoc[0];
182         }
183         if (supports.length == 1 && supports[0].equals("all")) {
184             return allTypes();
185         }
186         return supports;
187     }
188     
189     public String JavaDoc[] allTypes() {
190         return (String JavaDoc[]) types.keySet().toArray(new String JavaDoc[0]);
191     }
192     
193     /**
194      * Creat a type for the given type name.
195      *
196      * @param name The type name.
197      * @return The created type.
198      *
199      * @throws ArooaException If the type can't be created.
200      */

201     public DesignElementType createType(String JavaDoc 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 JavaDoc type = typedef.getTypeClass();
209         if (type == null) {
210             throw new IllegalStateException JavaDoc("Type definition for ["
211                     + name + "] does not specify a class.");
212         }
213         
214         Object JavaDoc o = null;
215         try {
216             o = type.newInstance();
217         } catch (IllegalAccessException JavaDoc e) {
218             throw new ArooaException("Illegal access attempting to create object of class "
219                 + type+ " for shorthand " + name, e);
220         } catch (InstantiationException JavaDoc 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 JavaDoc createObject(String JavaDoc name) throws ArooaException {
234                 return createType(name);
235             }
236         };
237     }
238
239 }
240
Popular Tags