Return-Path: X-Original-To: apmail-ace-commits-archive@www.apache.org Delivered-To: apmail-ace-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 35FDF9DC6 for ; Thu, 12 Apr 2012 13:58:47 +0000 (UTC) Received: (qmail 60082 invoked by uid 500); 12 Apr 2012 13:58:47 -0000 Delivered-To: apmail-ace-commits-archive@ace.apache.org Received: (qmail 60057 invoked by uid 500); 12 Apr 2012 13:58:47 -0000 Mailing-List: contact commits-help@ace.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@ace.apache.org Delivered-To: mailing list commits@ace.apache.org Received: (qmail 60048 invoked by uid 99); 12 Apr 2012 13:58:47 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 12 Apr 2012 13:58:47 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 12 Apr 2012 13:58:43 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id D9AFC2388865 for ; Thu, 12 Apr 2012 13:58:03 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1325263 - in /ace/trunk/ace-client-repository-helper-base/src/main/java/org/apache/ace/client/repository/helper/base: ArtifactPreprocessorBase.java VelocityArtifactPreprocessor.java Date: Thu, 12 Apr 2012 13:58:03 -0000 To: commits@ace.apache.org From: jawi@apache.org X-Mailer: svnmailer-1.0.8-patched Message-Id: <20120412135803.D9AFC2388865@eris.apache.org> Author: jawi Date: Thu Apr 12 13:58:03 2012 New Revision: 1325263 URL: http://svn.apache.org/viewvc?rev=1325263&view=rev Log: ACE-257: some small bugs fixed that caused sometimes the JUnits to fail. Modified: ace/trunk/ace-client-repository-helper-base/src/main/java/org/apache/ace/client/repository/helper/base/ArtifactPreprocessorBase.java ace/trunk/ace-client-repository-helper-base/src/main/java/org/apache/ace/client/repository/helper/base/VelocityArtifactPreprocessor.java Modified: ace/trunk/ace-client-repository-helper-base/src/main/java/org/apache/ace/client/repository/helper/base/ArtifactPreprocessorBase.java URL: http://svn.apache.org/viewvc/ace/trunk/ace-client-repository-helper-base/src/main/java/org/apache/ace/client/repository/helper/base/ArtifactPreprocessorBase.java?rev=1325263&r1=1325262&r2=1325263&view=diff ============================================================================== --- ace/trunk/ace-client-repository-helper-base/src/main/java/org/apache/ace/client/repository/helper/base/ArtifactPreprocessorBase.java (original) +++ ace/trunk/ace-client-repository-helper-base/src/main/java/org/apache/ace/client/repository/helper/base/ArtifactPreprocessorBase.java Thu Apr 12 13:58:03 2012 @@ -19,6 +19,8 @@ package org.apache.ace.client.repository.helper.base; import java.io.Closeable; +import java.io.File; +import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; @@ -26,6 +28,7 @@ import java.io.PipedInputStream; import java.io.PipedOutputStream; import java.net.HttpURLConnection; import java.net.MalformedURLException; +import java.net.URISyntaxException; import java.net.URL; import java.net.URLConnection; @@ -42,12 +45,13 @@ public abstract class ArtifactPreprocess /** * Uploads an artifact to an OBR. + * * @param input A inputstream from which the artifact can be read. * @param name The name of the artifact. If the name is not unique, an IOException will be thrown. * @param obrBase The base URL of the obr to which this artifact should be written. * @return A URL to the uploaded artifact; this is identical to calling determineNewUrl(name, obrBase) * @throws IOException If there was an error reading from input, or if there was a problem communicating - * with the OBR. + * with the OBR. */ protected URL upload(InputStream input, String name, URL obrBase) throws IOException { if (obrBase == null) { @@ -57,31 +61,15 @@ public abstract class ArtifactPreprocess throw new IllegalArgumentException("None of the parameters can be null."); } - OutputStream output = null; URL url = null; try { url = determineNewUrl(name, obrBase); - URLConnection connection = url.openConnection(); - connection.setDoOutput(true); - connection.setDoInput(true); - output = connection.getOutputStream(); - byte[] buffer = new byte[BUFFER_SIZE]; - for (int count = input.read(buffer); count != -1; count = input.read(buffer)) { - output.write(buffer, 0, count); + + if ("file".equals(url.getProtocol())) { + uploadToFile(input, url); } - output.close(); - if (connection instanceof HttpURLConnection) { - int responseCode = ((HttpURLConnection) connection).getResponseCode(); - switch (responseCode) { - case HttpURLConnection.HTTP_OK : - break; - case HttpURLConnection.HTTP_CONFLICT: - throw new IOException("Artifact already exists in storage."); - case HttpURLConnection.HTTP_INTERNAL_ERROR: - throw new IOException("The storage server returned an internal server error."); - default: - throw new IOException("The storage server returned code " + responseCode + " writing to " + url.toString()); - } + else { + uploadToRemote(input, url); } } catch (IOException ioe) { @@ -89,7 +77,6 @@ public abstract class ArtifactPreprocess } finally { silentlyClose(input); - silentlyClose(output); } return url; @@ -97,6 +84,7 @@ public abstract class ArtifactPreprocess /** * Gets a stream to write an artifact to, which will be uploaded to the OBR. + * * @param name The name of the artifact. * @param obrBase The base URL of the obr to which this artifact should be written. * @return An outputstream, to which the artifact can be written. @@ -142,7 +130,8 @@ public abstract class ArtifactPreprocess // We cannot signal this to the user, but he will notice (in the original thread) // that the pipe has been broken. e.printStackTrace(); - } finally { + } + finally { silentlyClose(internalInput); silentlyClose(externalOutput); } @@ -156,10 +145,10 @@ public abstract class ArtifactPreprocess return new URL(obrBase, name); } - public abstract String preprocess(String url, PropertyResolver props, String targetID, String version, URL obrBase) throws IOException; + public abstract String preprocess(String url, PropertyResolver props, String targetID, String version, URL obrBase) + throws IOException; public abstract boolean needsNewVersion(String url, PropertyResolver props, String targetID, String fromVersion); - /** * @param closable @@ -176,4 +165,76 @@ public abstract class ArtifactPreprocess } } + /** + * Uploads an artifact to a local file location. + * + * @param input the input stream of the (local) artifact to upload. + * @param url the URL of the (file) artifact to upload to. + * @throws IOException in case of I/O problems. + */ + private void uploadToFile(InputStream input, URL url) throws IOException { + File file; + try { + file = new File(url.toURI()); + } + catch (URISyntaxException e) { + file = new File(url.getPath()); + } + + OutputStream output = null; + + try { + output = new FileOutputStream(file); + + byte[] buffer = new byte[BUFFER_SIZE]; + for (int count = input.read(buffer); count != -1; count = input.read(buffer)) { + output.write(buffer, 0, count); + } + } + finally { + silentlyClose(output); + } + } + + /** + * Uploads an artifact to a remote location. + * + * @param input the input stream of the (local) artifact to upload. + * @param url the URL of the (remote) artifact to upload to. + * @throws IOException in case of I/O problems, or when the upload was refused by the remote. + */ + private void uploadToRemote(InputStream input, URL url) throws IOException { + OutputStream output = null; + + try { + URLConnection connection = url.openConnection(); + connection.setDoOutput(true); + connection.setDoInput(true); + output = connection.getOutputStream(); + + byte[] buffer = new byte[BUFFER_SIZE]; + for (int count = input.read(buffer); count != -1; count = input.read(buffer)) { + output.write(buffer, 0, count); + } + output.close(); + + if (connection instanceof HttpURLConnection) { + int responseCode = ((HttpURLConnection) connection).getResponseCode(); + switch (responseCode) { + case HttpURLConnection.HTTP_OK: + break; + case HttpURLConnection.HTTP_CONFLICT: + throw new IOException("Artifact already exists in storage."); + case HttpURLConnection.HTTP_INTERNAL_ERROR: + throw new IOException("The storage server returned an internal server error."); + default: + throw new IOException("The storage server returned code " + responseCode + " writing to " + + url.toString()); + } + } + } + finally { + silentlyClose(output); + } + } } Modified: ace/trunk/ace-client-repository-helper-base/src/main/java/org/apache/ace/client/repository/helper/base/VelocityArtifactPreprocessor.java URL: http://svn.apache.org/viewvc/ace/trunk/ace-client-repository-helper-base/src/main/java/org/apache/ace/client/repository/helper/base/VelocityArtifactPreprocessor.java?rev=1325263&r1=1325262&r2=1325263&view=diff ============================================================================== --- ace/trunk/ace-client-repository-helper-base/src/main/java/org/apache/ace/client/repository/helper/base/VelocityArtifactPreprocessor.java (original) +++ ace/trunk/ace-client-repository-helper-base/src/main/java/org/apache/ace/client/repository/helper/base/VelocityArtifactPreprocessor.java Thu Apr 12 13:58:03 2012 @@ -27,6 +27,7 @@ import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.Writer; +import java.lang.ref.Reference; import java.lang.ref.WeakReference; import java.net.MalformedURLException; import java.net.URL; @@ -49,8 +50,8 @@ public class VelocityArtifactPreprocesso private static Object m_initLock = new Object(); private static boolean m_velocityInitialized = false; - private final Map> m_cachedArtifacts; - private final Map> m_cachedHashes; + private final Map> m_cachedArtifacts; + private final Map> m_cachedHashes; private final MessageDigest m_md5; /** @@ -64,8 +65,8 @@ public class VelocityArtifactPreprocesso throw new RuntimeException("Failed to create VelocityArtifactPreprocessor instance!", e); } - m_cachedArtifacts = new ConcurrentHashMap>(); - m_cachedHashes = new ConcurrentHashMap>(); + m_cachedArtifacts = new ConcurrentHashMap>(); + m_cachedHashes = new ConcurrentHashMap>(); } @Override @@ -185,21 +186,21 @@ public class VelocityArtifactPreprocesso */ private String getHashForVersion(String url, String target, String version) { String key = createHashKey(url, target, version); - String result = null; - WeakReference ref = m_cachedHashes.get(key); - if (ref == null || ((result = ref.get()) == null)) { + Reference ref = m_cachedHashes.get(key); + String hash = (ref != null) ? ref.get() : null; + if (hash == null) { try { - result = hash(getBytesFromUrl(getFullUrl(url, target, version))); + hash = hash(getBytesFromUrl(getFullUrl(url, target, version))); - m_cachedHashes.put(key, new WeakReference(result)); + m_cachedHashes.put(key, new WeakReference(hash)); } catch (IOException e) { // we cannot retrieve the artifact, so we cannot say anything about it. } } - return result; + return hash; } /** @@ -248,7 +249,7 @@ public class VelocityArtifactPreprocesso private byte[] getArtifactAsBytes(String url) throws IOException { byte[] result = null; - WeakReference ref = m_cachedArtifacts.get(url); + Reference ref = m_cachedArtifacts.get(url); if (ref == null || ((result = ref.get()) == null)) { result = getBytesFromUrl(url); }