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 List<String> locations = getLocations(tableNames);
80 getLog().info("Located " + tableNames.size() + " tables");
81 File outputFile = new File(databaseSQLDir, artifactId + "-tables.txt");
82 try {
83 getLog().info("Generating " + outputFile);
84 FileUtils.writeLines(outputFile, locations);
85 } catch (IOException e) {
86 throw new MojoExecutionException("Unexpected IO error", e);
87 }
88 }
89
90 protected List<String> getLocations(List<String> tableNames) {
91 List<String> locations = new ArrayList<String>();
92 for (String tableName : tableNames) {
93 locations.add("classpath:sql/" + targetDatabase + "/" + tableName + ".sql");
94 }
95 return locations;
96 }
97
98 @SuppressWarnings("unchecked")
99 protected List<File> getFileListing(File dir) {
100 String[] extensions = new String[] { extension };
101 Collection<File> files = FileUtils.listFiles(dir, extensions, false);
102 List<File> fileList = new ArrayList<File>();
103 if (files != null) {
104 fileList.addAll(files);
105 }
106 return fileList;
107 }
108
109 protected List<String> getTableNames(List<File> files) {
110 String[] skipTokens = new String[] { artifactId + "." + extension, artifactId + "-constraints." + extension };
111 List<String> tableNames = new ArrayList<String>();
112 for (File file : files) {
113 if (isSkip(file, skipTokens)) {
114 continue;
115 }
116 String fragment = file.getName();
117 int beginIndex = 0;
118 int endIndex = fragment.length() - (extension.length() + 1);
119 String fragmentMinusExtension = fragment.substring(beginIndex, endIndex);
120 tableNames.add(fragmentMinusExtension);
121 }
122 Collections.sort(tableNames);
123 return tableNames;
124 }
125
126 protected boolean isSkip(File file, String[] skipTokens) {
127 String path = file.getAbsolutePath();
128 for (String skipToken : skipTokens) {
129 if (path.endsWith(skipToken)) {
130 return true;
131 }
132 }
133 return false;
134 }
135
136 public String getExtension() {
137 return extension;
138 }
139
140 public void setExtension(String extension) {
141 this.extension = extension;
142 }
143
144 public String getTargetDatabase() {
145 return targetDatabase;
146 }
147
148 public void setTargetDatabase(String targetDatabase) {
149 this.targetDatabase = targetDatabase;
150 }
151
152 public String getArtifactId() {
153 return artifactId;
154 }
155
156 public void setArtifactId(String artifactId) {
157 this.artifactId = artifactId;
158 }
159
160 public File getBaseSqlDir() {
161 return baseSqlDir;
162 }
163
164 public void setBaseSqlDir(File sqlDir) {
165 this.baseSqlDir = sqlDir;
166 }
167
168 }