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 AndroMDAServerStopTask 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: * Stops 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 if something goes wrong
055: */
056: public void execute() throws BuildException {
057: // initialize before the execute as well in case we
058: // want to execute more than once
059: initializeContextClassLoader();
060: try {
061: if (this .configurationUri == null) {
062: throw new BuildException(
063: "Configuration is not a valid URI --> '"
064: + this .configurationUri + "'");
065: }
066:
067: // Create the configuration file
068: final Configuration configuration = Configuration
069: .getInstance(this .configurationUri);
070:
071: final AndroMDAServer andromdaServer = AndroMDAServer
072: .newInstance();
073: if (andromdaServer != null) {
074: andromdaServer.stop(configuration);
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: }
086: throw new BuildException(throwable);
087: } finally {
088: // Set the context class loader back ot its system class loaders
089: // so that any processes running after won't be trying to use
090: // the ContextClassLoader for this class.
091: Thread.currentThread().setContextClassLoader(
092: ClassLoader.getSystemClassLoader());
093: }
094: }
095:
096: /**
097: * Set the context class loader so that any classes using it (the
098: * contextContextClassLoader) have access to the correct loader.
099: */
100: private final static void initializeContextClassLoader() {
101: Thread.currentThread().setContextClassLoader(
102: AndroMDAServerStopTask.class.getClassLoader());
103: }
104: }
|