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 AndroMDAServerStartTask
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       * Starts 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
57       *             if something goes wrong
58       */
59      public void execute()
60          throws BuildException
61      {
62          // Initialize before the execute as well in case we
63          // want to execute more than once
64          initializeContextClassLoader();
65          try
66          {
67              if (this.configurationUri == null)
68              {
69                  throw new BuildException("Configuration is not a valid URI --> '" + this.configurationUri + '\'');
70              }
71  
72              // Create the configuration file from URI
73              final Configuration configuration = Configuration.getInstance(this.configurationUri);
74  
75              // Create and start the server
76              final AndroMDAServer andromdaServer = AndroMDAServer.newInstance();
77              if (andromdaServer != null)
78              {
79                  andromdaServer.start(configuration);
80              }
81          }
82          catch (Throwable throwable)
83          {
84              final Throwable cause = ExceptionUtils.getCause(throwable);
85              if (cause != null)
86              {
87                  throwable = cause;
88              }
89              if (throwable instanceof FileNotFoundException)
90              {
91                  throw new BuildException("No configuration could be loaded from --> '" + configurationUri + '\'');
92              }
93              throw new BuildException(throwable);
94          }
95          finally
96          {
97              // Set the context class loader back ot its system class loaders
98              // so that any processes running after won't be trying to use
99              // the ContextClassLoader for this class.
100             Thread.currentThread().setContextClassLoader(ClassLoader.getSystemClassLoader());
101         }
102     }
103 
104     /**
105      * Set the context class loader so that any classes using it (the
106      * contextContextClassLoader) have access to the correct loader.
107      */
108     private static void initializeContextClassLoader()
109     {
110         Thread.currentThread().setContextClassLoader(AndroMDAServerStartTask.class.getClassLoader());
111     }
112 }