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    // TODO apiProducer is reported unused, if it is needed, then place @SuppressWarnings("unused")
043    private transient Producer apiProducer;
044
045    public ManagedRestRegistry(CamelContext context, RestRegistry registry) {
046        super(context, registry);
047        this.registry = registry;
048    }
049
050    public RestRegistry getRegistry() {
051        return registry;
052    }
053
054    @Override
055    public int getNumberOfRestServices() {
056        return registry.size();
057    }
058
059    @Override
060    public TabularData listRestServices() {
061        try {
062            TabularData answer = new TabularDataSupport(CamelOpenMBeanTypes.listRestServicesTabularType());
063            List<RestRegistry.RestService> services = registry.listAllRestServices();
064            for (RestRegistry.RestService entry : services) {
065                CompositeType ct = CamelOpenMBeanTypes.listRestServicesCompositeType();
066                String url = entry.getUrl();
067                String baseUrl = entry.getBaseUrl();
068                String basePath = entry.getBasePath();
069                String uriTemplate = entry.getUriTemplate();
070                String method = entry.getMethod();
071                String consumes = entry.getConsumes();
072                String produces = entry.getProduces();
073                String state = entry.getState();
074                String inType = entry.getInType();
075                String outType = entry.getOutType();
076                String routeId = entry.getRouteId();
077                String description = entry.getDescription();
078
079                CompositeData data = new CompositeDataSupport(
080                        ct,
081                        new String[] {
082                                "url", "baseUrl", "basePath", "uriTemplate", "method", "consumes", "produces", "inType",
083                                "outType", "state", "routeId", "description" },
084                        new Object[] {
085                                url, baseUrl, basePath, uriTemplate, method, consumes, produces, inType, outType, state,
086                                routeId, description });
087                answer.put(data);
088            }
089            return answer;
090        } catch (Exception e) {
091            throw RuntimeCamelException.wrapRuntimeCamelException(e);
092        }
093    }
094
095    @Override
096    public String apiDocAsJson() {
097        return registry.apiDocAsJson();
098    }
099
100}