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.List;
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.ManagedRuntimeEndpointRegistryMBean;
030import org.apache.camel.spi.EndpointRegistry;
031import org.apache.camel.spi.ManagementStrategy;
032import org.apache.camel.spi.RuntimeEndpointRegistry;
033import org.apache.camel.util.ObjectHelper;
034import org.apache.camel.util.URISupport;
035
036@ManagedResource(description = "Managed RuntimeEndpointRegistry")
037public class ManagedRuntimeEndpointRegistry extends ManagedService implements ManagedRuntimeEndpointRegistryMBean {
038
039    private final RuntimeEndpointRegistry registry;
040    private boolean sanitize;
041
042    public ManagedRuntimeEndpointRegistry(CamelContext context, RuntimeEndpointRegistry registry) {
043        super(context, registry);
044        this.registry = registry;
045    }
046
047    public void init(ManagementStrategy strategy) {
048        super.init(strategy);
049        sanitize = strategy.getManagementAgent().getMask() != null ? strategy.getManagementAgent().getMask() : false;
050    }
051
052    @Override
053    public void clear() {
054        registry.clear();
055    }
056
057    @Override
058    public void reset() {
059        registry.reset();
060    }
061
062    @Override
063    public boolean isEnabled() {
064        return registry.isEnabled();
065    }
066
067    @Override
068    public void setEnabled(boolean enabled) {
069        registry.setEnabled(enabled);
070    }
071
072    @Override
073    public int getLimit() {
074        return registry.getLimit();
075    }
076
077    @Override
078    public int getSize() {
079        return registry.size();
080    }
081
082    @Override
083    public List<String> getAllEndpoints(boolean includeInputs) {
084        return registry.getAllEndpoints(includeInputs);
085    }
086
087    @Override
088    public List<String> getEndpointsPerRoute(String routeId, boolean includeInputs) {
089        return registry.getEndpointsPerRoute(routeId, includeInputs);
090    }
091
092    @Override
093    public TabularData endpointStatistics() {
094        try {
095            TabularData answer = new TabularDataSupport(CamelOpenMBeanTypes.listRuntimeEndpointsTabularType());
096
097            EndpointRegistry staticRegistry = getContext().getEndpointRegistry();
098            int index = 0;
099
100            for (RuntimeEndpointRegistry.Statistic stat : registry.getEndpointStatistics()) {
101                CompositeType ct = CamelOpenMBeanTypes.listRuntimeEndpointsCompositeType();
102
103                String url = stat.getUri();
104                Boolean isStatic = staticRegistry.isStatic(url);
105                Boolean isDynamic = staticRegistry.isDynamic(url);
106                if (sanitize) {
107                    url = URISupport.sanitizeUri(url);
108                }
109                String routeId = stat.getRouteId();
110                String direction = stat.getDirection();
111                long hits = stat.getHits();
112
113                CompositeData data = new CompositeDataSupport(ct, new String[]{"index", "url", "routeId", "direction", "static", "dynamic", "hits"},
114                        new Object[]{index, url, routeId, direction, isStatic, isDynamic, hits});
115                answer.put(data);
116
117                // use a counter as the single index in the TabularData as we do not want a multi-value index
118                index++;
119            }
120            return answer;
121        } catch (Exception e) {
122            throw ObjectHelper.wrapRuntimeCamelException(e);
123        }
124    }
125}