1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.mybatis.generator.internal;
17
18 import static org.mybatis.generator.internal.util.StringUtility.stringHasValue;
19 import static org.mybatis.generator.internal.util.messages.Messages.getString;
20
21 import java.net.URL;
22 import java.util.ArrayList;
23 import java.util.List;
24
25 import org.mybatis.generator.api.CommentGenerator;
26 import org.mybatis.generator.api.FullyQualifiedTable;
27 import org.mybatis.generator.api.JavaFormatter;
28 import org.mybatis.generator.api.Plugin;
29 import org.mybatis.generator.api.IntrospectedColumn;
30 import org.mybatis.generator.api.IntrospectedTable;
31 import org.mybatis.generator.api.JavaTypeResolver;
32 import org.mybatis.generator.api.XmlFormatter;
33 import org.mybatis.generator.api.dom.DefaultJavaFormatter;
34 import org.mybatis.generator.api.dom.DefaultXmlFormatter;
35 import org.mybatis.generator.codegen.ibatis2.IntrospectedTableIbatis2Java2Impl;
36 import org.mybatis.generator.codegen.ibatis2.IntrospectedTableIbatis2Java5Impl;
37 import org.mybatis.generator.codegen.mybatis3.IntrospectedTableMyBatis3Impl;
38 import org.mybatis.generator.codegen.mybatis3.IntrospectedTableMyBatis3SimpleImpl;
39 import org.mybatis.generator.config.CommentGeneratorConfiguration;
40 import org.mybatis.generator.config.Context;
41 import org.mybatis.generator.config.PluginConfiguration;
42 import org.mybatis.generator.config.JavaTypeResolverConfiguration;
43 import org.mybatis.generator.config.PropertyRegistry;
44 import org.mybatis.generator.config.TableConfiguration;
45 import org.mybatis.generator.internal.types.JavaTypeResolverDefaultImpl;
46
47
48
49
50
51
52 public class ObjectFactory {
53
54
55 private static List<ClassLoader> externalClassLoaders;
56
57
58 private static List<ClassLoader> resourceClassLoaders;
59
60 static {
61 externalClassLoaders = new ArrayList<ClassLoader>();
62 resourceClassLoaders = new ArrayList<ClassLoader>();
63 }
64
65
66
67
68 private ObjectFactory() {
69 super();
70 }
71
72
73
74
75
76
77
78
79 public static synchronized void addResourceClassLoader(
80 ClassLoader classLoader) {
81 ObjectFactory.resourceClassLoaders.add(classLoader);
82 }
83
84
85
86
87
88
89
90
91
92 public static synchronized void addExternalClassLoader(
93 ClassLoader classLoader) {
94 ObjectFactory.externalClassLoaders.add(classLoader);
95 }
96
97
98
99
100
101
102
103
104
105
106
107
108 public static Class<?> externalClassForName(String type)
109 throws ClassNotFoundException {
110
111 Class<?> clazz;
112
113 for (ClassLoader classLoader : externalClassLoaders) {
114 try {
115 clazz = Class.forName(type, true, classLoader);
116 return clazz;
117 } catch (Throwable e) {
118
119 }
120 }
121
122 return internalClassForName(type);
123 }
124
125
126
127
128
129
130
131
132 public static Object createExternalObject(String type) {
133 Object answer;
134
135 try {
136 Class<?> clazz = externalClassForName(type);
137 answer = clazz.newInstance();
138 } catch (Exception e) {
139 throw new RuntimeException(getString(
140 "RuntimeError.6", type), e);
141 }
142
143 return answer;
144 }
145
146
147
148
149
150
151
152
153
154
155 public static Class<?> internalClassForName(String type)
156 throws ClassNotFoundException {
157 Class<?> clazz = null;
158
159 try {
160 ClassLoader cl = Thread.currentThread().getContextClassLoader();
161 clazz = Class.forName(type, true, cl);
162 } catch (Exception e) {
163
164 }
165
166 if (clazz == null) {
167 clazz = Class.forName(type, true, ObjectFactory.class.getClassLoader());
168 }
169
170 return clazz;
171 }
172
173
174
175
176
177
178
179
180 public static URL getResource(String resource) {
181 URL url;
182
183 for (ClassLoader classLoader : resourceClassLoaders) {
184 url = classLoader.getResource(resource);
185 if (url != null) {
186 return url;
187 }
188 }
189
190 ClassLoader cl = Thread.currentThread().getContextClassLoader();
191 url = cl.getResource(resource);
192
193 if (url == null) {
194 url = ObjectFactory.class.getClassLoader().getResource(resource);
195 }
196
197 return url;
198 }
199
200
201
202
203
204
205
206
207 public static Object createInternalObject(String type) {
208 Object answer;
209
210 try {
211 Class<?> clazz = internalClassForName(type);
212
213 answer = clazz.newInstance();
214 } catch (Exception e) {
215 throw new RuntimeException(getString(
216 "RuntimeError.6", type), e);
217
218 }
219
220 return answer;
221 }
222
223
224
225
226
227
228
229
230
231
232 public static JavaTypeResolver createJavaTypeResolver(Context context,
233 List<String> warnings) {
234 JavaTypeResolverConfiguration config = context
235 .getJavaTypeResolverConfiguration();
236 String type;
237
238 if (config != null && config.getConfigurationType() != null) {
239 type = config.getConfigurationType();
240 if ("DEFAULT".equalsIgnoreCase(type)) {
241 type = JavaTypeResolverDefaultImpl.class.getName();
242 }
243 } else {
244 type = JavaTypeResolverDefaultImpl.class.getName();
245 }
246
247 JavaTypeResolver answer = (JavaTypeResolver) createInternalObject(type);
248 answer.setWarnings(warnings);
249
250 if (config != null) {
251 answer.addConfigurationProperties(config.getProperties());
252 }
253
254 answer.setContext(context);
255
256 return answer;
257 }
258
259
260
261
262
263
264
265
266
267
268 public static Plugin createPlugin(Context context,
269 PluginConfiguration pluginConfiguration) {
270 Plugin plugin = (Plugin) createInternalObject(pluginConfiguration
271 .getConfigurationType());
272 plugin.setContext(context);
273 plugin.setProperties(pluginConfiguration.getProperties());
274 return plugin;
275 }
276
277
278
279
280
281
282
283
284 public static CommentGenerator createCommentGenerator(Context context) {
285
286 CommentGeneratorConfiguration config = context
287 .getCommentGeneratorConfiguration();
288 CommentGenerator answer;
289
290 String type;
291 if (config == null || config.getConfigurationType() == null) {
292 type = DefaultCommentGenerator.class.getName();
293 } else {
294 type = config.getConfigurationType();
295 }
296
297 answer = (CommentGenerator) createInternalObject(type);
298
299 if (config != null) {
300 answer.addConfigurationProperties(config.getProperties());
301 }
302
303 return answer;
304 }
305
306
307
308
309
310
311
312
313 public static JavaFormatter createJavaFormatter(Context context) {
314 String type = context.getProperty(PropertyRegistry.CONTEXT_JAVA_FORMATTER);
315 if (!stringHasValue(type)) {
316 type = DefaultJavaFormatter.class.getName();
317 }
318
319 JavaFormatter answer = (JavaFormatter) createInternalObject(type);
320
321 answer.setContext(context);
322
323 return answer;
324 }
325
326
327
328
329
330
331
332
333 public static XmlFormatter createXmlFormatter(Context context) {
334 String type = context.getProperty(PropertyRegistry.CONTEXT_XML_FORMATTER);
335 if (!stringHasValue(type)) {
336 type = DefaultXmlFormatter.class.getName();
337 }
338
339 XmlFormatter answer = (XmlFormatter) createInternalObject(type);
340
341 answer.setContext(context);
342
343 return answer;
344 }
345
346
347
348
349
350
351
352
353
354
355
356
357 public static IntrospectedTable createIntrospectedTable(
358 TableConfiguration tableConfiguration, FullyQualifiedTable table,
359 Context context) {
360
361 IntrospectedTable answer = createIntrospectedTableForValidation(context);
362 answer.setFullyQualifiedTable(table);
363 answer.setTableConfiguration(tableConfiguration);
364
365 return answer;
366 }
367
368
369
370
371
372
373
374
375
376
377 public static IntrospectedTable createIntrospectedTableForValidation(Context context) {
378 String type = context.getTargetRuntime();
379 if (!stringHasValue(type)) {
380 type = IntrospectedTableMyBatis3Impl.class.getName();
381 } else if ("Ibatis2Java2".equalsIgnoreCase(type)) {
382 type = IntrospectedTableIbatis2Java2Impl.class.getName();
383 } else if ("Ibatis2Java5".equalsIgnoreCase(type)) {
384 type = IntrospectedTableIbatis2Java5Impl.class.getName();
385 } else if ("Ibatis3".equalsIgnoreCase(type)) {
386 type = IntrospectedTableMyBatis3Impl.class.getName();
387 } else if ("MyBatis3".equalsIgnoreCase(type)) {
388 type = IntrospectedTableMyBatis3Impl.class.getName();
389 } else if ("MyBatis3Simple".equalsIgnoreCase(type)) {
390 type = IntrospectedTableMyBatis3SimpleImpl.class.getName();
391 }
392
393 IntrospectedTable answer = (IntrospectedTable) createInternalObject(type);
394 answer.setContext(context);
395
396 return answer;
397 }
398
399
400
401
402
403
404
405
406 public static IntrospectedColumn createIntrospectedColumn(Context context) {
407 String type = context.getIntrospectedColumnImpl();
408 if (!stringHasValue(type)) {
409 type = IntrospectedColumn.class.getName();
410 }
411
412 IntrospectedColumn answer = (IntrospectedColumn) createInternalObject(type);
413 answer.setContext(context);
414
415 return answer;
416 }
417 }