ContentHealthCheck.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>ContentHealthCheck</code> contains the health check for the
 * content section.
 *
 * @author <a href="mailto:gene@ctan.org">Gerd Neugebauer</a>
 */
@SuppressFBWarnings(value = "EI_EXPOSE_REP2")
public class ContentHealthCheck extends HealthCheck {

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

    /**
     * This is the constructor for the class <code>ContentHealthCheck</code>.
     *
     * @param config the configuration
     */
    public ContentHealthCheck(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");
        }
        var ctan = config.getCtan();
        if (ctan == null) {
            return Result.unhealthy("Missing CTAN config section");
        }
        var content = config.getContent().getDirectory();
        if (content == null || "".equals(content)) {
            return Result.unhealthy("Missing CTAN config ctan.content");
        }
        var dir = new File(content);
        if (!dir.exists()) {
            return Result.unhealthy(
                String.format(
                    "Config ctan.content '%s' does not exist",
                    content));
        }
        if (!dir.isDirectory()) {
            return Result.unhealthy(
                String.format(
                    "Config ctan.content '%s' isn't a directory",
                    content));
        }
        return Result.healthy();
    }
}