/*
 * JBoss, Home of Professional Open Source.
 * Copyright 2010, 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.torquebox.web;

import org.apache.catalina.Host;
import org.apache.catalina.Valve;
import org.apache.catalina.core.StandardHost;
import org.apache.catalina.valves.AccessLogValve;
import org.apache.catalina.valves.ExtendedAccessLogValve;
import org.jboss.as.web.WebServer;
import org.jboss.dmr.ModelNode;
import org.jboss.msc.service.Service;
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.jboss.web.rewrite.RewriteValve;


/**
 * Service creating and registering a virtual host.
 *
 * @author Emanuel Muckenhuber
 */
public class VirtualHostService implements Service<Host> {

    private final String name;
    private final String[] aliases;
    private String defaultWebModule;
    private ModelNode accessLog;
    private ModelNode rewrite;

    private final InjectedValue<String> tempPathInjector = new InjectedValue<String>();
    private final InjectedValue<String> accessLogPathInjector = new InjectedValue<String>();
    private final InjectedValue<WebServer> webServer = new InjectedValue<WebServer>();

    private Host host;

    public VirtualHostService(String name, String[] aliases) {
        this.name = name;
        this.aliases = aliases;
    }

    /** {@inheritDoc} */
    public synchronized void start(StartContext context) throws StartException {
        final StandardHost host = new StandardHost();
        host.setAppBase(tempPathInjector.getValue());
        host.setName(name);
        for(final String alias : aliases) {
            host.addAlias(alias);
        }
        if (defaultWebModule != null) {
            host.setDefaultWebapp(defaultWebModule);
        }
        try {
            final WebServer server = webServer.getValue();
            server.addHost(host);
        } catch(Exception e) {
            throw new StartException(e);
        }
        this.host = host;
    }

    /** {@inheritDoc} */
    public synchronized void stop(StopContext context) {
        final Host host = this.host;
        this.host = null;
        final WebServer server = webServer.getValue();
        server.removeHost(host);
    }

    /** {@inheritDoc} */
    public synchronized Host getValue() throws IllegalStateException {
        final Host host = this.host;
        if(host == null) {
            throw new IllegalStateException();
        }
        return host;
    }

    protected String getDefaultWebModule() {
        return defaultWebModule;
    }

    protected void setDefaultWebModule(String defaultWebModule) {
        this.defaultWebModule = defaultWebModule;
    }

    InjectedValue<String> getAccessLogPathInjector() {
        return accessLogPathInjector;
    }

    InjectedValue<String> getTempPathInjector() {
        return tempPathInjector;
    }

    InjectedValue<WebServer> getWebServer() {
        return webServer;
    }

}

@@ -1076,200 +1076,62 @@
 ina.
-Host;%0Aimport org.apache.catalina.Valve;%0Aimport org.apache.catalina.core.StandardHost;%0Aimport org.apache.catalina.valves.AccessLogValve;%0Aimport org.apache.catalina.valves.ExtendedAccessLogValve
+core.StandardHost;%0Aimport org.jboss.as.web.VirtualHost
 ;%0Aim
@@ -1167,40 +1167,8 @@
 er;%0A
-import org.jboss.dmr.ModelNode;%0A
 impo
@@ -1377,51 +1377,8 @@
 ue;%0A
-import org.jboss.web.rewrite.RewriteValve;%0A
 %0A%0A/*
@@ -1520,16 +1520,23 @@
 Service%3C
+Virtual
 Host%3E %7B%0A
@@ -1643,72 +1643,8 @@
 ule;
-%0A    private ModelNode accessLog;%0A    private ModelNode rewrite;
 %0A%0A  
@@ -1922,16 +1922,23 @@
 private 
+Virtual
 Host hos
@@ -2711,20 +2711,46 @@
 .host = 
-host
+new VirtualHost( host, false )
 ;%0A    %7D%0A
@@ -2838,32 +2838,39 @@
 %7B%0A        final 
+Virtual
 Host host = this
@@ -2983,24 +2983,34 @@
 oveHost(host
+.getHost()
 );%0A    %7D%0A%0A  
@@ -3056,16 +3056,23 @@
 ronized 
+Virtual
 Host get
@@ -3124,16 +3124,23 @@
   final 
+Virtual
 Host hos

Fix VirtualHost management for AS7-SNAPSHOT.