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