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