Coverage Report - org.apache.maven.wagon.providers.file.FileWagon
 
Classes in this File Line Coverage Branch Coverage Complexity
FileWagon
75%
62/83
62%
26/42
5,222
 
 1  
 package org.apache.maven.wagon.providers.file;
 2  
 
 3  
 /*
 4  
  * Licensed to the Apache Software Foundation (ASF) under one
 5  
  * or more contributor license agreements.  See the NOTICE file
 6  
  * distributed with this work for additional information
 7  
  * regarding copyright ownership.  The ASF licenses this file
 8  
  * to you under the Apache License, Version 2.0 (the
 9  
  * "License"); you may not use this file except in compliance
 10  
  * with the License.  You may obtain a copy of the License at
 11  
  *
 12  
  *   http://www.apache.org/licenses/LICENSE-2.0
 13  
  *
 14  
  * Unless required by applicable law or agreed to in writing,
 15  
  * software distributed under the License is distributed on an
 16  
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 17  
  * KIND, either express or implied.  See the License for the
 18  
  * specific language governing permissions and limitations
 19  
  * under the License.
 20  
  */
 21  
 
 22  
 import java.io.BufferedInputStream;
 23  
 import java.io.BufferedOutputStream;
 24  
 import java.io.File;
 25  
 import java.io.FileInputStream;
 26  
 import java.io.FileNotFoundException;
 27  
 import java.io.IOException;
 28  
 import java.io.InputStream;
 29  
 import java.io.OutputStream;
 30  
 import java.util.ArrayList;
 31  
 import java.util.Arrays;
 32  
 import java.util.List;
 33  
 
 34  
 import org.apache.maven.wagon.ConnectionException;
 35  
 import org.apache.maven.wagon.InputData;
 36  
 import org.apache.maven.wagon.LazyFileOutputStream;
 37  
 import org.apache.maven.wagon.OutputData;
 38  
 import org.apache.maven.wagon.ResourceDoesNotExistException;
 39  
 import org.apache.maven.wagon.StreamWagon;
 40  
 import org.apache.maven.wagon.TransferFailedException;
 41  
 import org.apache.maven.wagon.authorization.AuthorizationException;
 42  
 import org.apache.maven.wagon.resource.Resource;
 43  
 import org.codehaus.plexus.util.FileUtils;
 44  
 import org.codehaus.plexus.util.StringUtils;
 45  
 
 46  
 /**
 47  
  * Wagon Provider for Local File System
 48  
  * 
 49  
  * @author <a href="michal.maczka@dimatics.com">Michal Maczka</a>
 50  
  * @version $Id: FileWagon.java 745730 2009-02-19 05:15:51Z brett $
 51  
  * @plexus.component role="org.apache.maven.wagon.Wagon" role-hint="file" instantiation-strategy="per-lookup"
 52  
  */
 53  36
 public class FileWagon
 54  
     extends StreamWagon
 55  
 {
 56  
     public void fillInputData( InputData inputData )
 57  
         throws TransferFailedException, ResourceDoesNotExistException
 58  
     {
 59  30
         if ( getRepository().getBasedir() == null )
 60  
         {
 61  0
             throw new TransferFailedException( "Unable to operate with a null basedir." );
 62  
         }
 63  
 
 64  30
         Resource resource = inputData.getResource();
 65  
 
 66  30
         File file = new File( getRepository().getBasedir(), resource.getName() );
 67  
 
 68  30
         if ( !file.exists() )
 69  
         {
 70  4
             throw new ResourceDoesNotExistException( "File: " + file + " does not exist" );
 71  
         }
 72  
 
 73  
         try
 74  
         {
 75  26
             InputStream in = new BufferedInputStream( new FileInputStream( file ) );
 76  
 
 77  26
             inputData.setInputStream( in );
 78  
 
 79  26
             resource.setContentLength( file.length() );
 80  
 
 81  26
             resource.setLastModified( file.lastModified() );
 82  
         }
 83  0
         catch ( FileNotFoundException e )
 84  
         {
 85  0
             throw new TransferFailedException( "Could not read from file: " + file.getAbsolutePath(), e );
 86  26
         }
 87  26
     }
 88  
 
 89  
     public void fillOutputData( OutputData outputData )
 90  
         throws TransferFailedException
 91  
     {
 92  14
         if ( getRepository().getBasedir() == null )
 93  
         {
 94  0
             throw new TransferFailedException( "Unable to operate with a null basedir." );
 95  
         }
 96  
 
 97  14
         Resource resource = outputData.getResource();
 98  
 
 99  14
         File file = new File( getRepository().getBasedir(), resource.getName() );
 100  
 
 101  14
         createParentDirectories( file );
 102  
 
 103  14
         OutputStream outputStream = new BufferedOutputStream( new LazyFileOutputStream( file ) );
 104  
 
 105  14
         outputData.setOutputStream( outputStream );
 106  14
     }
 107  
 
 108  
     protected void openConnectionInternal()
 109  
         throws ConnectionException
 110  
     {
 111  36
         if ( getRepository() == null )
 112  
         {
 113  0
             throw new ConnectionException( "Unable to operate with a null repository." );
 114  
         }
 115  
 
 116  36
         if ( getRepository().getBasedir() == null )
 117  
         {
 118  
             // This condition is possible when using wagon-file under integration testing conditions.
 119  1
             fireSessionDebug( "Using a null basedir." );
 120  1
             return;
 121  
         }
 122  
 
 123  
         // Check the File repository exists
 124  35
         File basedir = new File( getRepository().getBasedir() );
 125  35
         if ( !basedir.exists() )
 126  
         {
 127  0
             if ( !basedir.mkdirs() )
 128  
             {
 129  0
                 throw new ConnectionException( "Repository path " + basedir + " does not exist,"
 130  
                                                + " and cannot be created." );
 131  
             }
 132  
         }
 133  
 
 134  35
         if ( !basedir.canRead() )
 135  
         {
 136  0
             throw new ConnectionException( "Repository path " + basedir + " cannot be read" );
 137  
         }
 138  35
     }
 139  
 
 140  
     public void closeConnection()
 141  
     {
 142  35
     }
 143  
 
 144  
     public boolean supportsDirectoryCopy()
 145  
     {
 146  
         // TODO: should we test for null basedir here?
 147  4
         return true;
 148  
     }
 149  
 
 150  
     public void putDirectory( File sourceDirectory, String destinationDirectory )
 151  
         throws TransferFailedException, ResourceDoesNotExistException, AuthorizationException
 152  
     {
 153  4
         if ( getRepository().getBasedir() == null )
 154  
         {
 155  0
             throw new TransferFailedException( "Unable to putDirectory() with a null basedir." );
 156  
         }
 157  
 
 158  4
         File path = resolveDestinationPath( destinationDirectory );
 159  
 
 160  
         try
 161  
         {
 162  
             /*
 163  
              * Done to address issue found in HP-UX with regards to "." directory references. Details found in ..
 164  
              * WAGON-30 - wagon-file failed when used by maven-site-plugin WAGON-33 - FileWagon#putDirectory() fails in
 165  
              * HP-UX if destinationDirectory is "."
 166  
              * http://www.nabble.com/With-maven-2.0.2-site%3Adeploy-doesn%27t-work-t934716.html for details. Using
 167  
              * path.getCanonicalFile() ensures that the path is fully resolved before an attempt to create it. TODO:
 168  
              * consider moving this to FileUtils.mkdirs()
 169  
              */
 170  4
             File realFile = path.getCanonicalFile();
 171  4
             realFile.mkdirs();
 172  
         }
 173  0
         catch ( IOException e )
 174  
         {
 175  
             // Fall back to standard way if getCanonicalFile() fails.
 176  0
             path.mkdirs();
 177  4
         }
 178  
 
 179  4
         if ( !path.exists() || !path.isDirectory() )
 180  
         {
 181  0
             String emsg = "Could not make directory '" + path.getAbsolutePath() + "'.";
 182  
 
 183  
             // Add assistive message in case of failure.
 184  0
             File basedir = new File( getRepository().getBasedir() );
 185  0
             if ( !basedir.canWrite() )
 186  
             {
 187  0
                 emsg += "  The base directory " + basedir + " is read-only.";
 188  
             }
 189  
 
 190  0
             throw new TransferFailedException( emsg );
 191  
         }
 192  
 
 193  
         try
 194  
         {
 195  4
             FileUtils.copyDirectoryStructure( sourceDirectory, path );
 196  
         }
 197  0
         catch ( IOException e )
 198  
         {
 199  0
             throw new TransferFailedException( "Error copying directory structure", e );
 200  4
         }
 201  4
     }
 202  
 
 203  
     private File resolveDestinationPath( String destinationPath )
 204  
     {
 205  13
         String basedir = getRepository().getBasedir();
 206  
 
 207  13
         destinationPath = StringUtils.replace( destinationPath, "\\", "/" );
 208  
 
 209  
         File path;
 210  
 
 211  13
         if ( destinationPath.equals( "." ) )
 212  
         {
 213  1
             path = new File( basedir );
 214  
         }
 215  
         else
 216  
         {
 217  12
             path = new File( basedir, destinationPath );
 218  
         }
 219  
 
 220  13
         return path;
 221  
     }
 222  
 
 223  
     public List getFileList( String destinationDirectory )
 224  
         throws TransferFailedException, ResourceDoesNotExistException, AuthorizationException
 225  
     {
 226  3
         if ( getRepository().getBasedir() == null )
 227  
         {
 228  0
             throw new TransferFailedException( "Unable to getFileList() with a null basedir." );
 229  
         }
 230  
 
 231  3
         File path = resolveDestinationPath( destinationDirectory );
 232  
 
 233  3
         if ( !path.exists() )
 234  
         {
 235  1
             throw new ResourceDoesNotExistException( "Directory does not exist: " + destinationDirectory );
 236  
         }
 237  
 
 238  2
         if ( !path.isDirectory() )
 239  
         {
 240  0
             throw new ResourceDoesNotExistException( "Path is not a directory: " + destinationDirectory );
 241  
         }
 242  
 
 243  2
         File[] files = path.listFiles();
 244  
 
 245  2
         List list = new ArrayList( files.length );
 246  8
         for ( int i = 0; i < files.length; i++ )
 247  
         {
 248  6
             String name = files[i].getName();
 249  6
             if ( files[i].isDirectory() && !name.endsWith( "/" ) )
 250  
             {
 251  1
                 name += "/";
 252  
             }
 253  6
             list.add( name );
 254  
         }
 255  2
         return list;
 256  
     }
 257  
 
 258  
     public boolean resourceExists( String resourceName )
 259  
         throws TransferFailedException, AuthorizationException
 260  
     {
 261  6
         if ( getRepository().getBasedir() == null )
 262  
         {
 263  0
             throw new TransferFailedException( "Unable to getFileList() with a null basedir." );
 264  
         }
 265  
 
 266  6
         File file = resolveDestinationPath( resourceName );
 267  
 
 268  6
         if ( resourceName.endsWith( "/" ) )
 269  
         {
 270  2
             return file.isDirectory();
 271  
         }
 272  
         
 273  4
         return file.exists();
 274  
     }
 275  
 }