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.StatefulService;
025    import org.apache.camel.api.management.ManagedInstance;
026    import org.apache.camel.api.management.ManagedResource;
027    import org.apache.camel.api.management.mbean.ManagedProcessorMBean;
028    import org.apache.camel.model.ProcessorDefinition;
029    import org.apache.camel.util.ServiceHelper;
030    
031    /**
032     * @version 
033     */
034    @ManagedResource(description = "Managed Processor")
035    public class ManagedProcessor extends ManagedPerformanceCounter implements ManagedInstance, ManagedProcessorMBean {
036    
037        private final CamelContext context;
038        private final Processor processor;
039        private final ProcessorDefinition<?> definition;
040        private final String id;
041        private Route route;
042    
043        public ManagedProcessor(CamelContext context, Processor processor, ProcessorDefinition<?> definition) {
044            this.context = context;
045            this.processor = processor;
046            this.definition = definition;
047            this.id = definition.idOrCreate(context.getNodeIdFactory());
048    
049            boolean enabled = context.getManagementStrategy().getStatisticsLevel() == ManagementStatisticsLevel.All;
050            setStatisticsEnabled(enabled);
051        }
052    
053        public CamelContext getContext() {
054            return context;
055        }
056    
057        public Processor getProcessor() {
058            return processor;
059        }
060    
061        public ProcessorDefinition<?> getDefinition() {
062            return definition;
063        }
064    
065        public String getId() {
066            return id;
067        }
068    
069        public Integer getIndex() {
070            return definition.getIndex();
071        }
072    
073        public Route getRoute() {
074            return route;
075        }
076    
077        public void setRoute(Route route) {
078            this.route = route;
079        }
080    
081        public String getState() {
082            // must use String type to be sure remote JMX can read the attribute without requiring Camel classes.
083            if (processor instanceof StatefulService) {
084                ServiceStatus status = ((StatefulService) processor).getStatus();
085                return status.name();
086            }
087    
088            // assume started if not a ServiceSupport instance
089            return ServiceStatus.Started.name();
090        }
091    
092        public String getCamelId() {
093            return context.getName();
094        }
095    
096        public String getCamelManagementName() {
097            return context.getManagementName();
098        }
099    
100        public String getRouteId() {
101            if (route != null) {
102                return route.getId();
103            }
104            return null;
105        }
106    
107        public String getProcessorId() {
108            return id;
109        }
110    
111        public void start() throws Exception {
112            if (!context.getStatus().isStarted()) {
113                throw new IllegalArgumentException("CamelContext is not started");
114            }
115            ServiceHelper.startService(getProcessor());
116        }
117    
118        public void stop() throws Exception {
119            if (!context.getStatus().isStarted()) {
120                throw new IllegalArgumentException("CamelContext is not started");
121            }
122            ServiceHelper.stopService(getProcessor());
123        }
124    
125        public Object getInstance() {
126            return processor;
127        }
128    }