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; 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.Component; 029import org.apache.camel.ComponentVerifier; 030import org.apache.camel.ServiceStatus; 031import org.apache.camel.StatefulService; 032import org.apache.camel.VerifiableComponent; 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.ManagedComponentMBean; 037import org.apache.camel.component.extension.verifier.ResultBuilder; 038import org.apache.camel.component.extension.verifier.ResultErrorBuilder; 039import org.apache.camel.spi.ManagementStrategy; 040import org.apache.camel.util.CastUtils; 041import org.apache.camel.util.JsonSchemaHelper; 042import org.apache.camel.util.ObjectHelper; 043 044/** 045 * @version 046 */ 047@ManagedResource(description = "Managed Component") 048public class ManagedComponent implements ManagedInstance, ManagedComponentMBean { 049 private final Component component; 050 private final String name; 051 052 public ManagedComponent(String name, Component component) { 053 this.name = name; 054 this.component = component; 055 } 056 057 public void init(ManagementStrategy strategy) { 058 // do nothing 059 } 060 061 public Component getComponent() { 062 return component; 063 } 064 065 public String getComponentName() { 066 return name; 067 } 068 069 public String getState() { 070 // must use String type to be sure remote JMX can read the attribute without requiring Camel classes. 071 if (component instanceof StatefulService) { 072 ServiceStatus status = ((StatefulService) component).getStatus(); 073 return status.name(); 074 } 075 076 // assume started if not a ServiceSupport instance 077 return ServiceStatus.Started.name(); 078 } 079 080 public String getCamelId() { 081 return component.getCamelContext().getName(); 082 } 083 084 public String getCamelManagementName() { 085 return component.getCamelContext().getManagementName(); 086 } 087 088 public Object getInstance() { 089 return component; 090 } 091 092 public String informationJson() { 093 try { 094 // a component may have been given a different name, so resolve its default name by its java type 095 // as we can find the component json information from the default component name 096 String defaultName = component.getCamelContext().resolveComponentDefaultName(component.getClass().getName()); 097 String target = defaultName != null ? defaultName : name; 098 return component.getCamelContext().getComponentParameterJsonSchema(target); 099 } catch (IOException e) { 100 throw ObjectHelper.wrapRuntimeCamelException(e); 101 } 102 } 103 104 public TabularData explain(boolean allOptions) { 105 try { 106 // a component may have been given a different name, so resolve its default name by its java type 107 // as we can find the component json information from the default component name 108 String defaultName = component.getCamelContext().resolveComponentDefaultName(component.getClass().getName()); 109 String target = defaultName != null ? defaultName : name; 110 String json = component.getCamelContext().explainComponentJson(target, allOptions); 111 112 List<Map<String, String>> rows = JsonSchemaHelper.parseJsonSchema("componentProperties", json, true); 113 114 TabularData answer = new TabularDataSupport(CamelOpenMBeanTypes.explainComponentTabularType()); 115 116 for (Map<String, String> row : rows) { 117 String name = row.get("name"); 118 String kind = row.get("kind"); 119 String group = row.get("group") != null ? row.get("group") : ""; 120 String label = row.get("label") != null ? row.get("label") : ""; 121 String type = row.get("type"); 122 String javaType = row.get("javaType"); 123 String deprecated = row.get("deprecated") != null ? row.get("deprecated") : ""; 124 String secret = row.get("secret") != null ? row.get("secret") : ""; 125 String value = row.get("value") != null ? row.get("value") : ""; 126 String defaultValue = row.get("defaultValue") != null ? row.get("defaultValue") : ""; 127 String description = row.get("description") != null ? row.get("description") : ""; 128 129 CompositeType ct = CamelOpenMBeanTypes.explainComponentCompositeType(); 130 CompositeData data = new CompositeDataSupport(ct, 131 new String[]{"option", "kind", "group", "label", "type", "java type", "deprecated", "secret", "value", "default value", "description"}, 132 new Object[]{name, kind, group, label, type, javaType, deprecated, secret, value, defaultValue, description}); 133 answer.put(data); 134 } 135 136 return answer; 137 } catch (Exception e) { 138 throw ObjectHelper.wrapRuntimeCamelException(e); 139 } 140 } 141 142 @Override 143 public boolean isVerifySupported() { 144 return component instanceof VerifiableComponent; 145 } 146 147 @Override 148 public ComponentVerifier.Result verify(String scope, Map<String, String> options) { 149 try { 150 ComponentVerifier.Scope scopeEnum = ComponentVerifier.Scope.fromString(scope); 151 152 if (component instanceof VerifiableComponent) { 153 return ((VerifiableComponent) component).getVerifier().verify(scopeEnum, CastUtils.cast(options)); 154 } else { 155 return ResultBuilder.unsupported().build(); 156 } 157 } catch (IllegalArgumentException e) { 158 return ResultBuilder.withStatus(ComponentVerifier.Result.Status.UNSUPPORTED) 159 .error(ResultErrorBuilder.withUnsupportedScope(scope).build()) 160 .build(); 161 } 162 } 163}