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