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 private final ChoiceProcessor processor; 041 042 public ManagedChoice(CamelContext context, ChoiceProcessor processor, ProcessorDefinition<?> definition) { 043 super(context, processor, definition); 044 this.processor = processor; 045 } 046 047 @Override 048 public ChoiceDefinition getDefinition() { 049 return (ChoiceDefinition) super.getDefinition(); 050 } 051 052 @Override 053 public void reset() { 054 processor.reset(); 055 super.reset(); 056 } 057 058 @Override 059 public Boolean getSupportExtendedInformation() { 060 return true; 061 } 062 063 @Override 064 public TabularData choiceStatistics() { 065 try { 066 TabularData answer = new TabularDataSupport(CamelOpenMBeanTypes.choiceTabularType()); 067 068 List<WhenDefinition> whens = getDefinition().getWhenClauses(); 069 List<FilterProcessor> filters = processor.getFilters(); 070 071 for (int i = 0; i < filters.size(); i++) { 072 WhenDefinition when = whens.get(i); 073 FilterProcessor filter = filters.get(i); 074 075 CompositeType ct = CamelOpenMBeanTypes.choiceCompositeType(); 076 String predicate = when.getExpression().getExpression(); 077 String language = when.getExpression().getLanguage(); 078 long matches = filter.getFilteredCount(); 079 080 CompositeData data = new CompositeDataSupport( 081 ct, 082 new String[] { "predicate", "language", "matches" }, 083 new Object[] { predicate, language, matches }); 084 answer.put(data); 085 } 086 if (getDefinition().getOtherwise() != null) { 087 CompositeType ct = CamelOpenMBeanTypes.choiceCompositeType(); 088 String predicate = "otherwise"; 089 String language = ""; 090 long matches = processor.getNotFilteredCount(); 091 092 CompositeData data = new CompositeDataSupport( 093 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 RuntimeCamelException.wrapRuntimeCamelException(e); 102 } 103 } 104}