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