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