001/**
002 * Copyright 2005-2018 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.rice.web.health;
017
018import com.amazonaws.services.s3.AmazonS3;
019import com.amazonaws.services.s3.model.Bucket;
020import com.codahale.metrics.Gauge;
021import com.codahale.metrics.health.HealthCheck;
022
023import java.util.List;
024
025/**
026 * A combination of health check and gauge which will check connection with Amazon's S3 service using the provided
027 * {@link AmazonS3} client.
028 *
029 * @author Eric Westfall
030 */
031public class AmazonS3ConnectionHealthGauge extends HealthCheck implements Gauge<Boolean> {
032
033    private final AmazonS3 amazonS3;
034
035    public AmazonS3ConnectionHealthGauge(AmazonS3 amazonS3) {
036        this.amazonS3 = amazonS3;
037    }
038
039    @Override
040    public Boolean getValue() {
041        Result result = execute();
042        return result.isHealthy();
043    }
044
045    @Override
046    protected Result check() throws Exception {
047        List<Bucket> buckets = amazonS3.listBuckets();
048        if (buckets.isEmpty()) {
049            return Result.unhealthy("Amazon S3 returned an empty list of buckets, there should be at least one.");
050        }
051        // if there are connection difficulties, the listBuckets method should throw an exception which is handled by the superclass execute method
052        return Result.healthy();
053    }
054
055}