play2-maven-plugin/src/main/java/com/google/code/play2/plugin/AbstractPlay2StartServerMojo.java
protected void waitForServerStarted( String rootUrl, JavaRunnable runner, int startTimeout, boolean spawned )
        throws MojoExecutionException, IOException
    {
        long endTimeMillis = startTimeout > 0  ? System.currentTimeMillis() + startTimeout : 0L;
        boolean started = false;

        URL connectUrl = new URL( rootUrl );
        int verifyWaitDelay = 1000;
        while ( !started )
        {
            if ( startTimeout > 0 && endTimeMillis - System.currentTimeMillis() < 0L )
            {
                if ( spawned )
                {
                    InternalPlay2StopMojo internalStop = new InternalPlay2StopMojo();
                    internalStop.project = project;
                    try
                    {
                        internalStop.execute();
                    }
                    catch ( MojoExecutionException e )
                    {
                        // just ignore
                    }
                    catch ( MojoFailureException e )
                    {
                        // just ignore
                    }
                }
                throw new MojoExecutionException( String.format( "Failed to start Play! server in %d ms",
                                                                 Integer.valueOf( startTimeout ) ) );
            }

            BuildException runnerException = runner.getException();
            if ( runnerException != null )
            {
                throw new MojoExecutionException( "Play! server start exception", runnerException );
            }

            try
            {
                URLConnection conn = connectUrl.openConnection();
                if ( startTimeout > 0 )
                {
                    int connectTimeOut =
                        Long.valueOf( Math.min( endTimeMillis - System.currentTimeMillis(),
                                                Integer.valueOf( Integer.MAX_VALUE ).longValue() ) ).intValue();
                    if ( connectTimeOut > 0 )
                    {
                        conn.setConnectTimeout( connectTimeOut );
                    }
                }
                connectUrl.openConnection().getContent();
                started = true;
            }
            catch ( Exception e )
            {
                // return false;
            }

            if ( !started )
            {
                long sleepTime = verifyWaitDelay;
                if ( startTimeout > 0 )
                {
                    sleepTime = Math.min( sleepTime, endTimeMillis - System.currentTimeMillis() );
                }
                if ( sleepTime > 0 )
                {
                    try
                    {
                        Thread.sleep( sleepTime );
                    }
                    catch ( InterruptedException e )
                    {
                        throw new MojoExecutionException( "?", e );
                    }
                }
            }
        }
    }
protected void waitForServerStarted( String rootUrl, JavaRunnable runner, int startTimeout, boolean spawned )
        throws MojoExecutionException, IOException
    {
        long endTimeMillis = startTimeout > 0  ? System.currentTimeMillis() + startTimeout : 0L;
        boolean started = false;

        URL connectUrl = new URL( rootUrl );
        int verifyWaitDelay = 1000;
        while ( !started )
        {
            if ( startTimeout > 0 && endTimeMillis - System.currentTimeMillis() < 0L )
            {
                if ( spawned )
                {
                    InternalPlay2StopMojo internalStop = new InternalPlay2StopMojo();
                    internalStop.project = project;
                    try
                    {
                        internalStop.execute();
                    }
                    catch ( MojoExecutionException e )
                    {
                        // just ignore
                    }
                    catch ( MojoFailureException e )
                    {
                        // just ignore
                    }
                }
                throw new MojoExecutionException( String.format( "Failed to start Play! server in %d ms",
                                                                 Integer.valueOf( startTimeout ) ) );
            }

            BuildException runnerException = runner.getException();
            if ( runnerException != null )
            {
                throw new MojoExecutionException( "Play! server start exception", runnerException );
            }

            try
            {
                URLConnection conn = connectUrl.openConnection();
                if ( startTimeout > 0 )
                {
                    int connectTimeOut =
                        Long.valueOf( Math.min( endTimeMillis - System.currentTimeMillis(),
                                                Integer.valueOf( Integer.MAX_VALUE ).longValue() ) ).intValue();
                    if ( connectTimeOut > 0 )
                    {
                        conn.setConnectTimeout( connectTimeOut );
                    }
                }
                connectUrl.openConnection().getContent();
                started = true;
            }
            catch ( Exception e )
            {
                // return false;
            }

            if ( !started )
            {
                long sleepTime = verifyWaitDelay;
                if ( startTimeout > 0 )
                {
                    sleepTime = Math.min( sleepTime, endTimeMillis - System.currentTimeMillis() );
                }
                if ( sleepTime > 0 )
                {
                    try
                    {
                        Thread.sleep( sleepTime );
                    }
                    catch ( InterruptedException e )
                    {
                        throw new MojoExecutionException( "?", e );
                    }
                }
            }
        }
    }
startTimeout in milliseconds