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