1 /** 2 * Copyright 2004-2012 The Kuali Foundation 3 * 4 * Licensed under the Educational Community License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.opensource.org/licenses/ecl2.php 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package org.apache.torque.mojo; 17 18 import java.io.File; 19 import java.util.Iterator; 20 import java.util.Map; 21 22 import org.apache.commons.configuration.ConfigurationException; 23 import org.apache.commons.configuration.FileConfiguration; 24 import org.apache.commons.configuration.PropertiesConfiguration; 25 import org.apache.maven.plugin.MojoExecutionException; 26 import org.apache.texen.ant.TexenTask; 27 28 /** 29 * The base class for mojos that wrap Texen Ant Tasks 30 */ 31 public abstract class TexenTaskMojo extends AntTaskMojo { 32 /** 33 * The directory where the generator output is written 34 * 35 * @required 36 */ 37 private String outputDir; 38 39 /** 40 * The base path where the templates are read from, if they are not read from the classpath. 41 * 42 * @parameter expression="${templatePath}" default-value="${basedir}/src/main/impex/templates" 43 */ 44 private String templatePath; 45 46 /** 47 * Whether the templates should be loaded from the classpath. 48 * 49 * @parameter expression="${useClasspath}" default-value="true" 50 */ 51 private boolean useClasspath; 52 53 /** 54 * A map where all user-defined context properties can be set. Overrides all other mojo configuration settings which 55 * are mapped to context properties. 56 * 57 * @parameter 58 */ 59 private Map<?, ?> userContextProperties; 60 61 /** 62 * The path to the generated context property file. 63 * 64 * @required 65 */ 66 private String contextPropertiesPath; 67 68 /** 69 * Sets the path to Torque's output directory. 70 * 71 * @param outputDir 72 * the path to Torque's output directory. 73 */ 74 public void setOutputDir(String outputDir) { 75 this.outputDir = outputDir; 76 } 77 78 /** 79 * Returns the path to Torque's output directory. 80 * 81 * @return the path to Torque's output directory. Not null if initialized correctly. 82 */ 83 public String getOutputDir() { 84 return this.outputDir; 85 } 86 87 /** 88 * Sets the path to Torque's templates, if the classpath is not used to load the templates. 89 * 90 * @param templatePath 91 * the path to Torque's templates. 92 */ 93 public void setTemplatePath(String templatePath) { 94 this.templatePath = templatePath; 95 } 96 97 /** 98 * Returns the path to Torque's templates, if the classpath is not used to load the templates. 99 * 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 }