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 javax.management.openmbean.CompositeData;
020import javax.management.openmbean.CompositeDataSupport;
021import javax.management.openmbean.CompositeType;
022import javax.management.openmbean.TabularData;
023import javax.management.openmbean.TabularDataSupport;
024
025import org.apache.camel.CamelContext;
026import org.apache.camel.RuntimeCamelException;
027import org.apache.camel.api.management.ManagedResource;
028import org.apache.camel.api.management.mbean.CamelOpenMBeanTypes;
029import org.apache.camel.api.management.mbean.ManagedSendProcessorMBean;
030import org.apache.camel.model.ProcessorDefinition;
031import org.apache.camel.processor.SendProcessor;
032import org.apache.camel.spi.ManagementStrategy;
033import org.apache.camel.util.URISupport;
034
035@ManagedResource(description = "Managed SendProcessor")
036public class ManagedSendProcessor extends ManagedProcessor implements ManagedSendProcessorMBean {
037    private final SendProcessor processor;
038    private String destination;
039
040    public ManagedSendProcessor(CamelContext context, SendProcessor processor, ProcessorDefinition<?> definition) {
041        super(context, processor, definition);
042        this.processor = processor;
043    }
044
045    public void init(ManagementStrategy strategy) {
046        super.init(strategy);
047        boolean sanitize = strategy.getManagementAgent().getMask() != null ? strategy.getManagementAgent().getMask() : false;
048        if (sanitize) {
049            destination = URISupport.sanitizeUri(processor.getDestination().getEndpointUri());
050        } else {
051            destination = processor.getDestination().getEndpointUri();
052        }
053    }
054
055    @Override
056    public Boolean getSupportExtendedInformation() {
057        return true;
058    }
059
060    @Override
061    public void reset() {
062        super.reset();
063        processor.reset();
064    }
065
066    public SendProcessor getProcessor() {
067        return processor;
068    }
069
070    public String getDestination() {
071        return destination;
072    }
073
074    public String getMessageExchangePattern() {
075        if (processor.getPattern() != null) {
076            return processor.getPattern().name();
077        } else {
078            return null;
079        }
080    }
081
082    @Override
083    public TabularData extendedInformation() {
084        try {
085            TabularData answer = new TabularDataSupport(CamelOpenMBeanTypes.endpointsUtilizationTabularType());
086
087            // we only have 1 endpoint
088            CompositeType ct = CamelOpenMBeanTypes.endpointsUtilizationCompositeType();
089            String url = getDestination();
090            Long hits = processor.getCounter();
091
092            CompositeData data = new CompositeDataSupport(ct, new String[]{"url", "hits"}, new Object[]{url, hits});
093            answer.put(data);
094            return answer;
095        } catch (Exception e) {
096            throw RuntimeCamelException.wrapRuntimeCamelException(e);
097        }
098    }
099}