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 public class ResourceExtractor
37 {
38
39 public static File simpleExtractResources( Class<?> cl, String resourcePath )
40 throws IOException
41 {
42 String tempDirPath = System.getProperty( "maven.test.tmpdir", System.getProperty( "java.io.tmpdir" ) );
43 File tempDir = new File( tempDirPath );
44
45 File testDir = new File( tempDir, resourcePath );
46
47 FileUtils.deleteDirectory( testDir );
48
49 testDir = ResourceExtractor.extractResourcePath( cl, resourcePath, tempDir, false );
50 return testDir;
51 }
52
53 public static File extractResourcePath( String resourcePath, File dest )
54 throws IOException
55 {
56 return extractResourcePath( ResourceExtractor.class, resourcePath, dest );
57 }
58
59 public static File extractResourcePath( Class<?> cl, String resourcePath, File dest )
60 throws IOException
61 {
62 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 File dest = new File( tempDir, resourcePath );
69 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 URL url = cl.getResource( resourcePath );
77 if ( url == null )
78 {
79 throw new IllegalArgumentException( "Resource not found: " + resourcePath );
80 }
81 if ( "jar".equalsIgnoreCase( url.getProtocol() ) )
82 {
83 File jarFile = getJarFileFromUrl( url );
84 extractResourcePathFromJar( cl, jarFile, resourcePath, destination );
85 }
86 else
87 {
88 try
89 {
90 File resourceFile = new File( new URI( url.toExternalForm() ) );
91 if ( !alwaysExtract )
92 {
93 return resourceFile;
94 }
95 if ( resourceFile.isDirectory() )
96 {
97 FileUtils.copyDirectoryStructure( resourceFile, destination );
98 }
99 else
100 {
101 FileUtils.copyFile( resourceFile, destination );
102 }
103 }
104 catch ( URISyntaxException e )
105 {
106 throw new RuntimeException( "Couldn't convert URL to File:" + url, e );
107 }
108 }
109 return destination;
110 }
111
112 private static void extractResourcePathFromJar( Class<?> cl, File jarFile, String resourcePath, File dest )
113 throws IOException
114 {
115 ZipFile z = new ZipFile( jarFile, ZipFile.OPEN_READ );
116 String zipStyleResourcePath = resourcePath.substring( 1 ) + "/";
117 ZipEntry ze = z.getEntry( zipStyleResourcePath );
118 if ( ze != null )
119 {
120
121 for ( Enumeration<? extends ZipEntry> entries = z.entries(); entries.hasMoreElements(); )
122 {
123 ze = entries.nextElement();
124 if ( ze.getName().startsWith( zipStyleResourcePath ) )
125 {
126 String relativePath = ze.getName().substring( zipStyleResourcePath.length() );
127 File destFile = new File( dest, relativePath );
128 if ( ze.isDirectory() )
129 {
130 destFile.mkdirs();
131 }
132 else
133 {
134 FileOutputStream fos = new FileOutputStream( destFile );
135 try
136 {
137 IOUtil.copy( z.getInputStream( ze ), fos );
138 }
139 finally
140 {
141 IOUtil.close( fos );
142 }
143 }
144 }
145 }
146 }
147 else
148 {
149 FileOutputStream fos = new FileOutputStream( dest );
150 try
151 {
152 IOUtil.copy( cl.getResourceAsStream( resourcePath ), fos );
153 }
154 finally
155 {
156 IOUtil.close( fos );
157 }
158 }
159 }
160
161 private static File getJarFileFromUrl( URL url )
162 {
163 if ( !"jar".equalsIgnoreCase( url.getProtocol() ) )
164 {
165 throw new IllegalArgumentException( "This is not a Jar URL:" + url.toString() );
166 }
167 String resourceFilePath = url.getFile();
168 int index = resourceFilePath.indexOf( "!" );
169 if ( index == -1 )
170 {
171 throw new RuntimeException( "Bug! " + url.toExternalForm() + " does not have a '!'" );
172 }
173 String jarFileURI = resourceFilePath.substring( 0, index );
174 try
175 {
176 File jarFile = new File( new URI( jarFileURI ) );
177 return jarFile;
178 }
179 catch ( URISyntaxException e )
180 {
181 throw new RuntimeException( "Bug! URI failed to parse: " + jarFileURI, e );
182 }
183
184 }
185 }