1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.mybatis.generator.api;
17
18 import org.mybatis.generator.config.Configuration;
19 import org.mybatis.generator.config.Context;
20 import org.mybatis.generator.config.MergeConstants;
21 import org.mybatis.generator.exception.InvalidConfigurationException;
22 import org.mybatis.generator.exception.ShellException;
23 import org.mybatis.generator.internal.DefaultShellCallback;
24 import org.mybatis.generator.internal.NullProgressCallback;
25 import org.mybatis.generator.internal.ObjectFactory;
26 import org.mybatis.generator.internal.XmlFileMergerJaxp;
27
28 import java.io.*;
29 import java.sql.SQLException;
30 import java.util.ArrayList;
31 import java.util.HashSet;
32 import java.util.List;
33 import java.util.Set;
34
35 import static org.mybatis.generator.internal.util.ClassloaderUtility.getCustomClassloader;
36 import static org.mybatis.generator.internal.util.messages.Messages.getString;
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53 public class MyBatisGenerator {
54
55 private Configuration configuration;
56
57 private ShellCallback shellCallback;
58
59 private List<GeneratedJavaFile> generatedJavaFiles;
60
61 private List<GeneratedExtjsFile> generatedExtjsFiles;
62
63 private List<GeneratedXmlFile> generatedXmlFiles;
64
65 private List<String> warnings;
66
67 private Set<String> projects;
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88 public MyBatisGenerator(Configuration configuration, ShellCallback shellCallback,
89 List<String> warnings) throws InvalidConfigurationException {
90 super();
91 if (configuration == null) {
92 throw new IllegalArgumentException(getString("RuntimeError.2"));
93 } else {
94 this.configuration = configuration;
95 }
96
97 if (shellCallback == null) {
98 this.shellCallback = new DefaultShellCallback(false);
99 } else {
100 this.shellCallback = shellCallback;
101 }
102
103 if (warnings == null) {
104 this.warnings = new ArrayList<String>();
105 } else {
106 this.warnings = warnings;
107 }
108 generatedJavaFiles = new ArrayList<GeneratedJavaFile>();
109 generatedExtjsFiles = new ArrayList<GeneratedExtjsFile>();
110 generatedXmlFiles = new ArrayList<GeneratedXmlFile>();
111 projects = new HashSet<String>();
112
113 this.configuration.validate();
114 }
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130 public void generate(ProgressCallback callback) throws SQLException,
131 IOException, InterruptedException {
132 generate(callback, null, null);
133 }
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153 public void generate(ProgressCallback callback, Set<String> contextIds)
154 throws SQLException, IOException, InterruptedException {
155 generate(callback, contextIds, null);
156 }
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183 public void generate(ProgressCallback callback, Set<String> contextIds,
184 Set<String> fullyQualifiedTableNames) throws SQLException,
185 IOException, InterruptedException {
186
187 if (callback == null) {
188 callback = new NullProgressCallback();
189 }
190
191 generatedJavaFiles.clear();
192 generatedExtjsFiles.clear();
193 generatedXmlFiles.clear();
194
195
196 List<Context> contextsToRun;
197 if (contextIds == null || contextIds.size() == 0) {
198 contextsToRun = configuration.getContexts();
199 } else {
200 contextsToRun = new ArrayList<Context>();
201 for (Context context : configuration.getContexts()) {
202 if (contextIds.contains(context.getId())) {
203 contextsToRun.add(context);
204 }
205 }
206 }
207
208
209 if (configuration.getClassPathEntries().size() > 0) {
210 ClassLoader classLoader = getCustomClassloader(configuration.getClassPathEntries());
211 ObjectFactory.addExternalClassLoader(classLoader);
212 }
213
214
215 int totalSteps = 0;
216 for (Context context : contextsToRun) {
217 totalSteps += context.getIntrospectionSteps();
218 }
219 callback.introspectionStarted(totalSteps);
220
221 for (Context context : contextsToRun) {
222 context.introspectTables(callback, warnings,
223 fullyQualifiedTableNames);
224 }
225
226
227 totalSteps = 0;
228 for (Context context : contextsToRun) {
229 totalSteps += context.getGenerationSteps();
230 }
231 callback.generationStarted(totalSteps);
232
233 for (Context context : contextsToRun) {
234 context.generateFiles(callback, generatedJavaFiles,
235 generatedExtjsFiles,
236 generatedXmlFiles, warnings);
237 }
238
239
240 callback.saveStarted(generatedXmlFiles.size()
241 + generatedExtjsFiles.size()
242 + generatedJavaFiles.size());
243
244 for (GeneratedXmlFile gxf : generatedXmlFiles) {
245 projects.add(gxf.getTargetProject());
246
247 File targetFile;
248 String source;
249 try {
250 File directory = shellCallback.getDirectory(gxf
251 .getTargetProject(), gxf.getTargetPackage());
252 targetFile = new File(directory, gxf.getFileName());
253 if (targetFile.exists()) {
254 if (gxf.isMergeable()) {
255 source = XmlFileMergerJaxp.getMergedSource(gxf,
256 targetFile);
257 } else if (shellCallback.isOverwriteEnabled()) {
258 source = gxf.getFormattedContent();
259 warnings.add(getString("Warning.11",
260 targetFile.getAbsolutePath()));
261 } else {
262 source = gxf.getFormattedContent();
263 targetFile = getUniqueFileName(directory, gxf
264 .getFileName());
265 warnings.add(getString(
266 "Warning.2", targetFile.getAbsolutePath()));
267 }
268 } else {
269 source = gxf.getFormattedContent();
270 }
271 } catch (ShellException e) {
272 warnings.add(e.getMessage());
273 continue;
274 }
275
276 callback.checkCancel();
277 callback.startTask(getString(
278 "Progress.15", targetFile.getName()));
279 writeFile(targetFile, source, "UTF-8");
280 }
281
282 for (GeneratedJavaFile gjf : generatedJavaFiles) {
283 projects.add(gjf.getTargetProject());
284
285 File targetFile;
286 String source;
287 try {
288 File directory = shellCallback.getDirectory(gjf
289 .getTargetProject(), gjf.getTargetPackage());
290 targetFile = new File(directory, gjf.getFileName());
291 if (targetFile.exists()) {
292 if (shellCallback.isMergeSupported()) {
293 source = shellCallback.mergeJavaFile(gjf
294 .getFormattedContent(), targetFile
295 .getAbsolutePath(),
296 MergeConstants.OLD_ELEMENT_TAGS,
297 gjf.getFileEncoding());
298 } else if (shellCallback.isOverwriteEnabled()) {
299 source = gjf.getFormattedContent();
300 warnings.add(getString("Warning.11",
301 targetFile.getAbsolutePath()));
302 } else {
303 source = gjf.getFormattedContent();
304 targetFile = getUniqueFileName(directory, gjf
305 .getFileName());
306 warnings.add(getString(
307 "Warning.2", targetFile.getAbsolutePath()));
308 }
309 } else {
310 source = gjf.getFormattedContent();
311 }
312
313 callback.checkCancel();
314 callback.startTask(getString(
315 "Progress.15", targetFile.getName()));
316 writeFile(targetFile, source, gjf.getFileEncoding());
317 } catch (ShellException e) {
318 warnings.add(e.getMessage());
319 }
320 }
321
322
323
324 for (GeneratedExtjsFile extf : generatedExtjsFiles) {
325 projects.add(extf.getTargetProject());
326
327 File targetFile;
328 String source;
329 try {
330 File directory = shellCallback.getDirectory(extf
331 .getTargetProject(), extf.getTargetPackage());
332 targetFile = new File(directory, extf.getFileName());
333 if (targetFile.exists()) {
334 if (shellCallback.isMergeSupported()) {
335
336 source = extf.getFormattedContent();
337 } else if (shellCallback.isOverwriteEnabled()) {
338 source = extf.getFormattedContent();
339 warnings.add(getString("Warning.11",
340 targetFile.getAbsolutePath()));
341 } else {
342 source = extf.getFormattedContent();
343 targetFile = getUniqueFileName(directory, extf
344 .getFileName());
345 warnings.add(getString(
346 "Warning.2", targetFile.getAbsolutePath()));
347 }
348 } else {
349 source = extf.getFormattedContent();
350 }
351
352 callback.checkCancel();
353 callback.startTask(getString(
354 "Progress.15", targetFile.getName()));
355 writeFile(targetFile, source, extf.getFileEncoding());
356 } catch (ShellException e) {
357 warnings.add(e.getMessage());
358 }
359 }
360
361 for (String project : projects) {
362 shellCallback.refreshProject(project);
363 }
364
365 callback.done();
366 }
367
368
369
370
371
372
373
374 private void writeFile(File file, String content, String fileEncoding) throws IOException {
375 FileOutputStream fos = new FileOutputStream(file, false);
376 OutputStreamWriter osw;
377 if (fileEncoding == null) {
378 osw = new OutputStreamWriter(fos);
379 } else {
380 osw = new OutputStreamWriter(fos, fileEncoding);
381 }
382
383 BufferedWriter bw = new BufferedWriter(osw);
384 bw.write(content);
385 bw.close();
386 }
387
388 private File getUniqueFileName(File directory, String fileName) {
389 File answer = null;
390
391
392 StringBuilder sb = new StringBuilder();
393 for (int i = 1; i < 1000; i++) {
394 sb.setLength(0);
395 sb.append(fileName);
396 sb.append('.');
397 sb.append(i);
398
399 File testFile = new File(directory, sb.toString());
400 if (!testFile.exists()) {
401 answer = testFile;
402 break;
403 }
404 }
405
406 if (answer == null) {
407 throw new RuntimeException(getString(
408 "RuntimeError.3", directory.getAbsolutePath()));
409 }
410
411 return answer;
412 }
413 }