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 Route getRoute() {
066 return route;
067 }
068
069 public void setRoute(Route route) {
070 this.route = route;
071 }
072
073 public String getState() {
074 // must use String type to be sure remote JMX can read the attribute without requiring Camel classes.
075 if (processor instanceof StatefulService) {
076 ServiceStatus status = ((StatefulService) processor).getStatus();
077 return status.name();
078 }
079
080 // assume started if not a ServiceSupport instance
081 return ServiceStatus.Started.name();
082 }
083
084 public String getCamelId() {
085 return context.getName();
086 }
087
088 public String getRouteId() {
089 if (route != null) {
090 return route.getId();
091 }
092 return null;
093 }
094
095 public String getProcessorId() {
096 return id;
097 }
098
099 public void start() throws Exception {
100 if (!context.getStatus().isStarted()) {
101 throw new IllegalArgumentException("CamelContext is not started");
102 }
103 ServiceHelper.startService(getProcessor());
104 }
105
106 public void stop() throws Exception {
107 if (!context.getStatus().isStarted()) {
108 throw new IllegalArgumentException("CamelContext is not started");
109 }
110 ServiceHelper.stopService(getProcessor());
111 }
112
113 public Object getInstance() {
114 return processor;
115 }
116 }