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.Map;
020import javax.management.openmbean.CompositeData;
021import javax.management.openmbean.CompositeDataSupport;
022import javax.management.openmbean.CompositeType;
023import javax.management.openmbean.TabularData;
024import javax.management.openmbean.TabularDataSupport;
025
026import org.apache.camel.CamelContext;
027import org.apache.camel.api.management.ManagedResource;
028import org.apache.camel.api.management.mbean.CamelOpenMBeanTypes;
029import org.apache.camel.api.management.mbean.ManagedSendDynamicProcessorMBean;
030import org.apache.camel.model.ProcessorDefinition;
031import org.apache.camel.processor.SendDynamicProcessor;
032import org.apache.camel.spi.EndpointUtilizationStatistics;
033import org.apache.camel.spi.ManagementStrategy;
034import org.apache.camel.util.ObjectHelper;
035import org.apache.camel.util.URISupport;
036
037/**
038 * @version 
039 */
040@ManagedResource(description = "Managed SendDynamicProcessor")
041public class ManagedSendDynamicProcessor extends ManagedProcessor implements ManagedSendDynamicProcessorMBean {
042    private final SendDynamicProcessor processor;
043    private String uri;
044    private boolean sanitize;
045
046    public ManagedSendDynamicProcessor(CamelContext context, SendDynamicProcessor processor, ProcessorDefinition<?> definition) {
047        super(context, processor, definition);
048        this.processor = processor;
049    }
050
051    public void init(ManagementStrategy strategy) {
052        super.init(strategy);
053        this.sanitize = strategy.getManagementAgent().getMask() != null ? strategy.getManagementAgent().getMask() : false;
054        if (sanitize) {
055            uri = URISupport.sanitizeUri(processor.getUri());
056        } else {
057            uri = processor.getUri();
058        }
059    }
060
061    @Override
062    public void reset() {
063        super.reset();
064        if (processor.getEndpointUtilizationStatistics() != null) {
065            processor.getEndpointUtilizationStatistics().clear();
066        }
067    }
068
069    @Override
070    public Boolean getSupportExtendedInformation() {
071        return true;
072    }
073
074    public SendDynamicProcessor getProcessor() {
075        return processor;
076    }
077
078    public String getUri() {
079        return uri;
080    }
081
082    public String getMessageExchangePattern() {
083        if (processor.getPattern() != null) {
084            return processor.getPattern().name();
085        } else {
086            return null;
087        }
088    }
089
090    public Integer getCacheSize() {
091        return processor.getCacheSize();
092    }
093
094    public Boolean isIgnoreInvalidEndpoint() {
095        return processor.isIgnoreInvalidEndpoint();
096    }
097
098    public Boolean isAllowOptimisedComponents() {
099        return processor.isAllowOptimisedComponents();
100    }
101
102    public Boolean isOptimised() {
103        return processor.getDynamicAware() != null;
104    }
105
106    @Override
107    public TabularData extendedInformation() {
108        try {
109            TabularData answer = new TabularDataSupport(CamelOpenMBeanTypes.endpointsUtilizationTabularType());
110
111            EndpointUtilizationStatistics stats = processor.getEndpointUtilizationStatistics();
112            if (stats != null) {
113                for (Map.Entry<String, Long> entry : stats.getStatistics().entrySet()) {
114                    CompositeType ct = CamelOpenMBeanTypes.endpointsUtilizationCompositeType();
115                    String url = entry.getKey();
116                    if (sanitize) {
117                        url = URISupport.sanitizeUri(url);
118                    }
119
120                    Long hits = entry.getValue();
121                    if (hits == null) {
122                        hits = 0L;
123                    }
124
125                    CompositeData data = new CompositeDataSupport(ct, new String[]{"url", "hits"}, new Object[]{url, hits});
126                    answer.put(data);
127                }
128            }
129            return answer;
130        } catch (Exception e) {
131            throw ObjectHelper.wrapRuntimeCamelException(e);
132        }
133    }
134
135}