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