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 java.util.List; 020import java.util.Map; 021 022import javax.management.openmbean.CompositeData; 023import javax.management.openmbean.CompositeDataSupport; 024import javax.management.openmbean.CompositeType; 025import javax.management.openmbean.TabularData; 026import javax.management.openmbean.TabularDataSupport; 027 028import org.apache.camel.CamelContext; 029import org.apache.camel.CatalogCamelContext; 030import org.apache.camel.Processor; 031import org.apache.camel.Route; 032import org.apache.camel.RuntimeCamelException; 033import org.apache.camel.ServiceStatus; 034import org.apache.camel.StatefulService; 035import org.apache.camel.api.management.ManagedInstance; 036import org.apache.camel.api.management.ManagedResource; 037import org.apache.camel.api.management.mbean.CamelOpenMBeanTypes; 038import org.apache.camel.api.management.mbean.ManagedProcessorMBean; 039import org.apache.camel.model.ModelHelper; 040import org.apache.camel.model.ProcessorDefinition; 041import org.apache.camel.model.ProcessorDefinitionHelper; 042import org.apache.camel.model.StepDefinition; 043import org.apache.camel.spi.ManagementStrategy; 044import org.apache.camel.support.JSonSchemaHelper; 045import org.apache.camel.support.service.ServiceHelper; 046 047@ManagedResource(description = "Managed Processor") 048public class ManagedProcessor extends ManagedPerformanceCounter implements ManagedInstance, ManagedProcessorMBean { 049 050 private final CamelContext context; 051 private final Processor processor; 052 private final ProcessorDefinition<?> definition; 053 private final String id; 054 private String stepId; 055 private Route route; 056 057 public ManagedProcessor(CamelContext context, Processor processor, ProcessorDefinition<?> definition) { 058 this.context = context; 059 this.processor = processor; 060 this.definition = definition; 061 this.id = definition.idOrCreate(context.getNodeIdFactory()); 062 StepDefinition step; 063 if (definition instanceof StepDefinition) { 064 step = (StepDefinition) definition; 065 } else { 066 step = ProcessorDefinitionHelper.findFirstParentOfType(StepDefinition.class, definition, true); 067 } 068 this.stepId = step != null ? step.idOrCreate(context.getNodeIdFactory()) : null; 069 } 070 071 @Override 072 public void init(ManagementStrategy strategy) { 073 super.init(strategy); 074 boolean enabled = context.getManagementStrategy().getManagementAgent().getStatisticsLevel().isDefaultOrExtended(); 075 setStatisticsEnabled(enabled); 076 } 077 078 public CamelContext getContext() { 079 return context; 080 } 081 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 public String getStepId() { 099 return stepId; 100 } 101 102 public Integer getIndex() { 103 return definition.getIndex(); 104 } 105 106 public Boolean getSupportExtendedInformation() { 107 return false; 108 } 109 110 public Route getRoute() { 111 return route; 112 } 113 114 public void setRoute(Route route) { 115 this.route = route; 116 } 117 118 public String getState() { 119 // must use String type to be sure remote JMX can read the attribute without requiring Camel classes. 120 if (processor instanceof StatefulService) { 121 ServiceStatus status = ((StatefulService) processor).getStatus(); 122 return status.name(); 123 } 124 125 // assume started if not a ServiceSupport instance 126 return ServiceStatus.Started.name(); 127 } 128 129 public String getCamelId() { 130 return context.getName(); 131 } 132 133 public String getCamelManagementName() { 134 return context.getManagementName(); 135 } 136 137 public String getRouteId() { 138 if (route != null) { 139 return route.getId(); 140 } 141 return null; 142 } 143 144 public String getProcessorId() { 145 return id; 146 } 147 148 public void start() throws Exception { 149 if (!context.getStatus().isStarted()) { 150 throw new IllegalArgumentException("CamelContext is not started"); 151 } 152 ServiceHelper.startService(getProcessor()); 153 } 154 155 public void stop() throws Exception { 156 if (!context.getStatus().isStarted()) { 157 throw new IllegalArgumentException("CamelContext is not started"); 158 } 159 ServiceHelper.stopService(getProcessor()); 160 } 161 162 public String informationJson() { 163 return context.adapt(CatalogCamelContext.class).explainEipJson(id, true); 164 } 165 166 public TabularData explain(boolean allOptions) { 167 try { 168 String json = context.adapt(CatalogCamelContext.class).explainEipJson(id, allOptions); 169 List<Map<String, String>> rows = JSonSchemaHelper.parseJsonSchema("properties", json, true); 170 171 TabularData answer = new TabularDataSupport(CamelOpenMBeanTypes.explainEipTabularType()); 172 173 for (Map<String, String> row : rows) { 174 String name = row.get("name"); 175 String kind = row.get("kind"); 176 String label = row.get("label") != null ? row.get("label") : ""; 177 String type = row.get("type"); 178 String javaType = row.get("javaType"); 179 String deprecated = row.get("deprecated") != null ? row.get("deprecated") : ""; 180 String value = row.get("value") != null ? row.get("value") : ""; 181 String defaultValue = row.get("defaultValue") != null ? row.get("defaultValue") : ""; 182 String description = row.get("description") != null ? row.get("description") : ""; 183 184 CompositeType ct = CamelOpenMBeanTypes.explainEipsCompositeType(); 185 CompositeData data = new CompositeDataSupport(ct, 186 new String[]{"option", "kind", "label", "type", "java type", "deprecated", "value", "default value", "description"}, 187 new Object[]{name, kind, label, type, javaType, deprecated, value, defaultValue, description}); 188 answer.put(data); 189 } 190 191 return answer; 192 } catch (Exception e) { 193 throw RuntimeCamelException.wrapRuntimeCamelException(e); 194 } 195 } 196 197 @Override 198 public String dumpProcessorAsXml() throws Exception { 199 return ModelHelper.dumpModelAsXml(context, definition); 200 } 201}