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