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.List;
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.ManagedTypeConverterRegistryMBean;
030import org.apache.camel.spi.TypeConverterRegistry;
031import org.apache.camel.util.ObjectHelper;
032
033/**
034 *
035 */
036@ManagedResource(description = "Managed TypeConverterRegistry")
037public class ManagedTypeConverterRegistry extends ManagedService implements ManagedTypeConverterRegistryMBean {
038
039    private final TypeConverterRegistry registry;
040
041    public ManagedTypeConverterRegistry(CamelContext context, TypeConverterRegistry registry) {
042        super(context, registry);
043        this.registry = registry;
044    }
045
046    public TypeConverterRegistry getRegistry() {
047        return registry;
048    }
049
050    public long getNoopCounter() {
051        return registry.getStatistics().getNoopCounter();
052    }
053
054    public long getAttemptCounter() {
055        return registry.getStatistics().getAttemptCounter();
056    }
057
058    public long getHitCounter() {
059        return registry.getStatistics().getHitCounter();
060    }
061
062    public long getBaseHitCounter() {
063        return registry.getStatistics().getBaseHitCounter();
064    }
065
066    public long getMissCounter() {
067        return registry.getStatistics().getMissCounter();
068    }
069
070    public long getFailedCounter() {
071        return registry.getStatistics().getFailedCounter();
072    }
073
074    public void resetTypeConversionCounters() {
075        registry.getStatistics().reset();
076    }
077
078    public boolean isStatisticsEnabled() {
079        return registry.getStatistics().isStatisticsEnabled();
080    }
081
082    public void setStatisticsEnabled(boolean statisticsEnabled) {
083        registry.getStatistics().setStatisticsEnabled(statisticsEnabled);
084    }
085
086    public int getNumberOfTypeConverters() {
087        return registry.size();
088    }
089
090    public String getTypeConverterExistsLoggingLevel() {
091        return registry.getTypeConverterExistsLoggingLevel().name();
092    }
093
094    public String getTypeConverterExists() {
095        return registry.getTypeConverterExists().name();
096    }
097
098    public boolean hasTypeConverter(String fromType, String toType) {
099        try {
100            Class<?> from = getContext().getClassResolver().resolveMandatoryClass(fromType);
101            Class<?> to = getContext().getClassResolver().resolveMandatoryClass(toType);
102            return registry.lookup(to, from) != null;
103        } catch (ClassNotFoundException e) {
104            throw ObjectHelper.wrapRuntimeCamelException(e);
105        }
106    }
107
108    public TabularData listTypeConverters() {
109        try {
110            TabularData answer = new TabularDataSupport(CamelOpenMBeanTypes.listTypeConvertersTabularType());
111            List<Class<?>[]> converters = registry.listAllTypeConvertersFromTo();
112            for (Class<?>[] entry : converters) {
113                CompositeType ct = CamelOpenMBeanTypes.listTypeConvertersCompositeType();
114                String from = entry[0].getCanonicalName();
115                String to = entry[1].getCanonicalName();
116                CompositeData data = new CompositeDataSupport(ct, new String[]{"from", "to"}, new Object[]{from, to});
117                if (!answer.containsValue(data)) {
118                    answer.put(data);
119                }
120            }
121            return answer;
122        } catch (Exception e) {
123            throw ObjectHelper.wrapRuntimeCamelException(e);
124        }
125    }
126}