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 */
017 package org.apache.camel.management;
018
019 import java.util.Collection;
020
021 import javax.management.JMException;
022
023 import org.apache.camel.CamelContext;
024 import org.apache.camel.Endpoint;
025 import org.apache.camel.Exchange;
026 import org.apache.camel.Route;
027 import org.apache.camel.Service;
028 import org.apache.camel.impl.DefaultCamelContext;
029 import org.apache.camel.impl.RouteContext;
030 import org.apache.camel.impl.ServiceSupport;
031 import org.apache.camel.spi.InstrumentationAgent;
032 import org.apache.camel.spi.LifecycleStrategy;
033 import org.apache.commons.logging.Log;
034 import org.apache.commons.logging.LogFactory;
035
036 public class InstrumentationLifecycleStrategy implements LifecycleStrategy {
037 private static final transient Log LOG = LogFactory.getLog(InstrumentationProcessor.class);
038
039 private InstrumentationAgent agent;
040 private CamelNamingStrategy namingStrategy;
041
042 public InstrumentationLifecycleStrategy(InstrumentationAgent agent) {
043 this.agent = agent;
044 setNamingStrategy(agent.getNamingStrategy());
045 }
046
047 public void onContextCreate(CamelContext context) {
048 if (context instanceof DefaultCamelContext) {
049 try {
050 DefaultCamelContext dc = (DefaultCamelContext)context;
051 ManagedService ms = new ManagedService(dc);
052 agent.register(ms, getNamingStrategy().getObjectName(dc));
053 } catch (JMException e) {
054 LOG.warn("Could not register CamelContext MBean", e);
055 }
056 }
057 }
058
059 public void onEndpointAdd(Endpoint<? extends Exchange> endpoint) {
060 try {
061 ManagedEndpoint me = new ManagedEndpoint(endpoint);
062 agent.register(me, getNamingStrategy().getObjectName(me));
063 } catch (JMException e) {
064 LOG.warn("Could not register Endpoint MBean", e);
065 }
066 }
067
068 public void onRoutesAdd(Collection<Route> routes) {
069 for (Route route : routes) {
070 try {
071 ManagedRoute mr = new ManagedRoute(route);
072 agent.register(mr, getNamingStrategy().getObjectName(mr));
073 } catch (JMException e) {
074 LOG.warn("Could not register Route MBean", e);
075 }
076 }
077 }
078
079 public void onServiceAdd(CamelContext context, Service service) {
080 if (service instanceof ServiceSupport) {
081 try {
082 ManagedService ms = new ManagedService((ServiceSupport)service);
083 agent.register(ms, getNamingStrategy().getObjectName(context, ms));
084 } catch (JMException e) {
085 LOG.warn("Could not register Service MBean", e);
086 }
087 }
088 }
089
090 public void onRouteContextCreate(RouteContext routeContext) {
091 PerformanceCounter mc = new PerformanceCounter();
092 routeContext.getRoute().intercept(new InstrumentationProcessor(mc));
093
094 /*
095 * Merge performance counter with the MBean it represents instead of
096 * registering a new MBean
097 */
098 try {
099 agent.register(mc, getNamingStrategy().getObjectName(routeContext.getCamelContext(), mc,
100 routeContext));
101 } catch (JMException e) {
102 LOG.warn("Could not register Counter MBean", e);
103 }
104 }
105
106 public CamelNamingStrategy getNamingStrategy() {
107 return namingStrategy;
108 }
109
110 public void setNamingStrategy(CamelNamingStrategy strategy) {
111 this.namingStrategy = strategy;
112 }
113 }