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 java.util.Map;
021
022import javax.management.openmbean.CompositeData;
023import javax.management.openmbean.CompositeDataSupport;
024import javax.management.openmbean.CompositeType;
025import javax.management.openmbean.TabularData;
026import javax.management.openmbean.TabularDataSupport;
027
028import org.apache.camel.CamelContext;
029import org.apache.camel.ServiceStatus;
030import org.apache.camel.StatefulService;
031import org.apache.camel.api.management.ManagedInstance;
032import org.apache.camel.api.management.ManagedResource;
033import org.apache.camel.api.management.mbean.CamelOpenMBeanTypes;
034import org.apache.camel.api.management.mbean.ManagedDataFormatMBean;
035import org.apache.camel.spi.DataFormat;
036import org.apache.camel.spi.DataFormatName;
037import org.apache.camel.spi.ManagementStrategy;
038import org.apache.camel.util.JsonSchemaHelper;
039import org.apache.camel.util.ObjectHelper;
040
041@ManagedResource(description = "Managed DataFormat")
042public class ManagedDataFormat implements ManagedInstance, ManagedDataFormatMBean {
043    private final CamelContext camelContext;
044    private final DataFormat dataFormat;
045
046    public ManagedDataFormat(CamelContext camelContext, DataFormat dataFormat) {
047        this.camelContext = camelContext;
048        this.dataFormat = dataFormat;
049    }
050
051    public void init(ManagementStrategy strategy) {
052        // noop
053    }
054
055    public DataFormat getDataFormat() {
056        return dataFormat;
057    }
058
059    public CamelContext getContext() {
060        return camelContext;
061    }
062
063    @Override
064    public String getName() {
065        if (dataFormat instanceof DataFormatName) {
066            return ((DataFormatName) dataFormat).getDataFormatName();
067        }
068        return null;
069    }
070
071    @Override
072    public String getCamelId() {
073        return camelContext.getName();
074    }
075
076    @Override
077    public String getCamelManagementName() {
078        return camelContext.getManagementName();
079    }
080
081    @Override
082    public String getState() {
083        // must use String type to be sure remote JMX can read the attribute without requiring Camel classes.
084        if (dataFormat instanceof StatefulService) {
085            ServiceStatus status = ((StatefulService) dataFormat).getStatus();
086            return status.name();
087        }
088
089        // assume started if not a ServiceSupport instance
090        return ServiceStatus.Started.name();
091    }
092
093    @Override
094    public String informationJson() {
095        String dataFormatName = getName();
096        if (dataFormatName != null) {
097            return camelContext.explainDataFormatJson(dataFormatName, dataFormat, true);
098        } else {
099            return null;
100        }
101    }
102
103    @Override
104    public TabularData explain(boolean allOptions) {
105        String dataFormatName = getName();
106        if (dataFormatName != null) {
107            try {
108                TabularData answer = new TabularDataSupport(CamelOpenMBeanTypes.explainDataFormatTabularType());
109
110                String json = camelContext.explainDataFormatJson(dataFormatName, dataFormat, allOptions);
111                List<Map<String, String>> rows = JsonSchemaHelper.parseJsonSchema("properties", json, true);
112
113                for (Map<String, String> row : rows) {
114                    String name = row.get("name");
115                    String kind = row.get("kind");
116                    String label = row.get("label") != null ? row.get("label") : "";
117                    String type = row.get("type");
118                    String javaType = row.get("javaType");
119                    String deprecated = row.get("deprecated") != null ? row.get("deprecated") : "";
120                    String secret = row.get("secret") != null ? row.get("secret") : "";
121                    String value = row.get("value") != null ? row.get("value") : "";
122                    String defaultValue = row.get("defaultValue") != null ? row.get("defaultValue") : "";
123                    String description = row.get("description") != null ? row.get("description") : "";
124
125                    CompositeType ct = CamelOpenMBeanTypes.explainDataFormatsCompositeType();
126                    CompositeData data = new CompositeDataSupport(ct,
127                            new String[]{"option", "kind", "label", "type", "java type", "deprecated", "secret", "value", "default value", "description"},
128                            new Object[]{name, kind, label, type, javaType, deprecated, secret, value, defaultValue, description});
129                    answer.put(data);
130                }
131
132                return answer;
133            } catch (Exception e) {
134                throw ObjectHelper.wrapRuntimeCamelException(e);
135            }
136        } else {
137            return null;
138        }
139    }
140
141    @Override
142    public DataFormat getInstance() {
143        return dataFormat;
144    }
145
146}