Search in sources :

Example 1 with BuildCacheException

use of org.gradle.caching.BuildCacheException in project gradle by gradle.

the class DispatchingBuildCacheService method writeCacheEntryLocally.

private void writeCacheEntryLocally(BuildCacheEntryWriter writer, File destination) throws IOException {
    OutputStream fileOutputStream = null;
    try {
        fileOutputStream = new BufferedOutputStream(new FileOutputStream(destination));
        writer.writeTo(fileOutputStream);
    } catch (FileNotFoundException e) {
        throw new BuildCacheException("Couldn't create local file for cache entry", e);
    } finally {
        IOUtils.closeQuietly(fileOutputStream);
    }
}
Also used : BuildCacheException(org.gradle.caching.BuildCacheException) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) FileOutputStream(java.io.FileOutputStream) FileNotFoundException(java.io.FileNotFoundException) BufferedOutputStream(java.io.BufferedOutputStream)

Example 2 with BuildCacheException

use of org.gradle.caching.BuildCacheException in project gradle-s3-build-cache by myniva.

the class AwsS3BuildCacheService method store.

@Override
public void store(BuildCacheKey key, BuildCacheEntryWriter writer) {
    final String bucketPath = getBucketPath(key);
    logger.info("Start storing cache entry '{}' in S3 bucket", bucketPath);
    ObjectMetadata meta = new ObjectMetadata();
    meta.setContentType(BUILD_CACHE_CONTENT_TYPE);
    try (ByteArrayOutputStream os = new ByteArrayOutputStream()) {
        writer.writeTo(os);
        meta.setContentLength(os.size());
        try (InputStream is = new ByteArrayInputStream(os.toByteArray())) {
            PutObjectRequest request = getPutObjectRequest(bucketPath, meta, is);
            if (this.reducedRedundancy) {
                request.withStorageClass(StorageClass.ReducedRedundancy);
            }
            s3.putObject(request);
        }
    } catch (IOException e) {
        throw new BuildCacheException("Error while storing cache object in S3 bucket", e);
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) BuildCacheException(org.gradle.caching.BuildCacheException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) ObjectMetadata(com.amazonaws.services.s3.model.ObjectMetadata) PutObjectRequest(com.amazonaws.services.s3.model.PutObjectRequest)

Example 3 with BuildCacheException

use of org.gradle.caching.BuildCacheException in project gradle-s3-build-cache by myniva.

the class AwsS3BuildCacheService method load.

@Override
public boolean load(BuildCacheKey key, BuildCacheEntryReader reader) {
    final String bucketPath = getBucketPath(key);
    if (s3.doesObjectExist(bucketName, bucketPath)) {
        logger.info("Found cache item '{}' in S3 bucket", bucketPath);
        S3Object object = s3.getObject(bucketName, bucketPath);
        try (InputStream is = object.getObjectContent()) {
            reader.readFrom(is);
            return true;
        } catch (IOException e) {
            throw new BuildCacheException("Error while reading cache object from S3 bucket", e);
        }
    } else {
        logger.info("Did not find cache item '{}' in S3 bucket", bucketPath);
        return false;
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) BuildCacheException(org.gradle.caching.BuildCacheException) S3Object(com.amazonaws.services.s3.model.S3Object) IOException(java.io.IOException)

Aggregations

BuildCacheException (org.gradle.caching.BuildCacheException)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 ObjectMetadata (com.amazonaws.services.s3.model.ObjectMetadata)1 PutObjectRequest (com.amazonaws.services.s3.model.PutObjectRequest)1 S3Object (com.amazonaws.services.s3.model.S3Object)1 BufferedOutputStream (java.io.BufferedOutputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 FileOutputStream (java.io.FileOutputStream)1 OutputStream (java.io.OutputStream)1