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.Collections; 020import java.util.List; 021import java.util.Map; 022import java.util.Map.Entry; 023import java.util.Optional; 024import java.util.Set; 025import java.util.stream.Collectors; 026 027import org.apache.camel.Component; 028import org.apache.camel.ServiceStatus; 029import org.apache.camel.StatefulService; 030import org.apache.camel.api.management.ManagedInstance; 031import org.apache.camel.api.management.ManagedResource; 032import org.apache.camel.api.management.mbean.ComponentVerifierExtension; 033import org.apache.camel.api.management.mbean.ComponentVerifierExtension.Result; 034import org.apache.camel.api.management.mbean.ComponentVerifierExtension.Result.Status; 035import org.apache.camel.api.management.mbean.ComponentVerifierExtension.Scope; 036import org.apache.camel.api.management.mbean.ComponentVerifierExtension.VerificationError; 037import org.apache.camel.api.management.mbean.ComponentVerifierExtension.VerificationError.ExceptionAttribute; 038import org.apache.camel.api.management.mbean.ComponentVerifierExtension.VerificationError.GroupAttribute; 039import org.apache.camel.api.management.mbean.ComponentVerifierExtension.VerificationError.HttpAttribute; 040import org.apache.camel.api.management.mbean.ComponentVerifierExtension.VerificationError.StandardCode; 041import org.apache.camel.api.management.mbean.ManagedComponentMBean; 042import org.apache.camel.spi.ManagementStrategy; 043import org.apache.camel.util.CastUtils; 044 045@ManagedResource(description = "Managed Component") 046public class ManagedComponent implements ManagedInstance, ManagedComponentMBean { 047 private final Component component; 048 private final String name; 049 050 public ManagedComponent(String name, Component component) { 051 this.name = name; 052 this.component = component; 053 } 054 055 public void init(ManagementStrategy strategy) { 056 // do nothing 057 } 058 059 public Component getComponent() { 060 return component; 061 } 062 063 @Override 064 public String getComponentName() { 065 return name; 066 } 067 068 @Override 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 @Override 081 public String getCamelId() { 082 return component.getCamelContext().getName(); 083 } 084 085 @Override 086 public String getCamelManagementName() { 087 return component.getCamelContext().getManagementName(); 088 } 089 090 @Override 091 public Object getInstance() { 092 return component; 093 } 094 095 @Override 096 public boolean isVerifySupported() { 097 return component.getExtension(org.apache.camel.component.extension.ComponentVerifierExtension.class).isPresent(); 098 } 099 100 @Override 101 public ComponentVerifierExtension.Result verify(String scope, Map<String, String> options) { 102 try { 103 org.apache.camel.component.extension.ComponentVerifierExtension.Scope scopeEnum = org.apache.camel.component.extension.ComponentVerifierExtension.Scope.fromString(scope); 104 Optional<org.apache.camel.component.extension.ComponentVerifierExtension> verifier = component.getExtension(org.apache.camel.component.extension.ComponentVerifierExtension.class); 105 if (verifier.isPresent()) { 106 org.apache.camel.component.extension.ComponentVerifierExtension.Result result = verifier.get().verify(scopeEnum, CastUtils.cast(options)); 107 String rstatus = result.getStatus().toString(); 108 String rscope = result.getScope().toString(); 109 return new ResultImpl(Scope.valueOf(rscope), Status.valueOf(rstatus), 110 result.getErrors().stream().map(this::translate).collect(Collectors.toList())); 111 112 } else { 113 return new ResultImpl(Scope.PARAMETERS, Status.UNSUPPORTED, Collections.emptyList()); 114 } 115 } catch (IllegalArgumentException e) { 116 return new ResultImpl(Scope.PARAMETERS, Status.UNSUPPORTED, Collections.singletonList( 117 new VerificationErrorImpl(StandardCode.UNSUPPORTED_SCOPE, "Unsupported scope: " + scope))); 118 } 119 } 120 121 private VerificationError translate(org.apache.camel.component.extension.ComponentVerifierExtension.VerificationError error) { 122 return new VerificationErrorImpl(translate(error.getCode()), error.getDescription(), 123 error.getParameterKeys(), translate(error.getDetails())); 124 } 125 126 private Map<VerificationError.Attribute, Object> translate(Map<org.apache.camel.component.extension.ComponentVerifierExtension.VerificationError.Attribute, Object> details) { 127 return details.entrySet().stream().collect(Collectors.toMap(e -> translate(e.getKey()), Entry::getValue)); 128 } 129 130 private VerificationError.Attribute translate(org.apache.camel.component.extension.ComponentVerifierExtension.VerificationError.Attribute attribute) { 131 if (attribute == org.apache.camel.component.extension.ComponentVerifierExtension.VerificationError.GroupAttribute.GROUP_NAME) { 132 return GroupAttribute.GROUP_NAME; 133 } else if (attribute == org.apache.camel.component.extension.ComponentVerifierExtension.VerificationError.GroupAttribute.GROUP_OPTIONS) { 134 return GroupAttribute.GROUP_OPTIONS; 135 } else if (attribute == org.apache.camel.component.extension.ComponentVerifierExtension.VerificationError.HttpAttribute.HTTP_CODE) { 136 return HttpAttribute.HTTP_CODE; 137 } else if (attribute == org.apache.camel.component.extension.ComponentVerifierExtension.VerificationError.HttpAttribute.HTTP_REDIRECT) { 138 return HttpAttribute.HTTP_REDIRECT; 139 } else if (attribute == org.apache.camel.component.extension.ComponentVerifierExtension.VerificationError.HttpAttribute.HTTP_TEXT) { 140 return HttpAttribute.HTTP_TEXT; 141 } else if (attribute == org.apache.camel.component.extension.ComponentVerifierExtension.VerificationError.ExceptionAttribute.EXCEPTION_CLASS) { 142 return ExceptionAttribute.EXCEPTION_CLASS; 143 } else if (attribute == org.apache.camel.component.extension.ComponentVerifierExtension.VerificationError.ExceptionAttribute.EXCEPTION_INSTANCE) { 144 return ExceptionAttribute.EXCEPTION_INSTANCE; 145 } else if (attribute != null) { 146 return VerificationError.asAttribute(attribute.getName()); 147 } else { 148 return null; 149 } 150 } 151 152 private VerificationError.Code translate(org.apache.camel.component.extension.ComponentVerifierExtension.VerificationError.Code code) { 153 if (code == org.apache.camel.component.extension.ComponentVerifierExtension.VerificationError.StandardCode.AUTHENTICATION) { 154 return StandardCode.AUTHENTICATION; 155 } else if (code == org.apache.camel.component.extension.ComponentVerifierExtension.VerificationError.StandardCode.EXCEPTION) { 156 return StandardCode.EXCEPTION; 157 } else if (code == org.apache.camel.component.extension.ComponentVerifierExtension.VerificationError.StandardCode.INTERNAL) { 158 return StandardCode.INTERNAL; 159 } else if (code == org.apache.camel.component.extension.ComponentVerifierExtension.VerificationError.StandardCode.MISSING_PARAMETER) { 160 return StandardCode.MISSING_PARAMETER; 161 } else if (code == org.apache.camel.component.extension.ComponentVerifierExtension.VerificationError.StandardCode.UNKNOWN_PARAMETER) { 162 return StandardCode.UNKNOWN_PARAMETER; 163 } else if (code == org.apache.camel.component.extension.ComponentVerifierExtension.VerificationError.StandardCode.ILLEGAL_PARAMETER) { 164 return StandardCode.ILLEGAL_PARAMETER; 165 } else if (code == org.apache.camel.component.extension.ComponentVerifierExtension.VerificationError.StandardCode.ILLEGAL_PARAMETER_GROUP_COMBINATION) { 166 return StandardCode.ILLEGAL_PARAMETER_GROUP_COMBINATION; 167 } else if (code == org.apache.camel.component.extension.ComponentVerifierExtension.VerificationError.StandardCode.ILLEGAL_PARAMETER_VALUE) { 168 return StandardCode.ILLEGAL_PARAMETER_VALUE; 169 } else if (code == org.apache.camel.component.extension.ComponentVerifierExtension.VerificationError.StandardCode.INCOMPLETE_PARAMETER_GROUP) { 170 return StandardCode.ILLEGAL_PARAMETER_GROUP_COMBINATION; 171 } else if (code == org.apache.camel.component.extension.ComponentVerifierExtension.VerificationError.StandardCode.UNSUPPORTED) { 172 return StandardCode.UNSUPPORTED; 173 } else if (code == org.apache.camel.component.extension.ComponentVerifierExtension.VerificationError.StandardCode.UNSUPPORTED_SCOPE) { 174 return StandardCode.UNSUPPORTED_SCOPE; 175 } else if (code == org.apache.camel.component.extension.ComponentVerifierExtension.VerificationError.StandardCode.UNSUPPORTED_COMPONENT) { 176 return StandardCode.UNSUPPORTED_COMPONENT; 177 } else if (code == org.apache.camel.component.extension.ComponentVerifierExtension.VerificationError.StandardCode.GENERIC) { 178 return StandardCode.GENERIC; 179 } else if (code != null) { 180 return VerificationError.asCode(code.getName()); 181 } else { 182 return null; 183 } 184 } 185 186 public static class VerificationErrorImpl implements VerificationError { 187 private final Code code; 188 private final String description; 189 private final Set<String> parameterKeys; 190 private final Map<Attribute, Object> details; 191 192 public VerificationErrorImpl(Code code, String description) { 193 this.code = code; 194 this.description = description; 195 this.parameterKeys = Collections.emptySet(); 196 this.details = Collections.emptyMap(); 197 } 198 199 public VerificationErrorImpl(Code code, String description, Set<String> parameterKeys, Map<Attribute, Object> details) { 200 this.code = code; 201 this.description = description; 202 this.parameterKeys = parameterKeys; 203 this.details = details; 204 } 205 206 @Override 207 public Code getCode() { 208 return code; 209 } 210 211 @Override 212 public String getDescription() { 213 return description; 214 } 215 216 @Override 217 public Set<String> getParameterKeys() { 218 return parameterKeys; 219 } 220 221 @Override 222 public Map<Attribute, Object> getDetails() { 223 return details; 224 } 225 } 226 227 public static class ResultImpl implements Result { 228 private final Scope scope; 229 private final Status status; 230 private final List<VerificationError> errors; 231 232 public ResultImpl(Scope scope, Status status, List<VerificationError> errors) { 233 this.scope = scope; 234 this.status = status; 235 this.errors = errors; 236 } 237 238 @Override 239 public Scope getScope() { 240 return scope; 241 } 242 243 @Override 244 public Status getStatus() { 245 return status; 246 } 247 248 @Override 249 public List<VerificationError> getErrors() { 250 return errors; 251 } 252 } 253}