View Javadoc

1   package org.melati.poem.prepro;
2   
3   /*
4    * $Source: /usr/cvsroot/melati/maven-dsd-plugin/src/main/java/org/melati/poem/prepro/MelatiDsdProcessorMojo.java,v $
5    * $Revision: 1.14 $
6    *
7    * Copyright (C) 2006 Tim Pizey
8    *
9    * Part of Melati (http://melati.org), a framework for the rapid
10   * development of clean, maintainable web applications.
11   *
12   * Melati is free software; Permission is granted to copy, distribute
13   * and/or modify this software under the terms either:
14   *
15   * a) the GNU General Public License as published by the Free Software
16   *    Foundation; either version 2 of the License, or (at your option)
17   *    any later version,
18   *
19   *    or
20   *
21   * b) any version of the Melati Software License, as published
22   *    at http://melati.org
23   *
24   * You should have received a copy of the GNU General Public License and
25   * the Melati Software License along with this program;
26   * if not, write to the Free Software Foundation, Inc.,
27   * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA to obtain the
28   * GNU General Public License and visit http://melati.org to obtain the
29   * Melati Software License.
30   *
31   * Feel free to contact the Developers of Melati (http://melati.org),
32   * if you would like to work out a different arrangement than the options
33   * outlined here.  It is our intention to allow Melati to be used by as
34   * wide an audience as possible.
35   *
36   * This program is distributed in the hope that it will be useful,
37   * but WITHOUT ANY WARRANTY; without even the implied warranty of
38   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
39   * GNU General Public License for more details.
40   *
41   * Contact details for copyright holder:
42   *
43   *     Tim Pizey <timp At paneris.org>
44   *     http://paneris.org/~timp
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   * Process a DSD file.
56   * 
57   * @requiresDependencyResolution compile
58   * @goal generate
59   * @description Process a DSD file to generate java sources.
60   * @phase process-sources
61   */
62  public class MelatiDsdProcessorMojo extends AbstractMojo {
63    /**
64     * Location of the dsd.
65     * 
66     * @parameter expression=
67     */
68    private String dsdPackage;
69  
70    /**
71     * DSD file name.
72     * 
73     * @parameter expression=
74     */
75    private String dsdFile;
76  
77    /**
78     * Location of the source.
79     * 
80     * @parameter expression="${project.build.sourceDirectory}"
81     * @required
82     */
83    private File sourceDirectory;
84  
85    /**
86     * Location of the test source.
87     * 
88     * @parameter expression="${project.build.testSourceDirectory}"
89     * @required
90     */
91    private File testSourceDirectory;
92  
93    /**
94     * @parameter expression="${project.groupId}"
95     * @required
96     */
97    private String groupId;
98  
99    /**
100    * @parameter expression="${project.artifactId}"
101    * @required
102    */
103   private String artifactId;
104   
105   /**
106    * @parameter expression="${checkUptodate}" default-value=true
107    */
108   private boolean checkUptodate;
109   
110   /**
111    * @parameter expression="${isMain}" default-value=true
112    */
113   private boolean isMain = true;
114   
115 
116   private String searchedLocations = "";
117 
118   public void execute()
119       throws MojoExecutionException {
120     
121     //Get the System Classloader
122     ClassLoader sysClassLoader = ClassLoader.getSystemClassLoader();
123 
124     //Get the URLs
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    * Captialise the first character of the input string.
253    * 
254    * @param name
255    * @return the capitalised string
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 }