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