1 | |
package org.apache.torque.mojo; |
2 | |
|
3 | |
import static org.apache.commons.io.FileUtils.forceMkdir; |
4 | |
|
5 | |
import java.io.File; |
6 | |
import java.io.FileInputStream; |
7 | |
import java.io.FileOutputStream; |
8 | |
import java.io.IOException; |
9 | |
import java.util.ArrayList; |
10 | |
import java.util.List; |
11 | |
|
12 | |
import org.apache.maven.plugin.MojoExecutionException; |
13 | |
import org.apache.torque.mojo.morph.DataMorpher; |
14 | |
import org.apache.torque.mojo.morph.MorphRequest; |
15 | |
import org.apache.torque.mojo.morph.Morpher; |
16 | |
import org.codehaus.plexus.util.DirectoryScanner; |
17 | |
import org.codehaus.plexus.util.StringUtils; |
18 | |
import org.kuali.core.db.torque.PrettyPrint; |
19 | |
import org.kuali.core.db.torque.Utils; |
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | 0 | public class MorphDataMojo extends BaseMojo { |
28 | 0 | Utils utils = new Utils(); |
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
private File newDataOutputDir; |
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
private File oldDataXMLDir; |
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
private String oldDataXMLIncludes; |
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
private String oldDataXMLExcludes; |
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
protected String[] getOldFiles() throws IOException { |
65 | 0 | DirectoryScanner ds = new DirectoryScanner(); |
66 | 0 | ds.setIncludes(new String[] { getOldDataXMLIncludes() }); |
67 | 0 | if (getOldDataXMLExcludes() != null) { |
68 | 0 | ds.setExcludes(new String[] { getOldDataXMLExcludes() }); |
69 | |
} |
70 | 0 | ds.setBasedir(getOldDataXMLDir()); |
71 | 0 | ds.scan(); |
72 | 0 | return ds.getIncludedFiles(); |
73 | |
} |
74 | |
|
75 | |
protected boolean isMorphNeeded(final File oldFile, final File newFile) { |
76 | 0 | if (!newFile.exists()) { |
77 | 0 | return true; |
78 | |
} |
79 | |
|
80 | 0 | long lastModifiedOld = oldFile.lastModified(); |
81 | 0 | long lastModifiedNew = newFile.lastModified(); |
82 | 0 | return lastModifiedOld > lastModifiedNew; |
83 | |
} |
84 | |
|
85 | |
protected List<MorphRequest> getMorphRequests(final String[] oldFiles) throws IOException { |
86 | 0 | String inputPath = getOldDataXMLDir().getAbsolutePath(); |
87 | 0 | String outputPath = getNewDataOutputDir().getAbsolutePath(); |
88 | 0 | forceMkdir(getNewDataOutputDir()); |
89 | 0 | List<MorphRequest> requests = new ArrayList<MorphRequest>(); |
90 | 0 | for (String oldFilename : oldFiles) { |
91 | 0 | String oldFilePath = inputPath + FS + oldFilename; |
92 | 0 | String newFilePath = outputPath + FS + oldFilename; |
93 | 0 | File oldFile = new File(oldFilePath); |
94 | 0 | File newFile = new File(newFilePath); |
95 | 0 | if (!isMorphNeeded(oldFile, newFile)) { |
96 | 0 | getLog().debug("Skipping morph on " + oldFilename); |
97 | 0 | continue; |
98 | |
} |
99 | 0 | MorphRequest request = new MorphRequest(); |
100 | 0 | request.setEncoding(getEncoding()); |
101 | 0 | request.setName(oldFile.getName()); |
102 | 0 | request.setInputStream(new FileInputStream(oldFile)); |
103 | 0 | request.setOutputStream(new FileOutputStream(newFile)); |
104 | 0 | requests.add(request); |
105 | |
} |
106 | 0 | return requests; |
107 | |
} |
108 | |
|
109 | |
protected void showConfig() { |
110 | 0 | getLog().info(StringUtils.repeat("-", utils.getDefaultPrintableConsoleWidth() - 7)); |
111 | 0 | getLog().info(" From: " + oldDataXMLDir.getAbsolutePath()); |
112 | 0 | getLog().info(" To: " + newDataOutputDir.getAbsolutePath()); |
113 | 0 | getLog().info("Include: " + oldDataXMLIncludes); |
114 | 0 | getLog().info("Exclude: " + oldDataXMLExcludes); |
115 | 0 | } |
116 | |
|
117 | |
@Override |
118 | |
protected void executeMojo() throws MojoExecutionException { |
119 | |
try { |
120 | 0 | showConfig(); |
121 | 0 | String[] oldFiles = getOldFiles(); |
122 | 0 | PrettyPrint pp = new PrettyPrint("[INFO] Converting " + oldFiles.length + " data XML files"); |
123 | 0 | utils.left(pp); |
124 | 0 | List<MorphRequest> requests = getMorphRequests(oldFiles); |
125 | 0 | for (MorphRequest request : requests) { |
126 | 0 | Morpher morpher = new DataMorpher(request, getProject().getArtifactId()); |
127 | 0 | morpher.executeMorph(); |
128 | 0 | } |
129 | 0 | utils.right(pp); |
130 | 0 | int skipCount = oldFiles.length - requests.size(); |
131 | 0 | if (skipCount > 0) { |
132 | 0 | getLog().info("Skipped " + skipCount + " files that were unchanged."); |
133 | |
} |
134 | 0 | } catch (IOException e) { |
135 | 0 | throw new MojoExecutionException("Unexpected error", e); |
136 | 0 | } |
137 | 0 | } |
138 | |
|
139 | |
public File getNewDataOutputDir() { |
140 | 0 | return newDataOutputDir; |
141 | |
} |
142 | |
|
143 | |
public void setNewDataOutputDir(final File newDataOutputDir) { |
144 | 0 | this.newDataOutputDir = newDataOutputDir; |
145 | 0 | } |
146 | |
|
147 | |
public File getOldDataXMLDir() { |
148 | 0 | return oldDataXMLDir; |
149 | |
} |
150 | |
|
151 | |
public void setOldDataXMLDir(final File oldDataXMLDir) { |
152 | 0 | this.oldDataXMLDir = oldDataXMLDir; |
153 | 0 | } |
154 | |
|
155 | |
public String getOldDataXMLIncludes() { |
156 | 0 | return oldDataXMLIncludes; |
157 | |
} |
158 | |
|
159 | |
public void setOldDataXMLIncludes(final String oldDataXMLIncludes) { |
160 | 0 | this.oldDataXMLIncludes = oldDataXMLIncludes; |
161 | 0 | } |
162 | |
|
163 | |
public String getOldDataXMLExcludes() { |
164 | 0 | return oldDataXMLExcludes; |
165 | |
} |
166 | |
|
167 | |
public void setOldDataXMLExcludes(final String oldDataXMLExcludes) { |
168 | 0 | this.oldDataXMLExcludes = oldDataXMLExcludes; |
169 | 0 | } |
170 | |
} |