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