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