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