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 */ 017package org.apache.camel.management.mbean; 018 019import org.apache.camel.CamelContext; 020import org.apache.camel.ExtendedCamelContext; 021import org.apache.camel.Processor; 022import org.apache.camel.Route; 023import org.apache.camel.ServiceStatus; 024import org.apache.camel.StatefulService; 025import org.apache.camel.api.management.ManagedInstance; 026import org.apache.camel.api.management.ManagedResource; 027import org.apache.camel.api.management.mbean.ManagedProcessorMBean; 028import org.apache.camel.model.ProcessorDefinition; 029import org.apache.camel.model.ProcessorDefinitionHelper; 030import org.apache.camel.model.RouteDefinition; 031import org.apache.camel.model.StepDefinition; 032import org.apache.camel.spi.ManagementStrategy; 033import org.apache.camel.spi.RouteIdAware; 034import org.apache.camel.support.service.ServiceHelper; 035 036@ManagedResource(description = "Managed Processor") 037public class ManagedProcessor extends ManagedPerformanceCounter implements ManagedInstance, ManagedProcessorMBean { 038 039 private final CamelContext context; 040 private final Processor processor; 041 private final ProcessorDefinition<?> definition; 042 private final String id; 043 private String stepId; 044 private Route route; 045 private String sourceLocation; 046 047 public ManagedProcessor(CamelContext context, Processor processor, ProcessorDefinition<?> definition) { 048 this.context = context; 049 this.processor = processor; 050 this.definition = definition; 051 this.id = definition.idOrCreate(context.adapt(ExtendedCamelContext.class).getNodeIdFactory()); 052 StepDefinition step; 053 if (definition instanceof StepDefinition) { 054 step = (StepDefinition) definition; 055 } else { 056 step = ProcessorDefinitionHelper.findFirstParentOfType(StepDefinition.class, definition, true); 057 } 058 this.stepId = step != null ? step.idOrCreate(context.adapt(ExtendedCamelContext.class).getNodeIdFactory()) : null; 059 this.sourceLocation = definition.getLocation(); 060 if (sourceLocation == null) { 061 RouteDefinition rd = ProcessorDefinitionHelper.getRoute(definition); 062 sourceLocation = rd != null ? rd.getLocation() : null; 063 } 064 } 065 066 @Override 067 public void init(ManagementStrategy strategy) { 068 super.init(strategy); 069 boolean enabled = context.getManagementStrategy().getManagementAgent().getStatisticsLevel().isDefaultOrExtended(); 070 setStatisticsEnabled(enabled); 071 } 072 073 public CamelContext getContext() { 074 return context; 075 } 076 077 @Override 078 public Object getInstance() { 079 return processor; 080 } 081 082 public Processor getProcessor() { 083 return processor; 084 } 085 086 public ProcessorDefinition<?> getDefinition() { 087 return definition; 088 } 089 090 public String getId() { 091 return id; 092 } 093 094 @Override 095 public String getStepId() { 096 return stepId; 097 } 098 099 @Override 100 public Integer getIndex() { 101 return definition.getIndex(); 102 } 103 104 @Override 105 public String getSourceLocation() { 106 return sourceLocation; 107 } 108 109 @Override 110 public Integer getSourceLineNumber() { 111 int line = definition.getLineNumber(); 112 return line >= 0 ? line : null; 113 } 114 115 @Override 116 public Boolean getSupportExtendedInformation() { 117 return false; 118 } 119 120 public Route getRoute() { 121 return route; 122 } 123 124 public void setRoute(Route route) { 125 this.route = route; 126 } 127 128 @Override 129 public String getState() { 130 // must use String type to be sure remote JMX can read the attribute without requiring Camel classes. 131 if (processor instanceof StatefulService) { 132 ServiceStatus status = ((StatefulService) processor).getStatus(); 133 return status.name(); 134 } 135 136 // assume started if not a ServiceSupport instance 137 return ServiceStatus.Started.name(); 138 } 139 140 @Override 141 public String getCamelId() { 142 return context.getName(); 143 } 144 145 @Override 146 public String getCamelManagementName() { 147 return context.getManagementName(); 148 } 149 150 @Override 151 public String getRouteId() { 152 if (route != null) { 153 return route.getId(); 154 } else if (processor instanceof RouteIdAware) { 155 return ((RouteIdAware) processor).getRouteId(); 156 } 157 return null; 158 } 159 160 @Override 161 public String getProcessorId() { 162 return id; 163 } 164 165 @Override 166 public void start() throws Exception { 167 if (!context.getStatus().isStarted()) { 168 throw new IllegalArgumentException("CamelContext is not started"); 169 } 170 ServiceHelper.startService(getProcessor()); 171 } 172 173 @Override 174 public void stop() throws Exception { 175 if (!context.getStatus().isStarted()) { 176 throw new IllegalArgumentException("CamelContext is not started"); 177 } 178 ServiceHelper.stopService(getProcessor()); 179 } 180 181 @Override 182 public String dumpProcessorAsXml() throws Exception { 183 ExtendedCamelContext ecc = context.adapt(ExtendedCamelContext.class); 184 return ecc.getModelToXMLDumper().dumpModelAsXml(context, definition); 185 } 186}