View Javadoc

1   package org.apache.torque.ant.task;
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  import java.util.ArrayList;
24  import java.util.HashMap;
25  import java.util.List;
26  import java.util.Map;
27  import java.util.Set;
28  
29  import org.apache.tools.ant.BuildException;
30  import org.apache.tools.ant.Project;
31  import org.apache.tools.ant.Task;
32  import org.apache.torque.generator.configuration.ConfigurationException;
33  import org.apache.torque.generator.configuration.UnitDescriptor;
34  import org.apache.torque.generator.configuration.UnitDescriptor.Packaging;
35  import org.apache.torque.generator.configuration.controller.Loglevel;
36  import org.apache.torque.generator.configuration.option.MapOptionsConfiguration;
37  import org.apache.torque.generator.configuration.option.OptionsConfiguration;
38  import org.apache.torque.generator.configuration.paths.CustomProjectPaths;
39  import org.apache.torque.generator.configuration.paths.DefaultTorqueGeneratorPaths;
40  import org.apache.torque.generator.configuration.paths.Maven2DirectoryProjectPaths;
41  import org.apache.torque.generator.configuration.paths.Maven2JarProjectPaths;
42  import org.apache.torque.generator.configuration.paths.ProjectPaths;
43  import org.apache.torque.generator.control.Controller;
44  import org.apache.torque.generator.file.Fileset;
45  import org.apache.torque.generator.source.stream.FileSourceProvider;
46  
47  /**
48   * Executes a unit of generation within the torque generator.
49   *
50   * $Id: TorqueGeneratorTask.java 1353994 2012-06-26 12:33:26Z tfischer $
51   *
52   * @goal generate
53   */
54  public class TorqueGeneratorTask extends Task
55  {
56      /**
57       * The packaging type of the generation unit, either "directory" , "jar"
58       * or "classpath". Default is "directory".
59       */
60      private String packaging = "directory";
61  
62      /**
63       * The root directory of the project.
64       * Has no effect if packaging is "classpath".
65       * Default is ".".
66       */
67      private File projectRootDir = new File(".");
68  
69      /**
70       * The configuration directory of the generation unit.
71       * Has no effect if packaging is "classpath".
72       */
73      private File configDir;
74  
75      /**
76       * The configuration package of the generation unit.
77       * Has only effect if packaging is "classpath".
78       */
79      private String configPackage;
80  
81      /**
82       * The directory where the source files reside.
83       */
84      private File sourceDir;
85  
86      /**
87       * Include patterns for the source files.
88       * If set, the include and exclude patterns from the templates
89       * are overridden.
90       * If not set, then the include patterns from the templates are used.
91       * The patterns are case sensitive, wildcards are * and ?.
92       */
93      private Set<String> sourceIncludes;
94  
95      /**
96       * Exclude patterns for the source files.
97       * If set, the include and exclude patterns from the templates
98       * are overridden.
99       * If not set, then the include patterns from the templates are used.
100      * The patterns are case sensitive, wildcards are * and ?.
101      */
102     private Set<String> sourceExcludes;
103 
104     /**
105      * The target directory for files which are generated each time anew.
106      * Default is "target/generated-sources"
107      */
108     private File defaultOutputDir = new File("target/generated-sources");
109 
110     /**
111      * The target directories for files which are not generated each time anew.
112      * Default is modifiable -> "./src/main/generated-java"
113      */
114     private Map<String, File> outputDirMap = new HashMap<String, File>();
115 
116     /**
117      * The filename of the jar file of the generation unit.
118      * Has only effect if packaging is "jar".
119      */
120     private String jarFile;
121 
122     /**
123      * The config directory of the project overriding the settings.
124      * If set, the settings of this directory are used as "child"
125      * and the "normal" settings are used as "parent".
126      */
127     private File overrideConfigDir;
128 
129     /**
130      * The Loglevel to use in the generation process. Must be one of
131      * trace, debug, info, warn or error.
132      * If not set, the log level defined in the generation unit is used.
133      */
134     private String loglevel;
135 
136     /**
137      * The encoding which should be used for the files which do not have an
138      * output encoding set in the templates.
139      */
140     private String defaultOutputEncoding;
141 
142     /**
143      * Whether all source files should be combined into one source tree.
144      * If false, each source file will be read in its own source tree
145      * and start a new generation run.
146      * If true, a single source tree with the following structure will be
147      * built from all source files:
148      * &lt;source&gt;
149      *   &lt;file path="path/to/file1"&gt;
150      *      &lt;rootOfFile1&gt;
151      *        ...
152      *      &lt;/rootOfFile1&gt;
153      *   &lt;/file&gt;
154      *   &lt;file path="path/to/file2"&gt;
155      *      &lt;rootOfFile2&gt;
156      *        ...
157      *      &lt;/rootOfFile2&gt;
158      *   &lt;/file&gt;
159      *   ...
160      * &lt;/source&gt;
161      * If not set, the settings from the templates will be used.
162      */
163     private Boolean combineFiles;
164 
165     /** The list of options for the generation task. */
166     private final List<Option> options = new ArrayList<Option>();
167 
168     /**
169      * Creates a new option and adds it to the list of options.
170      *
171      * @return the newly created option.
172      */
173     public Option createOption()
174     {
175         Option option = new Option();
176         options.add(option);
177         return option;
178     }
179 
180     /**
181      * Runs the generation.
182      */
183     @Override
184     public void execute() throws BuildException
185     {
186         Controller controller = new Controller();
187         List<UnitDescriptor> unitDescriptors = new ArrayList<UnitDescriptor>();
188 
189         UnitDescriptor.Packaging packaging;
190         if ("jar".equals(this.packaging))
191         {
192             packaging = UnitDescriptor.Packaging.JAR;
193         }
194         else if ("directory".equals(this.packaging))
195         {
196             packaging = UnitDescriptor.Packaging.DIRECTORY;
197         }
198         else if ("classpath".equals(this.packaging))
199         {
200             packaging = UnitDescriptor.Packaging.CLASSPATH;
201         }
202         else
203         {
204             throw new IllegalArgumentException(
205                     "Unknown packaging " + this.packaging
206                         + ", must be jar, directory or classpath");
207         }
208         log("Packaging is " + packaging, Project.MSG_DEBUG);
209 
210         ProjectPaths defaultProjectPaths;
211         if (UnitDescriptor.Packaging.JAR == packaging)
212         {
213             defaultProjectPaths
214                     = new Maven2JarProjectPaths(projectRootDir, jarFile);
215         }
216         else if (UnitDescriptor.Packaging.DIRECTORY == packaging)
217         {
218             defaultProjectPaths
219                     = new Maven2DirectoryProjectPaths(projectRootDir);
220         }
221         else if (UnitDescriptor.Packaging.CLASSPATH == packaging)
222         {
223             defaultProjectPaths
224                     = new Maven2DirectoryProjectPaths(projectRootDir);
225         }
226         else
227         {
228              throw new IllegalStateException("Unknown packaging" + packaging);
229         }
230 
231         CustomProjectPaths projectPaths
232                 = new CustomProjectPaths(defaultProjectPaths);
233 
234         if (UnitDescriptor.Packaging.CLASSPATH == packaging)
235         {
236             if (configPackage == null)
237             {
238                 throw new BuildException(
239                     "configPackage must be set for packaging =\"classpath\"");
240             }
241             projectPaths.setConfigurationPackage(configPackage);
242             projectPaths.setConfigurationDir(null);
243         }
244         else
245         {
246             if (configDir != null)
247             {
248                 projectPaths.setConfigurationDir(configDir);
249                 log("Setting config dir to " + configDir.toString(),
250                         Project.MSG_DEBUG);
251             }
252         }
253 
254         if (sourceDir != null)
255         {
256             projectPaths.setSourceDir(sourceDir);
257             log("Setting source dir to " + sourceDir.toString(),
258                     Project.MSG_DEBUG);
259         }
260 
261         FileSourceProvider fileSourceProvider = null;
262         if (sourceIncludes != null || sourceExcludes != null)
263         {
264             Fileset sourceFileset
265                     = new Fileset(
266                             projectPaths.getDefaultSourcePath(),
267                             sourceIncludes,
268                             sourceExcludes);
269             log("Setting source includes to " + sourceIncludes,
270                     Project.MSG_DEBUG);
271             log("Setting source excludes to " + sourceExcludes,
272                     Project.MSG_DEBUG);
273             try
274             {
275                 fileSourceProvider = new FileSourceProvider(
276                         null,
277                         sourceFileset,
278                         combineFiles);
279             }
280             catch (ConfigurationException e)
281             {
282                 throw new BuildException(
283                         "The source provider cannot be instantiated", e);
284             }
285         }
286 
287         if (defaultOutputDir != null)
288         {
289             projectPaths.setOutputDirectory(null, defaultOutputDir);
290             log("Setting defaultOutputDir to "
291                     + defaultOutputDir.getAbsolutePath(),
292                 Project.MSG_DEBUG);
293         }
294         if (outputDirMap != null)
295         {
296             for (Map.Entry<String, File> dirEntry : outputDirMap.entrySet())
297             {
298                 projectPaths.setOutputDirectory(
299                         dirEntry.getKey(),
300                         dirEntry.getValue());
301                 log("Setting output directory with key " + dirEntry.getKey()
302                         + " to "
303                         + dirEntry.getValue(),
304                     Project.MSG_DEBUG);
305             }
306         }
307         log("ProjectPaths = " + projectPaths, Project.MSG_DEBUG);
308 
309         OptionsConfiguration optionConfiguration = null;
310         if (!options.isEmpty())
311         {
312             Map<String, String> optionsMap = new HashMap<String, String>();
313             for (Option option : options)
314             {
315                 optionsMap.put(option.getKey(), option.getValue());
316             }
317             optionConfiguration = new MapOptionsConfiguration(optionsMap);
318         }
319         Loglevel convertedLoglevel = null;
320         if (this.loglevel != null)
321         {
322             convertedLoglevel = Loglevel.getByKey(loglevel);
323         }
324         UnitDescriptor unitDescriptor = new UnitDescriptor(
325                 packaging,
326                 projectPaths,
327                 new DefaultTorqueGeneratorPaths());
328         unitDescriptor.setOverrideSourceProvider(fileSourceProvider);
329         unitDescriptor.setOverrideOptions(optionConfiguration);
330         unitDescriptor.setLoglevel(convertedLoglevel);
331         unitDescriptor.setDefaultOutputEncoding(defaultOutputEncoding);
332         log("unit descriptor created", Project.MSG_DEBUG);
333         if (overrideConfigDir != null)
334         {
335             CustomProjectPaths childProjectPaths
336                 = new CustomProjectPaths(projectPaths);
337             childProjectPaths.setConfigurationDir(overrideConfigDir);
338 
339             UnitDescriptor parentUnitDescriptor = new UnitDescriptor(
340                     Packaging.DIRECTORY,
341                     childProjectPaths,
342                     new DefaultTorqueGeneratorPaths());
343             parentUnitDescriptor.setInheritsFrom(unitDescriptor);
344             parentUnitDescriptor.setOverrideSourceProvider(fileSourceProvider);
345             parentUnitDescriptor.setOverrideOptions(optionConfiguration);
346             parentUnitDescriptor.setLoglevel(convertedLoglevel);
347             parentUnitDescriptor.setDefaultOutputEncoding(
348                     defaultOutputEncoding);
349             log("child unit descriptor created",Project.MSG_DEBUG);
350             unitDescriptor = parentUnitDescriptor;
351         }
352         unitDescriptors.add(unitDescriptor);
353         try
354         {
355             log("Generation started", Project.MSG_DEBUG);
356             controller.run(unitDescriptors);
357             log("Generation successful", Project.MSG_INFO);
358         }
359         catch (Exception e)
360         {
361             log("Error during generation", e, Project.MSG_ERR);
362             throw new BuildException(e.getMessage());
363         }
364     }
365 
366     /**
367      * Sets the packaging.
368      *
369      * @param packaging the packaging, either "jar" or "directory"
370      */
371     public void setPackaging(String packaging)
372     {
373         this.packaging = packaging;
374     }
375 
376     /**
377      * Sets the root directory of the project.
378      *
379      * @param projectRootDir the project root Directory.
380      */
381     public void setProjectRootDir(File projectRootDir)
382     {
383         this.projectRootDir = projectRootDir;
384     }
385 
386     public void setConfigDir(File configDir)
387     {
388         this.configDir = configDir;
389     }
390 
391     public void setConfigPackage(String configPackage)
392     {
393         this.configPackage = configPackage;
394     }
395 
396     /**
397      * Sets the default output base directory for generated files.
398      *
399      * @param targetDir the default output directory,
400      *                  or null to use the default.
401      */
402     public void setDefaultOutputDir(File defaultOutputDir)
403     {
404         this.defaultOutputDir = defaultOutputDir;
405     }
406 
407     /**
408      * Sets the mapping from outputDirKey to output directories.
409      * The outputDirKeys are defined in the templates you use.
410      *
411      * @param outputDirMap the new outputDirMap.
412      */
413     public void setOutputDirMap(Map<String, File> outputDirMap)
414     {
415         this.outputDirMap = outputDirMap;
416     }
417 
418     /**
419      * The path to the jar file to use.
420      *
421      * @param jarFile the jar file, or null.
422      */
423     public void setJarFile(String jarFile)
424     {
425         this.jarFile = jarFile;
426     }
427 
428     /**
429      * Sets the directory in which the source files are located.
430      *
431      * @param sourceDir the directory in which the source files are located.
432      */
433     public void setSourceDir(File sourceDir)
434     {
435         this.sourceDir = sourceDir;
436     }
437 
438     /**
439      * Sets the pattern which files are included in the generation process.
440      *
441      * @param sourceIncludes a list containing the include patterns, or null
442      *        if no include pattern should be used.
443      */
444     public void setSourceIncludes(Set<String> sourceIncludes)
445     {
446         this.sourceIncludes = sourceIncludes;
447     }
448 
449     /**
450      * Sets the pattern which files are excluded in the generation process.
451      *
452      * @param sourceExcludes a list containing the exclude patterns, or null
453      *        if no exclude pattern should be used.
454      */
455     public void setSourceExcludes(Set<String> sourceExcludes)
456     {
457         this.sourceExcludes = sourceExcludes;
458     }
459 
460     /**
461      * Sets the config directory overriding the template settings.
462      * If set, the settings of this directory are used as "child"
463      * and the "normal" settings are used as "parent".
464      *
465      * @parameter the config directory overriding the template settings,
466      *            or null if the template settings will not be overridden.
467      */
468     public void setOverrideConfigDir(File overrideConfigDir)
469     {
470         this.overrideConfigDir = overrideConfigDir;
471     }
472 
473     /**
474      * Sets the Loglevel to use in the generation process.
475      *
476      * @param loglevel the loglevel, must be one of trace, debug, info, warn
477      *        or error, or null if the loglevel defined in the templates
478      *        should be used.
479      */
480     public void setLoglevel(String loglevel)
481     {
482         this.loglevel = loglevel;
483     }
484 
485     /**
486      * Sets the encoding which should be used for the files which do not have
487      * an output encoding set in the templates.
488      *
489      * @param defaultOutputEncoding the default output encoding,
490      *        or null to use the generator default
491      *        (the platform default encoding).
492      */
493     public void setDefaultOutputEncoding(String defaultOutputEncoding)
494     {
495         this.defaultOutputEncoding = defaultOutputEncoding;
496     }
497 
498     /**
499      * Sets whether all source files should be combined into one source tree.
500      * If false, each source file will be read in its own source tree
501      * and start a new generation run.
502      * If true, a single source tree with the following structure will be
503      * built from all source files:
504      * &lt;source&gt;
505      *   &lt;file path="path/to/file1"&gt;
506      *      &lt;rootOfFile1&gt;
507      *        ...
508      *      &lt;/rootOfFile1&gt;
509      *   &lt;/file&gt;
510      *   &lt;file path="path/to/file2"&gt;
511      *      &lt;rootOfFile2&gt;
512      *        ...
513      *      &lt;/rootOfFile2&gt;
514      *   &lt;/file&gt;
515      *   ...
516      * &lt;/source&gt;
517      * If not set, the settings from the templates will be used.
518      *
519      * @param combineFiles whether all sources should be combined.
520      */
521     public void setCombineFiles(Boolean combineFiles)
522     {
523         this.combineFiles = combineFiles;
524     }
525 }