TexarchiveHealthCheck.java

/*
 * Copyright © 2022-2025 The CTAN Team and individual authors
 *
 * This file is distributed under the 3-clause BSD license.
 * See file LICENSE for details.
 */
package org.ctan.site.health;

import java.io.File;

import org.ctan.site.CtanConfiguration;

import com.codahale.metrics.health.HealthCheck;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;

/**
 * The class <code>TexarchiveHealthCheck</code> contains the health check for
 * the texarchive end-points.
 *
 * @author <a href="mailto:gene@ctan.org">Gerd Neugebauer</a>
 */
@SuppressFBWarnings(value = "EI_EXPOSE_REP2")
public class TexarchiveHealthCheck extends HealthCheck {

    /**
     * The field <code>config</code> contains the configuration to test.
     */
    private CtanConfiguration config;

    /**
     * This is the constructor for the class <code>AppHealthCheck</code>.
     *
     * @param config the configuration
     */
    public TexarchiveHealthCheck(CtanConfiguration config) {

        this.config = config;
    }

    /**
     * {@inheritDoc}
     *
     * @see com.codahale.metrics.health.HealthCheck#check()
     */
    @Override
    protected Result check() throws Exception {

        if (config == null) {
            return Result.unhealthy("Missing CTAN config");
        }
        if (config.getTexArchive() == null) {
            return Result.unhealthy("Missing CTAN config texarchive");
        }
        var texarchive = config.getTexArchive().getDirectory();
        if (texarchive == null || "".equals(texarchive)) {
            return Result.unhealthy("Missing CTAN config texArchive.directory");
        }
        var dir = new File(texarchive);
        if (!dir.exists()) {
            return Result.unhealthy(
                String.format(
                    "Config ctan.texarchive '%s' does not exist",
                    texarchive));
        }
        if (!dir.isDirectory()) {
            return Result.unhealthy(
                String.format(
                    "Config ctan.texarchive '%s' isn't a directory",
                    texarchive));
        }
        return Result.healthy();
    }
}