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.Collection;
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.Endpoint;
029import org.apache.camel.api.management.ManagedResource;
030import org.apache.camel.api.management.mbean.CamelOpenMBeanTypes;
031import org.apache.camel.api.management.mbean.ManagedEndpointRegistryMBean;
032import org.apache.camel.spi.EndpointRegistry;
033import org.apache.camel.spi.ManagementStrategy;
034import org.apache.camel.util.ObjectHelper;
035import org.apache.camel.util.URISupport;
036
037/**
038 * @version 
039 */
040@ManagedResource(description = "Managed EndpointRegistry")
041public class ManagedEndpointRegistry extends ManagedService implements ManagedEndpointRegistryMBean {
042    private final EndpointRegistry endpointRegistry;
043    private boolean sanitize;
044
045    public ManagedEndpointRegistry(CamelContext context, EndpointRegistry endpointRegistry) {
046        super(context, endpointRegistry);
047        this.endpointRegistry = endpointRegistry;
048    }
049
050    public void init(ManagementStrategy strategy) {
051        super.init(strategy);
052        sanitize = strategy.getManagementAgent().getMask() != null ? strategy.getManagementAgent().getMask() : false;
053    }
054
055    public EndpointRegistry getEndpointRegistry() {
056        return endpointRegistry;
057    }
058
059    public String getSource() {
060        return endpointRegistry.toString();
061    }
062
063    public Integer getDynamicSize() {
064        return endpointRegistry.dynamicSize();
065    }
066
067    public Integer getStaticSize() {
068        return endpointRegistry.staticSize();
069    }
070
071    public Integer getSize() {
072        return endpointRegistry.size();
073    }
074
075    public Integer getMaximumCacheSize() {
076        return endpointRegistry.getMaximumCacheSize();
077    }
078
079    public void purge() {
080        endpointRegistry.purge();
081    }
082
083    @SuppressWarnings("unchecked")
084    public TabularData listEndpoints() {
085        try {
086            TabularData answer = new TabularDataSupport(CamelOpenMBeanTypes.listEndpointsTabularType());
087            Collection<Endpoint> endpoints = endpointRegistry.values();
088            for (Endpoint endpoint : endpoints) {
089                CompositeType ct = CamelOpenMBeanTypes.listEndpointsCompositeType();
090                String url = endpoint.getEndpointUri();
091                if (sanitize) {
092                    url = URISupport.sanitizeUri(url);
093                }
094
095                boolean fromStatic = endpointRegistry.isStatic(url);
096                boolean fromDynamic = endpointRegistry.isDynamic(url);
097
098                CompositeData data = new CompositeDataSupport(ct, new String[]{"url", "static", "dynamic"}, new Object[]{url, fromStatic, fromDynamic});
099                answer.put(data);
100            }
101            return answer;
102        } catch (Exception e) {
103            throw ObjectHelper.wrapRuntimeCamelException(e);
104        }
105    }
106
107}