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