package org.apache.maven.wagon.providers.file;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import org.apache.maven.wagon.ConnectionException;
import org.apache.maven.wagon.InputData;
import org.apache.maven.wagon.LazyFileOutputStream;
import org.apache.maven.wagon.OutputData;
import org.apache.maven.wagon.ResourceDoesNotExistException;
import org.apache.maven.wagon.StreamWagon;
import org.apache.maven.wagon.TransferFailedException;
import org.apache.maven.wagon.authorization.AuthorizationException;
import org.apache.maven.wagon.resource.Resource;
import org.codehaus.plexus.util.FileUtils;
public class FileWagon
extends StreamWagon
{
public void fillInputData( InputData inputData )
throws TransferFailedException, ResourceDoesNotExistException
{
if ( getRepository().getBasedir() == null )
{
throw new TransferFailedException( "Unable to operate with a null basedir." );
}
Resource resource = inputData.getResource();
File file = new File( getRepository().getBasedir(), resource.getName() );
if ( !file.exists() )
{
throw new ResourceDoesNotExistException( "File: " + file + " does not exist" );
}
try
{
InputStream in = new BufferedInputStream( new FileInputStream( file ) );
inputData.setInputStream( in );
resource.setContentLength( file.length() );
resource.setLastModified( file.lastModified() );
}
catch ( FileNotFoundException e )
{
throw new TransferFailedException( "Could not read from file: " + file.getAbsolutePath(), e );
}
}
public void fillOutputData( OutputData outputData )
throws TransferFailedException
{
if ( getRepository().getBasedir() == null )
{
throw new TransferFailedException( "Unable to operate with a null basedir." );
}
Resource resource = outputData.getResource();
File file = new File( getRepository().getBasedir(), resource.getName() );
createParentDirectories( file );
OutputStream outputStream = new BufferedOutputStream( new LazyFileOutputStream( file ) );
outputData.setOutputStream( outputStream );
}
protected void openConnectionInternal()
throws ConnectionException
{
if ( getRepository() == null )
{
throw new ConnectionException( "Unable to operate with a null repository." );
}
if ( getRepository().getBasedir() == null )
{
fireSessionDebug( "Using a null basedir." );
return;
}
File basedir = new File( getRepository().getBasedir() );
if ( !basedir.exists() )
{
if ( !basedir.mkdirs() )
{
throw new ConnectionException( "Repository path " + basedir + " does not exist,"
+ " and cannot be created." );
}
}
if ( !basedir.canRead() )
{
throw new ConnectionException( "Repository path " + basedir + " cannot be read" );
}
}
public void closeConnection()
{
}
public boolean supportsDirectoryCopy()
{
return true;
}
public void putDirectory( File sourceDirectory, String destinationDirectory )
throws TransferFailedException, ResourceDoesNotExistException, AuthorizationException
{
if ( getRepository().getBasedir() == null )
{
throw new TransferFailedException( "Unable to putDirectory() with a null basedir." );
}
File path = resolveDestinationPath( destinationDirectory );
try
{
File realFile = path.getCanonicalFile();
realFile.mkdirs();
}
catch ( IOException e )
{
path.mkdirs();
}
if ( !path.exists() || !path.isDirectory() )
{
String emsg = "Could not make directory '" + path.getAbsolutePath() + "'.";
File basedir = new File( getRepository().getBasedir() );
if ( !basedir.canWrite() )
{
emsg += " The base directory " + basedir + " is read-only.";
}
throw new TransferFailedException( emsg );
}
try
{
FileUtils.copyDirectoryStructure( sourceDirectory, path );
}
catch ( IOException e )
{
throw new TransferFailedException( "Error copying directory structure", e );
}
}
private File resolveDestinationPath( String destinationPath )
{
String basedir = getRepository().getBasedir();
destinationPath = destinationPath.replace( "\\", "/" );
File path;
if ( destinationPath.equals( "." ) )
{
path = new File( basedir );
}
else
{
path = new File( basedir, destinationPath );
}
return path;
}
public List<String> getFileList( String destinationDirectory )
throws TransferFailedException, ResourceDoesNotExistException, AuthorizationException
{
if ( getRepository().getBasedir() == null )
{
throw new TransferFailedException( "Unable to getFileList() with a null basedir." );
}
File path = resolveDestinationPath( destinationDirectory );
if ( !path.exists() )
{
throw new ResourceDoesNotExistException( "Directory does not exist: " + destinationDirectory );
}
if ( !path.isDirectory() )
{
throw new ResourceDoesNotExistException( "Path is not a directory: " + destinationDirectory );
}
File[] files = path.listFiles();
List<String> list = new ArrayList<String>( files.length );
for ( File file : files )
{
String name = file.getName();
if ( file.isDirectory() && !name.endsWith( "/" ) )
{
name += "/";
}
list.add( name );
}
return list;
}
public boolean resourceExists( String resourceName )
throws TransferFailedException, AuthorizationException
{
if ( getRepository().getBasedir() == null )
{
throw new TransferFailedException( "Unable to getFileList() with a null basedir." );
}
File file = resolveDestinationPath( resourceName );
if ( resourceName.endsWith( "/" ) )
{
return file.isDirectory();
}
return file.exists();
}
}