001: package org.andromda.ant.task;
002:
003: import java.io.FileNotFoundException;
004:
005: import java.net.URL;
006:
007: import org.andromda.core.AndroMDAServer;
008: import org.andromda.core.configuration.Configuration;
009: import org.apache.commons.lang.exception.ExceptionUtils;
010: import org.apache.tools.ant.BuildException;
011: import org.apache.tools.ant.taskdefs.MatchingTask;
012:
013: /**
014: * <p>
015: * This class wraps the AndroMDA model processor so that AndroMDA Server can be
016: * used as an Ant task. Represents the <code><andromda></code> custom
017: * task which can be called from an Ant build script.
018: * </p>
019: *
020: * @author Lofi Dewanto
021: */
022: public class AndroMDAServerStartTask extends MatchingTask {
023: /**
024: * Initialize the context class loader.
025: */
026: static {
027: initializeContextClassLoader();
028: }
029:
030: /**
031: * Stores the configuration URI.
032: */
033: private URL configurationUri;
034:
035: /**
036: * Sets the URI to the configuration file.
037: *
038: * @param configurationUri
039: */
040: public void setConfigurationUri(final URL configurationUri) {
041: this .configurationUri = configurationUri;
042: }
043:
044: /**
045: * <p>
046: * Starts the AndroMDA server.
047: * </p>
048: * <p>
049: * This is the main entry point of the application when running Ant. It is
050: * called by ant whenever the surrounding task is executed (which could be
051: * multiple times).
052: * </p>
053: *
054: * @throws BuildException
055: * if something goes wrong
056: */
057: public void execute() throws BuildException {
058: // Initialize before the execute as well in case we
059: // want to execute more than once
060: initializeContextClassLoader();
061: try {
062: if (this .configurationUri == null) {
063: throw new BuildException(
064: "Configuration is not a valid URI --> '"
065: + this .configurationUri + "'");
066: }
067:
068: // Create the configuration file from URI
069: final Configuration configuration = Configuration
070: .getInstance(this .configurationUri);
071:
072: // Create and start the server
073: final AndroMDAServer andromdaServer = AndroMDAServer
074: .newInstance();
075: if (andromdaServer != null) {
076: andromdaServer.start(configuration);
077: }
078: } catch (Throwable throwable) {
079: final Throwable cause = ExceptionUtils.getCause(throwable);
080: if (cause != null) {
081: throwable = cause;
082: }
083: if (throwable instanceof FileNotFoundException) {
084: throw new BuildException(
085: "No configuration could be loaded from --> '"
086: + configurationUri + "'");
087: }
088: throw new BuildException(throwable);
089: } finally {
090: // Set the context class loader back ot its system class loaders
091: // so that any processes running after won't be trying to use
092: // the ContextClassLoader for this class.
093: Thread.currentThread().setContextClassLoader(
094: ClassLoader.getSystemClassLoader());
095: }
096: }
097:
098: /**
099: * Set the context class loader so that any classes using it (the
100: * contextContextClassLoader) have access to the correct loader.
101: */
102: private final static void initializeContextClassLoader() {
103: Thread.currentThread().setContextClassLoader(
104: AndroMDAServerStartTask.class.getClassLoader());
105: }
106: }
|