You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by jv...@apache.org on 2008/07/27 03:25:45 UTC
svn commit: r680058 - in /maven/shared/branches/embedder-verifier: pom.xml
src/main/java/org/apache/maven/it/ResourceExtractor.java
src/main/java/org/apache/maven/it/util/
Author: jvanzyl
Date: Sat Jul 26 18:25:45 2008
New Revision: 680058
URL: http://svn.apache.org/viewvc?rev=680058&view=rev
Log:
o use plexus utils
Added:
maven/shared/branches/embedder-verifier/src/main/java/org/apache/maven/it/ResourceExtractor.java (with props)
maven/shared/branches/embedder-verifier/src/main/java/org/apache/maven/it/util/
Modified:
maven/shared/branches/embedder-verifier/pom.xml
Modified: maven/shared/branches/embedder-verifier/pom.xml
URL: http://svn.apache.org/viewvc/maven/shared/branches/embedder-verifier/pom.xml?rev=680058&r1=680057&r2=680058&view=diff
==============================================================================
--- maven/shared/branches/embedder-verifier/pom.xml (original)
+++ maven/shared/branches/embedder-verifier/pom.xml Sat Jul 26 18:25:45 2008
@@ -1,23 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
-<!--
- Licensed to the Apache Software Foundation (ASF) under one
- or more contributor license agreements. See the NOTICE file
- distributed with this work for additional information
- regarding copyright ownership. The ASF licenses this file
- to you under the Apache License, Version 2.0 (the
- "License"); you may not use this file except in compliance
- with the License. You may obtain a copy of the License at
-
- http://www.apache.org/licenses/LICENSE-2.0
-
- Unless required by applicable law or agreed to in writing,
- software distributed under the License is distributed on an
- "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- KIND, either express or implied. See the License for the
- specific language governing permissions and limitations
- under the License.
--->
-<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+ <!--
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to you under the Apache License, Version
+ 2.0 (the "License"); you may not use this file except in compliance
+ with the License. You may obtain a copy of the License at
+ http://www.apache.org/licenses/LICENSE-2.0 Unless required by
+ applicable law or agreed to in writing, software distributed under
+ the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES
+ OR CONDITIONS OF ANY KIND, either express or implied. See the
+ License for the specific language governing permissions and
+ limitations under the License.
+ -->
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.apache.maven.shared</groupId>
@@ -33,10 +30,15 @@
<artifactId>junit</artifactId>
<version>3.8.1</version>
</dependency>
+ <dependency>
+ <groupId>org.codehaus.plexus</groupId>
+ <artifactId>plexus-utils</artifactId>
+ <version>1.5.4</version>
+ </dependency>
</dependencies>
<scm>
<connection>scm:svn:http://svn.apache.org/repos/asf/maven/shared/trunk/maven-verifier</connection>
<developerConnection>scm:svn:https://svn.apache.org/repos/asf/maven/shared/trunk/maven-verifier</developerConnection>
<url>http://svn.apache.org/viewcvs.cgi/maven/shared/trunk/maven-verifier</url>
</scm>
-</project>
+</project>
\ No newline at end of file
Added: maven/shared/branches/embedder-verifier/src/main/java/org/apache/maven/it/ResourceExtractor.java
URL: http://svn.apache.org/viewvc/maven/shared/branches/embedder-verifier/src/main/java/org/apache/maven/it/ResourceExtractor.java?rev=680058&view=auto
==============================================================================
--- maven/shared/branches/embedder-verifier/src/main/java/org/apache/maven/it/ResourceExtractor.java (added)
+++ maven/shared/branches/embedder-verifier/src/main/java/org/apache/maven/it/ResourceExtractor.java Sat Jul 26 18:25:45 2008
@@ -0,0 +1,153 @@
+package org.apache.maven.it;
+
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.net.URL;
+import java.util.Enumeration;
+import java.util.zip.ZipEntry;
+import java.util.zip.ZipFile;
+
+import org.codehaus.plexus.util.FileUtils;
+import org.codehaus.plexus.util.IOUtil;
+
+/* @todo this can be replaced with plexus-archiver */
+public class ResourceExtractor
+{
+
+ public static File simpleExtractResources( Class cl, String resourcePath )
+ throws IOException
+ {
+ String tempDirPath = System.getProperty( "maven.test.tmpdir", System.getProperty( "java.io.tmpdir" ) );
+ File tempDir = new File( tempDirPath );
+
+ File testDir = new File( tempDir, resourcePath );
+
+ FileUtils.deleteDirectory( testDir );
+
+ testDir = ResourceExtractor.extractResourcePath( cl, resourcePath, tempDir, false );
+ return testDir;
+ }
+
+ public static File extractResourcePath( String resourcePath, File dest )
+ throws IOException
+ {
+ return extractResourcePath( ResourceExtractor.class, resourcePath, dest );
+ }
+
+ public static File extractResourcePath( Class cl, String resourcePath, File dest )
+ throws IOException
+ {
+ return extractResourcePath( cl, resourcePath, dest, false );
+ }
+
+ public static File extractResourcePath( Class cl, String resourcePath, File tempDir, boolean alwaysExtract )
+ throws IOException
+ {
+ File dest = new File( tempDir, resourcePath );
+ URL url = cl.getResource( resourcePath );
+ if ( url == null )
+ throw new IllegalArgumentException( "Resource not found: " + resourcePath );
+ if ( "jar".equalsIgnoreCase( url.getProtocol() ) )
+ {
+ File jarFile = getJarFileFromUrl( url );
+ extractResourcePathFromJar( cl, jarFile, resourcePath, dest );
+ }
+ else
+ {
+ try
+ {
+ File resourceFile = new File( new URI( url.toExternalForm() ) );
+ if ( !alwaysExtract )
+ return resourceFile;
+ if ( resourceFile.isDirectory() )
+ {
+ FileUtils.copyDirectoryStructure( resourceFile, dest );
+ }
+ else
+ {
+ FileUtils.copyFile( resourceFile, dest );
+ }
+ }
+ catch ( URISyntaxException e )
+ {
+ throw new RuntimeException( "Couldn't convert URL to File:" + url, e );
+ }
+ }
+ return dest;
+ }
+
+ private static void extractResourcePathFromJar( Class cl, File jarFile, String resourcePath, File dest )
+ throws IOException
+ {
+ ZipFile z = new ZipFile( jarFile, ZipFile.OPEN_READ );
+ String zipStyleResourcePath = resourcePath.substring( 1 ) + "/";
+ ZipEntry ze = z.getEntry( zipStyleResourcePath );
+ if ( ze != null )
+ {
+ // DGF If it's a directory, then we need to look at all the entries
+ for ( Enumeration entries = z.entries(); entries.hasMoreElements(); )
+ {
+ ze = (ZipEntry) entries.nextElement();
+ if ( ze.getName().startsWith( zipStyleResourcePath ) )
+ {
+ String relativePath = ze.getName().substring( zipStyleResourcePath.length() );
+ File destFile = new File( dest, relativePath );
+ if ( ze.isDirectory() )
+ {
+ destFile.mkdirs();
+ }
+ else
+ {
+ FileOutputStream fos = new FileOutputStream( destFile );
+ try
+ {
+ IOUtil.copy( z.getInputStream( ze ), fos );
+ }
+ finally
+ {
+ IOUtil.close( fos );
+ }
+ }
+ }
+ }
+ }
+ else
+ {
+ FileOutputStream fos = new FileOutputStream( dest );
+ try
+ {
+ IOUtil.copy( cl.getResourceAsStream( resourcePath ), fos );
+ }
+ finally
+ {
+ IOUtil.close( fos );
+ }
+ }
+ }
+
+ private static File getJarFileFromUrl( URL url )
+ {
+ if ( !"jar".equalsIgnoreCase( url.getProtocol() ) )
+ throw new IllegalArgumentException( "This is not a Jar URL:" + url.toString() );
+ String resourceFilePath = url.getFile();
+ int index = resourceFilePath.indexOf( "!" );
+ if ( index == -1 )
+ {
+ throw new RuntimeException( "Bug! " + url.toExternalForm() + " does not have a '!'" );
+ }
+ String jarFileURI = resourceFilePath.substring( 0, index );
+ try
+ {
+ File jarFile = new File( new URI( jarFileURI ) );
+ return jarFile;
+ }
+ catch ( URISyntaxException e )
+ {
+ throw new RuntimeException( "Bug! URI failed to parse: " + jarFileURI, e );
+ }
+
+ }
+}
Propchange: maven/shared/branches/embedder-verifier/src/main/java/org/apache/maven/it/ResourceExtractor.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: maven/shared/branches/embedder-verifier/src/main/java/org/apache/maven/it/ResourceExtractor.java
------------------------------------------------------------------------------
svn:keywords = "Author Date Id Revision"