001: package org.andromda.maven;
002:
003: import java.io.FileNotFoundException;
004:
005: import java.net.MalformedURLException;
006: import java.net.URL;
007:
008: import java.util.Iterator;
009: import java.util.Map;
010:
011: import org.andromda.core.AndroMDA;
012: import org.andromda.core.common.ResourceUtils;
013: import org.andromda.core.configuration.Configuration;
014: import org.apache.commons.lang.StringUtils;
015: import org.apache.commons.lang.exception.ExceptionUtils;
016: import org.apache.tools.ant.BuildException;
017: import org.apache.tools.ant.taskdefs.MatchingTask;
018:
019: /**
020: * This task is used with the AndroMDA Maven plugin.
021: *
022: * @author Chad Brandon
023: * @see org.andromda.core.engine.ModelProcessor
024: */
025: public class AndroMDAMavenRunner extends MatchingTask {
026: static {
027: Thread.currentThread().setContextClassLoader(
028: AndroMDAMavenRunner.class.getClassLoader());
029: }
030:
031: /**
032: * The URI to the configuration;
033: */
034: private String configurationUri;
035:
036: /**
037: * Sets the URI to the configuration file.
038: *
039: * @param configurationUri
040: */
041: public void setConfigurationUri(final String configurationUri) {
042: this .configurationUri = configurationUri;
043: }
044:
045: /**
046: * Stores the search location for mapping files.
047: */
048: private String mappingsSearchLocation;
049:
050: /**
051: * Sets the mappings search location.
052: *
053: * @param MappingsSearchLocation
054: */
055: public void setMappingsSearchLocation(
056: final String mappingsSearchLocation) {
057: this .mappingsSearchLocation = mappingsSearchLocation;
058: }
059:
060: /**
061: * Runs AndroMDA.
062: */
063: public void execute() throws BuildException {
064: try {
065: final URL uri = new URL(configurationUri);
066: final Configuration configuration = Configuration
067: .getInstance(this .replaceProperties(ResourceUtils
068: .getContents(uri)));
069: configuration
070: .addMappingsSearchLocation(this .mappingsSearchLocation);
071: final AndroMDA andromda = AndroMDA.newInstance();
072: if (andromda != null) {
073: andromda.run(configuration);
074: andromda.shutdown();
075: }
076: } catch (Throwable throwable) {
077: final Throwable cause = ExceptionUtils.getCause(throwable);
078: if (cause != null) {
079: throwable = cause;
080: }
081: if (throwable instanceof FileNotFoundException) {
082: throw new BuildException(
083: "No configuration could be loaded from --> '"
084: + configurationUri + "'");
085: } else if (throwable instanceof MalformedURLException) {
086: throw new BuildException(
087: "Configuration is not a valid URI --> '"
088: + configurationUri + "'");
089: }
090: throw new BuildException(throwable);
091: } finally {
092: // Set the context class loader back ot its system class loaders
093: // so that any processes running after won't be trying to use
094: // the ContextClassLoader for this class.
095: Thread.currentThread().setContextClassLoader(
096: ClassLoader.getSystemClassLoader());
097: }
098: }
099:
100: /**
101: * Replaces all properties having the style
102: * <code>${some.property}</code> with the value
103: * of the specified property if there is one.
104: *
105: * @param fileContents the fileContents to perform replacement on.
106: */
107: protected String replaceProperties(String string) {
108: final Map properties = this .getProject().getProperties();
109: if (properties != null && !properties.isEmpty()) {
110: for (final Iterator iterator = properties.keySet()
111: .iterator(); iterator.hasNext();) {
112: final String name = (String) iterator.next();
113: final String property = "${" + name + "}";
114: final String value = (String) properties.get(name);
115: string = StringUtils.replace(string, property, value);
116: }
117: }
118:
119: // remove any left over property references
120: string = AndroMDAMavenUtils.removePropertyReferences(string);
121: return string;
122: }
123: }
|