View Javadoc

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  public class FileWagon
54      extends StreamWagon
55  {
56      public void fillInputData( InputData inputData )
57          throws TransferFailedException, ResourceDoesNotExistException
58      {
59          if ( getRepository().getBasedir() == null )
60          {
61              throw new TransferFailedException( "Unable to operate with a null basedir." );
62          }
63  
64          Resource resource = inputData.getResource();
65  
66          File file = new File( getRepository().getBasedir(), resource.getName() );
67  
68          if ( !file.exists() )
69          {
70              throw new ResourceDoesNotExistException( "File: " + file + " does not exist" );
71          }
72  
73          try
74          {
75              InputStream in = new BufferedInputStream( new FileInputStream( file ) );
76  
77              inputData.setInputStream( in );
78  
79              resource.setContentLength( file.length() );
80  
81              resource.setLastModified( file.lastModified() );
82          }
83          catch ( FileNotFoundException e )
84          {
85              throw new TransferFailedException( "Could not read from file: " + file.getAbsolutePath(), e );
86          }
87      }
88  
89      public void fillOutputData( OutputData outputData )
90          throws TransferFailedException
91      {
92          if ( getRepository().getBasedir() == null )
93          {
94              throw new TransferFailedException( "Unable to operate with a null basedir." );
95          }
96  
97          Resource resource = outputData.getResource();
98  
99          File file = new File( getRepository().getBasedir(), resource.getName() );
100 
101         createParentDirectories( file );
102 
103         OutputStream outputStream = new BufferedOutputStream( new LazyFileOutputStream( file ) );
104 
105         outputData.setOutputStream( outputStream );
106     }
107 
108     protected void openConnectionInternal()
109         throws ConnectionException
110     {
111         if ( getRepository() == null )
112         {
113             throw new ConnectionException( "Unable to operate with a null repository." );
114         }
115 
116         if ( getRepository().getBasedir() == null )
117         {
118             // This condition is possible when using wagon-file under integration testing conditions.
119             fireSessionDebug( "Using a null basedir." );
120             return;
121         }
122 
123         // Check the File repository exists
124         File basedir = new File( getRepository().getBasedir() );
125         if ( !basedir.exists() )
126         {
127             if ( !basedir.mkdirs() )
128             {
129                 throw new ConnectionException( "Repository path " + basedir + " does not exist,"
130                                                + " and cannot be created." );
131             }
132         }
133 
134         if ( !basedir.canRead() )
135         {
136             throw new ConnectionException( "Repository path " + basedir + " cannot be read" );
137         }
138     }
139 
140     public void closeConnection()
141     {
142     }
143 
144     public boolean supportsDirectoryCopy()
145     {
146         // TODO: should we test for null basedir here?
147         return true;
148     }
149 
150     public void putDirectory( File sourceDirectory, String destinationDirectory )
151         throws TransferFailedException, ResourceDoesNotExistException, AuthorizationException
152     {
153         if ( getRepository().getBasedir() == null )
154         {
155             throw new TransferFailedException( "Unable to putDirectory() with a null basedir." );
156         }
157 
158         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             File realFile = path.getCanonicalFile();
171             realFile.mkdirs();
172         }
173         catch ( IOException e )
174         {
175             // Fall back to standard way if getCanonicalFile() fails.
176             path.mkdirs();
177         }
178 
179         if ( !path.exists() || !path.isDirectory() )
180         {
181             String emsg = "Could not make directory '" + path.getAbsolutePath() + "'.";
182 
183             // Add assistive message in case of failure.
184             File basedir = new File( getRepository().getBasedir() );
185             if ( !basedir.canWrite() )
186             {
187                 emsg += "  The base directory " + basedir + " is read-only.";
188             }
189 
190             throw new TransferFailedException( emsg );
191         }
192 
193         try
194         {
195             FileUtils.copyDirectoryStructure( sourceDirectory, path );
196         }
197         catch ( IOException e )
198         {
199             throw new TransferFailedException( "Error copying directory structure", e );
200         }
201     }
202 
203     private File resolveDestinationPath( String destinationPath )
204     {
205         String basedir = getRepository().getBasedir();
206 
207         destinationPath = StringUtils.replace( destinationPath, "\\", "/" );
208 
209         File path;
210 
211         if ( destinationPath.equals( "." ) )
212         {
213             path = new File( basedir );
214         }
215         else
216         {
217             path = new File( basedir, destinationPath );
218         }
219 
220         return path;
221     }
222 
223     public List getFileList( String destinationDirectory )
224         throws TransferFailedException, ResourceDoesNotExistException, AuthorizationException
225     {
226         if ( getRepository().getBasedir() == null )
227         {
228             throw new TransferFailedException( "Unable to getFileList() with a null basedir." );
229         }
230 
231         File path = resolveDestinationPath( destinationDirectory );
232 
233         if ( !path.exists() )
234         {
235             throw new ResourceDoesNotExistException( "Directory does not exist: " + destinationDirectory );
236         }
237 
238         if ( !path.isDirectory() )
239         {
240             throw new ResourceDoesNotExistException( "Path is not a directory: " + destinationDirectory );
241         }
242 
243         File[] files = path.listFiles();
244 
245         List list = new ArrayList( files.length );
246         for ( int i = 0; i < files.length; i++ )
247         {
248             String name = files[i].getName();
249             if ( files[i].isDirectory() && !name.endsWith( "/" ) )
250             {
251                 name += "/";
252             }
253             list.add( name );
254         }
255         return list;
256     }
257 
258     public boolean resourceExists( String resourceName )
259         throws TransferFailedException, AuthorizationException
260     {
261         if ( getRepository().getBasedir() == null )
262         {
263             throw new TransferFailedException( "Unable to getFileList() with a null basedir." );
264         }
265 
266         File file = resolveDestinationPath( resourceName );
267 
268         if ( resourceName.endsWith( "/" ) )
269         {
270             return file.isDirectory();
271         }
272         
273         return file.exists();
274     }
275 }