<commit_before>/*
 *  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.
 */

package com.google.code.play

import org.apache.maven.project.MavenProject

import org.codehaus.gmaven.mojo.support.ProcessLauncher

/**
 * Start Play! server.
 * Based on <a href="http://mojo.codehaus.org/selenium-maven-plugin/start-server-mojo.html">selenium:start-server mojo</a>
 *
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 * @goal start-server
 * @requiresDependencyResolution test
 */
class StartServerMojo
    extends AbstractPlayGroovyMojo
{
    /**
     * ...
     *
     * @parameter expression="${play.home}"
     * @since 1.0.0
     */
    String playHome

    /**
     * ...
     *
     * @parameter expression="${play.testId}" default-value="test"
     * @since 1.0.0
     */
    String playTestId

    /**
     * The port number of the server to connect to.
     *
     * @parameter expression="${play.serverPort}" default-value="9000"
     * @required
     * @since 1.0.0
     */
    int serverPort
    
    /**
     * Enable logging mode.
     *
     * @parameter expression="${play.serverLogOutput}" default-value="true"
     * @since 1.0.0
     */
    boolean serverLogOutput

    /**
     * Allows the server startup to be skipped.
     *
     * @parameter expression="${play.seleniumSkip}" default-value="false"
     * @since 1.0.0
     */
    boolean seleniumSkip
    
    /**
     * Arbitrary JVM options to set on the command line.
     *
     * @parameter expression="${play.serverProcessArgLine}"
     * @since 1.0.0
     */
    private String serverProcessArgLine;
    
    //
    // Components
    //
    
    /**
     * The enclosing project.
     * 
     * @parameter expression="${project}"
     * @required
     * @readonly
     */
    MavenProject project;

    /**
     * List of artifacts this plugin depends on. Used for resolving the Findbugs coreplugin.
     *
     * @parameter expression="${plugin.artifacts}"
     * @required
     * @readonly
     */
    ArrayList pluginArtifacts

     /**
     * @parameter expression="${plugin.artifactMap}"
     * @required
     * @readonly
     */
    Map pluginArtifactMap

    //
    // Mojo
    //

    void execute() {
        if ( !"play".equals( project.packaging ) )
        {
            return;
        }

        if (seleniumSkip) {
            log.info('Skipping startup')
            return
        }
        
        playHome = checkPlayHome(playHome);
        
        def applicationPath = project.basedir;
        
        File buildDirectory = new File( project.build.directory );
        File workingDirectory = new File( buildDirectory, "play" );
        ant.mkdir(dir: workingDirectory)
        
        //if (serverLogOutput) {
        //    ant.mkdir(dir: logFile.parentFile)
        //}
        
        def pluginArifact = { id ->
            def artifact = pluginArtifactMap[id]
            if (!artifact) {
                fail("Unable to locate '$id' in the list of plugin artifacts")
            }
            
            log.debug("Using plugin artifact: ${artifact.file}")
            
            return artifact.file
        }

//System.out.println("playHome:"+playHome);
//System.out.println("applicationPath:"+applicationPath);
//System.out.println("playId:"+playTestId);
        
        def launcher = new ProcessLauncher(name: 'Play! Server', background: true)
        
        launcher.process = {
            ant.java(classname: 'com.google.code.play.PlayServerBooter',
                     fork: true,
                     dir: workingDirectory,
                     failonerror: true)
            {
                classpath() {
                    def projectClasspathElements = project.testClasspathElements
                    def projectClasspathList = projectClasspathElements.findAll{project.build.outputDirectory != it.toString()}

                    projectClasspathList.each() {projectClasspathElement ->
                        log.debug("  Adding to projectArtifact ->" + projectClasspathElement.toString())
                        pathelement(location: projectClasspathElement.toString())
                    }
                    pathelement(location: pluginArifact('com.google.code.maven-play-plugin:play-server-booter'))
                }
                /*classpath() {
                    // Add our plugin artifact to pick up log4j configuration
                    //GS pathelement(location: getClass().protectionDomain.codeSource.location.file)
                    //GS pathelement(location: pluginArifact('log4j:log4j'))
                }*/
                
                if (serverLogOutput) {
                    File logFile = new File( workingDirectory, "server.log" );
                    log.info("Redirecting output to: $logFile")
                    redirector(output: logFile)
                }
                
                sysproperty(key: 'play.home', value: playHome)
                sysproperty(key: 'play.id', value: playTestId)
                sysproperty(key: 'application.path', value: applicationPath)
                
                if (serverProcessArgLine != null) {
                    String argLine = serverProcessArgLine.trim();
                    if (!"".equals(argLine)) {
                        String[] args = argLine.split( " " );
                        for (String arg: args) {
                            jvmarg(value: arg);
                            //System.out.println("jvmarg:'"+arg+"'");
                        }
                    }
                }
            }
        }
        <commit_msg>"start-server" mojo: - proper handling situation where "play.home" variable is not set. This is continuation of latest changes (play:initialize does not set "play.home" variable to ${basedir}/target/play/home" location anymore). Other mojos and Surefire provider must handle this situation by themselves, - setting current (working) directory to ${basedir} instead of ${project.build.directory}/play - some plugins and applications read files using relative paths and they expect current (working) directory to be project root directory, - log.debug jvm arguments.<commit_after>/*
 *  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.
 */

package com.google.code.play

import org.apache.maven.project.MavenProject

import org.codehaus.plexus.util.FileUtils;

import org.codehaus.gmaven.mojo.support.ProcessLauncher

/**
 * Start Play! server.
 * Based on <a href="http://mojo.codehaus.org/selenium-maven-plugin/start-server-mojo.html">selenium:start-server mojo</a>
 *
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 * @goal start-server
 * @requiresDependencyResolution test
 */
class StartServerMojo
    extends AbstractPlayGroovyMojo
{
    /**
     * ...
     *
     * @parameter expression="${play.home}"
     * @since 1.0.0
     */
    String playHome

    /**
     * ...
     *
     * @parameter expression="${play.testId}" default-value="test"
     * @since 1.0.0
     */
    String playTestId

    /**
     * The port number of the server to connect to.
     *
     * @parameter expression="${play.serverPort}" default-value="9000"
     * @required
     * @since 1.0.0
     */
    int serverPort
    
    /**
     * Enable logging mode.
     *
     * @parameter expression="${play.serverLogOutput}" default-value="true"
     * @since 1.0.0
     */
    boolean serverLogOutput

    /**
     * Allows the server startup to be skipped.
     *
     * @parameter expression="${play.seleniumSkip}" default-value="false"
     * @since 1.0.0
     */
    boolean seleniumSkip
    
    /**
     * Arbitrary JVM options to set on the command line.
     *
     * @parameter expression="${play.serverProcessArgLine}"
     * @since 1.0.0
     */
    private String serverProcessArgLine;
    
    //
    // Components
    //
    
    /**
     * The enclosing project.
     * 
     * @parameter expression="${project}"
     * @required
     * @readonly
     */
    MavenProject project;

    /**
     * List of artifacts this plugin depends on. Used for resolving the Findbugs coreplugin.
     *
     * @parameter expression="${plugin.artifacts}"
     * @required
     * @readonly
     */
    ArrayList pluginArtifacts

     /**
     * @parameter expression="${plugin.artifactMap}"
     * @required
     * @readonly
     */
    Map pluginArtifactMap

    //
    // Mojo
    //

    void execute() {
        if ( !"play".equals( project.packaging ) )
        {
            return;
        }

        if (seleniumSkip) {
            log.info('Skipping startup')
            return
        }
        
        playHome = checkPlayHome(playHome);
        
        File applicationPath = project.basedir;
        
        File buildDirectory = new File( project.build.directory );
        File logDirectory = new File( buildDirectory, "play" );
        ant.mkdir(dir: logDirectory)
        
        File userExtensionsJsFile = new File( playHome, "modules/testrunner/public/test-runner/selenium/scripts/user-extensions.js" );
        if ( userExtensionsJsFile.isFile() )
        {
            File seleniumDirectory = new File( buildDirectory, "selenium" );
            ant.mkdir(dir: seleniumDirectory)
            FileUtils.copyFileToDirectoryIfModified( userExtensionsJsFile, seleniumDirectory );
        }
        //else??
        
        //if (serverLogOutput) {
        //    ant.mkdir(dir: logFile.parentFile)
        //}
        
        def pluginArifact = { id ->
            def artifact = pluginArtifactMap[id]
            if (!artifact) {
                fail("Unable to locate '$id' in the list of plugin artifacts")
            }
            
            log.debug("Using plugin artifact: ${artifact.file}")
            
            return artifact.file
        }

//System.out.println("playHome:"+playHome);
//System.out.println("applicationPath:"+applicationPath);
//System.out.println("playId:"+playTestId);
        
        def launcher = new ProcessLauncher(name: 'Play! Server', background: true)
        
        launcher.process = {
            ant.java(classname: 'com.google.code.play.PlayServerBooter',
                     fork: true,
                     dir: applicationPath,
                     failonerror: true)
            {
                classpath() {
                    def projectClasspathElements = project.testClasspathElements
                    def projectClasspathList = projectClasspathElements.findAll{project.build.outputDirectory != it.toString()}

                    projectClasspathList.each() {projectClasspathElement ->
                        log.debug("  Adding to projectArtifact ->" + projectClasspathElement.toString())
                        pathelement(location: projectClasspathElement.toString())
                    }
                    pathelement(location: pluginArifact('com.google.code.maven-play-plugin:play-server-booter'))
                }
                /*classpath() {
                    // Add our plugin artifact to pick up log4j configuration
                    //GS pathelement(location: getClass().protectionDomain.codeSource.location.file)
                    //GS pathelement(location: pluginArifact('log4j:log4j'))
                }*/
                
                if (serverLogOutput) {
                    File logFile = new File( logDirectory, "server.log" );
                    log.info("Redirecting output to: $logFile")
                    redirector(output: logFile)
                }
                
                sysproperty(key: 'play.home', value: playHome)
                sysproperty(key: 'play.id', value: playTestId)
                sysproperty(key: 'application.path', value: applicationPath)
                
                if (serverProcessArgLine != null) {
                    String argLine = serverProcessArgLine.trim();
                    if (!"".equals(argLine)) {
                        String[] args = argLine.split( " " );
                        for (String arg: args) {
                            jvmarg(value: arg);
                            log.debug("  Adding jvmarg '" + arg + "'")
                        }
                    }
                }
            }
        }
        
{'fraction_non_alphanumeric': 0.10602409638554217, 'fraction_numerical': 0.004235122307411464, 'mean_word_length': 1.837373109591879, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3231121', 'n_tokens_mistral': 3696, 'n_tokens_neox': 3521, 'n_words': 894}