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