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.List;
020
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.RuntimeCamelException;
029import org.apache.camel.api.management.ManagedResource;
030import org.apache.camel.api.management.mbean.CamelOpenMBeanTypes;
031import org.apache.camel.api.management.mbean.ManagedChoiceMBean;
032import org.apache.camel.model.ChoiceDefinition;
033import org.apache.camel.model.ProcessorDefinition;
034import org.apache.camel.model.WhenDefinition;
035import org.apache.camel.processor.ChoiceProcessor;
036import org.apache.camel.processor.FilterProcessor;
037
038@ManagedResource(description = "Managed Choice")
039public class ManagedChoice extends ManagedProcessor implements ManagedChoiceMBean {
040
041    public ManagedChoice(CamelContext context, ChoiceProcessor processor, ProcessorDefinition<?> definition) {
042        super(context, processor, definition);
043    }
044
045    @Override
046    public ChoiceProcessor getProcessor() {
047        return (ChoiceProcessor) super.getProcessor();
048    }
049
050    @Override
051    public ChoiceDefinition getDefinition() {
052        return (ChoiceDefinition) super.getDefinition();
053    }
054
055    @Override
056    public void reset() {
057        getProcessor().reset();
058        super.reset();
059    }
060
061    @Override
062    public Boolean getSupportExtendedInformation() {
063        return true;
064    }
065
066    @Override
067    public TabularData extendedInformation() {
068        try {
069            TabularData answer = new TabularDataSupport(CamelOpenMBeanTypes.choiceTabularType());
070
071            List<WhenDefinition> whens = getDefinition().getWhenClauses();
072            List<FilterProcessor> filters = getProcessor().getFilters();
073
074            for (int i = 0; i < filters.size(); i++) {
075                WhenDefinition when = whens.get(i);
076                FilterProcessor filter = filters.get(i);
077
078                CompositeType ct = CamelOpenMBeanTypes.choiceCompositeType();
079                String predicate = when.getExpression().getExpression();
080                String language = when.getExpression().getLanguage();
081                long matches = filter.getFilteredCount();
082
083                CompositeData data = new CompositeDataSupport(
084                        ct,
085                        new String[] { "predicate", "language", "matches" },
086                        new Object[] { predicate, language, matches });
087                answer.put(data);
088            }
089            if (getDefinition().getOtherwise() != null) {
090                CompositeType ct = CamelOpenMBeanTypes.choiceCompositeType();
091                String predicate = "otherwise";
092                String language = "";
093                long matches = getProcessor().getNotFilteredCount();
094
095                CompositeData data = new CompositeDataSupport(
096                        ct,
097                        new String[] { "predicate", "language", "matches" },
098                        new Object[] { predicate, language, matches });
099                answer.put(data);
100            }
101
102            return answer;
103        } catch (Exception e) {
104            throw RuntimeCamelException.wrapRuntimeCamelException(e);
105        }
106    }
107}