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