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