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.ManagedPollEnricherMBean;
032import org.apache.camel.model.PollEnrichDefinition;
033import org.apache.camel.processor.PollEnricher;
034import org.apache.camel.spi.EndpointUtilizationStatistics;
035import org.apache.camel.spi.ManagementStrategy;
036import org.apache.camel.util.URISupport;
037
038@ManagedResource(description = "Managed PollEnricher")
039public class ManagedPollEnricher extends ManagedProcessor implements ManagedPollEnricherMBean {
040
041    private String uri;
042    private boolean sanitize;
043
044    public ManagedPollEnricher(CamelContext context, PollEnricher processor, PollEnrichDefinition definition) {
045        super(context, processor, definition);
046    }
047
048    @Override
049    public void init(ManagementStrategy strategy) {
050        super.init(strategy);
051        sanitize = strategy.getManagementAgent().getMask() != null ? strategy.getManagementAgent().getMask() : true;
052        uri = getDefinition().getExpression().getExpression();
053        if (sanitize) {
054            uri = URISupport.sanitizeUri(uri);
055        }
056    }
057
058    @Override
059    public PollEnricher getProcessor() {
060        return (PollEnricher) super.getProcessor();
061    }
062
063    @Override
064    public void reset() {
065        super.reset();
066        if (getProcessor().getEndpointUtilizationStatistics() != null) {
067            getProcessor().getEndpointUtilizationStatistics().clear();
068        }
069    }
070
071    @Override
072    public Boolean getSupportExtendedInformation() {
073        return true;
074    }
075
076    @Override
077    public PollEnrichDefinition getDefinition() {
078        return (PollEnrichDefinition) super.getDefinition();
079    }
080
081    @Override
082    public String getDestination() {
083        return uri;
084    }
085
086    @Override
087    public String getExpressionLanguage() {
088        return getDefinition().getExpression().getLanguage();
089    }
090
091    @Override
092    public String getExpression() {
093        return uri;
094    }
095
096    @Override
097    public String getVariableReceive() {
098        return getProcessor().getVariableReceive();
099    }
100
101    @Override
102    public Long getTimeout() {
103        return getProcessor().getTimeout();
104    }
105
106    @Override
107    public Integer getCacheSize() {
108        return getProcessor().getCacheSize();
109    }
110
111    @Override
112    public Boolean isIgnoreInvalidEndpoint() {
113        return getProcessor().isIgnoreInvalidEndpoint();
114    }
115
116    @Override
117    public Boolean isAggregateOnException() {
118        return getProcessor().isAggregateOnException();
119    }
120
121    @Override
122    public Boolean isAllowOptimisedComponents() {
123        return getProcessor().isAllowOptimisedComponents();
124    }
125
126    @Override
127    public Boolean isOptimised() {
128        return getProcessor().getDynamicAware() != null;
129    }
130
131    @Override
132    public TabularData extendedInformation() {
133        try {
134            TabularData answer = new TabularDataSupport(CamelOpenMBeanTypes.endpointsUtilizationTabularType());
135
136            EndpointUtilizationStatistics stats = getProcessor().getEndpointUtilizationStatistics();
137            if (stats != null) {
138                for (Map.Entry<String, Long> entry : stats.getStatistics().entrySet()) {
139                    CompositeType ct = CamelOpenMBeanTypes.endpointsUtilizationCompositeType();
140                    String url = entry.getKey();
141                    if (sanitize) {
142                        url = URISupport.sanitizeUri(url);
143                    }
144
145                    Long hits = entry.getValue();
146                    if (hits == null) {
147                        hits = 0L;
148                    }
149
150                    CompositeData data
151                            = new CompositeDataSupport(ct, new String[] { "url", "hits" }, new Object[] { url, hits });
152                    answer.put(data);
153                }
154            }
155            return answer;
156        } catch (Exception e) {
157            throw RuntimeCamelException.wrapRuntimeCamelException(e);
158        }
159    }
160
161}