You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@ant.apache.org by hi...@apache.org on 2012/08/21 15:36:41 UTC

svn commit: r1375539 - in /ant/ivy/core/trunk: src/java/org/apache/ivy/core/cache/ src/java/org/apache/ivy/core/report/ src/java/org/apache/ivy/core/resolve/ src/java/org/apache/ivy/plugins/parser/xml/ src/java/org/apache/ivy/plugins/resolver/ test/jav...

Author: hibou
Date: Tue Aug 21 13:36:40 2012
New Revision: 1375539

URL: http://svn.apache.org/viewvc?rev=1375539&view=rev
Log:
Add support for 'compressed' artifacts:
- add 'compression' attribute to indicate artifact which should be used uncompressed
- make the cache downloader uncompress data if requested
- path to the uncompressed data is referenced in the artifact download report

This can be useful for OSGi bundle which contains an inner classpath: they should be uncompressed before being used.
This could also be useful for managing multiple little files.

Added:
    ant/ivy/core/trunk/test/repositories/1/compression/
    ant/ivy/core/trunk/test/repositories/1/compression/module1/
    ant/ivy/core/trunk/test/repositories/1/compression/module1/ivys/
    ant/ivy/core/trunk/test/repositories/1/compression/module1/ivys/ivy-1.0.xml   (with props)
    ant/ivy/core/trunk/test/repositories/1/compression/module2/
    ant/ivy/core/trunk/test/repositories/1/compression/module2/ivys/
    ant/ivy/core/trunk/test/repositories/1/compression/module2/ivys/ivy-1.0.xml   (with props)
    ant/ivy/core/trunk/test/repositories/1/compression/module2/jars/
    ant/ivy/core/trunk/test/repositories/1/compression/module2/jars/module2-1.0.jar   (with props)
Modified:
    ant/ivy/core/trunk/src/java/org/apache/ivy/core/cache/CacheDownloadOptions.java
    ant/ivy/core/trunk/src/java/org/apache/ivy/core/cache/DefaultRepositoryCacheManager.java
    ant/ivy/core/trunk/src/java/org/apache/ivy/core/report/ArtifactDownloadReport.java
    ant/ivy/core/trunk/src/java/org/apache/ivy/core/resolve/DownloadOptions.java
    ant/ivy/core/trunk/src/java/org/apache/ivy/core/resolve/ResolveEngine.java
    ant/ivy/core/trunk/src/java/org/apache/ivy/core/resolve/ResolveOptions.java
    ant/ivy/core/trunk/src/java/org/apache/ivy/plugins/parser/xml/ivy.xsd
    ant/ivy/core/trunk/src/java/org/apache/ivy/plugins/resolver/AbstractResolver.java
    ant/ivy/core/trunk/test/java/org/apache/ivy/core/resolve/ResolveTest.java

Modified: ant/ivy/core/trunk/src/java/org/apache/ivy/core/cache/CacheDownloadOptions.java
URL: http://svn.apache.org/viewvc/ant/ivy/core/trunk/src/java/org/apache/ivy/core/cache/CacheDownloadOptions.java?rev=1375539&r1=1375538&r2=1375539&view=diff
==============================================================================
--- ant/ivy/core/trunk/src/java/org/apache/ivy/core/cache/CacheDownloadOptions.java (original)
+++ ant/ivy/core/trunk/src/java/org/apache/ivy/core/cache/CacheDownloadOptions.java Tue Aug 21 13:36:40 2012
@@ -17,23 +17,38 @@
  */
 package org.apache.ivy.core.cache;
 
-
 public class CacheDownloadOptions {
+
     private DownloadListener listener = null;
+
     private boolean force = false;
 
+    private boolean expandCompressed = false;
+
     public DownloadListener getListener() {
         return listener;
     }
+
     public CacheDownloadOptions setListener(DownloadListener listener) {
         this.listener = listener;
         return this;
     }
+
     public boolean isForce() {
         return force;
     }
+
     public CacheDownloadOptions setForce(boolean force) {
         this.force = force;
         return this;
     }
+
+    public boolean isExpandCompressed() {
+        return expandCompressed;
+    }
+
+    public CacheDownloadOptions setExpandCompressed(boolean expandCompressed) {
+        this.expandCompressed = expandCompressed;
+        return this;
+    }
 }

Modified: ant/ivy/core/trunk/src/java/org/apache/ivy/core/cache/DefaultRepositoryCacheManager.java
URL: http://svn.apache.org/viewvc/ant/ivy/core/trunk/src/java/org/apache/ivy/core/cache/DefaultRepositoryCacheManager.java?rev=1375539&r1=1375538&r2=1375539&view=diff
==============================================================================
--- ant/ivy/core/trunk/src/java/org/apache/ivy/core/cache/DefaultRepositoryCacheManager.java (original)
+++ ant/ivy/core/trunk/src/java/org/apache/ivy/core/cache/DefaultRepositoryCacheManager.java Tue Aug 21 13:36:40 2012
@@ -18,14 +18,20 @@
 package org.apache.ivy.core.cache;
 
 import java.io.File;
+import java.io.FileOutputStream;
 import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
 import java.io.UnsupportedEncodingException;
 import java.security.MessageDigest;
 import java.security.NoSuchAlgorithmException;
 import java.text.ParseException;
 import java.util.Date;
+import java.util.Enumeration;
 import java.util.Map;
 import java.util.regex.Pattern;
+import java.util.zip.ZipEntry;
+import java.util.zip.ZipFile;
 
 import org.apache.ivy.Ivy;
 import org.apache.ivy.core.IvyPatternHelper;
@@ -901,6 +907,9 @@ public class DefaultRepositoryCacheManag
                     adr.setDownloadTimeMillis(System.currentTimeMillis() - start);
                 }
             }
+            if (options.isExpandCompressed() && adr.getDownloadStatus() != DownloadStatus.FAILED) {
+                expandCompressedArtifact(artifact, adr, options);
+            }
             if (listener != null) {
                 listener.endArtifactDownload(this, artifact, adr, archiveFile);
             }
@@ -910,6 +919,81 @@ public class DefaultRepositoryCacheManag
         }
     }
 
+    private void expandCompressedArtifact(Artifact artifact, ArtifactDownloadReport adr,
+            CacheDownloadOptions options) {
+        String compression = artifact.getExtraAttribute("compression");
+        if (compression == null) {
+            // not declared as compressed, nothing to do
+            return;
+        }
+
+        // the artifact for the folder of the uncompressed data
+        DefaultArtifact uncompressed = new DefaultArtifact(artifact.getModuleRevisionId(),
+                artifact.getPublicationDate(), artifact.getName(), "_uncompressed", "");
+        adr.setUncompressedArtifact(uncompressed);
+
+        File archiveFile = getArchiveFileInCache(uncompressed, null, false);
+        if (archiveFile.exists() && !options.isForce()) {
+            adr.setUncompressedLocalDir(archiveFile);
+        } else {
+            if (compression.equals("zip") || compression.equals("jar") || compression.equals("war")) {
+                Message.info("\tUncompressing " + artifact.getId());
+                try {
+                    ZipFile zipFile = new ZipFile(adr.getLocalFile());
+                    Enumeration entries = zipFile.entries();
+                    while (entries.hasMoreElements()) {
+                        ZipEntry entry = (ZipEntry) entries.nextElement();
+                        File f = new File(archiveFile, entry.getName());
+                        Message.verbose("\t\texpanding " + entry.getName() + " to " + f);
+
+                        // create intermediary directories - sometimes zip don't add them
+                        File dirF = f.getParentFile();
+                        if (dirF != null) {
+                            dirF.mkdirs();
+                        }
+
+                        if (entry.isDirectory()) {
+                            f.mkdirs();
+                        } else {
+                            InputStream in = zipFile.getInputStream(entry);
+                            OutputStream out = new FileOutputStream(f);
+                            try {
+                                byte[] buffer = new byte[1024];
+                                int length = 0;
+                                while ((length = in.read(buffer)) >= 0) {
+                                    out.write(buffer, 0, length);
+                                }
+                            } finally {
+                                try {
+                                    in.close();
+                                } catch (IOException e) {
+                                    // ignore
+                                }
+                                try {
+                                    out.close();
+                                } catch (IOException e) {
+                                    // ignore
+                                }
+                            }
+                        }
+
+                        f.setLastModified(entry.getTime());
+                    }
+                    adr.setUncompressedLocalDir(archiveFile);
+                } catch (Exception e) {
+                    Message.debug(e);
+                    adr.setDownloadStatus(DownloadStatus.FAILED);
+                    adr.setDownloadDetails("The compressed artifact " + artifact.getId()
+                            + " could not be uncompressed (" + e.getMessage() + ")");
+                }
+            } else {
+                adr.setDownloadStatus(DownloadStatus.FAILED);
+                adr.setDownloadDetails("Compression algorithm " + compression
+                        + " is not supported, " + artifact.getId() + " won't be uncompressed");
+            }
+        }
+    }
+
     public ArtifactDownloadReport downloadRepositoryResource(final Resource resource, String name,
             String type, String extension, CacheResourceOptions options, Repository repository) {
 

Modified: ant/ivy/core/trunk/src/java/org/apache/ivy/core/report/ArtifactDownloadReport.java
URL: http://svn.apache.org/viewvc/ant/ivy/core/trunk/src/java/org/apache/ivy/core/report/ArtifactDownloadReport.java?rev=1375539&r1=1375538&r2=1375539&view=diff
==============================================================================
--- ant/ivy/core/trunk/src/java/org/apache/ivy/core/report/ArtifactDownloadReport.java (original)
+++ ant/ivy/core/trunk/src/java/org/apache/ivy/core/report/ArtifactDownloadReport.java Tue Aug 21 13:36:40 2012
@@ -55,6 +55,10 @@ public class ArtifactDownloadReport {
 
     private long downloadTimeMillis;
 
+    private File uncompressedLocalDir;
+
+    private Artifact uncompressedArtifact;
+
     public ArtifactDownloadReport(Artifact artifact) {
         this.artifact = artifact;
     }
@@ -155,6 +159,22 @@ public class ArtifactDownloadReport {
         return DownloadStatus.SUCCESSFUL == downloadStatus;
     }
 
+    public void setUncompressedArtifact(Artifact uncompressedArtifact) {
+        this.uncompressedArtifact = uncompressedArtifact;
+    }
+
+    public Artifact getUncompressedArtifact() {
+        return uncompressedArtifact;
+    }
+
+    public void setUncompressedLocalDir(File uncompressedLocalDir) {
+        this.uncompressedLocalDir = uncompressedLocalDir;
+    }
+
+    public File getUncompressedLocalDir() {
+        return uncompressedLocalDir;
+    }
+
     public int hashCode() {
         final int prime = 31;
         int result = 1;

Modified: ant/ivy/core/trunk/src/java/org/apache/ivy/core/resolve/DownloadOptions.java
URL: http://svn.apache.org/viewvc/ant/ivy/core/trunk/src/java/org/apache/ivy/core/resolve/DownloadOptions.java?rev=1375539&r1=1375538&r2=1375539&view=diff
==============================================================================
--- ant/ivy/core/trunk/src/java/org/apache/ivy/core/resolve/DownloadOptions.java (original)
+++ ant/ivy/core/trunk/src/java/org/apache/ivy/core/resolve/DownloadOptions.java Tue Aug 21 13:36:40 2012
@@ -22,6 +22,17 @@ import org.apache.ivy.core.LogOptions;
 
 public class DownloadOptions extends LogOptions {
 
+    private boolean expandCompressed;
+
     public DownloadOptions() {
     }
+
+    public DownloadOptions setExpandCompressed(boolean expandCompressed) {
+        this.expandCompressed = expandCompressed;
+        return this;
+    }
+
+    public boolean isExpandCompressed() {
+        return expandCompressed;
+    }
 }

Modified: ant/ivy/core/trunk/src/java/org/apache/ivy/core/resolve/ResolveEngine.java
URL: http://svn.apache.org/viewvc/ant/ivy/core/trunk/src/java/org/apache/ivy/core/resolve/ResolveEngine.java?rev=1375539&r1=1375538&r2=1375539&view=diff
==============================================================================
--- ant/ivy/core/trunk/src/java/org/apache/ivy/core/resolve/ResolveEngine.java (original)
+++ ant/ivy/core/trunk/src/java/org/apache/ivy/core/resolve/ResolveEngine.java Tue Aug 21 13:36:40 2012
@@ -331,8 +331,10 @@ public class ResolveEngine {
             if (options.isDownload()) {
                 Message.verbose(":: downloading artifacts ::");
 
-                downloadArtifacts(report, options.getArtifactFilter(), 
-                    (DownloadOptions) new DownloadOptions().setLog(options.getLog()));
+                DownloadOptions downloadOptions = new DownloadOptions();
+                downloadOptions.setLog(options.getLog());
+                downloadOptions.setExpandCompressed(options.isExpandCompressed());
+                downloadArtifacts(report, options.getArtifactFilter(), downloadOptions);
             }
 
             if (options.isOutputReport()) {

Modified: ant/ivy/core/trunk/src/java/org/apache/ivy/core/resolve/ResolveOptions.java
URL: http://svn.apache.org/viewvc/ant/ivy/core/trunk/src/java/org/apache/ivy/core/resolve/ResolveOptions.java?rev=1375539&r1=1375538&r2=1375539&view=diff
==============================================================================
--- ant/ivy/core/trunk/src/java/org/apache/ivy/core/resolve/ResolveOptions.java (original)
+++ ant/ivy/core/trunk/src/java/org/apache/ivy/core/resolve/ResolveOptions.java Tue Aug 21 13:36:40 2012
@@ -117,6 +117,7 @@ public class ResolveOptions extends LogO
      *  True if the resolve should compare the new resolution against the previous report 
      **/  
     private boolean checkIfChanged = false;
+    private boolean expandCompressed;
 
     public ResolveOptions() {
     }
@@ -306,6 +307,13 @@ public class ResolveOptions extends LogO
         return moduleId.getOrganisation() + "-" + moduleId.getName();
     }
 
+    public ResolveOptions setExpandCompressed(boolean expandCompressed) {
+        this.expandCompressed = expandCompressed;
+        return this;
+    }
 
+    public boolean isExpandCompressed() {
+        return expandCompressed;
+    }
 
 }

Modified: ant/ivy/core/trunk/src/java/org/apache/ivy/plugins/parser/xml/ivy.xsd
URL: http://svn.apache.org/viewvc/ant/ivy/core/trunk/src/java/org/apache/ivy/plugins/parser/xml/ivy.xsd?rev=1375539&r1=1375538&r2=1375539&view=diff
==============================================================================
--- ant/ivy/core/trunk/src/java/org/apache/ivy/plugins/parser/xml/ivy.xsd (original)
+++ ant/ivy/core/trunk/src/java/org/apache/ivy/plugins/parser/xml/ivy.xsd Tue Aug 21 13:36:40 2012
@@ -147,6 +147,7 @@
 						            <xs:attribute name="ext" type="xs:string"/>
 						            <xs:attribute name="conf" type="xs:string"/>
 						            <xs:attribute name="url" type="xs:string"/>
+                                    <xs:attribute name="compression" type="xs:string"/>
 						            <xs:anyAttribute namespace="##other" processContents="lax" />
 				            	</xs:complexType>
 				      		</xs:element>

Modified: ant/ivy/core/trunk/src/java/org/apache/ivy/plugins/resolver/AbstractResolver.java
URL: http://svn.apache.org/viewvc/ant/ivy/core/trunk/src/java/org/apache/ivy/plugins/resolver/AbstractResolver.java?rev=1375539&r1=1375538&r2=1375539&view=diff
==============================================================================
--- ant/ivy/core/trunk/src/java/org/apache/ivy/plugins/resolver/AbstractResolver.java (original)
+++ ant/ivy/core/trunk/src/java/org/apache/ivy/plugins/resolver/AbstractResolver.java Tue Aug 21 13:36:40 2012
@@ -445,7 +445,10 @@ public abstract class AbstractResolver 
     }
 
     protected CacheDownloadOptions getCacheDownloadOptions(DownloadOptions options) {
-        return new CacheDownloadOptions().setListener(getDownloadListener(options));
+        CacheDownloadOptions cacheDownloadOptions = new CacheDownloadOptions();
+        cacheDownloadOptions.setExpandCompressed(options.isExpandCompressed());
+        cacheDownloadOptions.setListener(getDownloadListener(options));
+        return cacheDownloadOptions;
     }
 
     protected DownloadOptions getDownloadOptions(ResolveOptions options) {

Modified: ant/ivy/core/trunk/test/java/org/apache/ivy/core/resolve/ResolveTest.java
URL: http://svn.apache.org/viewvc/ant/ivy/core/trunk/test/java/org/apache/ivy/core/resolve/ResolveTest.java?rev=1375539&r1=1375538&r2=1375539&view=diff
==============================================================================
--- ant/ivy/core/trunk/test/java/org/apache/ivy/core/resolve/ResolveTest.java (original)
+++ ant/ivy/core/trunk/test/java/org/apache/ivy/core/resolve/ResolveTest.java Tue Aug 21 13:36:40 2012
@@ -5433,4 +5433,28 @@ public class ResolveTest extends TestCas
         report = ivy.resolve(url, option);
         assertFalse(report.hasError());
     }
+
+    public void testCompressed() throws Exception {
+        ResolveOptions options = getResolveOptions(new String[] {"*"});
+        options.setExpandCompressed(true);
+
+        URL url = new File("test/repositories/1/compression/module1/ivys/ivy-1.0.xml").toURI().toURL();
+
+        // normal resolve, the file goes in the cache
+        ResolveReport report = ivy.resolve(url, options);
+        assertFalse(report.hasError());
+
+        ArtifactDownloadReport adr = report.getAllArtifactsReports()[0];
+        File cacheDir = ivy.getSettings().getDefaultRepositoryCacheBasedir();
+        assertEquals(new File(cacheDir, "compression/module2/jars/module2-1.0.jar"),
+            adr.getLocalFile());
+        assertEquals(new File(cacheDir, "compression/module2/_uncompresseds/module2-1.0"),
+            adr.getUncompressedLocalDir());
+
+        File[] jarContents = adr.getUncompressedLocalDir().listFiles();
+        assertEquals(new File(adr.getUncompressedLocalDir(), "META-INF"), jarContents[0]);
+        assertEquals(new File(adr.getUncompressedLocalDir(), "test.txt"), jarContents[1]);
+        assertEquals(new File(adr.getUncompressedLocalDir(), "META-INF/MANIFEST.MF"),
+            jarContents[0].listFiles()[0]);
+    }
 }

Added: ant/ivy/core/trunk/test/repositories/1/compression/module1/ivys/ivy-1.0.xml
URL: http://svn.apache.org/viewvc/ant/ivy/core/trunk/test/repositories/1/compression/module1/ivys/ivy-1.0.xml?rev=1375539&view=auto
==============================================================================
--- ant/ivy/core/trunk/test/repositories/1/compression/module1/ivys/ivy-1.0.xml (added)
+++ ant/ivy/core/trunk/test/repositories/1/compression/module1/ivys/ivy-1.0.xml Tue Aug 21 13:36:40 2012
@@ -0,0 +1,27 @@
+<!--
+   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.    
+-->
+<ivy-module version="1.0">
+    <info organisation="compression" module="module1" revision="1.0" />
+    <configurations>
+        <conf name="default" />
+    </configurations>
+    <dependencies>
+        <dependency org="compression" name="module2" rev="1.0" />
+	</dependencies>
+</ivy-module>

Propchange: ant/ivy/core/trunk/test/repositories/1/compression/module1/ivys/ivy-1.0.xml
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: ant/ivy/core/trunk/test/repositories/1/compression/module1/ivys/ivy-1.0.xml
------------------------------------------------------------------------------
    svn:keywords = Date Revision Author HeadURL Id

Propchange: ant/ivy/core/trunk/test/repositories/1/compression/module1/ivys/ivy-1.0.xml
------------------------------------------------------------------------------
    svn:mime-type = text/xml

Added: ant/ivy/core/trunk/test/repositories/1/compression/module2/ivys/ivy-1.0.xml
URL: http://svn.apache.org/viewvc/ant/ivy/core/trunk/test/repositories/1/compression/module2/ivys/ivy-1.0.xml?rev=1375539&view=auto
==============================================================================
--- ant/ivy/core/trunk/test/repositories/1/compression/module2/ivys/ivy-1.0.xml (added)
+++ ant/ivy/core/trunk/test/repositories/1/compression/module2/ivys/ivy-1.0.xml Tue Aug 21 13:36:40 2012
@@ -0,0 +1,27 @@
+<!--
+   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.    
+-->
+<ivy-module version="1.0">
+    <info organisation="compression" module="module2" revision="1.0" />
+    <configurations>
+        <conf name="default" />
+    </configurations>
+    <publications>
+        <artifact name="module2" type="jar" ext="jar" compression="jar" />
+    </publications>
+</ivy-module>

Propchange: ant/ivy/core/trunk/test/repositories/1/compression/module2/ivys/ivy-1.0.xml
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: ant/ivy/core/trunk/test/repositories/1/compression/module2/ivys/ivy-1.0.xml
------------------------------------------------------------------------------
    svn:keywords = Date Revision Author HeadURL Id

Propchange: ant/ivy/core/trunk/test/repositories/1/compression/module2/ivys/ivy-1.0.xml
------------------------------------------------------------------------------
    svn:mime-type = text/xml

Added: ant/ivy/core/trunk/test/repositories/1/compression/module2/jars/module2-1.0.jar
URL: http://svn.apache.org/viewvc/ant/ivy/core/trunk/test/repositories/1/compression/module2/jars/module2-1.0.jar?rev=1375539&view=auto
==============================================================================
Binary file - no diff available.

Propchange: ant/ivy/core/trunk/test/repositories/1/compression/module2/jars/module2-1.0.jar
------------------------------------------------------------------------------
    svn:mime-type = application/zip