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.impl.health; 018 019import java.util.Collection; 020import java.util.Collections; 021import java.util.Set; 022import java.util.concurrent.CopyOnWriteArraySet; 023import java.util.stream.Stream; 024 025import org.apache.camel.CamelContext; 026import org.apache.camel.CamelContextAware; 027import org.apache.camel.health.HealthCheck; 028import org.apache.camel.health.HealthCheckRegistry; 029import org.apache.camel.health.HealthCheckRepository; 030import org.slf4j.Logger; 031import org.slf4j.LoggerFactory; 032 033public class DefaultHealthCheckRegistry implements HealthCheckRegistry { 034 private static final Logger LOGGER = LoggerFactory.getLogger(DefaultHealthCheckRegistry.class); 035 036 private final Set<HealthCheck> checks; 037 private final Set<HealthCheckRepository> repositories; 038 private CamelContext camelContext; 039 040 public DefaultHealthCheckRegistry() { 041 this(null); 042 } 043 044 public DefaultHealthCheckRegistry(CamelContext camelContext) { 045 this.checks = new CopyOnWriteArraySet<>(); 046 047 this.repositories = new CopyOnWriteArraySet<>(); 048 this.repositories.add(new RegistryRepository()); 049 this.repositories.addAll(repositories); 050 051 setCamelContext(camelContext); 052 } 053 054 // ************************************ 055 // Properties 056 // ************************************ 057 058 @Override 059 public final void setCamelContext(CamelContext camelContext) { 060 this.camelContext = camelContext; 061 062 for (HealthCheck check: checks) { 063 if (check instanceof CamelContextAware) { 064 ((CamelContextAware) check).setCamelContext(camelContext); 065 } 066 } 067 068 for (HealthCheckRepository repository: repositories) { 069 if (repository instanceof CamelContextAware) { 070 ((CamelContextAware) repository).setCamelContext(camelContext); 071 } 072 } 073 } 074 075 @Override 076 public final CamelContext getCamelContext() { 077 return camelContext; 078 } 079 080 @Override 081 public boolean register(HealthCheck check) { 082 boolean result = checks.add(check); 083 if (result) { 084 if (check instanceof CamelContextAware) { 085 ((CamelContextAware) check).setCamelContext(camelContext); 086 } 087 088 LOGGER.debug("HealthCheck with id {} successfully registered", check.getId()); 089 } 090 091 return result; 092 } 093 094 @Override 095 public boolean unregister(HealthCheck check) { 096 boolean result = checks.remove(check); 097 if (result) { 098 LOGGER.debug("HealthCheck with id {} successfully un-registered", check.getId()); 099 } 100 101 return result; 102 } 103 104 @Override 105 public void setRepositories(Collection<HealthCheckRepository> repositories) { 106 repositories.clear(); 107 repositories.addAll(repositories); 108 } 109 110 @Override 111 public Collection<HealthCheckRepository> getRepositories() { 112 return Collections.unmodifiableCollection(repositories); 113 } 114 115 @Override 116 public boolean addRepository(HealthCheckRepository repository) { 117 boolean result = repositories.add(repository); 118 if (result) { 119 if (repository instanceof CamelContextAware) { 120 ((CamelContextAware) repository).setCamelContext(getCamelContext()); 121 122 LOGGER.debug("HealthCheckRepository {} successfully registered", repository); 123 } 124 } 125 126 return result; 127 } 128 129 @Override 130 public boolean removeRepository(HealthCheckRepository repository) { 131 boolean result = repositories.remove(repository); 132 if (result) { 133 LOGGER.debug("HealthCheckRepository with {} successfully un-registered", repository); 134 } 135 136 return result; 137 } 138 139 // ************************************ 140 // 141 // ************************************ 142 143 @Override 144 public Stream<HealthCheck> stream() { 145 return Stream.concat( 146 checks.stream(), 147 repositories.stream().flatMap(HealthCheckRepository::stream) 148 ).distinct(); 149 } 150}