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