package org.codehaus.mojo.gwt.webxml;

/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

import static org.apache.maven.artifact.Artifact.SCOPE_COMPILE;

import java.io.File;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;

import org.apache.commons.io.FileUtils;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.codehaus.classworlds.ClassRealm;
import org.codehaus.classworlds.ClassWorld;
import org.codehaus.mojo.gwt.GwtModule;
import org.codehaus.mojo.gwt.shell.AbstractGwtWebMojo;

/**
 * Merges GWT servlet elements into deployment descriptor (and non GWT servlets into shell).
 *
 * @goal mergewebxml
 * @phase process-resources
 * @requiresDependencyResolution compile
 * @description Merges GWT servlet elements into deployment descriptor (and non GWT servlets into shell).
 * @author cooper
 * @version $Id$
 */
public class MergeWebXmlMojo
    extends AbstractGwtWebMojo
{

    /**
     * Location on filesystem where merged web.xml will be created. The maven-war-plugin must be configured to use this
     * path as <a href="http://maven.apache.org/plugins/maven-war-plugin/war-mojo.html#webXml"> webXml</a> parameter
     * 
     * @parameter default-value="${project.build.directory}/web.xml"
     */
    private File mergedWebXml;

    /** Creates a new instance of MergeWebXmlMojo */
    public MergeWebXmlMojo()
    {
        super();
    }

    public void doExecute( )
        throws MojoExecutionException, MojoFailureException
    {

        try
        {
            if ( !mergedWebXml.exists() )
            {
                mergedWebXml.getParentFile().mkdirs();
                mergedWebXml.createNewFile();
            }

            FileUtils.copyFile( getWebXml(), mergedWebXml );

            Set<ServletDescriptor> servlets = new LinkedHashSet<ServletDescriptor>();
            for ( String module : getModules() )
            {
                GwtModule gwtModule = readModule( module );
                Map<String, String> moduleServlets = gwtModule.getServlets();
                getLog().debug( "merge " + moduleServlets.size() + " servlets from module " + module );
                for ( Map.Entry<String, String> servlet : moduleServlets.entrySet() )
                {
                    servlets.add( new ServletDescriptor( servlet.getKey(), servlet.getValue() ) );
                }
            }
            new GwtWebInfProcessor().process( mergedWebXml, mergedWebXml, servlets );
            getLog().info( servlets.size() + " servlet(s) merged into " + mergedWebXml );
        }
        catch ( Exception e )
        {
            throw new MojoExecutionException( "Unable to merge web.xml", e );
        }
    }

    /**
     * Helper hack for classpath problems, used as a fallback.
     *
     * @param runtime TODO
     *
     * @return
     */
    protected ClassLoader fixThreadClasspath()
    {
        try
        {
            ClassWorld world = new ClassWorld();

            // use the existing ContextClassLoader in a realm of the classloading space
            ClassRealm root = world.newRealm( "gwt-plugin", Thread.currentThread().getContextClassLoader() );
            ClassRealm realm = root.createChildRealm( "gwt-project" );

            Collection<File> classpath = getClasspath( SCOPE_COMPILE );
            for ( Iterator<File> it = classpath.iterator(); it.hasNext(); )
            {
                realm.addConstituent( it.next().toURI().toURL() );
            }

            Thread.currentThread().setContextClassLoader( realm.getClassLoader() );
            // /System.out.println("AbstractGwtMojo realm classloader = " + realm.getClassLoader().toString());

            return realm.getClassLoader();
        }
        catch ( Exception e )
        {
            e.printStackTrace();
            throw new RuntimeException( e );
        }
    }

}

@@ -852,141 +852,20 @@
 ort 
-static org.apache.maven.artifact.Artifact.SCOPE_COMPILE;%0A%0Aimport java.io.File;%0Aimport java.util.Collection;%0Aimport java.util.Iterator
+java.io.File
 ;%0Aim
@@ -1091,96 +1091,8 @@
 on;%0A
-import org.codehaus.classworlds.ClassRealm;%0Aimport org.codehaus.classworlds.ClassWorld;%0A
 impo
@@ -3357,1166 +3357,6 @@
   %7D%0A
-%0A    /**%0A     * Helper hack for classpath problems, used as a fallback.%0A     *%0A     * @param runtime TODO%0A     *%0A     * @return%0A     */%0A    protected ClassLoader fixThreadClasspath()%0A    %7B%0A        try%0A        %7B%0A            ClassWorld world = new ClassWorld();%0A%0A            // use the existing ContextClassLoader in a realm of the classloading space%0A            ClassRealm root = world.newRealm( %22gwt-plugin%22, Thread.currentThread().getContextClassLoader() );%0A            ClassRealm realm = root.createChildRealm( %22gwt-project%22 );%0A%0A            Collection%3CFile%3E classpath = getClasspath( SCOPE_COMPILE );%0A            for ( Iterator%3CFile%3E it = classpath.iterator(); it.hasNext(); )%0A            %7B%0A                realm.addConstituent( it.next().toURI().toURL() );%0A            %7D%0A%0A            Thread.currentThread().setContextClassLoader( realm.getClassLoader() );%0A            // /System.out.println(%22AbstractGwtMojo realm classloader = %22 + realm.getClassLoader().toString());%0A%0A            return realm.getClassLoader();%0A        %7D%0A        catch ( Exception e )%0A        %7B%0A            e.printStackTrace();%0A            throw new RuntimeException( e );%0A        %7D%0A    %7D%0A%0A
 %7D%0A

remove unused code.