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