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;
018
019import javax.management.MalformedObjectNameException;
020import javax.management.ObjectName;
021
022import org.apache.camel.CamelContext;
023import org.apache.camel.Processor;
024import org.apache.camel.Route;
025import org.apache.camel.RuntimeCamelException;
026import org.apache.camel.api.management.ManagedCamelContext;
027import org.apache.camel.api.management.mbean.ManagedCamelContextMBean;
028import org.apache.camel.api.management.mbean.ManagedProcessorMBean;
029import org.apache.camel.api.management.mbean.ManagedRouteMBean;
030import org.apache.camel.model.ModelCamelContext;
031import org.apache.camel.model.ProcessorDefinition;
032import org.apache.camel.spi.ManagementStrategy;
033
034public class ManagedCamelContextImpl implements ManagedCamelContext {
035
036    private final CamelContext camelContext;
037
038    public ManagedCamelContextImpl(CamelContext camelContext) {
039        this.camelContext = camelContext;
040    }
041
042    private ManagementStrategy getManagementStrategy() {
043        return camelContext.getManagementStrategy();
044    }
045
046    public <T extends ManagedProcessorMBean> T getManagedProcessor(String id, Class<T> type) {
047        // jmx must be enabled
048        if (getManagementStrategy().getManagementAgent() == null) {
049            return null;
050        }
051
052        Processor processor = camelContext.getProcessor(id);
053        ProcessorDefinition def = camelContext.adapt(ModelCamelContext.class).getProcessorDefinition(id);
054
055        // processor may be null if its anonymous inner class or as lambda
056        if (def != null) {
057            try {
058                ObjectName on = getManagementStrategy().getManagementObjectNameStrategy()
059                        .getObjectNameForProcessor(camelContext, processor, def);
060                return getManagementStrategy().getManagementAgent().newProxyClient(on, type);
061            } catch (MalformedObjectNameException e) {
062                throw RuntimeCamelException.wrapRuntimeCamelException(e);
063            }
064        }
065
066        return null;
067    }
068
069    public <T extends ManagedRouteMBean> T getManagedRoute(String routeId, Class<T> type) {
070        // jmx must be enabled
071        if (getManagementStrategy().getManagementAgent() == null) {
072            return null;
073        }
074
075        Route route = camelContext.getRoute(routeId);
076
077        if (route != null) {
078            try {
079                ObjectName on = getManagementStrategy().getManagementObjectNameStrategy().getObjectNameForRoute(route);
080                return getManagementStrategy().getManagementAgent().newProxyClient(on, type);
081            } catch (MalformedObjectNameException e) {
082                throw RuntimeCamelException.wrapRuntimeCamelException(e);
083            }
084        }
085
086        return null;
087    }
088
089    public ManagedCamelContextMBean getManagedCamelContext() {
090        // jmx must be enabled
091        if (getManagementStrategy().getManagementAgent() == null) {
092            return null;
093        }
094
095        try {
096            ObjectName on = getManagementStrategy().getManagementObjectNameStrategy()
097                    .getObjectNameForCamelContext(camelContext);
098            return getManagementStrategy().getManagementAgent().newProxyClient(on, ManagedCamelContextMBean.class);
099        } catch (MalformedObjectNameException e) {
100            throw RuntimeCamelException.wrapRuntimeCamelException(e);
101        }
102    }
103
104}