1 package org.apache.maven.wagon.providers.file;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
48
49
50
51
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
119 fireSessionDebug( "Using a null basedir." );
120 return;
121 }
122
123
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
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
164
165
166
167
168
169
170 File realFile = path.getCanonicalFile();
171 realFile.mkdirs();
172 }
173 catch ( IOException e )
174 {
175
176 path.mkdirs();
177 }
178
179 if ( !path.exists() || !path.isDirectory() )
180 {
181 String emsg = "Could not make directory '" + path.getAbsolutePath() + "'.";
182
183
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 }