001/** 002 * Copyright 2005-2016 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.codahale.metrics.Gauge; 019import com.codahale.metrics.health.HealthCheck; 020import org.kuali.rice.core.framework.persistence.platform.DatabasePlatform; 021import org.springframework.jdbc.core.JdbcTemplate; 022 023import javax.sql.DataSource; 024 025/** 026 * A combination of health check and gauge which will check for successful connection to the given {@link DataSource} 027 * using the validation query defined on the given {@link DatabasePlatform}. 028 * 029 * @author Eric Westfall 030 */ 031public class DatabaseConnectionHealthGauge extends HealthCheck implements Gauge<Boolean> { 032 033 private final DataSource dataSource; 034 private final DatabasePlatform platform; 035 036 public DatabaseConnectionHealthGauge(DataSource dataSource, DatabasePlatform platform) { 037 this.dataSource = dataSource; 038 this.platform = platform; 039 } 040 041 @Override 042 public Boolean getValue() { 043 Result result = execute(); 044 return result.isHealthy(); 045 } 046 047 @Override 048 protected Result check() throws Exception { 049 JdbcTemplate template = new JdbcTemplate(dataSource); 050 template.execute(platform.getValidationQuery()); 051 // if it's unhealthy, above method will throw an exception 052 return Result.healthy(); 053 } 054 055}