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 private static final String FS = File.separator;
37
38
39
40
41
42
43
44 private String extension;
45
46
47
48
49
50
51
52 private String targetDatabase;
53
54
55
56
57
58
59
60 private File baseSqlDir;
61
62
63
64
65
66
67
68 private String artifactId;
69
70
71
72
73
74
75
76 private String metaFileSuffix;
77
78 @Override
79 protected void executeMojo() throws MojoExecutionException, MojoFailureException {
80 File databaseSQLDir = new File(baseSqlDir, targetDatabase);
81 if (!databaseSQLDir.exists()) {
82 throw new MojoExecutionException(databaseSQLDir + " does not exist");
83 }
84 try {
85 getLog().info("Examining " + databaseSQLDir.getCanonicalPath());
86 List<File> files = getFileListing(databaseSQLDir);
87 getLog().info("Located " + files.size() + " " + extension + " files");
88 List<String> tableNames = getTableNames(files);
89 List<String> locations = getLocations(tableNames);
90 getLog().info("Located " + tableNames.size() + " tables");
91 String outputFilename = databaseSQLDir.getAbsolutePath() + FS + ".." + FS + ".." + FS + "META-INF" + FS + targetDatabase + FS + artifactId + "." + metaFileSuffix;
92 File outputFile = new File(outputFilename);
93 getLog().info("Generating " + outputFile.getCanonicalPath());
94 FileUtils.writeLines(outputFile, locations);
95 } catch (IOException e) {
96 throw new MojoExecutionException("Unexpected IO error", e);
97 }
98 }
99
100 protected List<String> getLocations(List<String> tableNames) {
101 List<String> locations = new ArrayList<String>();
102 for (String tableName : tableNames) {
103 locations.add("classpath:sql/" + targetDatabase + "/" + tableName + ".sql");
104 }
105 return locations;
106 }
107
108 @SuppressWarnings("unchecked")
109 protected List<File> getFileListing(File dir) {
110 String[] extensions = new String[] { extension };
111 Collection<File> files = FileUtils.listFiles(dir, extensions, false);
112 List<File> fileList = new ArrayList<File>();
113 if (files != null) {
114 fileList.addAll(files);
115 }
116 return fileList;
117 }
118
119 protected List<String> getTableNames(List<File> files) {
120 String[] skipTokens = new String[] { artifactId + "." + extension, artifactId + "-constraints." + extension };
121 List<String> tableNames = new ArrayList<String>();
122 for (File file : files) {
123 if (isSkip(file, skipTokens)) {
124 continue;
125 }
126 String fragment = file.getName();
127 int beginIndex = 0;
128 int endIndex = fragment.length() - (extension.length() + 1);
129 String fragmentMinusExtension = fragment.substring(beginIndex, endIndex);
130 tableNames.add(fragmentMinusExtension);
131 }
132 Collections.sort(tableNames);
133 return tableNames;
134 }
135
136 protected boolean isSkip(File file, String[] skipTokens) {
137 String path = file.getAbsolutePath();
138 for (String skipToken : skipTokens) {
139 if (path.endsWith(skipToken)) {
140 return true;
141 }
142 }
143 return false;
144 }
145
146 public String getExtension() {
147 return extension;
148 }
149
150 public void setExtension(String extension) {
151 this.extension = extension;
152 }
153
154 public String getTargetDatabase() {
155 return targetDatabase;
156 }
157
158 public void setTargetDatabase(String targetDatabase) {
159 this.targetDatabase = targetDatabase;
160 }
161
162 public String getArtifactId() {
163 return artifactId;
164 }
165
166 public void setArtifactId(String artifactId) {
167 this.artifactId = artifactId;
168 }
169
170 public File getBaseSqlDir() {
171 return baseSqlDir;
172 }
173
174 public void setBaseSqlDir(File sqlDir) {
175 this.baseSqlDir = sqlDir;
176 }
177
178 public String getMetaFileSuffix() {
179 return metaFileSuffix;
180 }
181
182 public void setMetaFileSuffix(String metaFileSuffix) {
183 this.metaFileSuffix = metaFileSuffix;
184 }
185
186 }