/*
 * JBoss, Home of Professional Open Source.
 * Copyright 2013, Red Hat, Inc., and individual contributors
 * as indicated by the @author tags. See the copyright.txt file in the
 * distribution for a full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */

package org.wildfly.extension.undertow.deployment;

import java.io.File;

import io.undertow.server.HttpHandler;
import io.undertow.servlet.api.Deployment;
import io.undertow.servlet.api.DeploymentInfo;
import io.undertow.servlet.api.DeploymentManager;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;

import org.jboss.as.web.common.StartupContext;
import org.jboss.as.web.common.WebInjectionContainer;
import org.jboss.as.web.host.ContextActivator;
import org.jboss.msc.service.Service;
import org.jboss.msc.service.ServiceController;
import org.jboss.msc.service.StartContext;
import org.jboss.msc.service.StartException;
import org.jboss.msc.service.StopContext;
import org.jboss.msc.value.InjectedValue;
import org.wildfly.extension.undertow.Host;
import org.wildfly.extension.undertow.ServletContainerService;
import org.wildfly.extension.undertow.UndertowMessages;

/**
 * @author Stuart Douglas
 */
public class UndertowDeploymentService implements Service<UndertowDeploymentService> {

    private final InjectedValue<ServletContainerService> container = new InjectedValue<>();
    private final WebInjectionContainer webInjectionContainer;
    private final InjectedValue<Host> host = new InjectedValue<>();
    private final InjectedValue<DeploymentInfo> deploymentInfoInjectedValue = new InjectedValue<>();
    private final boolean autostart;

    private volatile DeploymentManager deploymentManager;

    public UndertowDeploymentService(final WebInjectionContainer webInjectionContainer, boolean autostart) {
        this.webInjectionContainer = webInjectionContainer;
        this.autostart = autostart;
    }

    @Override
    public void start(final StartContext startContext) throws StartException {
        if (autostart) {
            try {
                startContext();
            } catch (ServletException e) {
                throw new StartException(e);
            }
        }
    }

    public void startContext() throws ServletException {
        DeploymentInfo deploymentInfo = deploymentInfoInjectedValue.getValue();
        StartupContext.setInjectionContainer(webInjectionContainer);
        try {
            deploymentManager = container.getValue().getServletContainer().addDeployment(deploymentInfo);
            deploymentManager.deploy();
            HttpHandler handler = deploymentManager.start();
            Deployment deployment = deploymentManager.getDeployment();
            host.getValue().registerDeployment(deployment, handler);
        } finally {
            StartupContext.setInjectionContainer(null);
        }
    }

    @Override
    public void stop(final StopContext stopContext) {
        stopContext();
    }

    public void stopContext() {
        if (deploymentManager != null) {
            Deployment deployment = deploymentManager.getDeployment();
            try {
                deploymentManager.stop();
            } catch (ServletException e) {
                throw new RuntimeException(e);
            }
            deploymentManager.undeploy();
            host.getValue().unregisterDeployment(deployment);
        }
        recursiveDelete(deploymentInfoInjectedValue.getValue().getTempDir());
    }

    @Override
    public UndertowDeploymentService getValue() throws IllegalStateException, IllegalArgumentException {
        return this;
    }

    public InjectedValue<ServletContainerService> getContainer() {
        return container;
    }

    public InjectedValue<Host> getHost() {
        return host;
    }

    public InjectedValue<DeploymentInfo> getDeploymentInfoInjectedValue() {
        return deploymentInfoInjectedValue;
    }


    /**
     * Provides an API to start/stop the {@link UndertowDeploymentService}.
     * This should register/deregister the web context.
     */
    protected static class ContextActivatorImpl implements ContextActivator {

        private final ServiceController<UndertowDeploymentService> controller;

        ContextActivatorImpl(ServiceController<UndertowDeploymentService> controller) {
            this.controller = controller;
        }

        /**
         * Start the web context synchronously.
         * <p/>
         * This would happen when the OSGi webapp gets explicitly started.
         */
        @Override
        public synchronized boolean startContext() {
            try {
                UndertowDeploymentService service = controller.getValue();
                service.startContext();
            } catch (Exception ex) {
                throw UndertowMessages.MESSAGES.cannotActivateContext(ex, controller.getName());
            }
            return true;
        }

        /**
         * Stop the web context synchronously.
         * <p/>
         * This would happen when the OSGi webapp gets explicitly stops.
         */
        @Override
        public synchronized boolean stopContext() {
            UndertowDeploymentService service = controller.getValue();
            service.stopContext();
            return true;
        }

        @Override
        public ServletContext getServletContext() {
            //todo UndertowDeploymentService should be fully started before this method is called
            UndertowDeploymentService service = controller.getValue();
            DeploymentManager manager = service.deploymentManager;
            Deployment deployment = manager != null ? manager.getDeployment() : null;
            return deployment != null ? deployment.getServletContext() : null;
        }
    }

    private static void recursiveDelete(File file) {
        if(file == null) {
            return;
        }
        for(File f : file.listFiles()) {
            recursiveDelete(f);
        }
        if(!file.delete()) {
            throw UndertowMessages.MESSAGES.couldNotDeleteTempFile(file);
        }
    }
}

@@ -6652,20 +6652,22 @@
     
-for(File f :
+File%5B%5D files =
  fil
@@ -6679,16 +6679,102 @@
 tFiles()
+;%0A        if (files == null)%7B%0A            return;%0A        %7D%0A        for(File f : files
 ) %7B%0A    

Fix potential NPE on undeploy