1 | |
package org.apache.maven.it.util; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
import java.io.File; |
23 | |
import java.io.FileOutputStream; |
24 | |
import java.io.IOException; |
25 | |
import java.net.URI; |
26 | |
import java.net.URISyntaxException; |
27 | |
import java.net.URL; |
28 | |
import java.util.Enumeration; |
29 | |
import java.util.zip.ZipEntry; |
30 | |
import java.util.zip.ZipFile; |
31 | |
import org.apache.maven.shared.utils.io.FileUtils; |
32 | |
import org.apache.maven.shared.utils.io.IOUtil; |
33 | |
|
34 | |
|
35 | |
|
36 | 0 | public class ResourceExtractor |
37 | |
{ |
38 | |
|
39 | |
public static File simpleExtractResources( Class cl, String resourcePath ) |
40 | |
throws IOException |
41 | |
{ |
42 | 0 | String tempDirPath = System.getProperty( "maven.test.tmpdir", System.getProperty( "java.io.tmpdir" ) ); |
43 | 0 | File tempDir = new File( tempDirPath ); |
44 | |
|
45 | 0 | File testDir = new File( tempDir, resourcePath ); |
46 | |
|
47 | 0 | FileUtils.deleteDirectory( testDir ); |
48 | |
|
49 | 0 | testDir = ResourceExtractor.extractResourcePath( cl, resourcePath, tempDir, false ); |
50 | 0 | return testDir; |
51 | |
} |
52 | |
|
53 | |
public static File extractResourcePath( String resourcePath, File dest ) |
54 | |
throws IOException |
55 | |
{ |
56 | 0 | return extractResourcePath( ResourceExtractor.class, resourcePath, dest ); |
57 | |
} |
58 | |
|
59 | |
public static File extractResourcePath( Class cl, String resourcePath, File dest ) |
60 | |
throws IOException |
61 | |
{ |
62 | 0 | return extractResourcePath( cl, resourcePath, dest, false ); |
63 | |
} |
64 | |
|
65 | |
public static File extractResourcePath( Class cl, String resourcePath, File tempDir, boolean alwaysExtract ) |
66 | |
throws IOException |
67 | |
{ |
68 | 0 | File dest = new File( tempDir, resourcePath ); |
69 | 0 | return extractResourceToDestination( cl, resourcePath, dest, alwaysExtract ); |
70 | |
} |
71 | |
|
72 | |
public static File extractResourceToDestination( Class cl, String resourcePath, File destination, |
73 | |
boolean alwaysExtract ) |
74 | |
throws IOException |
75 | |
{ |
76 | 0 | URL url = cl.getResource( resourcePath ); |
77 | 0 | if ( url == null ) |
78 | |
{ |
79 | 0 | throw new IllegalArgumentException( "Resource not found: " + resourcePath ); |
80 | |
} |
81 | 0 | if ( "jar".equalsIgnoreCase( url.getProtocol() ) ) |
82 | |
{ |
83 | 0 | File jarFile = getJarFileFromUrl( url ); |
84 | 0 | extractResourcePathFromJar( cl, jarFile, resourcePath, destination ); |
85 | 0 | } |
86 | |
else |
87 | |
{ |
88 | |
try |
89 | |
{ |
90 | 0 | File resourceFile = new File( new URI( url.toExternalForm() ) ); |
91 | 0 | if ( !alwaysExtract ) |
92 | |
{ |
93 | 0 | return resourceFile; |
94 | |
} |
95 | 0 | if ( resourceFile.isDirectory() ) |
96 | |
{ |
97 | 0 | FileUtils.copyDirectoryStructure( resourceFile, destination ); |
98 | |
} |
99 | |
else |
100 | |
{ |
101 | 0 | FileUtils.copyFile( resourceFile, destination ); |
102 | |
} |
103 | |
} |
104 | 0 | catch ( URISyntaxException e ) |
105 | |
{ |
106 | 0 | throw new RuntimeException( "Couldn't convert URL to File:" + url, e ); |
107 | 0 | } |
108 | |
} |
109 | 0 | return destination; |
110 | |
} |
111 | |
|
112 | |
private static void extractResourcePathFromJar( Class cl, File jarFile, String resourcePath, File dest ) |
113 | |
throws IOException |
114 | |
{ |
115 | 0 | ZipFile z = new ZipFile( jarFile, ZipFile.OPEN_READ ); |
116 | 0 | String zipStyleResourcePath = resourcePath.substring( 1 ) + "/"; |
117 | 0 | ZipEntry ze = z.getEntry( zipStyleResourcePath ); |
118 | 0 | if ( ze != null ) |
119 | |
{ |
120 | |
|
121 | 0 | for ( Enumeration entries = z.entries(); entries.hasMoreElements(); ) |
122 | |
{ |
123 | 0 | ze = (ZipEntry) entries.nextElement(); |
124 | 0 | if ( ze.getName().startsWith( zipStyleResourcePath ) ) |
125 | |
{ |
126 | 0 | String relativePath = ze.getName().substring( zipStyleResourcePath.length() ); |
127 | 0 | File destFile = new File( dest, relativePath ); |
128 | 0 | if ( ze.isDirectory() ) |
129 | |
{ |
130 | 0 | destFile.mkdirs(); |
131 | |
} |
132 | |
else |
133 | |
{ |
134 | 0 | FileOutputStream fos = new FileOutputStream( destFile ); |
135 | |
try |
136 | |
{ |
137 | 0 | IOUtil.copy( z.getInputStream( ze ), fos ); |
138 | |
} |
139 | |
finally |
140 | |
{ |
141 | 0 | IOUtil.close( fos ); |
142 | 0 | } |
143 | |
} |
144 | 0 | } |
145 | |
} |
146 | |
} |
147 | |
else |
148 | |
{ |
149 | 0 | FileOutputStream fos = new FileOutputStream( dest ); |
150 | |
try |
151 | |
{ |
152 | 0 | IOUtil.copy( cl.getResourceAsStream( resourcePath ), fos ); |
153 | |
} |
154 | |
finally |
155 | |
{ |
156 | 0 | IOUtil.close( fos ); |
157 | 0 | } |
158 | |
} |
159 | 0 | } |
160 | |
|
161 | |
private static File getJarFileFromUrl( URL url ) |
162 | |
{ |
163 | 0 | if ( !"jar".equalsIgnoreCase( url.getProtocol() ) ) |
164 | |
{ |
165 | 0 | throw new IllegalArgumentException( "This is not a Jar URL:" + url.toString() ); |
166 | |
} |
167 | 0 | String resourceFilePath = url.getFile(); |
168 | 0 | int index = resourceFilePath.indexOf( "!" ); |
169 | 0 | if ( index == -1 ) |
170 | |
{ |
171 | 0 | throw new RuntimeException( "Bug! " + url.toExternalForm() + " does not have a '!'" ); |
172 | |
} |
173 | 0 | String jarFileURI = resourceFilePath.substring( 0, index ); |
174 | |
try |
175 | |
{ |
176 | 0 | File jarFile = new File( new URI( jarFileURI ) ); |
177 | 0 | return jarFile; |
178 | |
} |
179 | 0 | catch ( URISyntaxException e ) |
180 | |
{ |
181 | 0 | throw new RuntimeException( "Bug! URI failed to parse: " + jarFileURI, e ); |
182 | |
} |
183 | |
|
184 | |
} |
185 | |
} |