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.Iterator;
020import java.util.List;
021
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.ManagedResource;
030import org.apache.camel.api.management.mbean.CamelOpenMBeanTypes;
031import org.apache.camel.api.management.mbean.ManagedCircuitBreakerLoadBalancerMBean;
032import org.apache.camel.model.LoadBalanceDefinition;
033import org.apache.camel.processor.loadbalancer.CircuitBreakerLoadBalancer;
034import org.apache.camel.processor.loadbalancer.ExceptionFailureStatistics;
035import org.apache.camel.util.CollectionStringBuffer;
036import org.apache.camel.util.ObjectHelper;
037
038/**
039 * @version 
040 */
041@ManagedResource(description = "Managed CircuitBreaker LoadBalancer")
042public class ManagedCircuitBreakerLoadBalancer extends ManagedProcessor implements ManagedCircuitBreakerLoadBalancerMBean {
043    private final CircuitBreakerLoadBalancer processor;
044    private String exceptions;
045
046    public ManagedCircuitBreakerLoadBalancer(CamelContext context, CircuitBreakerLoadBalancer processor, LoadBalanceDefinition definition) {
047        super(context, processor, definition);
048        this.processor = processor;
049    }
050
051    @Override
052    public Boolean getSupportExtendedInformation() {
053        return true;
054    }
055
056    @Override
057    public Integer getSize() {
058        return processor.getProcessors().size();
059    }
060
061    @Override
062    public Long getHalfOpenAfter() {
063        return processor.getHalfOpenAfter();
064    }
065
066    @Override
067    public Integer getThreshold() {
068        return processor.getThreshold();
069    }
070
071    @Override
072    public String getExceptions() {
073        if (exceptions != null) {
074            return exceptions;
075        }
076
077        List<Class<?>> classes = processor.getExceptions();
078        if (classes == null || classes.isEmpty()) {
079            exceptions = "";
080        } else {
081            CollectionStringBuffer csb = new CollectionStringBuffer(",");
082            for (Class<?> clazz : classes) {
083                csb.append(clazz.getCanonicalName());
084            }
085            exceptions = csb.toString();
086        }
087        return exceptions;
088    }
089
090    @Override
091    public String getCircuitBreakerState() {
092        int num = processor.getState();
093        if (num == 0) {
094            return "closed";
095        } else if (num == 1) {
096            return "half open";
097        } else {
098            return "open";
099        }
100    }
101
102    @Override
103    public String dumpState() {
104        return processor.dumpState();
105    }
106
107    @Override
108    public TabularData exceptionStatistics() {
109        try {
110            TabularData answer = new TabularDataSupport(CamelOpenMBeanTypes.loadbalancerExceptionsTabularType());
111
112            ExceptionFailureStatistics statistics = processor.getExceptionFailureStatistics();
113
114            Iterator<Class<?>> it = statistics.getExceptions();
115            boolean empty = true;
116            while (it.hasNext()) {
117                empty = false;
118                Class<?> exception = it.next();
119                String name = ObjectHelper.name(exception);
120                long counter = statistics.getFailureCounter(exception);
121
122                CompositeType ct = CamelOpenMBeanTypes.loadbalancerExceptionsCompositeType();
123                CompositeData data = new CompositeDataSupport(ct,
124                        new String[]{"exception", "failures"},
125                        new Object[]{name, counter});
126                answer.put(data);
127            }
128            if (empty) {
129                // use Exception as a single general
130                String name = ObjectHelper.name(Exception.class);
131                long counter = statistics.getFailureCounter(Exception.class);
132
133                CompositeType ct = CamelOpenMBeanTypes.loadbalancerExceptionsCompositeType();
134                CompositeData data = new CompositeDataSupport(ct,
135                        new String[]{"exception", "failures"},
136                        new Object[]{name, counter});
137                answer.put(data);
138            }
139
140            return answer;
141        } catch (Exception e) {
142            throw ObjectHelper.wrapRuntimeCamelException(e);
143        }
144    }
145
146}