/*
 *  Copyright 2006 The Apache Software Foundation
 *
 *  Licensed 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.
 */

package org.apache.geronimo.mavenplugins.server;

import org.apache.maven.project.MavenProject;
import org.apache.maven.artifact.resolver.ArtifactResolver;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.artifact.factory.ArtifactFactory;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.net.ConnectException;

import org.apache.geronimo.genesis.AntMojoSupport;
import org.apache.geronimo.plugin.ArtifactItem;
import org.apache.commons.lang.SystemUtils;
import org.apache.tools.ant.taskdefs.ExecTask;

/**
 * Start the Selenium server.
 *
 * @goal start
 *
 * @version $Id$
 */
public class StartServerMojo
    extends AntMojoSupport
{
    /**
     * The port number the server will use.
     *
     * @parameter default-value="4444"
     */
    private int port;
    
    /**
     * Timeout for the server in seconds.
     *
     * @parameter default-value="-1"
     */
    private int timeout;
    
    
    /**
     * Enable the server's debug mode..
     *
     * @parameter default-value="false"
     */
    private boolean debug;
    
    //
    // MojoSupport Hooks
    //

    /**
     * The maven project.
     *
     * @parameter expression="${project}"
     * @required
     * @readonly
     */
    private MavenProject project = null;


    protected MavenProject getProject() {
        return project;
    }

    /**
     * ???
     *
     * @component
     * @required
     * @readonly
     */
    private ArtifactFactory artifactFactory = null;

    protected ArtifactFactory getArtifactFactory() {
        return artifactFactory;
    }

    /**
     * ???
     *
     * @component
     * @required
     * @readonly
     */
    private ArtifactResolver artifactResolver = null;

    protected ArtifactResolver getArtifactResolver() {
        return artifactResolver;
    }

    /**
     * ???
     *
     * @parameter expression="${localRepository}"
     * @readonly
     * @required
     */
    private ArtifactRepository artifactRepository = null;

    protected ArtifactRepository getArtifactRepository() {
        return artifactRepository;
    }

    //
    // Mojo
    //

    protected void doExecute() throws Exception {
        log.info("Starting server...");
        
        //
        // HACK: Need a better way to find this jar
        //
        
        ArtifactItem item = new ArtifactItem();
        item.setGroupId("org.openqa.selenium.server");
        item.setArtifactId("selenium-server");
        item.setVersion("0.8.1");
        
        final Artifact artifact = getArtifact(item);

        final String executable = "java" +  (SystemUtils.IS_OS_WINDOWS ? ".exe" : "");

        // Start the server int a seperate thread
        Thread t = new Thread("Server Runner") {
            public void run() {
                try {
                    ExecTask exec = (ExecTask)createTask("exec");
                    exec.setExecutable(executable);
                    exec.createArg().setValue("-jar");
                    exec.createArg().setFile(artifact.getFile());
                    exec.createArg().setValue("-port");
                    exec.createArg().setValue(String.valueOf(port));
                    if (debug) {
                        exec.createArg().setValue("-debug");
                    }
                    if (timeout > 0) {
                        exec.createArg().setValue("-timeout");
                        exec.createArg().setValue(String.valueOf(timeout));
                    }
                    
                    exec.setLogError(true);
                    exec.execute();

                    synchronized(this) {
                        wait();
                    }
                }
                catch (Exception e) {
                    // ignore
                }
            }
        };
        t.start();

        log.info("Server started");
    }
}

@@ -963,91 +963,8 @@
 ile;
-%0Aimport java.io.IOException;%0Aimport java.net.URL;%0Aimport java.net.ConnectException;
 %0A%0Aim
@@ -1104,16 +1104,17 @@
 mUtils;%0A
+%0A
 import o
@@ -1688,24 +1688,170 @@
 debug;%0A    %0A
+    /**%0A     * Location of the user-extentions.js to load into the server.%0A     *%0A     * @parameter%0A     */%0A    private File userExtensions;%0A    %0A
     //%0A    /
@@ -3921,24 +3921,45 @@
 eOf(port));%0A
+                    %0A
             
@@ -3975,24 +3975,24 @@
 f (debug) %7B%0A
-
             
@@ -4054,32 +4054,53 @@
               %7D%0A
+                    %0A
                 
@@ -4181,24 +4181,24 @@
 -timeout%22);%0A
-
             
@@ -4296,32 +4296,261 @@
                %0A
+                    if (userExtensions != null) %7B%0A                        exec.createArg().setValue(%22-userExtensions%22);%0A                        exec.createArg().setFile(userExtensions);%0A                    %7D%0A                    %0A
                 

Support -userExtensions