package com.codacy.play.tomcatcp.pool;

import com.codacy.play.tomcatcp.metrics.MetricsTrackerFactory;
import com.codahale.metrics.MetricRegistry;
import com.codahale.metrics.health.HealthCheckRegistry;
import org.apache.tomcat.jdbc.pool.ConnectionPool;
import org.apache.tomcat.jdbc.pool.DataSource;
import org.apache.tomcat.jdbc.pool.PoolConfiguration;

import java.sql.SQLException;

public class TomcatCPDataSource extends DataSource {

    private MetricsTrackerFactory metricsTrackerFactory;
    private MetricRegistry metricRegistry;
    private HealthCheckRegistry healthCheckRegistry;
    private TomcatCPPool TCPool;


    public TomcatCPDataSource() {
        super();
    }


    public TomcatCPDataSource(PoolConfiguration poolProperties) {
        super(poolProperties);
    }

    @Override
    public ConnectionPool createPool() throws SQLException {
        if (pool != null) {
            return pool;
        } else {
            TCPool = new TomcatCPPool(poolProperties);
            pool = TCPool;
            return pool;
        }
    }


    public MetricRegistry getMetricRegistry() {
        return metricRegistry;
    }

    public void setMetricRegistry(MetricRegistry metricRegistry) {
        if (metricsTrackerFactory != null) {
            throw new IllegalStateException("cannot use setMetricRegistry() and setMetricsTrackerFactory() together");
        }

        boolean isAlreadySet = getMetricRegistry() != null;

        if (pool != null) {
            if (isAlreadySet) {
                throw new IllegalStateException("MetricRegistry can only be set one time");
            } else {
                TCPool.setMetricRegistry(metricRegistry);
            }
        }

        this.metricRegistry = metricRegistry;
    }

    public MetricsTrackerFactory getMetricsTrackerFactory() {
        return metricsTrackerFactory;
    }

    public void setMetricsTrackerFactory(MetricsTrackerFactory metricsTrackerFactory) {
        if (metricRegistry != null) {
            throw new IllegalStateException("cannot use setMetricsTrackerFactory() and setMetricRegistry() together");
        }

        boolean isAlreadySet = getMetricsTrackerFactory() != null;

        if (pool != null) {
            if (isAlreadySet) {
                throw new IllegalStateException("MetricsTrackerFactory can only be set one time");
            } else {
                TCPool.setMetricsTrackerFactory(metricsTrackerFactory);
            }
        }

        this.metricsTrackerFactory = metricsTrackerFactory;
    }

    public HealthCheckRegistry getHealthCheckRegistry() {
        return healthCheckRegistry;
    }

    public void setHealthCheckRegistry(HealthCheckRegistry healthCheckRegistry) {
        boolean isAlreadySet = getHealthCheckRegistry() != null;

        if (pool != null) {
            if (isAlreadySet) {
                throw new IllegalStateException("HealthCheckRegistry can only be set one time");
            } else {
                TCPool.setHealthCheckRegistry(healthCheckRegistry);
            }
        }

        this.healthCheckRegistry = healthCheckRegistry;
    }

}

@@ -1448,36 +1448,41 @@
 l;%0A%0A        if (
-p
+getP
 ool
+()
  != null) %7B%0A    
@@ -2193,36 +2193,41 @@
 l;%0A%0A        if (
-p
+getP
 ool
+()
  != null) %7B%0A    
@@ -2791,36 +2791,41 @@
 l;%0A%0A        if (
-p
+getP
 ool
+()
  != null) %7B%0A    

Add pool creation if not created yet