package org.codehaus.mojo.buildhelper;

/*
 * The MIT License
 * 
 * Copyright (c) 2004, The Codehaus
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
 * associated documentation files (the "Software"), to deal in the Software without restriction,
 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all copies or
 * substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
 * NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

import java.io.IOException;
import java.net.ServerSocket;

import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.project.MavenProject;

/**
 * Reserve a list of random and not in used network ports and place them 
 * in a configurable project properties. 
 * 
 * @goal reserve-network-ports
 * @phase process-test-classes
 * @author <a href="dantran@gmail.com">Dan T. Tran</a>
 * @version $Id: ReserveListnerPortMojo.java 6754 2008-04-13 15:14:04Z dantran $
 * @since 1.1
 */
public class ReserveListenerPortMojo
    extends AbstractMojo
{

    /**
     * A List to property names to be placed in maven project
     * @parameter 
     * @required
     * 
     */
    private String [] names = new String[0];
    
    /**
     * @parameter expression="${project}"
     * @required
     * @readonly
     */
    private MavenProject project;


    public void execute()
        throws MojoExecutionException, MojoFailureException
    {
        for ( int i = 0 ; i < names.length; ++i )
        {
            String unusedPort = Integer.toString( getNextAvailablePort()  );
            project.getProperties().setProperty( names[i], unusedPort );
        }
    }
    
    private int getNextAvailablePort()
        throws MojoExecutionException
    {
        int unusedPort = 0;
        try
        {
            ServerSocket socket = new ServerSocket( 0 );
            unusedPort = socket.getLocalPort();
            socket.close();
        }
        catch ( IOException e )
        {
            throw new MojoExecutionException( "Error getting an avaiable port from system", e );
        }

        return unusedPort;
    }
    
}

Add a mojo to reserve a list of random and unused network ports and place in a configurable maven project properties
Add a mojo to reserve a list of random and unused network ports and place in a configurable maven project properties

git-svn-id: 194011dfe2cc7b86c43175a0957ab3bfc09bff25@7298 52ab4f32-60fc-0310-b215-8acea882cd1b
