Issue 144: replaced exception handling with guava's Throwables methods

git-svn-id: http://jclouds.googlecode.com/svn/trunk@2624 3d8758e0-26b5-11de-8745-db77d3ebf521
This commit is contained in:
adrian.f.cole 2010-01-10 05:07:42 +00:00
parent 480fc9b31a
commit e69831b1a9
31 changed files with 118 additions and 177 deletions

View File

@ -31,7 +31,8 @@ import org.jclouds.blobstore.internal.BlobRuntimeException;
import org.jclouds.blobstore.options.ListContainerOptions;
import org.jclouds.blobstore.strategy.ContainsValueInListStrategy;
import org.jclouds.blobstore.strategy.ListBlobMetadataStrategy;
import org.jclouds.util.Utils;
import com.google.common.base.Throwables;
/**
* Searches Content-MD5 tag for the value associated with the value
@ -64,7 +65,7 @@ public class FindMD5InUserMetadata implements ContainsValueInListStrategy {
}
return false;
} catch (Exception e) {
Utils.<BlobRuntimeException> rethrowIfRuntimeOrSameType(e);
Throwables.propagateIfPossible(e, BlobRuntimeException.class);
throw new BlobRuntimeException(String.format(
"Error searching for ETAG of value: [%2$s] in container:%1$s", containerName,
value), e);

View File

@ -42,6 +42,7 @@ import org.jclouds.util.Utils;
import com.google.common.base.Function;
import com.google.common.base.Supplier;
import com.google.common.base.Throwables;
import com.google.common.collect.Sets;
import com.google.common.util.concurrent.Futures;
import com.google.inject.Inject;
@ -125,7 +126,7 @@ public class RecursiveRemove implements ClearListStrategy, ClearContainerStrateg
isdeleted.get(requestTimeoutMilliseconds, TimeUnit.MILLISECONDS);
}
} catch (Exception e) {
Utils.<BlobRuntimeException> rethrowIfRuntimeOrSameType(e);
Throwables.propagateIfPossible(e, BlobRuntimeException.class);
throw new BlobRuntimeException("Error deleting path: " + path, e);
}
}

View File

@ -34,6 +34,8 @@ import org.jclouds.http.HttpResponseException;
import org.jclouds.logging.Logger;
import org.jclouds.util.Utils;
import com.google.common.base.Throwables;
/**
* This will parse and set an appropriate exception on the command object.
*
@ -76,14 +78,14 @@ public class ParseAtmosStorageErrorFromXmlContent implements HttpErrorHandler {
}
} catch (Exception he) {
command.setException(new HttpResponseException(command, response, content));
Utils.rethrowIfRuntime(he);
Throwables.propagateIfPossible(he);
}
} else {
command.setException(new HttpResponseException(command, response));
}
} catch (Exception e) {
command.setException(new HttpResponseException(command, response));
Utils.rethrowIfRuntime(e);
Throwables.propagateIfPossible(e);
}
}

View File

@ -31,6 +31,8 @@ import org.jclouds.http.HttpResponseException;
import org.jclouds.logging.Logger;
import org.jclouds.util.Utils;
import com.google.common.base.Throwables;
/**
* This will parse and set an appropriate exception on the command object.
*
@ -64,15 +66,14 @@ public class ParseAWSErrorFromXmlContent implements HttpErrorHandler {
}
} catch (Exception he) {
command.setException(new HttpResponseException(command, response, content));
Utils.rethrowIfRuntime(he);
Throwables.propagateIfPossible(he);
}
} else if (response.getStatusCode() == 404) {
command.setException(new HttpResponseException(command, response));
}
} catch (Exception e) {
command.setException(new HttpResponseException(command, response));
Utils.rethrowIfRuntime(e);
Throwables.propagateIfPossible(e);
}
}
}

View File

@ -33,8 +33,8 @@ import org.jclouds.aws.s3.domain.AccessControlList.GroupGrantee;
import org.jclouds.aws.s3.reference.S3Constants;
import org.jclouds.http.HttpRequest;
import org.jclouds.rest.Binder;
import org.jclouds.util.Utils;
import com.google.common.base.Throwables;
import com.jamesmurty.utils.XMLBuilder;
/**
@ -53,7 +53,7 @@ public class BindACLToXMLPayload implements Binder {
request.getHeaders().put(HttpHeaders.CONTENT_LENGTH, stringPayload.getBytes().length + "");
request.setPayload(stringPayload);
} catch (Exception e) {
Utils.rethrowIfRuntime(e);
Throwables.propagateIfPossible(e);
throw new RuntimeException("error transforming acl: " + from, e);
}
}

View File

@ -33,8 +33,8 @@ import org.jclouds.aws.s3.domain.AccessControlList.GroupGrantee;
import org.jclouds.aws.s3.reference.S3Constants;
import org.jclouds.http.HttpRequest;
import org.jclouds.rest.Binder;
import org.jclouds.util.Utils;
import com.google.common.base.Throwables;
import com.jamesmurty.utils.XMLBuilder;
/**
@ -53,7 +53,7 @@ public class BindBucketLoggingToXmlPayload implements Binder {
request.getHeaders().put(HttpHeaders.CONTENT_LENGTH, stringPayload.getBytes().length + "");
request.setPayload(stringPayload);
} catch (Exception e) {
Utils.rethrowIfRuntime(e);
Throwables.propagateIfPossible(e);
throw new RuntimeException("error transforming bucketLogging: " + from, e);
}
}

View File

@ -56,7 +56,7 @@ import com.google.inject.Injector;
*
* @author Adrian Cole
*/
@Test(groups = "live", enabled = true, sequential = true, testName = "ec2.EC2ComputeServiceLiveTest")
@Test(groups = "live", enabled = false, sequential = true, testName = "ec2.EC2ComputeServiceLiveTest")
public class EC2ComputeServiceLiveTest {
private EC2ComputeService client;
@ -80,7 +80,7 @@ public class EC2ComputeServiceLiveTest {
}
public void testCreate() throws Exception {
server = client.createServer(serverPrefix, Profile.SMALLEST, Image.CENTOS_53);
server = client.createServer(serverPrefix, Profile.SMALLEST, Image.RHEL_53);
assertNotNull(server.getId());
assertEquals(server.getLoginPort(), 22);
assertEquals(server.getLoginType(), LoginType.SSH);

View File

@ -52,6 +52,7 @@ import org.jclouds.http.HttpResponseException;
import org.jclouds.util.Utils;
import org.testng.annotations.Test;
import com.google.common.base.Throwables;
import com.google.common.collect.Maps;
/**
@ -153,7 +154,7 @@ public class S3ClientLiveTest extends BaseBlobStoreIntegrationTest<S3AsyncClient
assertFalse(acl.hasPermission(GroupGranteeURI.ALL_USERS, Permission.WRITE_ACP));
assertFalse(acl.hasPermission(GroupGranteeURI.ALL_USERS, Permission.FULL_CONTROL));
} catch (Exception e) {
Utils.<RuntimeException> rethrowIfRuntimeOrSameType(e);
Throwables.propagateIfPossible(e);
}
}
});
@ -252,7 +253,7 @@ public class S3ClientLiveTest extends BaseBlobStoreIntegrationTest<S3AsyncClient
assertTrue(acl.hasPermission(ownerId, Permission.FULL_CONTROL));
assertTrue(acl.hasPermission(GroupGranteeURI.ALL_USERS, Permission.READ));
} catch (Exception e) {
Utils.<RuntimeException> rethrowIfRuntimeOrSameType(e);
Throwables.propagateIfPossible(e);
}
}
});

View File

@ -25,7 +25,8 @@ import static org.jclouds.aws.s3.options.ListBucketOptions.Builder.withPrefix;
import static org.jclouds.aws.s3.options.PutBucketOptions.Builder.createIn;
import static org.jclouds.aws.s3.options.PutBucketOptions.Builder.withBucketAcl;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.*;
import static org.testng.Assert.assertNull;
import static org.testng.Assert.assertTrue;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
@ -55,6 +56,7 @@ import org.jclouds.blobstore.integration.internal.BaseBlobStoreIntegrationTest;
import org.jclouds.util.Utils;
import org.testng.annotations.Test;
import com.google.common.base.Throwables;
import com.google.common.collect.ImmutableSet;
/**
@ -192,7 +194,7 @@ public class BucketsLiveTest extends BaseBlobStoreIntegrationTest<S3AsyncClient,
assertEquals(Payer.REQUESTER, context.getApi().getBucketPayer(bucketName));
} catch (Exception e) {
Utils.<RuntimeException> rethrowIfRuntimeOrSameType(e);
Throwables.propagateIfPossible(e);
}
}
});
@ -201,9 +203,8 @@ public class BucketsLiveTest extends BaseBlobStoreIntegrationTest<S3AsyncClient,
public void run() {
try {
assertEquals(Payer.BUCKET_OWNER, context.getApi().getBucketPayer(bucketName));
} catch (Exception e) {
Utils.<RuntimeException> rethrowIfRuntimeOrSameType(e);
Throwables.propagateIfPossible(e);
}
}
});
@ -242,7 +243,7 @@ public class BucketsLiveTest extends BaseBlobStoreIntegrationTest<S3AsyncClient,
assertEquals(logging.getTargetBucket(), newLogging.getTargetBucket());
assertEquals(logging.getTargetPrefix(), newLogging.getTargetPrefix());
} catch (Exception e) {
Utils.<RuntimeException> rethrowIfRuntimeOrSameType(e);
Throwables.propagateIfPossible(e);
}
}
});
@ -252,7 +253,7 @@ public class BucketsLiveTest extends BaseBlobStoreIntegrationTest<S3AsyncClient,
try {
assertNull(context.getApi().getBucketLogging(bucketName));
} catch (Exception e) {
Utils.<RuntimeException> rethrowIfRuntimeOrSameType(e);
Throwables.propagateIfPossible(e);
}
}
});
@ -285,7 +286,7 @@ public class BucketsLiveTest extends BaseBlobStoreIntegrationTest<S3AsyncClient,
assertTrue(acl.hasPermission(GroupGranteeURI.ALL_USERS, Permission.READ), acl
.toString());
} catch (Exception e) {
Utils.<RuntimeException> rethrowIfRuntimeOrSameType(e);
Throwables.propagateIfPossible(e);
}
}
});
@ -316,7 +317,7 @@ public class BucketsLiveTest extends BaseBlobStoreIntegrationTest<S3AsyncClient,
assertTrue(acl.hasPermission(GroupGranteeURI.ALL_USERS, Permission.READ), acl
.toString());
} catch (Exception e) {
Utils.<RuntimeException> rethrowIfRuntimeOrSameType(e);
Throwables.propagateIfPossible(e);
}
}
});

View File

@ -37,7 +37,6 @@ import org.jclouds.aws.s3.options.ListBucketOptions;
import org.jclouds.aws.s3.options.PutObjectOptions;
import org.jclouds.blobstore.BlobStoreContext;
import org.jclouds.http.options.GetOptions;
import org.jclouds.util.Utils;
import org.jets3t.service.S3ObjectsChunk;
import org.jets3t.service.S3Service;
import org.jets3t.service.S3ServiceException;
@ -47,6 +46,7 @@ import org.jets3t.service.model.S3BucketLoggingStatus;
import org.jets3t.service.model.S3Object;
import org.jets3t.service.security.AWSCredentials;
import com.google.common.base.Throwables;
import com.google.inject.Module;
/**
@ -107,7 +107,7 @@ public class JCloudsS3Service extends S3Service {
map.put("ETag", jcObjectMetadata.getETag());
return map;
} catch (Exception e) {
Utils.<S3ServiceException> rethrowIfRuntimeOrSameType(e);
Throwables.propagateIfPossible(e, S3ServiceException.class);
throw new S3ServiceException("error copying object", e);
}
}
@ -125,7 +125,7 @@ public class JCloudsS3Service extends S3Service {
// Bucket created.
}
} catch (Exception e) {
Utils.<S3ServiceException> rethrowIfRuntimeOrSameType(e);
Throwables.propagateIfPossible(e, S3ServiceException.class);
throw new S3ServiceException("error creating bucket: " + bucketName, e);
}
return new S3Bucket(bucketName);
@ -141,7 +141,7 @@ public class JCloudsS3Service extends S3Service {
try {
connection.deleteBucketIfEmpty(bucketName);
} catch (Exception e) {
Utils.<S3ServiceException> rethrowIfRuntimeOrSameType(e);
Throwables.propagateIfPossible(e, S3ServiceException.class);
throw new S3ServiceException("error deleting bucket: " + bucketName, e);
}
}
@ -156,7 +156,7 @@ public class JCloudsS3Service extends S3Service {
try {
connection.deleteObject(bucketName, objectKey);
} catch (Exception e) {
Utils.<S3ServiceException> rethrowIfRuntimeOrSameType(e);
Throwables.propagateIfPossible(e, S3ServiceException.class);
throw new S3ServiceException(String.format("error deleting object: %1$s:%2$s", bucketName,
objectKey), e);
}
@ -168,7 +168,7 @@ public class JCloudsS3Service extends S3Service {
org.jclouds.aws.s3.domain.AccessControlList jcACL = connection.getBucketACL(bucketName);
return Util.convertAccessControlList(jcACL);
} catch (Exception e) {
Utils.<S3ServiceException> rethrowIfRuntimeOrSameType(e);
Throwables.propagateIfPossible(e, S3ServiceException.class);
throw new S3ServiceException("error getting bucket's ACL: " + bucketName, e);
}
}
@ -194,7 +194,7 @@ public class JCloudsS3Service extends S3Service {
objectKey);
return Util.convertAccessControlList(jcACL);
} catch (Exception e) {
Utils.<S3ServiceException> rethrowIfRuntimeOrSameType(e);
Throwables.propagateIfPossible(e, S3ServiceException.class);
throw new S3ServiceException("error getting object's ACL", e);
}
}
@ -215,7 +215,7 @@ public class JCloudsS3Service extends S3Service {
return Util.convertObjectHead(connection.headObject(bucketName, objectKey));
} catch (Exception e) {
Utils.<S3ServiceException> rethrowIfRuntimeOrSameType(e);
Throwables.propagateIfPossible(e, S3ServiceException.class);
throw new S3ServiceException(String.format("error retrieving object head: %1$s:%2$s",
bucketName, objectKey), e);
}
@ -230,7 +230,7 @@ public class JCloudsS3Service extends S3Service {
ifMatchTags, ifNoneMatchTags);
return Util.convertObject(connection.getObject(bucketName, objectKey, options));
} catch (Exception e) {
Utils.<S3ServiceException> rethrowIfRuntimeOrSameType(e);
Throwables.propagateIfPossible(e, S3ServiceException.class);
throw new S3ServiceException(String.format("error retrieving object: %1$s:%2$s",
bucketName, objectKey), e);
}
@ -255,7 +255,7 @@ public class JCloudsS3Service extends S3Service {
.listOwnedBuckets();
return Util.convertBuckets(jcBucketList);
} catch (Exception e) {
Utils.<S3ServiceException> rethrowIfRuntimeOrSameType(e);
Throwables.propagateIfPossible(e, S3ServiceException.class);
throw new S3ServiceException("error listing buckets", e);
}
}
@ -289,7 +289,7 @@ public class JCloudsS3Service extends S3Service {
.size()]), (String[]) commonPrefixes.toArray(new String[commonPrefixes
.size()]), priorLastKey);
} catch (Exception e) {
Utils.<S3ServiceException> rethrowIfRuntimeOrSameType(e);
Throwables.propagateIfPossible(e, S3ServiceException.class);
throw new S3ServiceException("error listing objects in bucket " + bucketName, e);
}
}
@ -301,7 +301,7 @@ public class JCloudsS3Service extends S3Service {
return listObjectsChunked(bucketName, prefix, delimiter, maxListingLength, null, true)
.getObjects();
} catch (Exception e) {
Utils.<S3ServiceException> rethrowIfRuntimeOrSameType(e);
Throwables.propagateIfPossible(e, S3ServiceException.class);
throw new S3ServiceException("error listing objects in bucket " + bucketName, e);
}
}
@ -313,7 +313,7 @@ public class JCloudsS3Service extends S3Service {
org.jclouds.aws.s3.domain.AccessControlList jcACL = Util.convertAccessControlList(jsACL);
connection.putBucketACL(bucketName, jcACL);
} catch (Exception e) {
Utils.<S3ServiceException> rethrowIfRuntimeOrSameType(e);
Throwables.propagateIfPossible(e, S3ServiceException.class);
throw new S3ServiceException("error putting bucket's ACL: " + bucketName, e);
}
}
@ -325,7 +325,7 @@ public class JCloudsS3Service extends S3Service {
org.jclouds.aws.s3.domain.AccessControlList jcACL = Util.convertAccessControlList(jsACL);
connection.putObjectACL(bucketName, objectKey, jcACL);
} catch (Exception e) {
Utils.<S3ServiceException> rethrowIfRuntimeOrSameType(e);
Throwables.propagateIfPossible(e, S3ServiceException.class);
throw new S3ServiceException("error putting object's ACL", e);
}
}
@ -340,7 +340,7 @@ public class JCloudsS3Service extends S3Service {
jsObject.setETag(eTag);
return jsObject;
} catch (Exception e) {
Utils.<S3ServiceException> rethrowIfRuntimeOrSameType(e);
Throwables.propagateIfPossible(e, S3ServiceException.class);
throw new S3ServiceException("error putting object", e);
}
}

View File

@ -31,7 +31,8 @@ import org.jclouds.blobstore.internal.BlobRuntimeException;
import org.jclouds.blobstore.options.ListContainerOptions;
import org.jclouds.blobstore.strategy.ContainsValueInListStrategy;
import org.jclouds.blobstore.strategy.ListBlobMetadataStrategy;
import org.jclouds.util.Utils;
import com.google.common.base.Throwables;
/**
* Searches Content-MD5 tag for the value associated with the value
@ -63,7 +64,7 @@ public class FindMD5InBlobProperties implements ContainsValueInListStrategy {
}
return false;
} catch (Exception e) {
Utils.<BlobRuntimeException> rethrowIfRuntimeOrSameType(e);
Throwables.propagateIfPossible(e, BlobRuntimeException.class);
throw new BlobRuntimeException(String.format(
"Error searching for ETAG of value: [%2$s] in container:%1$s", containerName,
value), e);

View File

@ -32,6 +32,8 @@ import org.jclouds.http.HttpResponseException;
import org.jclouds.logging.Logger;
import org.jclouds.util.Utils;
import com.google.common.base.Throwables;
/**
* This will parse and set an appropriate exception on the command object.
*
@ -72,14 +74,14 @@ public class ParseAzureStorageErrorFromXmlContent implements HttpErrorHandler {
}
} catch (Exception he) {
command.setException(new HttpResponseException(command, response, content));
Utils.rethrowIfRuntime(he);
Throwables.propagateIfPossible(he);
}
} else {
command.setException(new HttpResponseException(command, response));
}
} catch (Exception e) {
command.setException(new HttpResponseException(command, response));
Utils.rethrowIfRuntime(e);
Throwables.propagateIfPossible(e);
}
}

View File

@ -30,9 +30,9 @@ import org.jclouds.blobstore.strategy.ClearContainerStrategy;
import org.jclouds.http.HttpResponseException;
import org.jclouds.rest.InvocationContext;
import org.jclouds.rest.internal.GeneratedHttpRequest;
import org.jclouds.util.Utils;
import com.google.common.base.Function;
import com.google.common.base.Throwables;
import com.google.inject.Inject;
public class ClearAndDeleteIfNotEmpty implements Function<Exception, Void>, InvocationContext {
@ -77,7 +77,7 @@ public class ClearAndDeleteIfNotEmpty implements Function<Exception, Void>, Invo
requestTimeoutMilliseconds, TimeUnit.MILLISECONDS);
return v;
} catch (Exception e) {
Utils.<BlobRuntimeException> rethrowIfRuntimeOrSameType(e);
Throwables.propagateIfPossible(e, BlobRuntimeException.class);
throw new BlobRuntimeException("Error deleting container: "
+ request.getArgs()[0].toString(), e);
}

View File

@ -43,7 +43,6 @@ import org.jclouds.blobstore.strategy.CountListStrategy;
import org.jclouds.blobstore.strategy.GetBlobsInListStrategy;
import org.jclouds.blobstore.strategy.ListBlobMetadataStrategy;
import org.jclouds.rest.ResourceNotFoundException;
import org.jclouds.util.Utils;
import com.google.common.base.Function;
import com.google.common.base.Throwables;
@ -209,7 +208,7 @@ public abstract class BaseBlobMap<V> {
if (Iterables.size(Iterables.filter(Throwables.getCausalChain(e),
ResourceNotFoundException.class)) >= 1)
return false;
Utils.<BlobRuntimeException> rethrowIfRuntimeOrSameType(e);
Throwables.propagateIfPossible(e, BlobRuntimeException.class);
throw new BlobRuntimeException(String.format("Error searching for %1$s:%2$s",
containerName, realKey), e);
}

View File

@ -37,11 +37,11 @@ import org.jclouds.blobstore.strategy.ContainsValueInListStrategy;
import org.jclouds.blobstore.strategy.CountListStrategy;
import org.jclouds.blobstore.strategy.GetBlobsInListStrategy;
import org.jclouds.blobstore.strategy.ListBlobMetadataStrategy;
import org.jclouds.util.Utils;
import com.google.common.base.Function;
import com.google.common.base.Throwables;
import com.google.common.collect.Collections2;
import com.google.common.collect.Iterables;
import com.google.common.collect.Sets;
/**
@ -119,13 +119,12 @@ public class BlobMapImpl extends BaseBlobMap<Blob> implements BlobMap {
return stripPrefix(connection.getBlob(containerName, realKey).get(
requestTimeoutMilliseconds, TimeUnit.MILLISECONDS));
} catch (Exception e) {
Throwable cause = Throwables.getRootCause(e);
if (cause instanceof KeyNotFoundException) {
if (Iterables.size(Iterables.filter(Throwables.getCausalChain(e),
KeyNotFoundException.class)) >= 1)
return null;
} else {
throw new BlobRuntimeException(String.format("Error geting object %1$s:%2$s",
containerName, realKey), cause);
}
Throwables.propagateIfPossible(e, BlobRuntimeException.class);
throw new BlobRuntimeException(String.format("Error geting blob %s:%s", containerName,
realKey), e);
}
}
@ -140,8 +139,8 @@ public class BlobMapImpl extends BaseBlobMap<Blob> implements BlobMap {
connection.putBlob(containerName, value).get(requestTimeoutMilliseconds,
TimeUnit.MILLISECONDS);
} catch (Exception e) {
Utils.<BlobRuntimeException> rethrowIfRuntimeOrSameType(e);
throw new BlobRuntimeException(String.format("Error putting object %1$s:%2$s%n%3$s",
Throwables.propagateIfPossible(e, BlobRuntimeException.class);
throw new BlobRuntimeException(String.format("Error putting blob %s:%s%n%3$s",
containerName, key, value), e);
}
return returnVal;
@ -163,7 +162,7 @@ public class BlobMapImpl extends BaseBlobMap<Blob> implements BlobMap {
// this will throw an exception if there was a problem
put.get(requestTimeoutMilliseconds, TimeUnit.MILLISECONDS);
} catch (Exception e) {
Utils.<BlobRuntimeException> rethrowIfRuntimeOrSameType(e);
Throwables.propagateIfPossible(e, BlobRuntimeException.class);
throw new BlobRuntimeException("Error putting into containerName" + containerName, e);
}
}
@ -180,8 +179,8 @@ public class BlobMapImpl extends BaseBlobMap<Blob> implements BlobMap {
connection.removeBlob(containerName, realKey).get(requestTimeoutMilliseconds,
TimeUnit.MILLISECONDS);
} catch (Exception e) {
Utils.<BlobRuntimeException> rethrowIfRuntimeOrSameType(e);
throw new BlobRuntimeException(String.format("Error removing object %1$s:%2$s",
Throwables.propagateIfPossible(e, BlobRuntimeException.class);
throw new BlobRuntimeException(String.format("Error removing blob %s:%s",
containerName, realKey), e);
}
return old;

View File

@ -45,12 +45,12 @@ import org.jclouds.http.payloads.ByteArrayPayload;
import org.jclouds.http.payloads.FilePayload;
import org.jclouds.http.payloads.InputStreamPayload;
import org.jclouds.http.payloads.StringPayload;
import org.jclouds.util.Utils;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Function;
import com.google.common.base.Throwables;
import com.google.common.collect.Collections2;
import com.google.common.collect.Iterables;
import com.google.common.collect.Sets;
/**
@ -82,17 +82,15 @@ public class InputStreamMapImpl extends BaseBlobMap<InputStream> implements Inpu
public InputStream get(Object o) {
String realKey = prefixer.apply(o.toString());
try {
return (InputStream) (connection.getBlob(containerName, realKey).get(
requestTimeoutMilliseconds, TimeUnit.MILLISECONDS)).getContent();
} catch (KeyNotFoundException e) {
return null;
return connection.getBlob(containerName, realKey).get(requestTimeoutMilliseconds,
TimeUnit.MILLISECONDS).getContent();
} catch (Exception e) {
Throwable cause = Throwables.getRootCause(e);
if (cause instanceof KeyNotFoundException)
if (Iterables.size(Iterables.filter(Throwables.getCausalChain(e),
KeyNotFoundException.class)) >= 1)
return null;
Throwables.propagateIfInstanceOf(e, BlobRuntimeException.class);
throw new BlobRuntimeException(String.format("Error geting object %1$s:%2$s",
containerName, realKey), e);
Throwables.propagateIfPossible(e, BlobRuntimeException.class);
throw new BlobRuntimeException(String.format("Error geting blob %s:%s", containerName,
realKey), e);
}
}
@ -108,8 +106,8 @@ public class InputStreamMapImpl extends BaseBlobMap<InputStream> implements Inpu
connection.removeBlob(containerName, realKey).get(requestTimeoutMilliseconds,
TimeUnit.MILLISECONDS);
} catch (Exception e) {
Utils.<BlobRuntimeException> rethrowIfRuntimeOrSameType(e);
throw new BlobRuntimeException(String.format("Error removing object %1$s:%2$s",
Throwables.propagateIfPossible(e, BlobRuntimeException.class);
throw new BlobRuntimeException(String.format("Error removing blob %s:%s",
containerName, realKey), e);
}
return old;
@ -134,7 +132,7 @@ public class InputStreamMapImpl extends BaseBlobMap<InputStream> implements Inpu
return Collections2.transform(getAllBlobs.execute(containerName, options),
new Function<Blob, InputStream>() {
public InputStream apply(Blob from) {
return (InputStream) from.getContent();
return from.getContent();
}
});
}
@ -147,8 +145,8 @@ public class InputStreamMapImpl extends BaseBlobMap<InputStream> implements Inpu
public Set<Map.Entry<String, InputStream>> entrySet() {
Set<Map.Entry<String, InputStream>> entrySet = new HashSet<Map.Entry<String, InputStream>>();
for (Blob object : this.getAllBlobs.execute(containerName, options)) {
entrySet.add(new Entry(pathStripper.apply(object.getMetadata().getName()),
(InputStream) object.getContent()));
entrySet.add(new Entry(pathStripper.apply(object.getMetadata().getName()), object
.getContent()));
}
return entrySet;
}
@ -233,15 +231,12 @@ public class InputStreamMapImpl extends BaseBlobMap<InputStream> implements Inpu
object.setPayload(Payloads.newPayload(entry.getValue()));
object.generateMD5();
puts.add(connection.putBlob(containerName, object));
// / ParamExtractor Funcion<?,String>
// / response transformer set key on the way out.
// / ExceptionHandler convert 404 to NOT_FOUND
}
for (Future<String> put : puts)
// this will throw an exception if there was a problem
put.get(requestTimeoutMilliseconds, TimeUnit.MILLISECONDS);
} catch (Exception e) {
Utils.<BlobRuntimeException> rethrowIfRuntimeOrSameType(e);
Throwables.propagateIfPossible(e, BlobRuntimeException.class);
throw new BlobRuntimeException("Error putting into containerName" + containerName, e);
}
}
@ -299,8 +294,8 @@ public class InputStreamMapImpl extends BaseBlobMap<InputStream> implements Inpu
TimeUnit.MILLISECONDS);
return returnVal;
} catch (Exception e) {
Utils.<BlobRuntimeException> rethrowIfRuntimeOrSameType(e);
throw new BlobRuntimeException(String.format("Error adding object %1$s:%2$s",
Throwables.propagateIfPossible(e, BlobRuntimeException.class);
throw new BlobRuntimeException(String.format("Error adding blob %s:%s",
containerName, object), e);
}
}

View File

@ -34,8 +34,8 @@ import org.jclouds.blobstore.reference.BlobStoreConstants;
import org.jclouds.blobstore.strategy.ClearContainerStrategy;
import org.jclouds.blobstore.strategy.ClearListStrategy;
import org.jclouds.blobstore.strategy.ListBlobMetadataStrategy;
import org.jclouds.util.Utils;
import com.google.common.base.Throwables;
import com.google.common.collect.Sets;
import com.google.inject.Inject;
@ -75,7 +75,7 @@ public class DeleteAllKeysInList implements ClearListStrategy, ClearContainerStr
try {
isdeleted.get(requestTimeoutMilliseconds, TimeUnit.MILLISECONDS);
} catch (Exception e) {
Utils.<BlobRuntimeException> rethrowIfRuntimeOrSameType(e);
Throwables.propagateIfPossible(e, BlobRuntimeException.class);
throw new BlobRuntimeException("Error deleting blob in container: " + containerName, e);
}
}

View File

@ -29,7 +29,8 @@ import org.jclouds.blobstore.internal.BlobRuntimeException;
import org.jclouds.blobstore.options.ListContainerOptions;
import org.jclouds.blobstore.strategy.ContainsValueInListStrategy;
import org.jclouds.blobstore.strategy.ListBlobMetadataStrategy;
import org.jclouds.util.Utils;
import com.google.common.base.Throwables;
/**
* Searches Content-MD5 tag for the value associated with the value
@ -57,7 +58,7 @@ public class FindMD5InList implements ContainsValueInListStrategy {
}
return false;
} catch (Exception e) {
Utils.<BlobRuntimeException> rethrowIfRuntimeOrSameType(e);
Throwables.propagateIfPossible(e, BlobRuntimeException.class);
throw new BlobRuntimeException(String.format(
"Error searching for ETAG of value: [%2$s] in container:%1$s", containerName,
value), e);

View File

@ -39,10 +39,10 @@ import org.jclouds.blobstore.options.ListContainerOptions;
import org.jclouds.blobstore.reference.BlobStoreConstants;
import org.jclouds.blobstore.strategy.GetBlobsInListStrategy;
import org.jclouds.blobstore.strategy.ListBlobMetadataStrategy;
import org.jclouds.util.Utils;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Throwables;
import com.google.common.collect.Iterables;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import com.google.inject.Inject;
@ -89,7 +89,7 @@ public class GetAllBlobsInListAndRetryOnFailure implements GetBlobsInListStrateg
ifNotFoundRetryOtherwiseAddToSet(container, futureObjectEntry.getKey(),
futureObjectEntry.getValue(), objects);
} catch (Exception e) {
Utils.<BlobRuntimeException> rethrowIfRuntimeOrSameType(e);
Throwables.propagateIfPossible(e, BlobRuntimeException.class);
throw new BlobRuntimeException(String.format("Error getting value from blob %1$s",
container), e);
}
@ -109,8 +109,8 @@ public class GetAllBlobsInListAndRetryOnFailure implements GetBlobsInListStrateg
objects.add(object);
return;
} catch (Exception e) {
Throwable cause = Throwables.getRootCause(e);
if (cause instanceof KeyNotFoundException) {
if (Iterables.size(Iterables.filter(Throwables.getCausalChain(e),
KeyNotFoundException.class)) >= 1) {
Thread.sleep(requestRetryMilliseconds);
value = connection.getBlob(container, key);
} else {

View File

@ -33,8 +33,8 @@ import org.jclouds.blobstore.internal.BlobRuntimeException;
import org.jclouds.blobstore.options.ListContainerOptions;
import org.jclouds.blobstore.reference.BlobStoreConstants;
import org.jclouds.blobstore.strategy.ListBlobMetadataStrategy;
import org.jclouds.util.Utils;
import com.google.common.base.Throwables;
import com.google.common.collect.Sets;
import com.google.inject.Inject;
@ -69,7 +69,7 @@ public class ListBlobMetadataInContainer implements ListBlobMetadataStrategy {
}
return blobM;
} catch (Exception e) {
Utils.<BlobRuntimeException> rethrowIfRuntimeOrSameType(e);
Throwables.propagateIfPossible(e, BlobRuntimeException.class);
throw new BlobRuntimeException("Error getting resource metadata in container: "
+ container, e);
}

View File

@ -29,8 +29,8 @@ import org.jclouds.blobstore.domain.Blob;
import org.jclouds.blobstore.internal.BlobRuntimeException;
import org.jclouds.blobstore.reference.BlobStoreConstants;
import org.jclouds.blobstore.strategy.MkdirStrategy;
import org.jclouds.util.Utils;
import com.google.common.base.Throwables;
import com.google.inject.Inject;
/**
@ -65,7 +65,7 @@ public class MarkerFileMkdirStrategy implements MkdirStrategy {
TimeUnit.MILLISECONDS);
}
} catch (Exception e) {
e = Utils.<BlobRuntimeException> rethrowIfRuntimeOrSameType(e);
Throwables.propagateIfPossible(e, BlobRuntimeException.class);
if (!(e instanceof KeyNotFoundException))
throw new BlobRuntimeException("Error creating marker directory: " + containerName
+ "/" + directory, e);

View File

@ -30,8 +30,8 @@ import org.jclouds.blobstore.functions.ResourceMetadataToRelativePathResourceMet
import org.jclouds.blobstore.internal.BlobRuntimeException;
import org.jclouds.blobstore.reference.BlobStoreConstants;
import org.jclouds.blobstore.strategy.GetDirectoryStrategy;
import org.jclouds.util.Utils;
import com.google.common.base.Throwables;
import com.google.inject.Inject;
/**
@ -76,7 +76,7 @@ public class MarkersGetDirectoryStrategy implements GetDirectoryStrategy {
directory + suffix).get(requestTimeoutMilliseconds, TimeUnit.MILLISECONDS));
} catch (KeyNotFoundException e) {
} catch (Exception e) {
e = Utils.<BlobRuntimeException> rethrowIfRuntimeOrSameType(e);
Throwables.propagateIfPossible(e, BlobRuntimeException.class);
if (!(e instanceof KeyNotFoundException))
throw new BlobRuntimeException("Error determining if a directory exists at: "
+ containerName + "/" + directory, e);

View File

@ -41,7 +41,6 @@ import org.jclouds.blobstore.domain.ResourceMetadata;
import org.jclouds.blobstore.domain.ResourceType;
import org.jclouds.blobstore.util.BlobStoreUtils;
import org.jclouds.http.config.JavaUrlHttpCommandExecutorServiceModule;
import org.jclouds.util.Utils;
import org.testng.ITestContext;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
@ -49,6 +48,7 @@ import org.testng.annotations.BeforeSuite;
import com.google.common.base.Function;
import com.google.common.base.Predicate;
import com.google.common.base.Throwables;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Iterables;
import com.google.common.collect.Sets;
@ -299,7 +299,7 @@ public class BaseBlobStoreIntegrationTest<A, S> {
})));
} catch (Exception e) {
Utils.<RuntimeException> rethrowIfRuntimeOrSameType(e);
Throwables.propagateIfPossible(e);
}
}
});
@ -395,7 +395,7 @@ public class BaseBlobStoreIntegrationTest<A, S> {
assert !context.getBlobStore().containerExists(name) : "container " + name
+ " still exists";
} catch (Exception e) {
Utils.<RuntimeException> rethrowIfRuntimeOrSameType(e);
Throwables.propagateIfPossible(e);
}
}
});

View File

@ -19,8 +19,8 @@
package org.jclouds.blobstore.integration.internal;
import static org.jclouds.blobstore.options.ListContainerOptions.Builder.afterMarker;
import static org.jclouds.blobstore.options.ListContainerOptions.Builder.maxResults;
import static org.jclouds.blobstore.options.ListContainerOptions.Builder.inDirectory;
import static org.jclouds.blobstore.options.ListContainerOptions.Builder.maxResults;
import static org.testng.Assert.assertEquals;
import java.io.UnsupportedEncodingException;
@ -32,9 +32,10 @@ import org.jclouds.blobstore.domain.Blob;
import org.jclouds.blobstore.domain.ListContainerResponse;
import org.jclouds.blobstore.domain.ListResponse;
import org.jclouds.blobstore.domain.ResourceMetadata;
import org.jclouds.util.Utils;
import org.testng.annotations.Test;
import com.google.common.base.Throwables;
/**
* @author Adrian Cole
*/
@ -174,7 +175,7 @@ public class BaseContainerIntegrationTest<A, S> extends BaseBlobStoreIntegration
assert !context.getBlobStore().containerExists(containerName) : "container " + containerName
+ " still exists";
} catch (Exception e) {
Utils.<RuntimeException> rethrowIfRuntimeOrSameType(e);
Throwables.propagateIfPossible(e);
}
}
});

View File

@ -78,10 +78,10 @@ import org.jclouds.http.HttpRequest;
import org.jclouds.http.HttpResponse;
import org.jclouds.http.HttpResponseException;
import org.jclouds.http.options.HttpRequestOptions;
import org.jclouds.util.Utils;
import com.google.common.base.Function;
import com.google.common.base.Predicate;
import com.google.common.base.Throwables;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
@ -562,8 +562,8 @@ public class StubAsyncBlobStore implements AsyncBlobStore {
try {
return immediateFuture((BlobMetadata) copy(getBlob(container, key).get().getMetadata()));
} catch (Exception e) {
Utils.<ContainerNotFoundException> rethrowIfRuntimeOrSameType(e);
Utils.<KeyNotFoundException> rethrowIfRuntimeOrSameType(e);
Throwables.propagateIfPossible(e, ContainerNotFoundException.class);
Throwables.propagateIfPossible(e, KeyNotFoundException.class);
return immediateFailedFuture(e);
}
}

View File

@ -23,8 +23,6 @@ import java.util.concurrent.Executor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import org.jclouds.logging.Logger;
import com.google.common.base.Function;
import com.google.common.util.concurrent.ListenableFuture;
@ -40,17 +38,10 @@ public class FutureExceptionParser<T> implements ListenableFuture<T> {
private final ListenableFuture<T> delegate;
private final Function<Exception, T> function;
private final Logger logger;
public FutureExceptionParser(ListenableFuture<T> delegate, Function<Exception, T> function) {
this(delegate, function, Logger.NULL);
}
public FutureExceptionParser(ListenableFuture<T> delegate, Function<Exception, T> function,
Logger logger) {
this.delegate = delegate;
this.function = function;
this.logger = logger;
}
public boolean cancel(boolean mayInterruptIfRunning) {
@ -67,10 +58,7 @@ public class FutureExceptionParser<T> implements ListenableFuture<T> {
private T attemptConvert(ExecutionException e) throws ExecutionException {
if (e.getCause() instanceof Exception) {
logger.debug("Processing exception for: %s", e.getCause());
T returnVal = function.apply((Exception) e.getCause());
logger.debug("Processed exception for: %s", e.getCause());
if (returnVal != null)
return returnVal;
}

View File

@ -30,13 +30,13 @@ import org.jclouds.http.HttpResponse;
import org.jclouds.logging.Logger;
import org.jclouds.rest.InvocationContext;
import org.jclouds.rest.internal.GeneratedHttpRequest;
import org.jclouds.util.Utils;
import org.xml.sax.ContentHandler;
import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;
import com.google.common.base.Function;
import com.google.common.base.Throwables;
import com.google.common.io.Closeables;
/**
@ -72,7 +72,7 @@ public class ParseSax<T> implements Function<HttpResponse, T>, InvocationContext
throw new HttpException("No input to parse");
}
} catch (Exception e) {
Utils.<HttpException> rethrowIfRuntimeOrSameType(e);
Throwables.propagateIfPossible(e, HttpException.class);
throw new HttpException("Error parsing input for " + from, e);
}
}
@ -92,8 +92,7 @@ public class ParseSax<T> implements Function<HttpResponse, T>, InvocationContext
StringBuilder message = new StringBuilder();
message.append("Error parsing input for ").append(handler);
logger.error(e, message.toString());
if (!(e instanceof NullPointerException))
Utils.<HttpException> rethrowIfRuntimeOrSameType(e);
Throwables.propagateIfPossible(e, HttpException.class);
throw new HttpException(message.toString(), e);
} finally {
Closeables.closeQuietly(xml);

View File

@ -25,7 +25,6 @@ import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@ -35,7 +34,6 @@ import org.jclouds.logging.Logger;
import com.google.common.base.Charsets;
import com.google.common.base.Supplier;
import com.google.common.collect.ComputationException;
import com.google.common.io.ByteStreams;
import com.google.common.io.Closeables;
import com.google.common.io.OutputSupplier;
@ -82,55 +80,6 @@ public class Utils {
@Resource
protected static Logger logger = Logger.NULL;
/**
*
* @param <E>
* Exception type you'd like rethrown
* @param e
* Exception you are inspecting
* @throws E
*/
public static void rethrowIfRuntime(Exception e) {
if (e instanceof ExecutionException || e instanceof ComputationException) {
Throwable nested = e.getCause();
if (nested instanceof Error)
throw (Error) nested;
e = (Exception) nested;
}
if (e instanceof RuntimeException) {
throw (RuntimeException) e;
}
}
/**
*
* @param <E>
* Exception type you'd like rethrown
* @param e
* Exception you are inspecting
* @throws E
*/
@SuppressWarnings("unchecked")
public static <E extends Exception> Exception rethrowIfRuntimeOrSameType(Exception e) throws E {
if (e instanceof ExecutionException || e instanceof ComputationException) {
Throwable nested = e.getCause();
if (nested instanceof Error)
throw (Error) nested;
e = (Exception) nested;
}
if (e instanceof RuntimeException) {
throw (RuntimeException) e;
} else {
try {
throw (E) e;
} catch (ClassCastException throwAway) {
// using cce as there's no way to do instanceof E in current java
}
}
return e;
}
public static String toStringAndClose(InputStream input) throws IOException {
try {

View File

@ -41,9 +41,9 @@ import org.jclouds.http.TransformingHttpCommandExecutorService;
import org.jclouds.lifecycle.BaseLifeCycle;
import org.jclouds.logging.Logger;
import org.jclouds.logging.Logger.LoggerFactory;
import org.jclouds.util.Utils;
import com.google.common.base.Function;
import com.google.common.base.Throwables;
import com.google.common.collect.MapMaker;
import com.google.common.util.concurrent.ListenableFuture;
@ -120,7 +120,7 @@ public class ConnectionPoolTransformingHttpCommandExecutorService<C> extends Bas
try {
invoke(rendezvous);
} catch (Exception e) {
Utils.<InterruptedException> rethrowIfRuntimeOrSameType(e);
Throwables.propagateIfPossible(e, InterruptedException.class);
logger.error(e, "Error processing command %s", rendezvous.getCommand());
}
}

View File

@ -58,9 +58,9 @@ import org.jclouds.blobstore.options.ListContainerOptions;
import org.jclouds.blobstore.strategy.IsDirectoryStrategy;
import org.jclouds.blobstore.strategy.internal.MarkersIsDirectoryStrategy;
import org.jclouds.blobstore.util.BlobStoreUtils;
import org.jclouds.util.Utils;
import com.google.common.base.Predicate;
import com.google.common.base.Throwables;
import com.google.common.collect.Iterables;
import com.google.common.collect.Sets;
@ -199,7 +199,7 @@ public class BlobStoreFileObject extends AbstractFileObject {
throw new FileNotFolderException(getName());
}
} catch (Exception ex) {
Utils.<FileNotFolderException> rethrowIfRuntimeOrSameType(ex);
Throwables.propagateIfPossible(ex, FileNotFolderException.class);
throw new FileNotFolderException(getName(), ex);
}
}

View File

@ -48,7 +48,6 @@ import org.jclouds.predicates.RetryablePredicate;
import org.jclouds.predicates.SocketOpen;
import org.jclouds.rest.ConfiguresRestClient;
import org.jclouds.rest.RestClientFactory;
import org.jclouds.util.Utils;
import org.jclouds.vcloud.VCloudAsyncClient;
import org.jclouds.vcloud.VCloudClient;
import org.jclouds.vcloud.VCloudToken;
@ -71,6 +70,7 @@ import org.jclouds.vcloud.predicates.TaskSuccess;
import com.google.common.base.Predicate;
import com.google.common.base.Supplier;
import com.google.common.base.Throwables;
import com.google.common.collect.Iterables;
import com.google.inject.AbstractModule;
import com.google.inject.Provides;
@ -141,7 +141,7 @@ public class VCloudRestClientModule extends AbstractModule {
try {
return login.login().get(180, TimeUnit.SECONDS);
} catch (Exception e) {
Utils.<RuntimeException> rethrowIfRuntimeOrSameType(e);
Throwables.propagateIfPossible(e);
throw new RuntimeException("Error logging in", e);
}
}