001    /**
002     *
003     * Licensed to the Apache Software Foundation (ASF) under one or more
004     * contributor license agreements.  See the NOTICE file distributed with
005     * this work for additional information regarding copyright ownership.
006     * The ASF licenses this file to You under the Apache License, Version 2.0
007     * (the "License"); you may not use this file except in compliance with
008     * the License.  You may obtain a copy of the License at
009     *
010     * http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing, software
013     * distributed under the License is distributed on an "AS IS" BASIS,
014     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015     * See the License for the specific language governing permissions and
016     * limitations under the License.
017     */
018    package org.apache.camel.management;
019    
020    import java.util.Collection;
021    
022    import javax.management.JMException;
023    
024    import org.apache.camel.CamelContext;
025    import org.apache.camel.Endpoint;
026    import org.apache.camel.InstrumentationAgent;
027    import org.apache.camel.LifecycleStrategy;
028    import org.apache.camel.Route;
029    import org.apache.camel.Service;
030    import org.apache.camel.impl.DefaultCamelContext;
031    import org.apache.camel.impl.ServiceSupport;
032    
033    public class InstrumentationLifecycleStrategy implements LifecycleStrategy {
034            InstrumentationAgent agent;
035            CamelNamingStrategy naming;
036            
037            public InstrumentationLifecycleStrategy(InstrumentationAgent agent) {
038                    this.agent = agent;
039                    naming = new CamelNamingStrategy(agent.getMBeanServer().getDefaultDomain()); 
040            }
041            
042            public void onContextCreate(CamelContext context) {
043                    if (context instanceof DefaultCamelContext) {
044                            try {   
045                                    DefaultCamelContext dc = (DefaultCamelContext)context;
046                                    ManagedService ms = new ManagedService(dc);
047                                    agent.register(ms, naming.getObjectName(dc));
048                            }
049                            catch(JMException e) {
050                                    // log a WARN
051                            }
052                    }
053            }
054            
055            public void onEndpointAdd(Endpoint endpoint) {
056                    try {
057                            ManagedEndpoint me = new ManagedEndpoint(endpoint);
058                            agent.register(me, naming.getObjectName(me));
059                    }
060                    catch(JMException e) {
061                            // log a WARN
062                    }
063            }
064    
065            public void onRoutesAdd(Collection<Route> routes) {
066                    for (Route route: routes) {
067                            try {
068                                    ManagedRoute mr = new ManagedRoute(route);
069                                    agent.register(mr, naming.getObjectName(mr));
070                            }
071                            catch(JMException e) {
072                                    // log a WARN
073                            }
074                    }
075            }
076    
077            public void onServiceAdd(CamelContext context, Service service) {
078                    if (service instanceof ServiceSupport) {
079                            try {
080                                    ManagedService ms = new ManagedService((ServiceSupport)service);
081                                    agent.register(ms, naming.getObjectName(context, ms));
082                            }
083                            catch(JMException e) {
084                                    // log a WARN
085                            }
086                    }
087            }
088    }