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.Endpoint;
031    import org.apache.camel.Exchange;
032    import org.apache.camel.Route;
033    import org.apache.camel.Service;
034    import org.apache.camel.impl.DefaultCamelContext;
035    import org.apache.camel.impl.ServiceSupport;
036    import org.apache.camel.model.ExceptionType;
037    import org.apache.camel.model.ProcessorType;
038    import org.apache.camel.model.RouteType;
039    import org.apache.camel.spi.InstrumentationAgent;
040    import org.apache.camel.spi.LifecycleStrategy;
041    import org.apache.camel.spi.RouteContext;
042    import org.apache.commons.logging.Log;
043    import org.apache.commons.logging.LogFactory;
044    
045    /**
046     * JMX agent that registeres Camel lifecycle events in JMX.
047     *
048     * @version $Revision: 43903 $
049     *
050     */
051    public class InstrumentationLifecycleStrategy implements LifecycleStrategy {
052        private static final transient Log LOG = LogFactory.getLog(InstrumentationProcessor.class);
053    
054        private InstrumentationAgent agent;
055        private CamelNamingStrategy namingStrategy;
056        private boolean initialized;
057    
058        // A map (Endpoint -> InstrumentationProcessor) to facilitate
059        // adding per-route interceptor and registering ManagedRoute MBean
060        private Map<Endpoint, InstrumentationProcessor> interceptorMap =
061            new HashMap<Endpoint, InstrumentationProcessor>();
062    
063        public InstrumentationLifecycleStrategy() {
064            this(new DefaultInstrumentationAgent());
065        }
066    
067        public InstrumentationLifecycleStrategy(InstrumentationAgent agent) {
068            this.agent = agent;
069        }
070        /**
071         * Constructor for camel context that has been started.
072         *
073         * @param agent
074         * @param context
075         */
076        public InstrumentationLifecycleStrategy(InstrumentationAgent agent,
077                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                    LOG.warn("Could not register CamelContext MBean", e);
094                }
095            }
096        }
097    
098        public void onEndpointAdd(Endpoint<? extends Exchange> endpoint) {
099    
100            // the agent hasn't been started
101            if (!initialized) {
102                return;
103            }
104    
105            try {
106                ManagedEndpoint me = new ManagedEndpoint(endpoint);
107                agent.register(me, getNamingStrategy().getObjectName(me));
108            } catch (JMException e) {
109                LOG.warn("Could not register Endpoint MBean", e);
110            }
111        }
112    
113        public void onRoutesAdd(Collection<Route> routes) {
114    
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    
140            // the agent hasn't been started
141            if (!initialized) {
142                return;
143            }
144            if (service instanceof ServiceSupport) {
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    
156            // the agent hasn't been started
157            if (!initialized) {
158                return;
159            }
160    
161            // Create a map (ProcessorType -> PerformanceCounter)
162            // to be passed to InstrumentationInterceptStrategy.
163            Map<ProcessorType, PerformanceCounter> counterMap =
164                new HashMap<ProcessorType, PerformanceCounter>();
165    
166            // Each processor in a route will have its own performance counter
167            // The performance counter are MBeans that we register with MBeanServer.
168            // These performance counter will be embedded
169            // to InstrumentationProcessor and wrap the appropriate processor
170            // by InstrumentationInterceptStrategy.
171            RouteType route = routeContext.getRoute();
172            
173            // build a local map for handlng multiple instances of a processor
174            Map<ObjectName, Integer> existingNames = new HashMap<ObjectName, Integer>();
175            Map<ProcessorType, ObjectName> nameMap = new HashMap<ProcessorType, ObjectName>();
176            
177            for (ProcessorType processor : route.getOutputs()) {
178                ObjectName name = null;
179                try {
180                    name = getNamingStrategy().getObjectName(routeContext, processor, null);
181                } catch (MalformedObjectNameException e) {
182                    LOG.warn("Could not register MBean: " + name, e);
183                }
184                
185                if (name != null) {
186                    Integer instanceCount = existingNames.get(name);
187                    if (instanceCount != null) {
188                        instanceCount++;
189                    } else {
190                        instanceCount = new Integer(0);
191                        existingNames.put(name, instanceCount);
192                    }
193                    
194                    try {
195                        name = getNamingStrategy().getObjectName(routeContext, processor, instanceCount);
196                        nameMap.put(processor, name);
197                    } catch (MalformedObjectNameException e) {
198                        LOG.warn("Could not register MBean: " + name, e);
199                    }
200                }
201            }
202            
203            for (Map.Entry<ProcessorType, ObjectName> entry : nameMap.entrySet()) {
204                PerformanceCounter pc = new PerformanceCounter();
205                try {
206                    agent.register(pc, entry.getValue());
207                } catch (JMException e) {
208                    LOG.warn("Could not register PerformanceCounter MBean", e);
209                }
210                counterMap.put(entry.getKey(), pc);
211            }
212    
213            routeContext.addInterceptStrategy(new InstrumentationInterceptStrategy(counterMap));
214    
215            routeContext.setErrorHandlerWrappingStrategy(
216                    new InstrumentationErrorHandlerWrappingStrategy(counterMap));
217    
218            // Add an InstrumentationProcessor at the beginning of each route and
219            // set up the interceptorMap for onRoutesAdd() method to register the
220            // ManagedRoute MBeans.
221    
222            RouteType routeType = routeContext.getRoute();
223            if (routeType.getInputs() != null && !routeType.getInputs().isEmpty()) {
224                if (routeType.getInputs().size() > 1) {
225                    LOG.warn("Add InstrumentationProcessor to first input only.");
226                }
227    
228                Endpoint endpoint  = routeType.getInputs().get(0).getEndpoint();
229    
230                List<ProcessorType<?>> exceptionHandlers = new ArrayList<ProcessorType<?>>();
231                List<ProcessorType<?>> outputs = new ArrayList<ProcessorType<?>>();
232    
233                // separate out the exception handers in the outputs
234                for (ProcessorType output : routeType.getOutputs()) {
235                    if (output instanceof ExceptionType) {
236                        exceptionHandlers.add(output);
237                    } else {
238                        outputs.add(output);
239                    }
240                }
241    
242                // clearing the outputs
243                routeType.clearOutput();
244    
245                // add exception handlers as top children
246                routeType.getOutputs().addAll(exceptionHandlers);
247    
248                // add an interceptor
249                InstrumentationProcessor processor = new InstrumentationProcessor();
250                routeType.intercept(processor);
251    
252                // add the output
253                for (ProcessorType<?> processorType : outputs) {
254                    routeType.addOutput(processorType);
255                }
256    
257                interceptorMap.put(endpoint, processor);
258            }
259    
260        }
261    
262        public CamelNamingStrategy getNamingStrategy() {
263            return namingStrategy;
264        }
265    
266        public void setNamingStrategy(CamelNamingStrategy strategy) {
267            this.namingStrategy = strategy;
268        }
269    
270        public void setAgent(InstrumentationAgent agent) {
271            this.agent = agent;
272        }
273    }