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.management.mbean;
018
019import java.util.Collection;
020import java.util.Collections;
021import java.util.Optional;
022import javax.management.openmbean.CompositeData;
023import javax.management.openmbean.CompositeDataSupport;
024import javax.management.openmbean.CompositeType;
025import javax.management.openmbean.TabularData;
026import javax.management.openmbean.TabularDataSupport;
027
028import org.apache.camel.CamelContext;
029import org.apache.camel.api.management.mbean.CamelOpenMBeanTypes;
030import org.apache.camel.api.management.mbean.ManagedCamelHealthMBean;
031import org.apache.camel.health.HealthCheck;
032import org.apache.camel.health.HealthCheckHelper;
033import org.apache.camel.health.HealthCheckRegistry;
034import org.apache.camel.spi.ManagementStrategy;
035import org.apache.camel.util.ObjectHelper;
036
037public class ManagedCamelHealth implements ManagedCamelHealthMBean {
038    private final CamelContext context;
039
040    public ManagedCamelHealth(CamelContext context) {
041        this.context = context;
042    }
043
044    public void init(ManagementStrategy strategy) {
045        // do nothing
046    }
047
048    public CamelContext getContext() {
049        return context;
050    }
051
052    @Override
053    public boolean getIsHealthy() {
054        for (HealthCheck.Result result: HealthCheckHelper.invoke(context)) {
055            if (result.getState() == HealthCheck.State.DOWN) {
056                return false;
057            }
058        }
059
060        return true;
061    }
062
063    @Override
064    public Collection<String> getHealthChecksIDs() {
065        HealthCheckRegistry registry = context.getHealthCheckRegistry();
066        if (registry != null) {
067            return registry.getCheckIDs();
068        }
069
070        return Collections.emptyList();
071    }
072
073    @Override
074    public TabularData details() {
075        try {
076            final TabularData answer = new TabularDataSupport(CamelOpenMBeanTypes.camelHealthDetailsTabularType());
077            final CompositeType type = CamelOpenMBeanTypes.camelHealthDetailsCompositeType();
078
079            for (HealthCheck.Result result: HealthCheckHelper.invoke(context)) {
080                CompositeData data = new CompositeDataSupport(
081                    type,
082                    new String[] {
083                        "id",
084                        "group",
085                        "state",
086                        "enabled",
087                        "interval",
088                        "failureThreshold"
089                    },
090                    new Object[] {
091                        result.getCheck().getId(),
092                        result.getCheck().getGroup(),
093                        result.getState().name(),
094                        result.getCheck().getConfiguration().isEnabled(),
095                        result.getCheck().getConfiguration().getInterval() != null
096                            ? result.getCheck().getConfiguration().getInterval().toMillis()
097                            : null,
098                        result.getCheck().getConfiguration().getFailureThreshold()
099                    }
100                );
101
102                answer.put(data);
103            }
104
105            return answer;
106        } catch (Exception e) {
107            throw ObjectHelper.wrapRuntimeCamelException(e);
108        }
109    }
110
111    @Override
112    public String invoke(String id) {
113        Optional<HealthCheck.Result> result = HealthCheckHelper.invoke(context, id, Collections.emptyMap());
114
115        return result.map(r -> r.getState().name()).orElse(HealthCheck.State.UNKNOWN.name());
116    }
117}