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