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