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