1 | |
package org.apache.maven.shared.io.download; |
2 | |
|
3 | |
import java.io.File; |
4 | |
import java.io.IOException; |
5 | |
import java.net.MalformedURLException; |
6 | |
import java.net.URL; |
7 | |
import java.util.Collections; |
8 | |
import java.util.HashMap; |
9 | |
import java.util.Iterator; |
10 | |
import java.util.List; |
11 | |
import java.util.Map; |
12 | |
|
13 | |
import org.apache.maven.artifact.manager.WagonManager; |
14 | |
import org.apache.maven.shared.io.logging.MessageHolder; |
15 | |
import org.apache.maven.wagon.ConnectionException; |
16 | |
import org.apache.maven.wagon.ResourceDoesNotExistException; |
17 | |
import org.apache.maven.wagon.TransferFailedException; |
18 | |
import org.apache.maven.wagon.UnsupportedProtocolException; |
19 | |
import org.apache.maven.wagon.Wagon; |
20 | |
import org.apache.maven.wagon.authentication.AuthenticationException; |
21 | |
import org.apache.maven.wagon.authorization.AuthorizationException; |
22 | |
import org.apache.maven.wagon.events.TransferListener; |
23 | |
import org.apache.maven.wagon.repository.Repository; |
24 | |
|
25 | |
public class DefaultDownloadManager |
26 | |
implements DownloadManager |
27 | |
{ |
28 | |
|
29 | |
public static final String ROLE_HINT = "default"; |
30 | |
|
31 | |
private WagonManager wagonManager; |
32 | |
|
33 | 14 | private Map cache = new HashMap(); |
34 | |
|
35 | 2 | public DefaultDownloadManager() |
36 | |
{ |
37 | 2 | } |
38 | |
|
39 | 12 | public DefaultDownloadManager( WagonManager wagonManager ) |
40 | |
{ |
41 | 12 | this.wagonManager = wagonManager; |
42 | 12 | } |
43 | |
|
44 | |
public File download( String url, MessageHolder messageHolder ) |
45 | |
throws DownloadFailedException |
46 | |
{ |
47 | 11 | return download( url, Collections.EMPTY_LIST, messageHolder ); |
48 | |
} |
49 | |
|
50 | |
public File download( String url, List transferListeners, MessageHolder messageHolder ) |
51 | |
throws DownloadFailedException |
52 | |
{ |
53 | 12 | File downloaded = (File) cache.get( url ); |
54 | |
|
55 | 12 | if ( downloaded != null && downloaded.exists() ) |
56 | |
{ |
57 | 1 | messageHolder.addMessage( "Using cached download: " + downloaded.getAbsolutePath() ); |
58 | |
|
59 | 1 | return downloaded; |
60 | |
} |
61 | |
|
62 | |
URL sourceUrl; |
63 | |
try |
64 | |
{ |
65 | 11 | sourceUrl = new URL( url ); |
66 | |
} |
67 | 1 | catch ( MalformedURLException e ) |
68 | |
{ |
69 | 1 | throw new DownloadFailedException( url, "Download failed due to invalid URL. Reason: " + e.getMessage(), e ); |
70 | |
} |
71 | |
|
72 | 10 | Wagon wagon = null; |
73 | |
|
74 | |
|
75 | |
try |
76 | |
{ |
77 | 10 | wagon = wagonManager.getWagon( sourceUrl.getProtocol() ); |
78 | |
} |
79 | 1 | catch ( UnsupportedProtocolException e ) |
80 | |
{ |
81 | 1 | throw new DownloadFailedException( url, "Download failed. Reason: " + e.getMessage(), e ); |
82 | |
} |
83 | |
|
84 | 9 | messageHolder.addMessage( "Using wagon: " + wagon + " to download: " + url ); |
85 | |
|
86 | |
try |
87 | |
{ |
88 | |
|
89 | 9 | downloaded = File.createTempFile( "download-", null ); |
90 | |
|
91 | |
|
92 | 9 | downloaded.deleteOnExit(); |
93 | |
} |
94 | 0 | catch ( IOException e ) |
95 | |
{ |
96 | 0 | throw new DownloadFailedException( url, "Failed to create temporary file target for download. Reason: " |
97 | 0 | + e.getMessage(), e ); |
98 | |
} |
99 | |
|
100 | 9 | messageHolder.addMessage( "Download target is: " + downloaded.getAbsolutePath() ); |
101 | |
|
102 | |
|
103 | 9 | String remotePath = sourceUrl.getPath(); |
104 | 9 | String baseUrl = url.substring( 0, url.length() - remotePath.length() ); |
105 | |
|
106 | 19 | for ( Iterator it = transferListeners.iterator(); it.hasNext(); ) |
107 | |
{ |
108 | 1 | TransferListener listener = (TransferListener) it.next(); |
109 | 1 | wagon.addTransferListener( listener ); |
110 | |
} |
111 | |
|
112 | |
|
113 | |
|
114 | 9 | Repository repo = new Repository( sourceUrl.getHost(), baseUrl ); |
115 | |
|
116 | 9 | messageHolder.addMessage( "Connecting to: " + repo.getHost() + "(baseUrl: " + repo.getUrl() + ")" ); |
117 | |
|
118 | |
try |
119 | |
{ |
120 | 18 | wagon.connect( repo, wagonManager.getAuthenticationInfo( repo.getId() ), wagonManager.getProxy( sourceUrl |
121 | 9 | .getProtocol() ) ); |
122 | |
} |
123 | 1 | catch ( ConnectionException e ) |
124 | |
{ |
125 | 1 | throw new DownloadFailedException( url, "Download failed. Reason: " + e.getMessage(), e ); |
126 | |
} |
127 | 1 | catch ( AuthenticationException e ) |
128 | |
{ |
129 | 1 | throw new DownloadFailedException( url, "Download failed. Reason: " + e.getMessage(), e ); |
130 | |
} |
131 | |
|
132 | 7 | messageHolder.addMessage( "Getting: " + remotePath ); |
133 | |
|
134 | |
try |
135 | |
{ |
136 | 7 | wagon.get( remotePath, downloaded ); |
137 | |
|
138 | |
|
139 | 4 | cache.put( url, downloaded ); |
140 | |
|
141 | 4 | return downloaded; |
142 | |
} |
143 | 1 | catch ( TransferFailedException e ) |
144 | |
{ |
145 | 1 | throw new DownloadFailedException( url, "Download failed. Reason: " + e.getMessage(), e ); |
146 | |
} |
147 | 1 | catch ( ResourceDoesNotExistException e ) |
148 | |
{ |
149 | 1 | throw new DownloadFailedException( url, "Download failed. Reason: " + e.getMessage(), e ); |
150 | |
} |
151 | 1 | catch ( AuthorizationException e ) |
152 | |
{ |
153 | 1 | throw new DownloadFailedException( url, "Download failed. Reason: " + e.getMessage(), e ); |
154 | |
} |
155 | |
finally |
156 | 10 | { |
157 | |
|
158 | 7 | if ( wagon != null ) |
159 | |
{ |
160 | |
try |
161 | |
{ |
162 | 7 | messageHolder.addMessage( "Disconnecting." ); |
163 | |
|
164 | 7 | wagon.disconnect(); |
165 | |
} |
166 | 1 | catch ( ConnectionException e ) |
167 | |
{ |
168 | 1 | messageHolder.addMessage( "Failed to disconnect wagon for: " + url, e ); |
169 | |
} |
170 | |
|
171 | 15 | for ( Iterator it = transferListeners.iterator(); it.hasNext(); ) |
172 | |
{ |
173 | 1 | TransferListener listener = (TransferListener) it.next(); |
174 | 1 | wagon.removeTransferListener( listener ); |
175 | |
} |
176 | |
} |
177 | 10 | } |
178 | |
} |
179 | |
|
180 | |
} |