001    /**
002     * Copyright 2004-2012 The Kuali Foundation
003     *
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.opensource.org/licenses/ecl2.php
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.apache.torque.mojo;
017    
018    import java.io.File;
019    import java.util.Iterator;
020    import java.util.Map;
021    
022    import org.apache.commons.configuration.ConfigurationException;
023    import org.apache.commons.configuration.FileConfiguration;
024    import org.apache.commons.configuration.PropertiesConfiguration;
025    import org.apache.maven.plugin.MojoExecutionException;
026    import org.apache.texen.ant.TexenTask;
027    
028    /**
029     * The base class for mojos that wrap Texen Ant Tasks
030     */
031    public abstract class TexenTaskMojo extends AntTaskMojo {
032            /**
033             * The directory where the generator output is written
034             * 
035             * @required
036             */
037            private String outputDir;
038    
039            /**
040             * The base path where the templates are read from, if they are not read from the classpath.
041             * 
042             * @parameter expression="${templatePath}" default-value="${basedir}/src/main/impex/templates"
043             */
044            private String templatePath;
045    
046            /**
047             * Whether the templates should be loaded from the classpath.
048             * 
049             * @parameter expression="${useClasspath}" default-value="true"
050             */
051            private boolean useClasspath;
052    
053            /**
054             * A map where all user-defined context properties can be set. Overrides all other mojo configuration settings which
055             * are mapped to context properties.
056             * 
057             * @parameter
058             */
059            private Map<?, ?> userContextProperties;
060    
061            /**
062             * The path to the generated context property file.
063             * 
064             * @required
065             */
066            private String contextPropertiesPath;
067    
068            /**
069             * Sets the path to Torque's output directory.
070             * 
071             * @param outputDir
072             *            the path to Torque's output directory.
073             */
074            public void setOutputDir(String outputDir) {
075                    this.outputDir = outputDir;
076            }
077    
078            /**
079             * Returns the path to Torque's output directory.
080             * 
081             * @return the path to Torque's output directory. Not null if initialized correctly.
082             */
083            public String getOutputDir() {
084                    return this.outputDir;
085            }
086    
087            /**
088             * Sets the path to Torque's templates, if the classpath is not used to load the templates.
089             * 
090             * @param templatePath
091             *            the path to Torque's templates.
092             */
093            public void setTemplatePath(String templatePath) {
094                    this.templatePath = templatePath;
095            }
096    
097            /**
098             * Returns the path to Torque's templates, if the classpath is not used to load the templates.
099             * 
100             * @return the path to Torque's templates.
101             */
102            public String getTemplatePath() {
103                    return this.templatePath;
104            }
105    
106            /**
107             * Sets whether the classpath should be used to locate the templates.
108             * 
109             * @param templatePath
110             *            the path to Torque's templates.
111             */
112            public void setUseClasspath(boolean useClasspath) {
113                    this.useClasspath = useClasspath;
114            }
115    
116            /**
117             * Returns whether the classpath is used to locate the templates.
118             * 
119             * @return true if the classpath is used to locate the templates, false otherwise
120             */
121            public boolean getUseClasspath() {
122                    return this.useClasspath;
123            }
124    
125            /**
126             * Sets the path to the generated property file used as Texen's context properties.
127             * 
128             * @param generatedPropertyFilePath
129             *            the path to the generated context properties file.
130             */
131            public void setContextPropertiesPath(String contextPropertiesPath) {
132                    this.contextPropertiesPath = contextPropertiesPath;
133            }
134    
135            /**
136             * Returns the path to the generated property file used as Texen's context properties.
137             * 
138             * @return the path to the generated context properties file.
139             */
140            public String getContextPropertiesPath() {
141                    return this.contextPropertiesPath;
142            }
143    
144            /**
145             * Sets the map which defines user-defined context properties. The settings override all other mojo configuration
146             * settings which are mapped to context properties.
147             * 
148             * @param contextProperties
149             *            the user-defined context properties.
150             */
151            public void setUserContextProperties(Map<?, ?> userContextProperties) {
152                    this.userContextProperties = userContextProperties;
153            }
154    
155            /**
156             * Returns the map which defines user-defined context properties.
157             * 
158             * @return the map containing user-defined context properties, or null if not set.
159             */
160            public Map<?, ?> getUserContextProperties() {
161                    return userContextProperties;
162            }
163    
164            /**
165             * returns the generator Task for this mojo.
166             * 
167             * @return the generator Task, not null.
168             */
169            protected TexenTask getGeneratorTask() {
170                    return (TexenTask) getAntTask();
171            }
172    
173            /**
174             * Generates the context properties file for Texen. The file is written to the path contextPropertiesPath.
175             * 
176             * @throws MojoExecutionException
177             *             if an error occurs.
178             */
179            protected void generateContextProperties() throws MojoExecutionException {
180                    try {
181                            FileConfiguration configuration = getMojoContextProperties();
182                            if (userContextProperties != null) {
183                                    for (Iterator<?> contextPropertyIt = userContextProperties.entrySet().iterator(); contextPropertyIt.hasNext();) {
184                                            Map.Entry<?, ?> entry = (Map.Entry<?, ?>) contextPropertyIt.next();
185                                            configuration.addProperty(entry.getKey().toString(), entry.getValue().toString());
186                                    }
187                            }
188                            configuration.save(contextPropertiesPath);
189                    } catch (ConfigurationException e) {
190                            getLog().error("Error writing temporary context properties: " + e.getMessage());
191                            throw new MojoExecutionException(e.getMessage());
192                    }
193            }
194    
195            /**
196             * Configures the Texen task wrapped by this mojo.
197             */
198            protected void configureTask() throws MojoExecutionException {
199                    super.configureTask();
200                    TexenTask task = getGeneratorTask();
201                    task.setContextProperties(contextPropertiesPath);
202                    task.setUseClasspath(useClasspath);
203                    try {
204                            task.setTemplatePath(templatePath);
205                    } catch (Exception e) {
206                            throw new MojoExecutionException("Error setting template path", e);
207                    }
208    
209                    File outputDirectory = new File(outputDir);
210                    outputDirectory.mkdirs();
211                    getGeneratorTask().setOutputDirectory(outputDirectory);
212            }
213    
214            /**
215             * Executes the wrapped Texen task. Before this is done, the context properties file is generated and the Texen task
216             * is configured.
217             * 
218             * @throws MojoExecutionException
219             *             if an error occurs during execution.
220             * 
221             * @see org.apache.maven.plugin.Mojo#execute()
222             */
223            public void executeMojo() throws MojoExecutionException {
224                    generateContextProperties();
225                    super.executeMojo();
226            }
227    
228            /**
229             * Returns the context properties for the Texen task which are defined in the configuration of the child mojo. This
230             * method needs to be overwritten in subclasses.
231             * 
232             * @return The PropertiesConfiguration containing all context properties, not null.
233             */
234            protected abstract PropertiesConfiguration getMojoContextProperties();
235    }