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