1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.torque.mojo;
17
18 import java.io.File;
19 import java.io.IOException;
20 import java.util.ArrayList;
21 import java.util.Collection;
22 import java.util.Collections;
23 import java.util.List;
24
25 import org.apache.commons.io.FileUtils;
26 import org.apache.maven.plugin.MojoExecutionException;
27 import org.apache.maven.plugin.MojoFailureException;
28
29
30
31
32
33
34
35 public class GenerateTableListingMojo extends BaseMojo {
36
37
38
39
40
41
42
43 private String extension;
44
45
46
47
48
49
50
51 private String targetDatabase;
52
53
54
55
56
57
58
59 private File baseSqlDir;
60
61
62
63
64
65
66
67 private String artifactId;
68
69 @Override
70 protected void executeMojo() throws MojoExecutionException, MojoFailureException {
71 File databaseSQLDir = new File(baseSqlDir, targetDatabase);
72 if (!databaseSQLDir.exists()) {
73 throw new MojoExecutionException(databaseSQLDir + " does not exist");
74 }
75 getLog().info("Examining " + databaseSQLDir);
76 List<File> files = getFileListing(databaseSQLDir);
77 getLog().info("Located " + files.size() + " " + extension + " files");
78 List<String> tableNames = getTableNames(files);
79 getLog().info("Located " + tableNames.size() + " tables");
80 File outputFile = new File(databaseSQLDir, artifactId + "-tables.txt");
81 try {
82 getLog().info("Generating " + outputFile);
83 FileUtils.writeLines(outputFile, tableNames);
84 } catch (IOException e) {
85 throw new MojoExecutionException("Unexpected IO error", e);
86 }
87 }
88
89 @SuppressWarnings("unchecked")
90 protected List<File> getFileListing(File dir) {
91 String[] extensions = new String[] { extension };
92 Collection<File> files = FileUtils.listFiles(dir, extensions, false);
93 List<File> fileList = new ArrayList<File>();
94 if (files != null) {
95 fileList.addAll(files);
96 }
97 return fileList;
98 }
99
100 protected List<String> getTableNames(List<File> files) {
101 String[] skipTokens = new String[] { artifactId + "." + extension, artifactId + "-constraints." + extension };
102 List<String> tableNames = new ArrayList<String>();
103 for (File file : files) {
104 if (isSkip(file, skipTokens)) {
105 continue;
106 }
107 String fragment = file.getName();
108 int beginIndex = 0;
109 int endIndex = fragment.length() - (extension.length() + 1);
110 String fragmentMinusExtension = fragment.substring(beginIndex, endIndex);
111 tableNames.add(fragmentMinusExtension);
112 }
113 Collections.sort(tableNames);
114 return tableNames;
115 }
116
117 protected boolean isSkip(File file, String[] skipTokens) {
118 String path = file.getAbsolutePath();
119 for (String skipToken : skipTokens) {
120 if (path.endsWith(skipToken)) {
121 return true;
122 }
123 }
124 return false;
125 }
126
127 public String getExtension() {
128 return extension;
129 }
130
131 public void setExtension(String extension) {
132 this.extension = extension;
133 }
134
135 public String getTargetDatabase() {
136 return targetDatabase;
137 }
138
139 public void setTargetDatabase(String targetDatabase) {
140 this.targetDatabase = targetDatabase;
141 }
142
143 public String getArtifactId() {
144 return artifactId;
145 }
146
147 public void setArtifactId(String artifactId) {
148 this.artifactId = artifactId;
149 }
150
151 public File getBaseSqlDir() {
152 return baseSqlDir;
153 }
154
155 public void setBaseSqlDir(File sqlDir) {
156 this.baseSqlDir = sqlDir;
157 }
158
159 }