View Javadoc

1   /*
2    ORG Usurper is a random value object generator library 
3    Copyright (C) 2007  Pierre-Antoine Grégoire
4    
5    This library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Lesser General Public
7    License as published by the Free Software Foundation; either
8    version 2.1 of the License, or (at your option) any later version.
9    
10   This library is distributed in the hope that it will be useful,
11   but WITHOUT ANY WARRANTY; without even the implied warranty of
12   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13   Lesser General Public License for more details.
14   
15   You should have received a copy of the GNU Lesser General Public
16   License along with this library; if not, write to the Free Software
17   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
18   */
19  package org.org.usurper.springframework;
20  
21  import java.util.HashMap;
22  import java.util.HashSet;
23  import java.util.List;
24  import java.util.Map;
25  import java.util.Set;
26  
27  import org.org.usurper.handlers.IHandler;
28  import org.org.usurper.handlers.basic.AbstractPropertyTypeHandler;
29  import org.org.usurper.handlers.basic.AbstractSpecificPropertyHandler;
30  import org.org.usurper.handlers.basic.ArrayHandler;
31  import org.org.usurper.handlers.basic.EnumHandler;
32  import org.org.usurper.model.ITargetDefinition;
33  import org.org.usurper.model.PropertyTypeDefinition;
34  import org.org.usurper.model.SpecificPropertyDefinition;
35  import org.org.usurper.setup.ICountCallback;
36  import org.org.usurper.setup.IUsurperGeneratorSetup;
37  import org.org.usurper.setup.UsurperGeneratorSetup;
38  import org.org.usurper.setup.constants.OnMissingHandlers;
39  import org.org.usurper.setup.constants.PropertyWritingMechanism;
40  import org.springframework.beans.factory.FactoryBean;
41  import org.springframework.beans.factory.InitializingBean;
42  
43  /**
44   * The Class UsurperGeneratorSetupFactoryBean is a Spring compliant FactoryBean and InitializingBean.
45   * As such, it can be used as any other Spring Factory Bean.
46   * It generates an UsurperGeneratorSetup.
47   */
48  public class UsurperGeneratorSetupFactoryBean implements FactoryBean, InitializingBean {
49  
50      private ArrayHandler arrayHandler;
51      private ICountCallback countCallback;
52      private EnumHandler enumHandler;
53      private OnMissingHandlers onMissingHandlers=null;
54      private PropertyWritingMechanism propertyWritingMechanism=null;
55      private Map<PropertyTypeDefinition, AbstractPropertyTypeHandler> propertyTypeHandlersMap;
56      private Map<SpecificPropertyDefinition, AbstractSpecificPropertyHandler> specificPropertyHandlersMap;
57      private UsurperGeneratorSetup usurperGeneratorSetup;
58      private IUsurperGeneratorSetup parentSetup;
59  
60      public UsurperGeneratorSetupFactoryBean() {
61          super();
62      }
63  
64      public Object getObject() throws Exception {
65          return usurperGeneratorSetup;
66      }
67  
68      @SuppressWarnings("unchecked")
69      public Class getObjectType() {
70          return IUsurperGeneratorSetup.class;
71      }
72  
73      public boolean isSingleton() {
74          return false;
75      }
76  
77      @SuppressWarnings("unchecked")
78      public void afterPropertiesSet() throws Exception {
79          if (parentSetup != null) {
80              usurperGeneratorSetup = new UsurperGeneratorSetup(parentSetup);
81          } else {
82              usurperGeneratorSetup = new UsurperGeneratorSetup();
83          }
84          if (arrayHandler != null) {
85              usurperGeneratorSetup.setArrayHandler(arrayHandler);
86          }
87          if (enumHandler != null) {
88              usurperGeneratorSetup.setArrayHandler(arrayHandler);
89          }
90          if (onMissingHandlers != null) {
91              usurperGeneratorSetup.onMissingHandlers(onMissingHandlers);
92          }
93          if (propertyWritingMechanism != null) {
94              usurperGeneratorSetup.usePropertyWritingMechanism(propertyWritingMechanism);
95          }
96          if (countCallback != null) {
97              usurperGeneratorSetup.setCountCallback(countCallback);
98          }
99          Map<ITargetDefinition, IHandler> handlers = new HashMap<ITargetDefinition, IHandler>();
100         if (propertyTypeHandlersMap != null) {
101             handlers.putAll(propertyTypeHandlersMap);
102         }
103         if (specificPropertyHandlersMap != null) {
104             handlers.putAll(specificPropertyHandlersMap);
105         }
106         usurperGeneratorSetup.setAllHandlers(handlers);
107     }
108 
109     public void setArrayHandlerName(String arrayHandler) {
110         try {
111             this.arrayHandler = (ArrayHandler) Class.forName(arrayHandler).newInstance();
112         } catch (Exception e) {
113             throw new IllegalArgumentException("Impossible to instanciate class " + arrayHandler, e);
114         }
115     }
116 
117     public void setArrayHandler(ArrayHandler arrayHandler) {
118         this.arrayHandler = arrayHandler;
119     }
120 
121     public void setCountCallback(ICountCallback countCallback) {
122         this.countCallback = countCallback;
123     }
124 
125     public void setCountCallbackName(String countCallback) {
126         try {
127             this.countCallback = (ICountCallback) Class.forName(countCallback).newInstance();
128         } catch (Exception e) {
129             throw new IllegalArgumentException("Impossible to instanciate class " + countCallback, e);
130         }
131     }
132 
133     public void setConstructorParameterTypes(List<String> constructorParameterTypesNames) {
134         Class<?>[] constructorParameterTypes = new Class<?>[constructorParameterTypesNames.size()];
135         int i = 0;
136         for (String constructorParameterType : constructorParameterTypesNames) {
137             try {
138                 constructorParameterTypes[i++] = Class.forName(constructorParameterType);
139             } catch (Exception e) {
140                 throw new IllegalArgumentException("Impossible to find class " + constructorParameterType, e);
141             }
142         }
143     }
144 
145     public void setEnumHandlerName(String enumHandler) {
146         try {
147             this.enumHandler = (EnumHandler) Class.forName(enumHandler).newInstance();
148         } catch (Exception e) {
149             throw new IllegalArgumentException("Impossible to instanciate class " + enumHandler, e);
150         }
151     }
152 
153     public void setEnumHandler(EnumHandler enumHandler) {
154         this.enumHandler = enumHandler;
155     }
156 
157     public void setOnMissingHandlers(String onMissingHandlers) {
158         this.onMissingHandlers = OnMissingHandlers.valueOf(onMissingHandlers.toUpperCase());
159     }
160 
161     public void setPropertyTypeHandlers(Set<AbstractPropertyTypeHandler> propertyTypeHandlers) {
162         initPropertyTypeHandlersMap();
163         for (AbstractPropertyTypeHandler propertyTypeHandler : propertyTypeHandlers) {
164             for (PropertyTypeDefinition propertyTypeDefinition : propertyTypeHandler.getHandledTypes()) {
165                 this.propertyTypeHandlersMap.put(propertyTypeDefinition, propertyTypeHandler);
166             }
167         }
168     }
169 
170     public void setSpecificPropertyHandlers(Set<AbstractSpecificPropertyHandler> specificPropertyHandlers) {
171         initSpecificPropertyHandlersMap();
172         for (AbstractSpecificPropertyHandler specificPropertyHandler : specificPropertyHandlers) {
173             this.specificPropertyHandlersMap.put(specificPropertyHandler.getTargetProperty(), specificPropertyHandler);
174         }
175     }
176 
177     public void setPropertyTypeHandlersClassNames(Map<Class<?>, Class<?>> propertyTypeHandlersMap) {
178         initPropertyTypeHandlersMap();
179         Map<PropertyTypeDefinition, AbstractPropertyTypeHandler> result = new HashMap<PropertyTypeDefinition, AbstractPropertyTypeHandler>();
180         for (Class<?> key : propertyTypeHandlersMap.keySet()) {
181             try {
182                 Set<PropertyTypeDefinition> propertyTypeDefinitions = new HashSet<PropertyTypeDefinition>();
183                 PropertyTypeDefinition propertyTypeDefinition = new PropertyTypeDefinition(key);
184                 propertyTypeDefinitions.add(propertyTypeDefinition);
185                 AbstractPropertyTypeHandler value = (AbstractPropertyTypeHandler) propertyTypeHandlersMap.get(key).getConstructor(Set.class).newInstance(new Object[] { propertyTypeDefinitions });
186                 result.put(new PropertyTypeDefinition(key), value);
187             } catch (Exception e) {
188                 throw new IllegalArgumentException();
189             }
190         }
191         this.propertyTypeHandlersMap.putAll(result);
192     }
193 
194     public void setSpecificPropertyHandlersClassNames(Map<String, Class<?>> specificPropertyHandlersMap) {
195         initSpecificPropertyHandlersMap();
196         Map<SpecificPropertyDefinition, AbstractSpecificPropertyHandler> result = new HashMap<SpecificPropertyDefinition, AbstractSpecificPropertyHandler>();
197         for (String key : specificPropertyHandlersMap.keySet()) {
198             SpecificPropertyDefinition specificPropertyDefinition = new SpecificPropertyDefinition(key);
199             try {
200                 AbstractSpecificPropertyHandler value = (AbstractSpecificPropertyHandler) specificPropertyHandlersMap.get(key).getConstructor(SpecificPropertyDefinition.class).newInstance(specificPropertyDefinition);
201                 result.put(specificPropertyDefinition, value);
202             } catch (Exception e) {
203                 throw new IllegalArgumentException();
204             }
205         }
206         this.specificPropertyHandlersMap.putAll(result);
207     }
208 
209     private synchronized void initPropertyTypeHandlersMap() {
210         if(propertyTypeHandlersMap==null)propertyTypeHandlersMap = new HashMap<PropertyTypeDefinition, AbstractPropertyTypeHandler>();
211     }
212     
213     private synchronized void initSpecificPropertyHandlersMap() {
214         if(specificPropertyHandlersMap==null)specificPropertyHandlersMap = new HashMap<SpecificPropertyDefinition, AbstractSpecificPropertyHandler>();
215     }
216     
217     public void setPropertyWritingMechanism(String propertyWritingMechanism) {
218         this.propertyWritingMechanism = PropertyWritingMechanism.valueOf(propertyWritingMechanism.toUpperCase());
219     }
220 
221     public void onMissingHandlers(OnMissingHandlers onMissingHandlers) {
222         this.onMissingHandlers = onMissingHandlers;
223     }
224 
225     public void usePropertyWritingMechanism(PropertyWritingMechanism propertyWritingMechanism) {
226         this.propertyWritingMechanism = propertyWritingMechanism;
227     }
228 
229     public void setParentSetup(IUsurperGeneratorSetup parentSetup) {
230         this.parentSetup = parentSetup;
231     }
232 
233 }