View Javadoc
1   package org.andromda.ant.task;
2   
3   import java.io.FileNotFoundException;
4   import java.net.URL;
5   import org.andromda.core.AndroMDAServer;
6   import org.andromda.core.configuration.Configuration;
7   import org.apache.commons.lang.exception.ExceptionUtils;
8   import org.apache.tools.ant.BuildException;
9   import org.apache.tools.ant.taskdefs.MatchingTask;
10  
11  /**
12   * <p>
13   * This class wraps the AndroMDA model processor so that AndroMDA Server can be
14   * used as an Ant task. Represents the <code>&lt;andromda&gt;</code> custom
15   * task which can be called from an Ant build script.
16   * </p>
17   *
18   * @author Lofi Dewanto
19   */
20  public class AndroMDAServerStopTask
21      extends MatchingTask
22  {
23      /**
24       * Initialize the context class loader.
25       */
26      static
27      {
28          initializeContextClassLoader();
29      }
30  
31      /**
32       * Stores the configuration URI.
33       */
34      private URL configurationUri;
35  
36      /**
37       * Sets the URI to the configuration file.
38       *
39       * @param configurationUri
40       */
41      public void setConfigurationUri(final URL configurationUri)
42      {
43          this.configurationUri = configurationUri;
44      }
45  
46      /**
47       * <p>
48       * Stops the AndroMDA server.
49       * </p>
50       * <p>
51       * This is the main entry point of the application when running Ant. It is
52       * called by ant whenever the surrounding task is executed (which could be
53       * multiple times).
54       * </p>
55       *
56       * @throws BuildException if something goes wrong
57       */
58      public void execute()
59          throws BuildException
60      {
61          // initialize before the execute as well in case we
62          // want to execute more than once
63          initializeContextClassLoader();
64          try
65          {
66              if (this.configurationUri == null)
67              {
68                  throw new BuildException("Configuration is not a valid URI --> '" + this.configurationUri + '\'');
69              }
70  
71              // Create the configuration file
72              final Configuration configuration = Configuration.getInstance(this.configurationUri);
73  
74              final AndroMDAServer andromdaServer = AndroMDAServer.newInstance();
75              if (andromdaServer != null)
76              {
77                  andromdaServer.stop(configuration);
78              }
79          }
80          catch (Throwable throwable)
81          {
82              final Throwable cause = ExceptionUtils.getCause(throwable);
83              if (cause != null)
84              {
85                  throwable = cause;
86              }
87              if (throwable instanceof FileNotFoundException)
88              {
89                  throw new BuildException("No configuration could be loaded from --> '" + configurationUri + '\'');
90              }
91              throw new BuildException(throwable);
92          }
93          finally
94          {
95              // Set the context class loader back ot its system class loaders
96              // so that any processes running after won't be trying to use
97              // the ContextClassLoader for this class.
98              Thread.currentThread().setContextClassLoader(ClassLoader.getSystemClassLoader());
99          }
100     }
101 
102     /**
103      * Set the context class loader so that any classes using it (the
104      * contextContextClassLoader) have access to the correct loader.
105      */
106     private static void initializeContextClassLoader()
107     {
108         Thread.currentThread().setContextClassLoader(AndroMDAServerStopTask.class.getClassLoader());
109     }
110 }