001: package org.andromda.maven.plugin.andromdapp;
002:
003: import java.io.File;
004: import java.io.IOException;
005: import java.io.StringReader;
006: import java.net.URL;
007: import java.util.Iterator;
008: import java.util.List;
009: import java.util.Properties;
010:
011: import org.andromda.core.common.ResourceUtils;
012: import org.apache.commons.lang.ObjectUtils;
013: import org.apache.maven.plugin.AbstractMojo;
014: import org.apache.maven.plugin.MojoExecutionException;
015: import org.apache.maven.plugin.resources.PropertyUtils;
016: import org.apache.maven.project.MavenProject;
017: import org.apache.maven.settings.Settings;
018: import org.codehaus.plexus.util.InterpolationFilterReader;
019:
020: /**
021: * The abstract AndroMDAapp mojo (this should be extended by any Mojo that
022: * executes AndroMDApp.
023: *
024: * @author Chad Brandon
025: */
026: public abstract class AbstractAndroMDAppMojo extends AbstractMojo {
027:
028: /**
029: * The URI to an optional AndroMDApp configuration file.
030: *
031: * @parameter expression="${configuration.uri}"
032: */
033: private String configurationUri;
034:
035: /**
036: * @parameter expression="${project}"
037: * @required
038: * @readonly
039: */
040: private MavenProject project;
041:
042: /**
043: * The current user system settings for use in Maven. (allows us to pass the user
044: * settings to the AndroMDA configuration).
045: *
046: * @parameter expression="${settings}"
047: * @required
048: * @readonly
049: */
050: protected Settings settings;
051:
052: /**
053: * @parameter expression="${project.build.filters}"
054: */
055: protected List propertyFiles;
056:
057: /**
058: * Collects and returns all properties as a Properties instance.
059: *
060: * @return the properties including those from the project, settings, etc.
061: * @throws IOException
062: */
063: private Properties getProperties() throws IOException {
064: // System properties
065: final Properties properties = new Properties();
066:
067: properties.put("settings", this .settings);
068:
069: // - project properties
070: properties.putAll(this .project.getProperties());
071: if (this .propertyFiles != null) {
072: for (final Iterator iterator = this .propertyFiles
073: .iterator(); iterator.hasNext();) {
074: final String propertiesFile = (String) iterator.next();
075: final Properties projectProperties = PropertyUtils
076: .loadPropertyFile(new File(propertiesFile),
077: true, true);
078:
079: properties.putAll(projectProperties);
080: }
081: }
082:
083: for (final Iterator iterator = properties.keySet().iterator(); iterator
084: .hasNext();) {
085: final String property = (String) iterator.next();
086: final String value = this .replaceProperties(properties,
087: ObjectUtils.toString(properties.get(property)));
088: properties.put(property, value);
089: }
090:
091: properties.putAll(System.getProperties());
092:
093: return properties;
094: }
095:
096: /**
097: * Replaces all properties having the style
098: * <code>${some.property}</code> with the value
099: * of the specified property if there is one.
100: *
101: * @param string the string to perform replacement on.
102: */
103: protected String replaceProperties(final String string)
104: throws IOException {
105: return this .replaceProperties(this .getProperties(), string);
106: }
107:
108: /**
109: * Retrieves the interpolated {@link #configurationUri} contents as a String.
110: *
111: * @return the contents of the configuration as a string.
112: * @throws MojoExecutionException
113: * @throws IOException
114: */
115: protected String getConfigurationContents()
116: throws MojoExecutionException, IOException {
117: String contents = null;
118: if (this .configurationUri != null
119: && this .configurationUri.trim().length() > 0) {
120: final URL configuration = ResourceUtils
121: .toURL(this .configurationUri);
122: if (configuration == null) {
123: throw new MojoExecutionException(
124: "No configuration could be loaded from --> '"
125: + this .configurationUri + "'");
126: }
127: contents = this .replaceProperties(ResourceUtils
128: .getContents(configuration));
129: }
130: return contents;
131: }
132:
133: /**
134: * The begin token for interpolation (we need it different
135: * than what Maven uses so that Maven variables aren't replace).
136: */
137: private static final String BEGIN_TOKEN = "$${";
138:
139: /**
140: * The end token for interpolation.
141: */
142: private static final String END_TOKEN = "}";
143:
144: /**
145: * Replaces all properties having the style
146: * <code>${some.property}</code> with the value
147: * of the specified property if there is one.
148: *
149: * @param properties the properties used to perform the replacement.
150: * @param fileContents the fileContents to perform replacement on.
151: */
152: private String replaceProperties(final Properties properties,
153: final String string) throws IOException {
154: final StringReader stringReader = new StringReader(string);
155: InterpolationFilterReader reader = new InterpolationFilterReader(
156: stringReader, properties, "${", "}");
157: reader.reset();
158: reader = new InterpolationFilterReader(reader,
159: new BeanProperties(this .project), BEGIN_TOKEN,
160: END_TOKEN);
161: reader = new InterpolationFilterReader(reader,
162: new BeanProperties(this.project), BEGIN_TOKEN,
163: END_TOKEN);
164: return ResourceUtils.getContents(reader);
165: }
166:
167: }
|