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.api.management.ManagedResource;
029import org.apache.camel.api.management.mbean.CamelOpenMBeanTypes;
030import org.apache.camel.api.management.mbean.ManagedValidatorRegistryMBean;
031import org.apache.camel.spi.DataType;
032import org.apache.camel.spi.ManagementStrategy;
033import org.apache.camel.spi.Validator;
034import org.apache.camel.spi.ValidatorRegistry;
035import org.apache.camel.util.ObjectHelper;
036
037/**
038 * @version 
039 */
040@ManagedResource(description = "Managed ValidatorRegistry")
041public class ManagedValidatorRegistry extends ManagedService implements ManagedValidatorRegistryMBean {
042    private final ValidatorRegistry validatorRegistry;
043
044    public ManagedValidatorRegistry(CamelContext context, ValidatorRegistry validatorRegistry) {
045        super(context, validatorRegistry);
046        this.validatorRegistry = validatorRegistry;
047    }
048
049    public void init(ManagementStrategy strategy) {
050        super.init(strategy);
051    }
052
053    public ValidatorRegistry getValidatorRegistry() {
054        return validatorRegistry;
055    }
056
057    public String getSource() {
058        return validatorRegistry.toString();
059    }
060
061    public Integer getDynamicSize() {
062        return validatorRegistry.dynamicSize();
063    }
064
065    public Integer getStaticSize() {
066        return validatorRegistry.staticSize();
067    }
068
069    public Integer getSize() {
070        return validatorRegistry.size();
071    }
072
073    public Integer getMaximumCacheSize() {
074        return validatorRegistry.getMaximumCacheSize();
075    }
076
077    public void purge() {
078        validatorRegistry.purge();
079    }
080
081    @SuppressWarnings("unchecked")
082    public TabularData listValidators() {
083        try {
084            TabularData answer = new TabularDataSupport(CamelOpenMBeanTypes.listValidatorsTabularType());
085            Collection<Validator> validators = validatorRegistry.values();
086            for (Validator validator : validators) {
087                CompositeType ct = CamelOpenMBeanTypes.listValidatorsCompositeType();
088                DataType type = validator.getType();
089                String desc = validator.toString();
090                boolean isStatic = validatorRegistry.isStatic(type);
091                boolean isDynamic = validatorRegistry.isDynamic(type);
092
093                CompositeData data = new CompositeDataSupport(ct, new String[]{"type", "static", "dynamic", "description"},
094                                                              new Object[]{type.toString(), isStatic, isDynamic, desc});
095                answer.put(data);
096            }
097            return answer;
098        } catch (Exception e) {
099            throw ObjectHelper.wrapRuntimeCamelException(e);
100        }
101    }
102
103}