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