View Javadoc

1   package org.apache.torque.mojo;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.File;
23  
24  import org.apache.maven.plugin.MojoExecutionException;
25  import org.apache.tools.ant.DirectoryScanner;
26  import org.apache.tools.ant.types.FileSet;
27  import org.apache.torque.task.TorqueDataModelTask;
28  
29  /**
30   * The base class for all mojos wrapping DataModelTasks.
31   *
32   * @author Raphael Pieroni (rafale_at_codehaus.org)
33   * @author <a href="fischer@seitenbau.de">Thomas Fischer</a>
34   */
35  public abstract class DataModelTaskMojo
36          extends TexenTaskMojo
37  {
38      /**
39       * The context property for the database.
40       */
41      public static final String TARGET_DATABASE_CONTEXT_PROPERTY
42              = "targetDatabase";
43  
44      /**
45       * The path to the directory where the schema files are located in.
46       *
47       * @parameter expression="${basedir}/src/main/torque/schema"
48       * @required
49       */
50      private String schemaDir;
51  
52      /**
53       * The schema files which should be included in generation
54       * (in ant-style notation).
55       *
56       * @parameter expression="*schema.xml"
57       * @required
58       */
59      private String schemaIncludes;
60  
61      /**
62       * The schema files which should be excluded in generation
63       * (in ant-style notation).
64       */
65      private String schemaExcludes;
66  
67      /**
68       * The database type (e.g. mysql, oracle, ...) for the generated persistence
69       * classes,
70       *
71       * @parameter
72       * @required
73       */
74      private String targetDatabase;
75  
76      /**
77       * The target package for the generated classes.
78       *
79       * @parameter expression="torque.generated"
80       */
81      private String targetPackage;
82  
83      /**
84       * The file containing the generation report, relative to
85       * <code>outputDir</code>.
86       *
87       * @required
88       */
89      private String reportFile;
90  
91      /**
92       * Determines if this task should run only if the schema has changed.
93       *
94       * @parameter expression="true"
95       */
96      private boolean runOnlyOnSchemaChange;
97  
98      /**
99       * The path to the properties file containing the mapping
100      * sql file -> target database.
101      *
102      * @parameter expression="${project.build.directory}/torque/sqldbmap.properties"
103      */
104     private String sqlDbMap;
105 
106     /**
107      * Creates a new TorqueOMMojo object.
108      */
109     DataModelTaskMojo()
110     {
111         super(new TorqueDataModelTask());
112     }
113 
114     /**
115      * Creates a new TorqueOMMojo object wrapping the passed
116      * TorqueDataModelTask.
117      *
118      * @param torqueDataModelTask the DataModelTask to be wrapped by this Mojo.
119      */
120     DataModelTaskMojo(TorqueDataModelTask torqueDataModelTask)
121     {
122         super(torqueDataModelTask);
123     }
124 
125     /**
126      * Configures the Texen task which is wrapped by this mojo.
127      * In this implementation, the context properties, useClasspath,
128      * the output directory, the control template, the schema Fileset,
129      * the target package and the target database are set.
130      *
131      * @throws MojoExecutionException if an error occurs when setting the Tasks
132      *         properties.
133      *
134      * @see TexenTaskMojo#configureTask()
135      */
136     protected void configureTask() throws MojoExecutionException
137     {
138         super.configureTask();
139 
140         TorqueDataModelTask task
141                 = (TorqueDataModelTask) super.getGeneratorTask();
142 
143         task.setControlTemplate(getControlTemplate());
144 
145         task.setOutputFile(reportFile);
146 
147         task.setTargetDatabase(targetDatabase);
148 
149         task.setTargetPackage(getTargetPackage());
150 
151         if (sqlDbMap != null)
152         {
153             task.setSqlDbMap(sqlDbMap);
154         }
155 
156         {
157             FileSet fileSet = new FileSet();
158             fileSet.setDir(new File(schemaDir));
159             fileSet.setIncludes(schemaIncludes);
160             fileSet.setExcludes(schemaExcludes);
161 
162             task.addFileset(fileSet);
163         }
164     }
165 
166     /**
167      * Returns whether the schema has changed. This is done by comparing
168      * the modification date of all schema files to the modification date
169      * of the report file.
170      *
171      * @return whether the schema has changed.
172      */
173     protected boolean schemaChanged()
174     {
175         boolean schemaChanged = true;
176         File report = new File(super.getOutputDir(), this.reportFile);
177         if (report.exists())
178         {
179             FileSet fileSet = new FileSet();
180             fileSet.setDir(new File(schemaDir));
181             fileSet.setIncludes(schemaIncludes);
182             fileSet.setExcludes(schemaExcludes);
183 
184             DirectoryScanner directoryScanner
185                 = fileSet.getDirectoryScanner(getAntProject());
186 
187             String[] fileNames = directoryScanner.getIncludedFiles();
188 
189 /*            File schemaDirectory = new File(schemaDir);
190             FileFilter schemaFileFilter = new FileFilter() {
191                 public boolean accept(File toCheck)
192                 {
193                     String path = toCheck.getAbsolutePath();
194                     if (schemaExcludes != null
195                         && !schemaExcludes.trim().equals("")
196                         && Pattern.matches(schemaExcludes, path))
197                     {
198                         return false;
199                     }
200                     return Pattern.matches(schemaIncludes, path);
201                 }
202             };
203             File[] schemaFiles = schemaDirectory.listFiles(schemaFileFilter);*/
204 
205             // schema has changed if one of the schema files has a modification
206             // Date larger than the report.
207             schemaChanged = false;
208             for (int i = 0; i < fileNames.length; ++i)
209             {
210                 File file = new File(fileNames[i]);
211                 if (file.lastModified() > report.lastModified())
212                 {
213                     schemaChanged = true;
214                     break;
215                 }
216             }
217         }
218 
219         return schemaChanged;
220     }
221 
222     /**
223      * Returns the directory where the schema files are located.
224      *
225      * @return the the directory where the schema files are located.
226      */
227     public String getSchemaDir()
228     {
229         return schemaDir;
230     }
231 
232     /**
233      * Sets the the directory where the schema files are located.
234      *
235      * @param schemaDir the directory where the schema files are located.
236      */
237     public void setSchemaDir(String schemaDir)
238     {
239         this.schemaDir = schemaDir;
240     }
241 
242     /**
243      * Returns the target database (e.g. mysql, oracle, ... )
244      * for the generated files.
245      *
246      * @return the target database for the generated files.
247      */
248     public String getTargetDatabase()
249     {
250         return targetDatabase;
251     }
252 
253     /**
254      * Sets the target database (e.g. mysql, oracle, ... )
255      * for the generated files.
256      *
257      * @param targetDatabase the target database for the generated files.
258      */
259     public void setTargetDatabase(String targetDatabase)
260     {
261         this.targetDatabase = targetDatabase;
262     }
263 
264     /**
265      * Returns the target package for the generated classes.
266      *
267      * @return the target package for the generated classes.
268      */
269     public String getTargetPackage()
270     {
271         return targetPackage;
272     }
273 
274     /**
275      * Sets the target package for the generated classes.
276      *
277      * param targetPackage the target package for the generated classes.
278      */
279     public void setTargetPackage(String targetPackage)
280     {
281         this.targetPackage = targetPackage;
282     }
283 
284     /**
285      * Gets the path to the report file. The path is relative to
286      * <code>outputDir</code>.
287      *
288      * @return the path to the report file.
289      */
290     public String getReportFile()
291     {
292         return reportFile;
293     }
294 
295     /**
296      * Sets the path to the report file. The path is relative to
297      * <code>outputDir</code>.
298      *
299      * @param reportFile the path to the report file.
300      */
301     public void setReportFile(String reportFile)
302     {
303         this.reportFile = reportFile;
304     }
305 
306     /**
307      * Returns whether this mojo should be executed only if the schema has
308      * changed.
309      *
310      * @return true if the mojo only runs if the schema has changed,
311      *         false otherwise.
312      */
313     public boolean isRunOnlyOnSchemaChange()
314     {
315         return runOnlyOnSchemaChange;
316     }
317 
318     /**
319      * Sets whether this mojo should be executed only if the schema has
320      * changed.
321      *
322      * @param runOnlyOnSchemaChange whether the mojo only should run
323      *        if the schema has changed.
324      */
325     public void setRunOnlyOnSchemaChange(boolean runOnlyOnSchemaChange)
326     {
327         this.runOnlyOnSchemaChange = runOnlyOnSchemaChange;
328     }
329 
330     /**
331      * Runs the generation for the database layout defined in the schema.xml
332      * files.
333      *
334      * @throws MojoExecutionException If an error occurs during generation.
335      *
336      * @see TexenTaskMojo#execute()
337      * @see org.apache.maven.plugin.Mojo#execute()
338      */
339     public void execute() throws MojoExecutionException
340     {
341         if (!schemaChanged() && isRunOnlyOnSchemaChange())
342         {
343             getLog().info("Schema has not changed; skipping generation");
344             return;
345         }
346 
347         super.execute();
348     }
349 
350     /**
351      * Returns the schema files which are excluded from generation.
352      *
353      * @return the pattern for the excluded files.
354      */
355     public String getSchemaExcludes()
356     {
357         return schemaExcludes;
358     }
359 
360     /**
361      * Sets the schema files which are excluded from generation.
362      *
363      * @param schemaExcludes the pattern for the excluded files.
364      */
365     public void setSchemaExcludes(String schemaExcludes)
366     {
367         this.schemaExcludes = schemaExcludes;
368     }
369 
370     /**
371      * Returns the schema files which are included in generation.
372      *
373      * @return the pattern for the included files.
374      */
375     public String getSchemaIncludes()
376     {
377         return schemaIncludes;
378     }
379 
380     /**
381      * Sets the schema files which are included in generation.
382      *
383      * @param schemaIncludes the pattern for the included files.
384      */
385     public void setSchemaIncludes(String schemaIncludes)
386     {
387         this.schemaIncludes = schemaIncludes;
388     }
389 
390     /**
391      * Returns the path to the mapping SQL Files -> database.
392      *
393      * @return the path to the mapping SQL Files -> database.
394      */
395     public String getSqlDbMap()
396     {
397         return sqlDbMap;
398     }
399 
400     /**
401      * Sets the path to the mapping SQL Files -> database.
402      *
403      * @param sqlDbMap the absolute path to the mapping SQL Files -> database.
404      */
405     public void setSqlDbMap(String sqlDbMap)
406     {
407         this.sqlDbMap = sqlDbMap;
408     }
409 
410     /**
411      * Returns the path to the control template.
412      *
413      * @return the path to the control template.
414      */
415     protected abstract String getControlTemplate();
416 }