You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ace.apache.org by ma...@apache.org on 2014/03/06 16:55:30 UTC

svn commit: r1574933 - in /ace/trunk/org.apache.ace.gogo/src/org/apache/ace/gogo/repo: ContinuousDeployer.java DeployerUtil.java

Author: marrs
Date: Thu Mar  6 15:55:30 2014
New Revision: 1574933

URL: http://svn.apache.org/r1574933
Log:
Cleaned up some print statements. Optimized the code that diffs two files to stop at the first byte that differs instead of always hashing the whole contents of both files. Ensured that the getBundleWithNewVersion properly cleans up on exceptions.

Modified:
    ace/trunk/org.apache.ace.gogo/src/org/apache/ace/gogo/repo/ContinuousDeployer.java
    ace/trunk/org.apache.ace.gogo/src/org/apache/ace/gogo/repo/DeployerUtil.java

Modified: ace/trunk/org.apache.ace.gogo/src/org/apache/ace/gogo/repo/ContinuousDeployer.java
URL: http://svn.apache.org/viewvc/ace/trunk/org.apache.ace.gogo/src/org/apache/ace/gogo/repo/ContinuousDeployer.java?rev=1574933&r1=1574932&r2=1574933&view=diff
==============================================================================
--- ace/trunk/org.apache.ace.gogo/src/org/apache/ace/gogo/repo/ContinuousDeployer.java (original)
+++ ace/trunk/org.apache.ace.gogo/src/org/apache/ace/gogo/repo/ContinuousDeployer.java Thu Mar  6 15:55:30 2014
@@ -30,10 +30,9 @@ import aQute.bnd.deployer.repository.Fix
 import aQute.bnd.service.Strategy;
 
 public class ContinuousDeployer {
-
-    FixedIndexedRepo m_deploymentRepo;
-    FixedIndexedRepo m_developmentRepo;
-    FixedIndexedRepo m_releaseRepo;
+    final FixedIndexedRepo m_deploymentRepo;
+    final FixedIndexedRepo m_developmentRepo;
+    final FixedIndexedRepo m_releaseRepo;
 
     public ContinuousDeployer(FixedIndexedRepo deploymentRepo, FixedIndexedRepo developmentRepo, FixedIndexedRepo releaseRepo) {
         m_deploymentRepo = deploymentRepo;
@@ -44,7 +43,7 @@ public class ContinuousDeployer {
     /**
      * Deploys all resources from the development repository into the deployment repository.
      * 
-     * @return
+     * @return a list of deployed resources
      * @throws Exception
      */
     public List<Resource> deployResources() throws Exception {
@@ -86,15 +85,15 @@ public class ContinuousDeployer {
     private Resource deployReleasedResource(Resource releasedResource) throws Exception {
         List<Resource> deployedResources = findResources(m_deploymentRepo, getIdentityVersionRequirement(releasedResource));
         if (deployedResources.size() == 0) {
-            System.out.println("Uploading released resource:  " + getString(releasedResource));
+            System.out.println("Uploading released resource: " + getString(releasedResource));
             List<Resource> copied = copyResources(m_releaseRepo, m_deploymentRepo, getIdentityVersionRequirement(releasedResource));
             if (copied.size() != 1) {
-                throw new IllegalStateException("Exepected 1 result ofter copy");
+                throw new IllegalStateException("Expected one result after copy: " + getString(releasedResource));
             }
             return copied.get(0);
         }
         else {
-            System.out.println("Released resource allready deployed:  " + getString(releasedResource));
+            System.out.println("Released resource already deployed: " + getString(releasedResource));
             return deployedResources.get(0);
         }
     }
@@ -109,16 +108,15 @@ public class ContinuousDeployer {
      * @throws Exception
      */
     private Resource deploySnapshotResource(Resource developmentResource) throws Exception {
-
         Version releasedBaseVersion = getReleasedBaseVersion(developmentResource);
         Resource snapshotResource = getHighestSnapshotResource(developmentResource, releasedBaseVersion);
 
         if (snapshotResource == null) {
-            System.out.println("Uploading initial snapshot:  " + getString(developmentResource) + " -> " + getNextSnapshotVersion(releasedBaseVersion));
+            System.out.println("Uploading initial snapshot: " + getString(developmentResource) + " -> " + getNextSnapshotVersion(releasedBaseVersion));
             return deploySnapshotResource(developmentResource, getNextSnapshotVersion(releasedBaseVersion));
         }
 
-        System.out.println("Found existing snapshot:  " + getString(snapshotResource));
+        System.out.println("Found existing snapshot: " + getString(snapshotResource));
         if (getIdentity(developmentResource).equals("com.google.guava")) {
             // FIXME workaround for BND#374
             System.out.println("Skipping snapshot diff on Google Guava to work around https://github.com/bndtools/bnd/issues/374");
@@ -144,11 +142,11 @@ public class ContinuousDeployer {
         }
 
         if (snapshotModified) {
-            System.out.println("Uploading new snapshot:  " + getString(developmentResource) + " -> " + getNextSnapshotVersion(getVersion(snapshotResource)));
+            System.out.println("Uploading new snapshot: " + getString(developmentResource) + " -> " + getNextSnapshotVersion(getVersion(snapshotResource)));
             return deploySnapshotResource(developmentResource, getNextSnapshotVersion(getVersion(snapshotResource)));
         }
         else {
-            System.out.println("Ignoring new snapshot:  " + getString(developmentResource));
+            System.out.println("Ignoring new snapshot: " + getString(developmentResource));
             List<Resource> resultResources = findResources(m_deploymentRepo, getIdentityVersionRequirement(snapshotResource));
             if (resultResources == null || resultResources.size() == 0) {
                 throw new IllegalStateException("Can not find target resource after put: " + developmentResource);
@@ -158,7 +156,6 @@ public class ContinuousDeployer {
     }
 
     private Resource deploySnapshotResource(Resource resource, Version snapshotVersion) throws Exception {
-
         File file = m_developmentRepo.get(getIdentity(resource), getVersion(resource).toString(), Strategy.EXACT, null);
         if (getType(resource).equals("osgi.bundle") || getType(resource).equals("osgi.fragment")) {
             file = getBundleWithNewVersion(file, snapshotVersion.toString());
@@ -185,13 +182,13 @@ public class ContinuousDeployer {
 
         }
         finally {
-            if (input != null)
+            if (input != null) {
                 input.close();
+            }
         }
     }
 
     private Resource getHighestSnapshotResource(Resource resource, Version base) throws Exception {
-
         List<Resource> resources = findResources(m_deploymentRepo, getIdentity(resource));
         Resource matchedResource = null;
         for (Resource candidateResource : resources) {
@@ -201,7 +198,6 @@ public class ContinuousDeployer {
                 matchedResource = candidateResource;
             }
         }
-
         return matchedResource;
     }
 

Modified: ace/trunk/org.apache.ace.gogo/src/org/apache/ace/gogo/repo/DeployerUtil.java
URL: http://svn.apache.org/viewvc/ace/trunk/org.apache.ace.gogo/src/org/apache/ace/gogo/repo/DeployerUtil.java?rev=1574933&r1=1574932&r2=1574933&view=diff
==============================================================================
--- ace/trunk/org.apache.ace.gogo/src/org/apache/ace/gogo/repo/DeployerUtil.java (original)
+++ ace/trunk/org.apache.ace.gogo/src/org/apache/ace/gogo/repo/DeployerUtil.java Thu Mar  6 15:55:30 2014
@@ -23,6 +23,7 @@ import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileOutputStream;
 import java.io.IOException;
+import java.io.InputStream;
 import java.io.StringReader;
 import java.nio.ByteBuffer;
 import java.security.DigestInputStream;
@@ -115,46 +116,35 @@ public final class DeployerUtil {
     }
 
     /**
-     * Check if there is a diff between two arbitrary files.
+     * Check if there is a difference between two arbitrary files.
      * 
-     * @param first
-     *            The first file
-     * @param second
-     *            The second file
+     * @param first the first file
+     * @param second the second file
      * @return <code>true</code> if there is a difference, otherwise <code>false</code>
-     * @throws Exception
-     *             On failure
+     * @throws Exception on failure
      */
     public static boolean filesDiffer(File first, File second) throws Exception {
-
         if (first.length() != second.length()) {
             return true;
         }
-
-        DigestInputStream stream = null;
-        byte[] firstHash = null;
-        byte[] secondHash = null;
-
+        InputStream firstStream = new FileInputStream(first);
+        InputStream secondStream = new FileInputStream(second);
         try {
-            stream = new DigestInputStream(new FileInputStream(first), MessageDigest.getInstance("MD5"));
-            while (stream.read() != -1) {
+            for (int i = 0; i < first.length(); i++) {
+                if (firstStream.read() != secondStream.read()) {
+                    return false;
+                }
             }
-            firstHash = stream.getMessageDigest().digest();
+            return true;
         }
         finally {
-            stream.close();
-        }
-
-        try {
-            stream = new DigestInputStream(new FileInputStream(second), MessageDigest.getInstance("MD5"));
-            while (stream.read() != -1) {
+            try {
+                firstStream.close();
+            }
+            finally {
+                secondStream.close();
             }
-            secondHash = stream.getMessageDigest().digest();
-        }
-        finally {
-            stream.close();
         }
-        return !Arrays.equals(firstHash, secondHash);
     }
 
     /**
@@ -169,45 +159,67 @@ public final class DeployerUtil {
      *             On failure
      */
     public static File getBundleWithNewVersion(File sourceJar, String version) throws IOException {
-
+        boolean deleteTargetFile = false;
         File targetFile = File.createTempFile("bundle", ".jar");
         byte[] buf = new byte[1024];
         ZipInputStream zin = new ZipInputStream(new FileInputStream(sourceJar));
         ZipOutputStream out = new ZipOutputStream(new FileOutputStream(targetFile));
-        ZipEntry entry = zin.getNextEntry();
-        while (entry != null) {
-            String name = entry.getName();
-            out.putNextEntry(new ZipEntry(name));
-
-            if (name.equals("META-INF/MANIFEST.MF")) {
-                // FIXME quick abort
-                ByteBuffer bb = ByteBuffer.allocate(100 * 1024);
-                int len;
-                while ((len = zin.read(buf)) > 0) {
-                    bb.put(buf, 0, len);
-                }
-
-                BufferedReader r = new BufferedReader(new StringReader(new String(bb.array(), 0, bb.position())));
-                String line;
-                while ((line = r.readLine()) != null) {
-                    if (line.startsWith("Bundle-Version:")) {
-                        out.write(("Bundle-Version: " + version + "\r\n").getBytes());
+        try {
+            ZipEntry entry = zin.getNextEntry();
+            while (entry != null) {
+                String name = entry.getName();
+                out.putNextEntry(new ZipEntry(name));
+    
+                if (name.equals("META-INF/MANIFEST.MF")) {
+                    // FIXME quick abort
+                    long manifestSize = entry.getSize();
+                    if (manifestSize > Integer.MAX_VALUE) {
+                        throw new IOException("Cannot handle extremely large manifest!");
+                    }
+                    ByteBuffer bb = ByteBuffer.allocate(manifestSize > 0 ? (int) (manifestSize + 1024) : 1024 * 1024);
+                    int len;
+                    while ((len = zin.read(buf)) > 0) {
+                        bb.put(buf, 0, len);
                     }
-                    else {
-                        out.write((line + "\r\n").getBytes());
+    
+                    BufferedReader r = new BufferedReader(new StringReader(new String(bb.array(), 0, bb.position())));
+                    String line;
+                    while ((line = r.readLine()) != null) {
+                        if (line.startsWith("Bundle-Version:")) {
+                            out.write(("Bundle-Version: " + version + "\r\n").getBytes());
+                        }
+                        else {
+                            out.write((line + "\r\n").getBytes());
+                        }
                     }
                 }
+                else {
+                    int len;
+                    while ((len = zin.read(buf)) > 0) {
+                        out.write(buf, 0, len);
+                    }
+                }
+                entry = zin.getNextEntry();
+            }
+        }
+        catch (IOException e) {
+            // if something went wrong, we are throwing an exception and the
+            // target file we created can be deleted (done in 'finally' after the
+            // streams are closed)
+            deleteTargetFile = true;
+            throw e;
+        }
+        finally {
+            try {
+                zin.close();
             }
-            else {
-                int len;
-                while ((len = zin.read(buf)) > 0) {
-                    out.write(buf, 0, len);
+            finally {
+                out.close();
+                if (deleteTargetFile) {
+                    targetFile.delete();
                 }
             }
-            entry = zin.getNextEntry();
         }
-        zin.close();
-        out.close();
         return targetFile;
     }