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.ArrayList; 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.Channel; 030import org.apache.camel.Navigate; 031import org.apache.camel.Processor; 032import org.apache.camel.RuntimeCamelException; 033import org.apache.camel.api.management.ManagedResource; 034import org.apache.camel.api.management.mbean.CamelOpenMBeanTypes; 035import org.apache.camel.api.management.mbean.ManagedDoTryMBean; 036import org.apache.camel.model.CatchDefinition; 037import org.apache.camel.model.TryDefinition; 038import org.apache.camel.processor.CatchProcessor; 039import org.apache.camel.processor.TryProcessor; 040 041@ManagedResource(description = "Managed DoTry") 042public class ManagedDoTry extends ManagedProcessor implements ManagedDoTryMBean { 043 044 private final TryProcessor processor; 045 private final List<CatchProcessor> catchProcessors; 046 047 public ManagedDoTry(CamelContext context, TryProcessor processor, TryDefinition definition) { 048 super(context, processor, definition); 049 this.processor = processor; 050 051 if (processor.getCatchClauses() != null) { 052 catchProcessors = new ArrayList<>(); 053 for (Processor p : processor.getCatchClauses()) { 054 Channel c = (Channel) p; 055 CatchProcessor caught = asCatchProcessor(c); 056 catchProcessors.add(caught); 057 } 058 } else { 059 catchProcessors = null; 060 } 061 } 062 063 @Override 064 public TryDefinition getDefinition() { 065 return (TryDefinition) super.getDefinition(); 066 } 067 068 @Override 069 public Boolean getSupportExtendedInformation() { 070 return true; 071 } 072 073 @Override 074 public TabularData extendedInformation() { 075 try { 076 TabularData answer = new TabularDataSupport(CamelOpenMBeanTypes.doTryTabularType()); 077 078 if (catchProcessors != null) { 079 List<CatchDefinition> exceptions = getDefinition().getCatchClauses(); 080 for (int i = 0; i < catchProcessors.size(); i++) { 081 CatchDefinition when = exceptions.get(i); 082 CatchProcessor caught = catchProcessors.get(i); 083 if (caught != null) { 084 for (String fqn : caught.getCaughtExceptionClassNames()) { 085 CompositeType ct = CamelOpenMBeanTypes.doTryCompositeType(); 086 String predicate = null; 087 String language = null; 088 if (when.getOnWhen() != null) { 089 predicate = when.getOnWhen().getExpression().getExpression(); 090 language = when.getOnWhen().getExpression().getLanguage(); 091 } 092 long matches = caught.getCaughtCount(fqn); 093 094 CompositeData data = new CompositeDataSupport( 095 ct, 096 new String[] { "exception", "predicate", "language", "matches" }, 097 new Object[] { fqn, predicate, language, matches }); 098 answer.put(data); 099 } 100 } 101 } 102 } 103 return answer; 104 } catch (Exception e) { 105 throw RuntimeCamelException.wrapRuntimeCamelException(e); 106 } 107 } 108 109 private CatchProcessor asCatchProcessor(Navigate<Processor> nav) { 110 // drill down and find 111 while (nav.hasNext()) { 112 for (Processor p : nav.next()) { 113 if (p instanceof CatchProcessor) { 114 return (CatchProcessor) p; 115 } 116 if (p instanceof Navigate<?>) { 117 Navigate<Processor> child = (Navigate<Processor>) p; 118 return asCatchProcessor(child); 119 } 120 } 121 } 122 return null; 123 } 124 125}