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
<code>determineNewUrl(name, obrBase)</code>
* @throws IOException If there was an error reading from
<code>input</code>, 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<String, WeakReference<byte[]>> m_cachedArtifacts;
- private final Map<String, WeakReference<String>> m_cachedHashes;
+ private final Map<String, Reference<byte[]>> m_cachedArtifacts;
+ private final Map<String, Reference<String>> 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<String,
WeakReference<byte[]>>();
- m_cachedHashes = new ConcurrentHashMap<String,
WeakReference<String>>();
+ m_cachedArtifacts = new ConcurrentHashMap<String, Reference<byte[]>>();
+ m_cachedHashes = new ConcurrentHashMap<String, Reference<String>>();
}
@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<String> ref = m_cachedHashes.get(key);
- if (ref == null || ((result = ref.get()) == null)) {
+ Reference<String> 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<String>(result));
+ m_cachedHashes.put(key, new WeakReference<String>(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<byte[]> ref = m_cachedArtifacts.get(url);
+ Reference<byte[]> ref = m_cachedArtifacts.get(url);
if (ref == null || ((result = ref.get()) == null)) {
result = getBytesFromUrl(url);
}