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.mbean;
018    
019    import org.apache.camel.CamelContext;
020    import org.apache.camel.ManagementStatisticsLevel;
021    import org.apache.camel.Processor;
022    import org.apache.camel.Route;
023    import org.apache.camel.ServiceStatus;
024    import org.apache.camel.impl.ServiceSupport;
025    import org.apache.camel.model.ProcessorDefinition;
026    import org.springframework.jmx.export.annotation.ManagedAttribute;
027    import org.springframework.jmx.export.annotation.ManagedResource;
028    
029    /**
030     * @version $Revision: 835732 $
031     */
032    @ManagedResource(description = "Managed Processor")
033    public class ManagedProcessor extends ManagedPerformanceCounter {
034    
035        private final CamelContext context;
036        private final Processor processor;
037        private final ProcessorDefinition<?> definition;
038        private final String id;
039        private Route route;
040    
041        public ManagedProcessor(CamelContext context, Processor processor, ProcessorDefinition<?> definition) {
042            this.context = context;
043            this.processor = processor;
044            this.definition = definition;
045            this.id = definition.idOrCreate(context.getNodeIdFactory());
046    
047            boolean enabled = context.getManagementStrategy().getStatisticsLevel() == ManagementStatisticsLevel.All;
048            setStatisticsEnabled(enabled);
049        }
050    
051        public CamelContext getContext() {
052            return context;
053        }
054    
055        public Processor getProcessor() {
056            return processor;
057        }
058    
059        public ProcessorDefinition<?> getDefinition() {
060            return definition;
061        }
062    
063        public Route getRoute() {
064            return route;
065        }
066    
067        public void setRoute(Route route) {
068            this.route = route;
069        }
070    
071        @ManagedAttribute(description = "Processor State")
072        public String getState() {
073            // must use String type to be sure remote JMX can read the attribute without requiring Camel classes.
074            if (processor instanceof ServiceSupport) {
075                ServiceStatus status = ((ServiceSupport) processor).getStatus();
076                // if no status exists then its stopped
077                if (status == null) {
078                    status = ServiceStatus.Stopped;
079                }
080                return status.name();
081            }
082    
083            // assume started if not a ServiceSupport instance
084            return ServiceStatus.Started.name();
085        }
086    
087        @ManagedAttribute(description = "Camel id")
088        public String getCamelId() {
089            return context.getName();
090        }
091    
092        @ManagedAttribute(description = "Route id")
093        public String getRouteId() {
094            if (route != null) {
095                return route.getId();
096            }
097            return null;
098        }
099    
100        @ManagedAttribute(description = "Processor id")
101        public String getProcessorId() {
102            return id;
103        }
104    
105    }