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.Map;
020import java.util.Set;
021import java.util.stream.Collectors;
022
023import javax.management.openmbean.CompositeData;
024import javax.management.openmbean.CompositeDataSupport;
025import javax.management.openmbean.CompositeType;
026import javax.management.openmbean.TabularData;
027import javax.management.openmbean.TabularDataSupport;
028
029import org.apache.camel.CamelContext;
030import org.apache.camel.RuntimeCamelException;
031import org.apache.camel.api.management.ManagedResource;
032import org.apache.camel.api.management.mbean.CamelOpenMBeanTypes;
033import org.apache.camel.api.management.mbean.ManagedVariableRepositoryMBean;
034import org.apache.camel.spi.BrowsableVariableRepository;
035
036@ManagedResource(description = "Managed VariableRepository")
037public class ManagedVariableRepository extends ManagedService implements ManagedVariableRepositoryMBean {
038    private final BrowsableVariableRepository variableRepository;
039
040    public ManagedVariableRepository(CamelContext context, BrowsableVariableRepository variableRepository) {
041        super(context, variableRepository);
042        this.variableRepository = variableRepository;
043    }
044
045    public BrowsableVariableRepository getVariableRepository() {
046        return variableRepository;
047    }
048
049    @Override
050    public String getId() {
051        return variableRepository.getId();
052    }
053
054    @Override
055    public int getSize() {
056        return variableRepository.size();
057    }
058
059    @Override
060    public void clear() {
061        variableRepository.clear();
062    }
063
064    @Override
065    public Set<String> names() {
066        return variableRepository.names().collect(Collectors.toSet());
067    }
068
069    @Override
070    public TabularData variables() {
071        try {
072            final TabularData answer = new TabularDataSupport(CamelOpenMBeanTypes.camelVariablesTabularType());
073            final CompositeType type = CamelOpenMBeanTypes.camelVariablesCompositeType();
074
075            for (Map.Entry<String, Object> entry : variableRepository.getVariables().entrySet()) {
076                String key = entry.getKey();
077                String className = entry.getValue() != null ? entry.getValue().getClass().getName() : "";
078                String value = entry.getValue() != null ? entry.getValue().toString() : "";
079                if (value.length() > 1000) {
080                    value = value.substring(0, 1000) + "...";
081                }
082
083                CompositeData data = new CompositeDataSupport(
084                        type,
085                        new String[] {
086                                "id",
087                                "key",
088                                "className",
089                                "value",
090                        },
091                        new Object[] {
092                                getId(),
093                                key,
094                                className,
095                                value
096                        });
097                answer.put(data);
098            }
099
100            return answer;
101        } catch (Exception e) {
102            throw RuntimeCamelException.wrapRuntimeCamelException(e);
103        }
104    }
105
106}