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.api.management.mbean.ManagedStepMBean;
031import org.apache.camel.model.Model;
032import org.apache.camel.model.ProcessorDefinition;
033import org.apache.camel.spi.ManagementStrategy;
034
035/**
036 * JMX capable {@link CamelContext}.
037 */
038public class ManagedCamelContextImpl implements ManagedCamelContext {
039
040    private final CamelContext camelContext;
041
042    public ManagedCamelContextImpl(CamelContext camelContext) {
043        this.camelContext = camelContext;
044    }
045
046    private ManagementStrategy getManagementStrategy() {
047        return camelContext.getManagementStrategy();
048    }
049
050    @Override
051    public <T extends ManagedProcessorMBean> T getManagedProcessor(String id, Class<T> type) {
052        // jmx must be enabled
053        if (getManagementStrategy().getManagementAgent() == null) {
054            return null;
055        }
056
057        Processor processor = camelContext.getProcessor(id);
058        ProcessorDefinition def = camelContext.getExtension(Model.class).getProcessorDefinition(id);
059
060        // processor may be null if its anonymous inner class or as lambda
061        if (def != null) {
062            try {
063                ObjectName on = getManagementStrategy().getManagementObjectNameStrategy()
064                        .getObjectNameForProcessor(camelContext, processor, def);
065                return getManagementStrategy().getManagementAgent().newProxyClient(on, type);
066            } catch (MalformedObjectNameException e) {
067                throw RuntimeCamelException.wrapRuntimeCamelException(e);
068            }
069        }
070
071        return null;
072    }
073
074    @Override
075    public ManagedStepMBean getManagedStep(String id) {
076        // jmx must be enabled
077        if (getManagementStrategy().getManagementAgent() == null) {
078            return null;
079        }
080
081        Processor processor = camelContext.getProcessor(id);
082        ProcessorDefinition def = camelContext.getExtension(Model.class).getProcessorDefinition(id);
083
084        // processor may be null if its anonymous inner class or as lambda
085        if (def != null) {
086            try {
087                ObjectName on = getManagementStrategy().getManagementObjectNameStrategy()
088                        .getObjectNameForStep(camelContext, processor, def);
089                return getManagementStrategy().getManagementAgent().newProxyClient(on, ManagedStepMBean.class);
090            } catch (MalformedObjectNameException e) {
091                throw RuntimeCamelException.wrapRuntimeCamelException(e);
092            }
093        }
094
095        return null;
096    }
097
098    @Override
099    public <T extends ManagedRouteMBean> T getManagedRoute(String routeId, Class<T> type) {
100        // jmx must be enabled
101        if (getManagementStrategy().getManagementAgent() == null) {
102            return null;
103        }
104
105        Route route = camelContext.getRoute(routeId);
106
107        if (route != null) {
108            try {
109                ObjectName on = getManagementStrategy().getManagementObjectNameStrategy().getObjectNameForRoute(route);
110                return getManagementStrategy().getManagementAgent().newProxyClient(on, type);
111            } catch (MalformedObjectNameException e) {
112                throw RuntimeCamelException.wrapRuntimeCamelException(e);
113            }
114        }
115
116        return null;
117    }
118
119    @Override
120    public ManagedCamelContextMBean getManagedCamelContext() {
121        // jmx must be enabled
122        if (getManagementStrategy().getManagementAgent() == null) {
123            return null;
124        }
125
126        try {
127            ObjectName on = getManagementStrategy().getManagementObjectNameStrategy()
128                    .getObjectNameForCamelContext(camelContext);
129            return getManagementStrategy().getManagementAgent().newProxyClient(on, ManagedCamelContextMBean.class);
130        } catch (MalformedObjectNameException e) {
131            throw RuntimeCamelException.wrapRuntimeCamelException(e);
132        }
133    }
134
135}