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.ArrayList;
020    import java.util.Collection;
021    import java.util.HashMap;
022    import java.util.List;
023    import java.util.Map;
024    
025    import javax.management.JMException;
026    import javax.management.MalformedObjectNameException;
027    import javax.management.ObjectName;
028    
029    import org.apache.camel.CamelContext;
030    import org.apache.camel.Consumer;
031    import org.apache.camel.Endpoint;
032    import org.apache.camel.Exchange;
033    import org.apache.camel.Route;
034    import org.apache.camel.Service;
035    import org.apache.camel.impl.DefaultCamelContext;
036    import org.apache.camel.impl.ServiceSupport;
037    import org.apache.camel.model.ExceptionType;
038    import org.apache.camel.model.ProcessorType;
039    import org.apache.camel.model.RouteType;
040    import org.apache.camel.spi.InstrumentationAgent;
041    import org.apache.camel.spi.LifecycleStrategy;
042    import org.apache.camel.spi.RouteContext;
043    import org.apache.camel.util.ObjectHelper;
044    import org.apache.commons.logging.Log;
045    import org.apache.commons.logging.LogFactory;
046    
047    /**
048     * JMX agent that registeres Camel lifecycle events in JMX.
049     *
050     * @version $Revision: 755493 $
051     */
052    public class InstrumentationLifecycleStrategy implements LifecycleStrategy {
053        private static final transient Log LOG = LogFactory.getLog(InstrumentationProcessor.class);
054    
055        private InstrumentationAgent agent;
056        private CamelNamingStrategy namingStrategy;
057        private boolean initialized;
058    
059        // A map (Endpoint -> InstrumentationProcessor) to facilitate
060        // adding per-route interceptor and registering ManagedRoute MBean
061        private Map<Endpoint, InstrumentationProcessor> interceptorMap =
062            new HashMap<Endpoint, InstrumentationProcessor>();
063    
064        public InstrumentationLifecycleStrategy() {
065            this(new DefaultInstrumentationAgent());
066        }
067    
068        public InstrumentationLifecycleStrategy(InstrumentationAgent agent) {
069            this.agent = agent;
070        }
071        /**
072         * Constructor for camel context that has been started.
073         *
074         * @param agent    the agent
075         * @param context  the camel context
076         */
077        public InstrumentationLifecycleStrategy(InstrumentationAgent agent, CamelContext context) {
078            this.agent = agent;
079            onContextStart(context);
080        }
081    
082        public void onContextStart(CamelContext context) {
083            if (context instanceof DefaultCamelContext) {
084                try {
085                    initialized = true;
086                    DefaultCamelContext dc = (DefaultCamelContext)context;
087                    // call addService so that context will start and stop the agent
088                    dc.addService(agent);
089                    namingStrategy = new CamelNamingStrategy(agent.getMBeanObjectDomainName());
090                    ManagedService ms = new ManagedService(dc);
091                    agent.register(ms, getNamingStrategy().getObjectName(dc));
092                } catch (Exception e) {
093                    // must rethrow to allow CamelContext fallback to non JMX agent to allow
094                    // Camel to continue to run
095                    throw ObjectHelper.wrapRuntimeCamelException(e);
096                }
097            }
098        }
099    
100        public void onEndpointAdd(Endpoint<? extends Exchange> endpoint) {
101            // the agent hasn't been started
102            if (!initialized) {
103                return;
104            }
105    
106            try {
107                ManagedEndpoint me = new ManagedEndpoint(endpoint);
108                agent.register(me, getNamingStrategy().getObjectName(me));
109            } catch (JMException e) {
110                LOG.warn("Could not register Endpoint MBean", e);
111            }
112        }
113    
114        public void onRoutesAdd(Collection<Route> routes) {
115            // the agent hasn't been started
116            if (!initialized) {
117                return;
118            }
119    
120            for (Route route : routes) {
121                try {
122                    ManagedRoute mr = new ManagedRoute(route);
123                    // retrieve the per-route intercept for this route
124                    InstrumentationProcessor interceptor = interceptorMap.get(route.getEndpoint());
125                    if (interceptor == null) {
126                        LOG.warn("Instrumentation processor not found for route endpoint "
127                                 + route.getEndpoint());
128                    } else {
129                        interceptor.setCounter(mr);
130                    }
131                    agent.register(mr, getNamingStrategy().getObjectName(mr));
132                } catch (JMException e) {
133                    LOG.warn("Could not register Route MBean", e);
134                }
135            }
136        }
137    
138        public void onServiceAdd(CamelContext context, Service service) {
139            // the agent hasn't been started
140            if (!initialized) {
141                return;
142            }
143            if (service instanceof ServiceSupport && service instanceof Consumer) {
144                // TODO: add support for non-consumer services?
145                try {
146                    ManagedService ms = new ManagedService((ServiceSupport)service);
147                    agent.register(ms, getNamingStrategy().getObjectName(context, ms));
148                } catch (JMException e) {
149                    LOG.warn("Could not register Service MBean", e);
150                }
151            }
152        }
153    
154        public void onRouteContextCreate(RouteContext routeContext) {
155            // the agent hasn't been started
156            if (!initialized) {
157                return;
158            }
159    
160            // Create a map (ProcessorType -> PerformanceCounter)
161            // to be passed to InstrumentationInterceptStrategy.
162            Map<ProcessorType, PerformanceCounter> counterMap =
163                new HashMap<ProcessorType, PerformanceCounter>();
164    
165            // Each processor in a route will have its own performance counter
166            // The performance counter are MBeans that we register with MBeanServer.
167            // These performance counter will be embedded
168            // to InstrumentationProcessor and wrap the appropriate processor
169            // by InstrumentationInterceptStrategy.
170            RouteType route = routeContext.getRoute();
171            
172            for (ProcessorType processor : route.getOutputs()) {
173                ObjectName name = null;
174                try {
175                    // get the mbean name
176                    name = getNamingStrategy().getObjectName(routeContext, processor);
177    
178                    // register mbean wrapped in the performance counter mbean
179                    PerformanceCounter pc = new PerformanceCounter();
180                    agent.register(pc, name);
181    
182                    // add to map now that it has been registered
183                    counterMap.put(processor, pc);
184                } catch (MalformedObjectNameException e) {
185                    LOG.warn("Could not create MBean name: " + name, e);
186                } catch (JMException e) {
187                    LOG.warn("Could not register PerformanceCounter MBean: " + name, e);
188                }
189            }
190    
191            // TODO: align this code with InstrumentationLifecycleStrategy
192            routeContext.addInterceptStrategy(new InstrumentationInterceptStrategy(counterMap));
193    
194            routeContext.setErrorHandlerWrappingStrategy(
195                    new InstrumentationErrorHandlerWrappingStrategy(counterMap));
196    
197            // Add an InstrumentationProcessor at the beginning of each route and
198            // set up the interceptorMap for onRoutesAdd() method to register the
199            // ManagedRoute MBeans.
200    
201            RouteType routeType = routeContext.getRoute();
202            if (routeType.getInputs() != null && !routeType.getInputs().isEmpty()) {
203                if (routeType.getInputs().size() > 1) {
204                    LOG.warn("Addding InstrumentationProcessor to first input only.");
205                }
206    
207                Endpoint endpoint  = routeType.getInputs().get(0).getEndpoint();
208    
209                List<ProcessorType<?>> exceptionHandlers = new ArrayList<ProcessorType<?>>();
210                List<ProcessorType<?>> outputs = new ArrayList<ProcessorType<?>>();
211    
212                // separate out the exception handers in the outputs
213                for (ProcessorType output : routeType.getOutputs()) {
214                    if (output instanceof ExceptionType) {
215                        exceptionHandlers.add(output);
216                    } else {
217                        outputs.add(output);
218                    }
219                }
220    
221                // clearing the outputs
222                routeType.clearOutput();
223    
224                // add exception handlers as top children
225                routeType.getOutputs().addAll(exceptionHandlers);
226    
227                // add an interceptor
228                InstrumentationProcessor processor = new InstrumentationProcessor();
229                routeType.intercept(processor);
230    
231                // add the output
232                for (ProcessorType<?> processorType : outputs) {
233                    routeType.addOutput(processorType);
234                }
235    
236                interceptorMap.put(endpoint, processor);
237            }
238    
239        }
240    
241        public CamelNamingStrategy getNamingStrategy() {
242            return namingStrategy;
243        }
244    
245        public void setNamingStrategy(CamelNamingStrategy strategy) {
246            this.namingStrategy = strategy;
247        }
248    
249        public void setAgent(InstrumentationAgent agent) {
250            this.agent = agent;
251        }
252    
253    }