1 |
|
package org.apache.torque.mojo; |
2 |
|
|
3 |
|
import java.io.File; |
4 |
|
import java.io.IOException; |
5 |
|
import java.io.OutputStream; |
6 |
|
import java.util.List; |
7 |
|
import java.util.Properties; |
8 |
|
import java.util.Set; |
9 |
|
import java.util.TreeSet; |
10 |
|
|
11 |
|
import org.apache.commons.io.FileUtils; |
12 |
|
import org.apache.commons.io.IOUtils; |
13 |
|
import org.apache.maven.plugin.MojoExecutionException; |
14 |
|
import org.apache.maven.plugin.MojoFailureException; |
15 |
|
import org.apache.tools.ant.DirectoryScanner; |
16 |
|
import org.apache.torque.engine.database.model.Database; |
17 |
|
import org.apache.torque.engine.database.model.Table; |
18 |
|
import org.kuali.core.db.torque.SetUtils; |
19 |
|
import org.kuali.core.db.torque.Utils; |
20 |
|
|
21 |
|
|
22 |
|
|
23 |
|
|
24 |
|
|
25 |
|
|
26 |
|
|
27 |
|
|
28 |
|
|
29 |
|
|
30 |
|
|
31 |
|
|
|
|
| 0% |
Uncovered Elements: 97 (97) |
Complexity: 24 |
Complexity Density: 0.34 |
|
32 |
|
public class IdentifyInvalidDataFiles extends BaseMojo { |
33 |
|
private static final String FS = System.getProperty("file.separator"); |
34 |
|
|
35 |
|
|
36 |
|
|
37 |
|
|
38 |
|
|
39 |
|
private String extension; |
40 |
|
|
41 |
|
|
42 |
|
|
43 |
|
|
44 |
|
|
45 |
|
private File dataDir; |
46 |
|
|
47 |
|
|
48 |
|
|
49 |
|
|
50 |
|
private String dataDirIncludes; |
51 |
|
|
52 |
|
|
53 |
|
|
54 |
|
|
55 |
|
private String dataDirExcludes; |
56 |
|
|
57 |
|
|
58 |
|
|
59 |
|
|
60 |
|
private String schemaXMLFile; |
61 |
|
|
62 |
|
|
63 |
|
|
64 |
|
|
65 |
|
private String targetDatabase; |
66 |
|
|
67 |
|
|
68 |
|
|
69 |
|
|
70 |
|
|
71 |
|
|
72 |
|
private File markedForRemoval; |
73 |
|
|
|
|
| 0% |
Uncovered Elements: 30 (30) |
Complexity: 4 |
Complexity Density: 0.15 |
|
74 |
0
|
@Override... |
75 |
|
protected void executeMojo() throws MojoExecutionException, MojoFailureException { |
76 |
0
|
Utils utils = new Utils(); |
77 |
0
|
try { |
78 |
0
|
getLog().info("Examining " + dataDir.getAbsolutePath()); |
79 |
0
|
Database db = utils.getDatabase(schemaXMLFile, targetDatabase); |
80 |
0
|
DirectoryScanner ds = getDirectoryScanner(); |
81 |
0
|
Set<File> existing = getExistingFiles(ds); |
82 |
0
|
Set<File> allowed = getDatabaseFiles(db); |
83 |
0
|
Set<File> invalid = SetUtils.difference(existing, allowed); |
84 |
0
|
getLog().info(existing.size() + " data files currently exist"); |
85 |
0
|
getLog().info(invalid.size() + " of those are invalid"); |
86 |
0
|
StringBuilder sb = new StringBuilder(); |
87 |
0
|
int count = 0; |
88 |
0
|
StringBuilder invalidFiles = new StringBuilder(); |
89 |
0
|
for (File file : invalid) { |
90 |
0
|
if (count != 0) { |
91 |
0
|
sb.append(","); |
92 |
|
} |
93 |
0
|
sb.append("**/src/main/impex/" + file.getName()); |
94 |
0
|
invalidFiles.append(file.getCanonicalPath() + "\n"); |
95 |
0
|
getLog().info("Marked for removal: " + file.getName()); |
96 |
0
|
count++; |
97 |
|
} |
98 |
0
|
Properties properties = getProject().getProperties(); |
99 |
0
|
properties.setProperty("impex.data.invalid", sb.toString()); |
100 |
0
|
if (count > 0) { |
101 |
0
|
properties.setProperty("impex.found.invalid", Boolean.TRUE.toString()); |
102 |
0
|
createFile(markedForRemoval, invalidFiles.toString()); |
103 |
|
} |
104 |
|
} catch (Exception e) { |
105 |
0
|
throw new MojoExecutionException("Error executing mojo", e); |
106 |
|
} |
107 |
|
} |
108 |
|
|
|
|
| 0% |
Uncovered Elements: 5 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
|
109 |
0
|
protected void createFile(File file, String contents) throws IOException {... |
110 |
0
|
OutputStream out = null; |
111 |
0
|
try { |
112 |
0
|
out = FileUtils.openOutputStream(file); |
113 |
0
|
IOUtils.write(contents, out); |
114 |
|
} finally { |
115 |
0
|
IOUtils.closeQuietly(out); |
116 |
|
} |
117 |
|
} |
118 |
|
|
|
|
| 0% |
Uncovered Elements: 9 (9) |
Complexity: 1 |
Complexity Density: 0.11 |
|
119 |
0
|
protected Set<File> getDatabaseFiles(Database db) {... |
120 |
0
|
List<?> tables = db.getTables(); |
121 |
0
|
Set<File> files = new TreeSet<File>(); |
122 |
0
|
for (Object object : tables) { |
123 |
0
|
Table table = (Table) object; |
124 |
0
|
String tableName = table.getName(); |
125 |
0
|
String filename = dataDir.getAbsolutePath() + FS + tableName + extension; |
126 |
0
|
File file = new File(filename); |
127 |
0
|
files.add(file); |
128 |
|
} |
129 |
0
|
return files; |
130 |
|
} |
131 |
|
|
|
|
| 0% |
Uncovered Elements: 10 (10) |
Complexity: 2 |
Complexity Density: 0.25 |
|
132 |
0
|
protected Set<File> getExistingFiles(DirectoryScanner ds) {... |
133 |
0
|
ds.scan(); |
134 |
0
|
String[] relativeFilenames = ds.getIncludedFiles(); |
135 |
0
|
Set<File> files = new TreeSet<File>(); |
136 |
0
|
for (int i = 0; i < relativeFilenames.length; i++) { |
137 |
0
|
String filename = ds.getBasedir().getAbsolutePath() + FS + relativeFilenames[i]; |
138 |
0
|
File file = new File(filename); |
139 |
0
|
files.add(file); |
140 |
|
} |
141 |
0
|
return files; |
142 |
|
} |
143 |
|
|
|
|
| 0% |
Uncovered Elements: 5 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
|
144 |
0
|
protected DirectoryScanner getDirectoryScanner() {... |
145 |
0
|
DirectoryScanner ds = new DirectoryScanner(); |
146 |
0
|
ds.setBasedir(dataDir); |
147 |
0
|
ds.setIncludes(new String[] { dataDirIncludes }); |
148 |
0
|
ds.setExcludes(new String[] { dataDirExcludes }); |
149 |
0
|
return ds; |
150 |
|
} |
151 |
|
|
|
|
| 0% |
Uncovered Elements: 4 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
152 |
0
|
protected File getFile(Table table) {... |
153 |
0
|
String tableName = table.getName(); |
154 |
0
|
String filename = dataDir.getAbsolutePath() + FS + tableName + extension; |
155 |
0
|
File file = new File(filename); |
156 |
0
|
return file; |
157 |
|
} |
158 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
159 |
0
|
public File getDataDir() {... |
160 |
0
|
return dataDir; |
161 |
|
} |
162 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
163 |
0
|
public void setDataDir(File dataDir) {... |
164 |
0
|
this.dataDir = dataDir; |
165 |
|
} |
166 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
167 |
0
|
public String getDataDirIncludes() {... |
168 |
0
|
return dataDirIncludes; |
169 |
|
} |
170 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
171 |
0
|
public void setDataDirIncludes(String dataDirIncludes) {... |
172 |
0
|
this.dataDirIncludes = dataDirIncludes; |
173 |
|
} |
174 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
175 |
0
|
public String getDataDirExcludes() {... |
176 |
0
|
return dataDirExcludes; |
177 |
|
} |
178 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
179 |
0
|
public void setDataDirExcludes(String dataDirExcludes) {... |
180 |
0
|
this.dataDirExcludes = dataDirExcludes; |
181 |
|
} |
182 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
183 |
0
|
public String getSchemaXMLFile() {... |
184 |
0
|
return schemaXMLFile; |
185 |
|
} |
186 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
187 |
0
|
public void setSchemaXMLFile(String schemaXMLFile) {... |
188 |
0
|
this.schemaXMLFile = schemaXMLFile; |
189 |
|
} |
190 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
191 |
0
|
public String getExtension() {... |
192 |
0
|
return extension; |
193 |
|
} |
194 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
195 |
0
|
public void setExtension(String extension) {... |
196 |
0
|
this.extension = extension; |
197 |
|
} |
198 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
199 |
0
|
public String getTargetDatabase() {... |
200 |
0
|
return targetDatabase; |
201 |
|
} |
202 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
203 |
0
|
public void setTargetDatabase(String targetDatabase) {... |
204 |
0
|
this.targetDatabase = targetDatabase; |
205 |
|
} |
206 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
207 |
0
|
public File getMarkedForRemoval() {... |
208 |
0
|
return markedForRemoval; |
209 |
|
} |
210 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
211 |
0
|
public void setMarkedForRemoval(File markedForRemoval) {... |
212 |
0
|
this.markedForRemoval = markedForRemoval; |
213 |
|
} |
214 |
|
|
215 |
|
} |