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;
018
019import java.util.Map;
020
021import org.apache.camel.NamedNode;
022import org.apache.camel.Processor;
023import org.apache.camel.management.mbean.ManagedPerformanceCounter;
024import org.apache.camel.spi.ManagementInterceptStrategy;
025import org.apache.camel.util.KeyValueHolder;
026
027/**
028 * This strategy class wraps targeted processors with a
029 * {@link InstrumentationProcessor}. Each InstrumentationProcessor has an
030 * embedded {@link ManagedPerformanceCounter} for monitoring performance metrics.
031 * <p/>
032 * This class looks up a map to determine which PerformanceCounter should go into the
033 * InstrumentationProcessor for any particular target processor.
034 */
035public class InstrumentationInterceptStrategy implements ManagementInterceptStrategy {
036
037    private Map<NamedNode, PerformanceCounter> registeredCounters;
038    private final Map<Processor, KeyValueHolder<NamedNode, InstrumentationProcessor>> wrappedProcessors;
039
040    public InstrumentationInterceptStrategy(Map<NamedNode, PerformanceCounter> registeredCounters,
041            Map<Processor, KeyValueHolder<NamedNode, InstrumentationProcessor>> wrappedProcessors) {
042        this.registeredCounters = registeredCounters;
043        this.wrappedProcessors = wrappedProcessors;
044    }
045
046    @Override
047    public InstrumentationProcessor<?> createProcessor(String type) {
048        return new DefaultInstrumentationProcessor(type);
049    }
050
051    @Override
052    public InstrumentationProcessor<?> createProcessor(NamedNode definition, Processor target) {
053        InstrumentationProcessor instrumentationProcessor = new DefaultInstrumentationProcessor(definition.getShortName(), target);
054        PerformanceCounter counter = registeredCounters.get(definition);
055        if (counter != null) {
056            // add it to the mapping of wrappers so we can later change it to a
057            // decorated counter when we register the processor
058            KeyValueHolder<NamedNode, InstrumentationProcessor> holder = new KeyValueHolder<>(definition, instrumentationProcessor);
059            wrappedProcessors.put(target, holder);
060        }
061        return instrumentationProcessor;
062    }
063
064}