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