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.io.IOException; 020import java.util.List; 021import java.util.Map; 022 023import javax.management.openmbean.CompositeData; 024import javax.management.openmbean.CompositeDataSupport; 025import javax.management.openmbean.CompositeType; 026import javax.management.openmbean.TabularData; 027import javax.management.openmbean.TabularDataSupport; 028 029import org.apache.camel.Component; 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.ManagedComponentMBean; 036import org.apache.camel.spi.ManagementStrategy; 037import org.apache.camel.util.JsonSchemaHelper; 038import org.apache.camel.util.ObjectHelper; 039 040/** 041 * @version 042 */ 043@ManagedResource(description = "Managed Component") 044public class ManagedComponent implements ManagedInstance, ManagedComponentMBean { 045 private final Component component; 046 private final String name; 047 048 public ManagedComponent(String name, Component component) { 049 this.name = name; 050 this.component = component; 051 } 052 053 public void init(ManagementStrategy strategy) { 054 // do nothing 055 } 056 057 public Component getComponent() { 058 return component; 059 } 060 061 public String getComponentName() { 062 return name; 063 } 064 065 public String getState() { 066 // must use String type to be sure remote JMX can read the attribute without requiring Camel classes. 067 if (component instanceof StatefulService) { 068 ServiceStatus status = ((StatefulService) component).getStatus(); 069 return status.name(); 070 } 071 072 // assume started if not a ServiceSupport instance 073 return ServiceStatus.Started.name(); 074 } 075 076 public String getCamelId() { 077 return component.getCamelContext().getName(); 078 } 079 080 public String getCamelManagementName() { 081 return component.getCamelContext().getManagementName(); 082 } 083 084 public Object getInstance() { 085 return component; 086 } 087 088 public String informationJson() { 089 try { 090 // a component may have been given a different name, so resolve its default name by its java type 091 // as we can find the component json information from the default component name 092 String defaultName = component.getCamelContext().resolveComponentDefaultName(component.getClass().getName()); 093 String target = defaultName != null ? defaultName : name; 094 return component.getCamelContext().getComponentParameterJsonSchema(target); 095 } catch (IOException e) { 096 throw ObjectHelper.wrapRuntimeCamelException(e); 097 } 098 } 099 100 public TabularData explain(boolean allOptions) { 101 try { 102 // a component may have been given a different name, so resolve its default name by its java type 103 // as we can find the component json information from the default component name 104 String defaultName = component.getCamelContext().resolveComponentDefaultName(component.getClass().getName()); 105 String target = defaultName != null ? defaultName : name; 106 String json = component.getCamelContext().explainComponentJson(target, allOptions); 107 108 List<Map<String, String>> rows = JsonSchemaHelper.parseJsonSchema("componentProperties", json, true); 109 110 TabularData answer = new TabularDataSupport(CamelOpenMBeanTypes.explainComponentTabularType()); 111 112 for (Map<String, String> row : rows) { 113 String name = row.get("name"); 114 String kind = row.get("kind"); 115 String label = row.get("label") != null ? row.get("label") : ""; 116 String type = row.get("type"); 117 String javaType = row.get("javaType"); 118 String deprecated = row.get("deprecated") != null ? row.get("deprecated") : ""; 119 String value = row.get("value") != null ? row.get("value") : ""; 120 String defaultValue = row.get("defaultValue") != null ? row.get("defaultValue") : ""; 121 String description = row.get("description") != null ? row.get("description") : ""; 122 123 CompositeType ct = CamelOpenMBeanTypes.explainComponentCompositeType(); 124 CompositeData data = new CompositeDataSupport(ct, 125 new String[]{"option", "kind", "label", "type", "java type", "deprecated", "value", "default value", "description"}, 126 new Object[]{name, kind, label, type, javaType, deprecated, value, defaultValue, description}); 127 answer.put(data); 128 } 129 130 return answer; 131 } catch (Exception e) { 132 throw ObjectHelper.wrapRuntimeCamelException(e); 133 } 134 } 135 136}