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.Endpoint;
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.ManagedEndpointMBean;
035import org.apache.camel.spi.ManagementStrategy;
036import org.apache.camel.util.JsonSchemaHelper;
037import org.apache.camel.util.ObjectHelper;
038
039@ManagedResource(description = "Managed Endpoint")
040public class ManagedEndpoint implements ManagedInstance, ManagedEndpointMBean {
041    private final Endpoint endpoint;
042
043    public ManagedEndpoint(Endpoint endpoint) {
044        this.endpoint = endpoint;
045    }
046
047    public void init(ManagementStrategy strategy) {
048        // noop
049    }
050
051    public Endpoint getEndpoint() {
052        return endpoint;
053    }
054
055    @Override
056    public String getCamelId() {
057        return endpoint.getCamelContext().getName();
058    }
059
060    @Override
061    public String getCamelManagementName() {
062        return endpoint.getCamelContext().getManagementName();
063    }
064
065    @Override
066    public String getEndpointUri() {
067        return endpoint.getEndpointUri();
068    }
069
070    @Override
071    public boolean isSingleton() {
072        return endpoint.isSingleton();
073    }
074
075    @Override
076    public String getState() {
077        // must use String type to be sure remote JMX can read the attribute without requiring Camel classes.
078        if (endpoint instanceof StatefulService) {
079            ServiceStatus status = ((StatefulService) endpoint).getStatus();
080            return status.name();
081        }
082
083        // assume started if not a ServiceSupport instance
084        return ServiceStatus.Started.name();
085    }
086
087    @Override
088    public String informationJson() {
089        return endpoint.getCamelContext().explainEndpointJson(getEndpointUri(), true);
090    }
091
092    @Override
093    public TabularData explain(boolean allOptions) {
094        try {
095            String json = endpoint.getCamelContext().explainEndpointJson(getEndpointUri(), allOptions);
096            List<Map<String, String>> rows = JsonSchemaHelper.parseJsonSchema("properties", json, true);
097
098            TabularData answer = new TabularDataSupport(CamelOpenMBeanTypes.explainEndpointTabularType());
099
100            for (Map<String, String> row : rows) {
101                String name = row.get("name");
102                String kind = row.get("kind");
103                String group = row.get("group") != null ? row.get("group") : "";
104                String label = row.get("label") != null ? row.get("label") : "";
105                String type = row.get("type");
106                String javaType = row.get("javaType");
107                String deprecated = row.get("deprecated") != null ? row.get("deprecated") : "";
108                String secret = row.get("secret") != null ? row.get("secret") : "";
109                String value = row.get("value") != null ? row.get("value") : "";
110                String defaultValue = row.get("defaultValue") != null ? row.get("defaultValue") : "";
111                String description = row.get("description") != null ? row.get("description") : "";
112
113                CompositeType ct = CamelOpenMBeanTypes.explainEndpointsCompositeType();
114                CompositeData data = new CompositeDataSupport(ct,
115                        new String[]{"option", "kind", "group", "label", "type", "java type", "deprecated", "secret", "value", "default value", "description"},
116                        new Object[]{name, kind, group, label, type, javaType, deprecated, secret, value, defaultValue, description});
117                answer.put(data);
118            }
119
120            return answer;
121        } catch (Exception e) {
122            throw ObjectHelper.wrapRuntimeCamelException(e);
123        }
124    }
125
126    @Override
127    public Endpoint getInstance() {
128        return endpoint;
129    }
130
131}