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 java.util.ArrayList; 020import java.util.List; 021 022import javax.management.MalformedObjectNameException; 023import javax.management.ObjectName; 024 025import org.apache.camel.CamelContext; 026import org.apache.camel.Consumer; 027import org.apache.camel.Processor; 028import org.apache.camel.Route; 029import org.apache.camel.RuntimeCamelException; 030import org.apache.camel.api.management.ManagedCamelContext; 031import org.apache.camel.api.management.mbean.ManagedCamelContextMBean; 032import org.apache.camel.api.management.mbean.ManagedConsumerMBean; 033import org.apache.camel.api.management.mbean.ManagedProcessorMBean; 034import org.apache.camel.api.management.mbean.ManagedRouteGroupMBean; 035import org.apache.camel.api.management.mbean.ManagedRouteMBean; 036import org.apache.camel.api.management.mbean.ManagedStepMBean; 037import org.apache.camel.model.Model; 038import org.apache.camel.model.ProcessorDefinition; 039import org.apache.camel.spi.ManagementStrategy; 040 041/** 042 * JMX capable {@link CamelContext}. 043 */ 044public class ManagedCamelContextImpl implements ManagedCamelContext { 045 046 private final CamelContext camelContext; 047 048 public ManagedCamelContextImpl(CamelContext camelContext) { 049 this.camelContext = camelContext; 050 } 051 052 private ManagementStrategy getManagementStrategy() { 053 return camelContext.getManagementStrategy(); 054 } 055 056 @Override 057 public <T extends ManagedProcessorMBean> T getManagedProcessor(String id, Class<T> type) { 058 // jmx must be enabled 059 if (getManagementStrategy().getManagementAgent() == null) { 060 return null; 061 } 062 063 Processor processor = camelContext.getProcessor(id); 064 ProcessorDefinition<?> def 065 = camelContext.getCamelContextExtension().getContextPlugin(Model.class).getProcessorDefinition(id); 066 067 // processor may be null if its anonymous inner class or as lambda 068 if (def != null) { 069 try { 070 ObjectName on = getManagementStrategy().getManagementObjectNameStrategy() 071 .getObjectNameForProcessor(camelContext, processor, def); 072 return getManagementStrategy().getManagementAgent().newProxyClient(on, type); 073 } catch (MalformedObjectNameException e) { 074 throw RuntimeCamelException.wrapRuntimeCamelException(e); 075 } 076 } 077 078 return null; 079 } 080 081 @Override 082 public ManagedStepMBean getManagedStep(String id) { 083 // jmx must be enabled 084 if (getManagementStrategy().getManagementAgent() == null) { 085 return null; 086 } 087 088 Processor processor = camelContext.getProcessor(id); 089 ProcessorDefinition<?> def 090 = camelContext.getCamelContextExtension().getContextPlugin(Model.class).getProcessorDefinition(id); 091 092 // processor may be null if its anonymous inner class or as lambda 093 if (def != null) { 094 try { 095 ObjectName on = getManagementStrategy().getManagementObjectNameStrategy() 096 .getObjectNameForStep(camelContext, processor, def); 097 return getManagementStrategy().getManagementAgent().newProxyClient(on, ManagedStepMBean.class); 098 } catch (MalformedObjectNameException e) { 099 throw RuntimeCamelException.wrapRuntimeCamelException(e); 100 } 101 } 102 103 return null; 104 } 105 106 @Override 107 public <T extends ManagedRouteMBean> T getManagedRoute(String routeId, Class<T> type) { 108 // jmx must be enabled 109 if (getManagementStrategy().getManagementAgent() == null) { 110 return null; 111 } 112 113 Route route = camelContext.getRoute(routeId); 114 if (route != null) { 115 try { 116 ObjectName on = getManagementStrategy().getManagementObjectNameStrategy().getObjectNameForRoute(route); 117 return getManagementStrategy().getManagementAgent().newProxyClient(on, type); 118 } catch (MalformedObjectNameException e) { 119 throw RuntimeCamelException.wrapRuntimeCamelException(e); 120 } 121 } 122 123 return null; 124 } 125 126 @Override 127 public List<ManagedRouteMBean> getManagedRoutes() { 128 // null group will return all 129 return getManagedRoutesByGroup(null); 130 } 131 132 @Override 133 public ManagedRouteGroupMBean getManagedRouteGroup(String group) { 134 // jmx must be enabled 135 if (getManagementStrategy().getManagementAgent() == null) { 136 return null; 137 } 138 139 if (group != null && !camelContext.getRoutesByGroup(group).isEmpty()) { 140 try { 141 ObjectName on = getManagementStrategy().getManagementObjectNameStrategy() 142 .getObjectNameForRouteGroup(camelContext, group); 143 return getManagementStrategy().getManagementAgent().newProxyClient(on, ManagedRouteGroupMBean.class); 144 } catch (MalformedObjectNameException e) { 145 throw RuntimeCamelException.wrapRuntimeCamelException(e); 146 } 147 } 148 149 return null; 150 } 151 152 @Override 153 public List<ManagedRouteMBean> getManagedRoutesByGroup(String groupId) { 154 // jmx must be enabled 155 if (getManagementStrategy().getManagementAgent() == null) { 156 return null; 157 } 158 159 List<ManagedRouteMBean> answer = new ArrayList<>(); 160 for (Route route : camelContext.getRoutes()) { 161 if (groupId == null || groupId.equals(route.getGroup())) { 162 try { 163 ObjectName on = getManagementStrategy().getManagementObjectNameStrategy().getObjectNameForRoute(route); 164 ManagedRouteMBean mr 165 = getManagementStrategy().getManagementAgent().newProxyClient(on, ManagedRouteMBean.class); 166 answer.add(mr); 167 } catch (MalformedObjectNameException e) { 168 throw RuntimeCamelException.wrapRuntimeCamelException(e); 169 } 170 } 171 } 172 return answer; 173 } 174 175 @Override 176 public <T extends ManagedConsumerMBean> T getManagedConsumer(String id, Class<T> type) { 177 // jmx must be enabled 178 if (getManagementStrategy().getManagementAgent() == null) { 179 return null; 180 } 181 182 Route route = camelContext.getRoute(id); 183 if (route != null) { 184 try { 185 Consumer consumer = route.getConsumer(); 186 ObjectName on = getManagementStrategy().getManagementObjectNameStrategy().getObjectNameForConsumer(camelContext, 187 consumer); 188 return getManagementStrategy().getManagementAgent().newProxyClient(on, type); 189 } catch (MalformedObjectNameException e) { 190 throw RuntimeCamelException.wrapRuntimeCamelException(e); 191 } 192 } 193 194 return null; 195 } 196 197 @Override 198 public ManagedCamelContextMBean getManagedCamelContext() { 199 // jmx must be enabled 200 if (getManagementStrategy().getManagementAgent() == null) { 201 return null; 202 } 203 // jmx must be started 204 if (getManagementStrategy().getManagementObjectNameStrategy() == null) { 205 return null; 206 } 207 208 try { 209 ObjectName on = getManagementStrategy().getManagementObjectNameStrategy() 210 .getObjectNameForCamelContext(camelContext); 211 return getManagementStrategy().getManagementAgent().newProxyClient(on, ManagedCamelContextMBean.class); 212 } catch (MalformedObjectNameException e) { 213 throw RuntimeCamelException.wrapRuntimeCamelException(e); 214 } 215 } 216 217}