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