1 package org.melati.poem.prepro;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47 import org.apache.maven.plugin.AbstractMojo;
48 import org.apache.maven.plugin.MojoExecutionException;
49
50 import java.io.File;
51 import java.net.URL;
52 import java.net.URLClassLoader;
53
54
55
56
57
58
59
60
61
62 public class MelatiDsdProcessorMojo extends AbstractMojo {
63
64
65
66
67
68 private String dsdPackage;
69
70
71
72
73
74
75 private String dsdFile;
76
77
78
79
80
81
82
83 private File sourceDirectory;
84
85
86
87
88
89
90
91 private File testSourceDirectory;
92
93
94
95
96
97 private String groupId;
98
99
100
101
102
103 private String artifactId;
104
105
106
107
108 private boolean checkUptodate;
109
110
111
112
113 private boolean isMain = true;
114
115
116 private String searchedLocations = "";
117
118 public void execute()
119 throws MojoExecutionException {
120
121
122 ClassLoader sysClassLoader = ClassLoader.getSystemClassLoader();
123
124
125 URL[] urls = ((URLClassLoader)sysClassLoader).getURLs();
126
127 for(int i=0; i< urls.length; i++)
128 {
129 System.out.println(urls[i].getFile());
130 }
131
132 File f = null;
133 if(isMain)
134 f = sourceDirectory;
135 else
136 f = testSourceDirectory;
137 if (f == null || !f.exists()) {
138 throw new MojoExecutionException("Source directory (" + f + ")could not be found");
139 }
140
141 String dsdPath = null;
142 if (dsdPackage != null) {
143 String lookupDir = f.getPath() + "/"
144 + dsdPackage.replace('.', '/') + "/";
145 dsdPath = dsdFileName(lookupDir, dsdFile);
146 if (dsdPath == null) {
147 if (dsdFile != null) {
148 throw new MojoExecutionException("Configured DSD file " + lookupDir
149 + dsdFile + " could not be found.");
150 } else {
151 throw new MojoExecutionException(
152 "DSD file could not be found on configured path "
153 + dsdPackage
154 + " in any of: \n"
155 + searchedLocations
156 + "Add an explicit dsdPackage and/or dsdFile parameter to your configuration.");
157
158 }
159 }
160 } else {
161 String groupDir = groupId.replace('.', '/');
162 String sourceDir = f.getPath() + "/" + groupDir + "/";
163 String foundDsdName = existingDsdFileName(sourceDir, dsdFile);
164 if (foundDsdName == null)
165 foundDsdName = existingDsdFileName(f + artifactId + "/",
166 dsdFile);
167 if (foundDsdName == null)
168 throw new MojoExecutionException(
169 "DSD file could not be found in any of: \n"
170 + searchedLocations
171 + "Add an explicit dsdPackage and/or dsdFile parameter to your configuration.");
172 getLog().info("Found DSD at " + foundDsdName + ":");
173 dsdPath = foundDsdName;
174 }
175 String modelDir = dsdPath.substring(0, dsdPath.lastIndexOf('/') + 1);
176 String modelName = capitalised(dsdPath.substring(dsdPath.lastIndexOf('/') + 1, dsdPath
177 .lastIndexOf('.')));
178 String databaseTablesFileName = modelDir + "generated/" + modelName
179 + "DatabaseBase.java";
180
181 File databaseTablesFile = new File(databaseTablesFileName);
182 long dsdTimestamp = new File(dsdPath).lastModified();
183 long databaseTablesTimestamp = 1;
184 if (databaseTablesFile.exists()) {
185 databaseTablesTimestamp = databaseTablesFile.lastModified();
186 }
187 boolean doIt = true;
188 if (checkUptodate) {
189 getLog().info(" Checking " + databaseTablesFileName);
190 if (dsdTimestamp < databaseTablesTimestamp) {
191 getLog().info("Generated files are uptodate. No action required.");
192 doIt = false;
193 }
194 } else
195 getLog().info(" Not checking - doing regardless");
196 if (doIt) {
197 DSD dsd;
198 try {
199 dsd = new DSD(dsdPath);
200 dsd.generateJava();
201 } catch (Exception e) {
202 throw new MojoExecutionException("Error processing DSD", e);
203 }
204 }
205 }
206
207 private String existingDsdFileName(String dir, String dsdFileName) {
208 String modelDirName = dir + "poem/";
209 File modelDir = new File(modelDirName);
210 if (!modelDir.exists()) {
211 searchedLocations += " " + modelDirName + "\n";
212 modelDirName = dir + "model/";
213 modelDir = new File(modelDirName);
214 }
215 if (!modelDir.exists()) {
216 searchedLocations += " " + modelDirName + "\n";
217 modelDirName = dir;
218 modelDir = new File(modelDirName);
219 }
220 if (!modelDir.exists()) {
221 searchedLocations += " " + modelDirName + "\n";
222 return null;
223 }
224 return dsdFileName(modelDirName, dsdFileName);
225 }
226
227 private String dsdFileName(String dir, String dsdFileName) {
228 if (dsdFileName != null) {
229 dsdFileName = dir + dsdFileName;
230 File foundDsdFile = new File(dsdFileName);
231 if ( !foundDsdFile.exists()) {
232 searchedLocations += " " + dsdFileName + "\n";
233 return null;
234 }
235 } else {
236 dsdFileName = dir + artifactId + ".dsd";
237 File foundDsdFile = new File(dsdFileName);
238 if (!foundDsdFile.exists()) {
239 searchedLocations += " " + dsdFileName + "\n";
240 dsdFileName = dir + capitalised(artifactId) + ".dsd";
241 foundDsdFile = new File(dsdFileName);
242 }
243 if (!foundDsdFile.exists()) {
244 searchedLocations += " " + dsdFileName + "\n";
245 return null;
246 }
247 }
248 return dsdFileName;
249 }
250
251
252
253
254
255
256
257 public static String capitalised(String name) {
258 char suffix[] = name.toCharArray();
259 suffix[0] = Character.toUpperCase(suffix[0]);
260 return new String(suffix);
261 }
262 }