001package org.andromda.maven.plugin.andromdapp;
002
003import java.net.URL;
004import java.net.URLClassLoader;
005import org.andromda.andromdapp.AndroMDApp;
006import org.apache.maven.plugin.MojoExecutionException;
007
008/**
009 * Removes the an AndroMDApp generated application structure.
010 *
011 * @author Chad Brandon
012 * @goal clean-structure
013 * @requiresProject false
014 */
015public class AndroMDAppCleanMojo
016    extends AbstractAndroMDAppMojo
017{
018    /**
019     * @see org.apache.maven.plugin.Mojo#execute()
020     */
021    public void execute()
022        throws MojoExecutionException
023    {
024        if (this.skipProcessing)
025        {
026            getLog().info("andromdapp:clean skipProcessing");
027            return;
028        }
029        try
030        {
031            final AndroMDApp andromdapp = new AndroMDApp();
032            final String configuration = this.getConfigurationContents();
033            if (configuration != null)
034            {
035                andromdapp.addConfiguration(this.getConfigurationContents());
036            }
037            andromdapp.clean();
038        }
039        catch (final NoClassDefFoundError ncdfe)
040        {
041            ClassLoader cl = this.getClass().getClassLoader();
042            // Its a RealmClassLoader but its a private class and so can't reference grrr !!!
043            // but it extends URLClassLoader
044            System.err.println( "classloader:" + cl );
045            if ( cl instanceof URLClassLoader ) {
046                URLClassLoader ucl = (URLClassLoader) cl;
047                URL [] urls = ucl.getURLs();
048                for (URL url : urls) {
049                    System.err.println("cp:" + url);
050                }
051                try
052                {
053                    ucl.close();
054                }
055                catch (Exception ex)
056                {
057                    // Ignore
058                }
059            }
060            System.out.println( "Exception:" + ncdfe );
061            ncdfe.printStackTrace(System.err);
062            Throwable th = ncdfe.getCause();
063            while ( th != null ) {
064                System.err.println( "================= cause =================");
065                th.printStackTrace(System.err);
066                th = th.getCause();
067            }
068        }
069        catch (final Throwable throwable)
070        {
071            if (throwable instanceof MojoExecutionException)
072            {
073                throw (MojoExecutionException)throwable;
074            }
075            throw new MojoExecutionException("An error occurred during application cleanup operation", throwable);
076        }
077    }
078}