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;
020
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.CamelContext;
028import org.apache.camel.RuntimeCamelException;
029import org.apache.camel.api.management.ManagedResource;
030import org.apache.camel.api.management.mbean.CamelOpenMBeanTypes;
031import org.apache.camel.api.management.mbean.ManagedRestRegistryMBean;
032import org.apache.camel.spi.RestRegistry;
033
034/**
035 *
036 */
037@ManagedResource(description = "Managed RestRegistry")
038public class ManagedRestRegistry extends ManagedService implements ManagedRestRegistryMBean {
039
040    private final RestRegistry registry;
041
042    public ManagedRestRegistry(CamelContext context, RestRegistry registry) {
043        super(context, registry);
044        this.registry = registry;
045    }
046
047    public RestRegistry getRegistry() {
048        return registry;
049    }
050
051    @Override
052    public int getNumberOfRestServices() {
053        return registry.size();
054    }
055
056    @Override
057    public TabularData listRestServices() {
058        try {
059            TabularData answer = new TabularDataSupport(CamelOpenMBeanTypes.listRestServicesTabularType());
060            List<RestRegistry.RestService> services = registry.listAllRestServices();
061            for (RestRegistry.RestService entry : services) {
062                CompositeType ct = CamelOpenMBeanTypes.listRestServicesCompositeType();
063                String url = entry.getUrl();
064                String baseUrl = entry.getBaseUrl();
065                String basePath = entry.getBasePath();
066                String uriTemplate = entry.getUriTemplate();
067                String method = entry.getMethod();
068                String consumes = entry.getConsumes();
069                String produces = entry.getProduces();
070                String state = entry.getState();
071                String inType = entry.getInType();
072                String outType = entry.getOutType();
073                String description = entry.getDescription();
074
075                CompositeData data = new CompositeDataSupport(
076                        ct,
077                        new String[] {
078                                "url", "baseUrl", "basePath", "uriTemplate", "method", "consumes", "produces", "inType",
079                                "outType", "state", "description" },
080                        new Object[] {
081                                url, baseUrl, basePath, uriTemplate, method, consumes, produces, inType, outType, state,
082                                description });
083                answer.put(data);
084            }
085            return answer;
086        } catch (Exception e) {
087            throw RuntimeCamelException.wrapRuntimeCamelException(e);
088        }
089    }
090
091    @Override
092    public String apiDocAsJson() {
093        return registry.apiDocAsJson();
094    }
095
096}