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 | 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 | |
|
119 | 1 | fireSessionDebug( "Using a null basedir." ); |
120 | 1 | return; |
121 | |
} |
122 | |
|
123 | |
|
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 | |
|
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 | |
|
164 | |
|
165 | |
|
166 | |
|
167 | |
|
168 | |
|
169 | |
|
170 | 4 | File realFile = path.getCanonicalFile(); |
171 | 4 | realFile.mkdirs(); |
172 | |
} |
173 | 0 | catch ( IOException e ) |
174 | |
{ |
175 | |
|
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 | |
|
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 | |
} |