001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.camel.health;
018
019import java.util.Collection;
020import java.util.Optional;
021import java.util.stream.Collectors;
022
023import org.apache.camel.CamelContext;
024import org.apache.camel.CamelContextAware;
025import org.apache.camel.util.ObjectHelper;
026
027/**
028 * A registry for health checks.
029 * <p>
030 * Note that this registry can be superseded by the future camel context internal
031 * registry, @see <a href="https://issues.apache.org/jira/browse/CAMEL-10792"/>.
032 */
033public interface HealthCheckRegistry extends HealthCheckRepository, CamelContextAware {
034    /**
035     * Registers a service {@link HealthCheck}.
036     */
037    boolean register(HealthCheck check);
038
039    /**
040     * Unregisters a service {@link HealthCheck}.
041     */
042    boolean unregister(HealthCheck check);
043
044    /**
045     * Set the health check repositories to use..
046     */
047    void setRepositories(Collection<HealthCheckRepository> repositories);
048
049    /**
050     * Get a collection of health check repositories.
051     */
052    Collection<HealthCheckRepository> getRepositories();
053
054    /**
055     * Add an Health Check repository.
056     */
057    boolean addRepository(HealthCheckRepository repository);
058
059    /**
060     * Remove an Health Check repository.
061     */
062    boolean removeRepository(HealthCheckRepository repository);
063
064    /**
065     * A collection of health check IDs.
066     */
067    default Collection<String> getCheckIDs() {
068        return stream()
069            .map(HealthCheck::getId)
070            .collect(Collectors.toList());
071    }
072
073    /**
074     * Returns the check identified by the given <code>id</code> if available.
075     */
076    default Optional<HealthCheck> getCheck(String id) {
077        return stream()
078            .filter(check -> ObjectHelper.equal(check.getId(), id))
079            .findFirst();
080    }
081
082    /**
083     * Returns an optional {@link HealthCheckRegistry}, by default no registry is
084     * present and it must be explicit activated. Components can register/unregister
085     * health checks in response to life-cycle events (i.e. start/stop).
086     *
087     * This registry is not used by the camel context but it is up to the impl to
088     * properly use it, i.e.
089     *
090     * - a RouteController could use the registry to decide to restart a route
091     *   with failing health checks
092     * - spring boot could integrate such checks within its health endpoint or
093     *   make it available only as separate endpoint.
094     */
095    static HealthCheckRegistry get(CamelContext context) {
096        return context.getExtension(HealthCheckRegistry.class);
097    }
098}