View Javadoc

1   package org.apache.maven.it.util;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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  /* @todo this can be replaced with plexus-archiver */
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             // DGF If it's a directory, then we need to look at all the entries
121             for ( Enumeration entries = z.entries(); entries.hasMoreElements(); )
122             {
123                 ze = (ZipEntry) 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 }