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