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