| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| TexenTaskMojo |
|
| 1.4;1.4 |
| 1 | package org.apache.torque.mojo; | |
| 2 | ||
| 3 | import java.io.File; | |
| 4 | import java.util.Iterator; | |
| 5 | import java.util.Map; | |
| 6 | ||
| 7 | import org.apache.commons.configuration.ConfigurationException; | |
| 8 | import org.apache.commons.configuration.FileConfiguration; | |
| 9 | import org.apache.commons.configuration.PropertiesConfiguration; | |
| 10 | import org.apache.maven.plugin.MojoExecutionException; | |
| 11 | import org.apache.texen.ant.TexenTask; | |
| 12 | ||
| 13 | /** | |
| 14 | * The base class for mojos that wrap Texen Ant Tasks | |
| 15 | */ | |
| 16 | 0 | public abstract class TexenTaskMojo extends AntTaskMojo { |
| 17 | /** | |
| 18 | * The directory where the generator output is written | |
| 19 | * | |
| 20 | * @required | |
| 21 | */ | |
| 22 | private String outputDir; | |
| 23 | ||
| 24 | /** | |
| 25 | * The base path where the templates are read from, if they are not read from the classpath. | |
| 26 | * | |
| 27 | * @parameter expression="${templatePath}" default-value="${basedir}/src/main/impex/templates" | |
| 28 | */ | |
| 29 | private String templatePath; | |
| 30 | ||
| 31 | /** | |
| 32 | * Whether the templates should be loaded from the classpath. | |
| 33 | * | |
| 34 | * @parameter expression="${useClasspath}" default-value="true" | |
| 35 | */ | |
| 36 | private boolean useClasspath; | |
| 37 | ||
| 38 | /** | |
| 39 | * A map where all user-defined context properties can be set. Overrides all other mojo configuration settings which | |
| 40 | * are mapped to context properties. | |
| 41 | * | |
| 42 | * @parameter | |
| 43 | */ | |
| 44 | private Map<?, ?> userContextProperties; | |
| 45 | ||
| 46 | /** | |
| 47 | * The path to the generated context property file. | |
| 48 | * | |
| 49 | * @required | |
| 50 | */ | |
| 51 | private String contextPropertiesPath; | |
| 52 | ||
| 53 | /** | |
| 54 | * Sets the path to Torque's output directory. | |
| 55 | * | |
| 56 | * @param outputDir | |
| 57 | * the path to Torque's output directory. | |
| 58 | */ | |
| 59 | public void setOutputDir(String outputDir) { | |
| 60 | 0 | this.outputDir = outputDir; |
| 61 | 0 | } |
| 62 | ||
| 63 | /** | |
| 64 | * Returns the path to Torque's output directory. | |
| 65 | * | |
| 66 | * @return the path to Torque's output directory. Not null if initialized correctly. | |
| 67 | */ | |
| 68 | public String getOutputDir() { | |
| 69 | 0 | return this.outputDir; |
| 70 | } | |
| 71 | ||
| 72 | /** | |
| 73 | * Sets the path to Torque's templates, if the classpath is not used to load the templates. | |
| 74 | * | |
| 75 | * @param templatePath | |
| 76 | * the path to Torque's templates. | |
| 77 | */ | |
| 78 | public void setTemplatePath(String templatePath) { | |
| 79 | 0 | this.templatePath = templatePath; |
| 80 | 0 | } |
| 81 | ||
| 82 | /** | |
| 83 | * Returns the path to Torque's templates, if the classpath is not used to load the templates. | |
| 84 | * | |
| 85 | * @return the path to Torque's templates. | |
| 86 | */ | |
| 87 | public String getTemplatePath() { | |
| 88 | 0 | return this.templatePath; |
| 89 | } | |
| 90 | ||
| 91 | /** | |
| 92 | * Sets whether the classpath should be used to locate the templates. | |
| 93 | * | |
| 94 | * @param templatePath | |
| 95 | * the path to Torque's templates. | |
| 96 | */ | |
| 97 | public void setUseClasspath(boolean useClasspath) { | |
| 98 | 0 | this.useClasspath = useClasspath; |
| 99 | 0 | } |
| 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 | 0 | 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 | 0 | this.contextPropertiesPath = contextPropertiesPath; |
| 118 | 0 | } |
| 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 | 0 | 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 | 0 | this.userContextProperties = userContextProperties; |
| 138 | 0 | } |
| 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 | 0 | 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 | 0 | 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 | 0 | FileConfiguration configuration = getMojoContextProperties(); |
| 167 | 0 | if (userContextProperties != null) { |
| 168 | 0 | for (Iterator<?> contextPropertyIt = userContextProperties.entrySet().iterator(); contextPropertyIt.hasNext();) { |
| 169 | 0 | Map.Entry<?, ?> entry = (Map.Entry<?, ?>) contextPropertyIt.next(); |
| 170 | 0 | configuration.addProperty(entry.getKey().toString(), entry.getValue().toString()); |
| 171 | 0 | } |
| 172 | } | |
| 173 | 0 | configuration.save(contextPropertiesPath); |
| 174 | 0 | } catch (ConfigurationException e) { |
| 175 | 0 | getLog().error("Error writing temporary context properties: " + e.getMessage()); |
| 176 | 0 | throw new MojoExecutionException(e.getMessage()); |
| 177 | 0 | } |
| 178 | 0 | } |
| 179 | ||
| 180 | /** | |
| 181 | * Configures the Texen task wrapped by this mojo. | |
| 182 | */ | |
| 183 | protected void configureTask() throws MojoExecutionException { | |
| 184 | 0 | super.configureTask(); |
| 185 | 0 | TexenTask task = getGeneratorTask(); |
| 186 | 0 | task.setContextProperties(contextPropertiesPath); |
| 187 | 0 | task.setUseClasspath(useClasspath); |
| 188 | try { | |
| 189 | 0 | task.setTemplatePath(templatePath); |
| 190 | 0 | } catch (Exception e) { |
| 191 | 0 | throw new MojoExecutionException("Error setting template path", e); |
| 192 | 0 | } |
| 193 | ||
| 194 | 0 | File outputDirectory = new File(outputDir); |
| 195 | 0 | outputDirectory.mkdirs(); |
| 196 | 0 | getGeneratorTask().setOutputDirectory(outputDirectory); |
| 197 | 0 | } |
| 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 | 0 | generateContextProperties(); |
| 210 | 0 | super.executeMojo(); |
| 211 | 0 | } |
| 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 | } |