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