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.ServiceStatus; 032import org.apache.camel.StatefulService; 033import org.apache.camel.api.management.ManagedInstance; 034import org.apache.camel.api.management.ManagedResource; 035import org.apache.camel.api.management.mbean.CamelOpenMBeanTypes; 036import org.apache.camel.api.management.mbean.ManagedProcessorMBean; 037import org.apache.camel.model.ModelHelper; 038import org.apache.camel.model.ProcessorDefinition; 039import org.apache.camel.spi.ManagementStrategy; 040import org.apache.camel.util.JsonSchemaHelper; 041import org.apache.camel.util.ObjectHelper; 042import org.apache.camel.util.ServiceHelper; 043 044/** 045 * @version 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 Route route; 055 056 public ManagedProcessor(CamelContext context, Processor processor, ProcessorDefinition<?> definition) { 057 this.context = context; 058 this.processor = processor; 059 this.definition = definition; 060 this.id = definition.idOrCreate(context.getNodeIdFactory()); 061 } 062 063 @Override 064 public void init(ManagementStrategy strategy) { 065 super.init(strategy); 066 boolean enabled = context.getManagementStrategy().getManagementAgent().getStatisticsLevel().isDefaultOrExtended(); 067 setStatisticsEnabled(enabled); 068 } 069 070 public CamelContext getContext() { 071 return context; 072 } 073 074 public Object getInstance() { 075 return processor; 076 } 077 078 public Processor getProcessor() { 079 return processor; 080 } 081 082 public ProcessorDefinition<?> getDefinition() { 083 return definition; 084 } 085 086 public String getId() { 087 return id; 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 public String informationJson() { 151 return context.explainEipJson(id, true); 152 } 153 154 public TabularData explain(boolean allOptions) { 155 try { 156 String json = context.explainEipJson(id, allOptions); 157 List<Map<String, String>> rows = JsonSchemaHelper.parseJsonSchema("properties", json, true); 158 159 TabularData answer = new TabularDataSupport(CamelOpenMBeanTypes.explainEipTabularType()); 160 161 for (Map<String, String> row : rows) { 162 String name = row.get("name"); 163 String kind = row.get("kind"); 164 String label = row.get("label") != null ? row.get("label") : ""; 165 String type = row.get("type"); 166 String javaType = row.get("javaType"); 167 String deprecated = row.get("deprecated") != null ? row.get("deprecated") : ""; 168 String value = row.get("value") != null ? row.get("value") : ""; 169 String defaultValue = row.get("defaultValue") != null ? row.get("defaultValue") : ""; 170 String description = row.get("description") != null ? row.get("description") : ""; 171 172 CompositeType ct = CamelOpenMBeanTypes.explainEipsCompositeType(); 173 CompositeData data = new CompositeDataSupport(ct, 174 new String[]{"option", "kind", "label", "type", "java type", "deprecated", "value", "default value", "description"}, 175 new Object[]{name, kind, label, type, javaType, deprecated, value, defaultValue, description}); 176 answer.put(data); 177 } 178 179 return answer; 180 } catch (Exception e) { 181 throw ObjectHelper.wrapRuntimeCamelException(e); 182 } 183 } 184 185 @Override 186 public String dumpProcessorAsXml() throws Exception { 187 return ModelHelper.dumpModelAsXml(context, definition); 188 } 189}