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