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 104 = org.apache.camel.component.extension.ComponentVerifierExtension.Scope.fromString(scope); 105 Optional<org.apache.camel.component.extension.ComponentVerifierExtension> verifier 106 = component.getExtension(org.apache.camel.component.extension.ComponentVerifierExtension.class); 107 if (verifier.isPresent()) { 108 org.apache.camel.component.extension.ComponentVerifierExtension.Result result 109 = verifier.get().verify(scopeEnum, CastUtils.cast(options)); 110 String rstatus = result.getStatus().toString(); 111 String rscope = result.getScope().toString(); 112 return new ResultImpl( 113 Scope.valueOf(rscope), Status.valueOf(rstatus), 114 result.getErrors().stream().map(this::translate).collect(Collectors.toList())); 115 116 } else { 117 return new ResultImpl(Scope.PARAMETERS, Status.UNSUPPORTED, Collections.emptyList()); 118 } 119 } catch (IllegalArgumentException e) { 120 return new ResultImpl( 121 Scope.PARAMETERS, Status.UNSUPPORTED, Collections.singletonList( 122 new VerificationErrorImpl(StandardCode.UNSUPPORTED_SCOPE, "Unsupported scope: " + scope))); 123 } 124 } 125 126 private VerificationError translate( 127 org.apache.camel.component.extension.ComponentVerifierExtension.VerificationError error) { 128 return new VerificationErrorImpl( 129 translate(error.getCode()), error.getDescription(), 130 error.getParameterKeys(), translate(error.getDetails())); 131 } 132 133 private Map<VerificationError.Attribute, Object> translate( 134 Map<org.apache.camel.component.extension.ComponentVerifierExtension.VerificationError.Attribute, Object> details) { 135 return details.entrySet().stream().collect(Collectors.toMap(e -> translate(e.getKey()), Entry::getValue)); 136 } 137 138 private VerificationError.Attribute translate( 139 org.apache.camel.component.extension.ComponentVerifierExtension.VerificationError.Attribute attribute) { 140 if (attribute 141 == org.apache.camel.component.extension.ComponentVerifierExtension.VerificationError.GroupAttribute.GROUP_NAME) { 142 return GroupAttribute.GROUP_NAME; 143 } else if (attribute 144 == org.apache.camel.component.extension.ComponentVerifierExtension.VerificationError.GroupAttribute.GROUP_OPTIONS) { 145 return GroupAttribute.GROUP_OPTIONS; 146 } else if (attribute 147 == org.apache.camel.component.extension.ComponentVerifierExtension.VerificationError.HttpAttribute.HTTP_CODE) { 148 return HttpAttribute.HTTP_CODE; 149 } else if (attribute 150 == org.apache.camel.component.extension.ComponentVerifierExtension.VerificationError.HttpAttribute.HTTP_REDIRECT) { 151 return HttpAttribute.HTTP_REDIRECT; 152 } else if (attribute 153 == org.apache.camel.component.extension.ComponentVerifierExtension.VerificationError.HttpAttribute.HTTP_TEXT) { 154 return HttpAttribute.HTTP_TEXT; 155 } else if (attribute 156 == org.apache.camel.component.extension.ComponentVerifierExtension.VerificationError.ExceptionAttribute.EXCEPTION_CLASS) { 157 return ExceptionAttribute.EXCEPTION_CLASS; 158 } else if (attribute 159 == org.apache.camel.component.extension.ComponentVerifierExtension.VerificationError.ExceptionAttribute.EXCEPTION_INSTANCE) { 160 return ExceptionAttribute.EXCEPTION_INSTANCE; 161 } else if (attribute != null) { 162 return VerificationError.asAttribute(attribute.getName()); 163 } else { 164 return null; 165 } 166 } 167 168 private VerificationError.Code translate( 169 org.apache.camel.component.extension.ComponentVerifierExtension.VerificationError.Code code) { 170 if (code 171 == org.apache.camel.component.extension.ComponentVerifierExtension.VerificationError.StandardCode.AUTHENTICATION) { 172 return StandardCode.AUTHENTICATION; 173 } else if (code 174 == org.apache.camel.component.extension.ComponentVerifierExtension.VerificationError.StandardCode.EXCEPTION) { 175 return StandardCode.EXCEPTION; 176 } else if (code 177 == org.apache.camel.component.extension.ComponentVerifierExtension.VerificationError.StandardCode.INTERNAL) { 178 return StandardCode.INTERNAL; 179 } else if (code 180 == org.apache.camel.component.extension.ComponentVerifierExtension.VerificationError.StandardCode.MISSING_PARAMETER) { 181 return StandardCode.MISSING_PARAMETER; 182 } else if (code 183 == org.apache.camel.component.extension.ComponentVerifierExtension.VerificationError.StandardCode.UNKNOWN_PARAMETER) { 184 return StandardCode.UNKNOWN_PARAMETER; 185 } else if (code 186 == org.apache.camel.component.extension.ComponentVerifierExtension.VerificationError.StandardCode.ILLEGAL_PARAMETER) { 187 return StandardCode.ILLEGAL_PARAMETER; 188 } else if (code 189 == org.apache.camel.component.extension.ComponentVerifierExtension.VerificationError.StandardCode.ILLEGAL_PARAMETER_GROUP_COMBINATION) { 190 return StandardCode.ILLEGAL_PARAMETER_GROUP_COMBINATION; 191 } else if (code 192 == org.apache.camel.component.extension.ComponentVerifierExtension.VerificationError.StandardCode.ILLEGAL_PARAMETER_VALUE) { 193 return StandardCode.ILLEGAL_PARAMETER_VALUE; 194 } else if (code 195 == org.apache.camel.component.extension.ComponentVerifierExtension.VerificationError.StandardCode.INCOMPLETE_PARAMETER_GROUP) { 196 return StandardCode.ILLEGAL_PARAMETER_GROUP_COMBINATION; 197 } else if (code 198 == org.apache.camel.component.extension.ComponentVerifierExtension.VerificationError.StandardCode.UNSUPPORTED) { 199 return StandardCode.UNSUPPORTED; 200 } else if (code 201 == org.apache.camel.component.extension.ComponentVerifierExtension.VerificationError.StandardCode.UNSUPPORTED_SCOPE) { 202 return StandardCode.UNSUPPORTED_SCOPE; 203 } else if (code 204 == org.apache.camel.component.extension.ComponentVerifierExtension.VerificationError.StandardCode.UNSUPPORTED_COMPONENT) { 205 return StandardCode.UNSUPPORTED_COMPONENT; 206 } else if (code 207 == org.apache.camel.component.extension.ComponentVerifierExtension.VerificationError.StandardCode.GENERIC) { 208 return StandardCode.GENERIC; 209 } else if (code != null) { 210 return VerificationError.asCode(code.getName()); 211 } else { 212 return null; 213 } 214 } 215 216 public static class VerificationErrorImpl implements VerificationError { 217 private final Code code; 218 private final String description; 219 private final Set<String> parameterKeys; 220 private final Map<Attribute, Object> details; 221 222 public VerificationErrorImpl(Code code, String description) { 223 this.code = code; 224 this.description = description; 225 this.parameterKeys = Collections.emptySet(); 226 this.details = Collections.emptyMap(); 227 } 228 229 public VerificationErrorImpl(Code code, String description, Set<String> parameterKeys, Map<Attribute, Object> details) { 230 this.code = code; 231 this.description = description; 232 this.parameterKeys = parameterKeys; 233 this.details = details; 234 } 235 236 @Override 237 public Code getCode() { 238 return code; 239 } 240 241 @Override 242 public String getDescription() { 243 return description; 244 } 245 246 @Override 247 public Set<String> getParameterKeys() { 248 return parameterKeys; 249 } 250 251 @Override 252 public Map<Attribute, Object> getDetails() { 253 return details; 254 } 255 } 256 257 public static class ResultImpl implements Result { 258 private final Scope scope; 259 private final Status status; 260 private final List<VerificationError> errors; 261 262 public ResultImpl(Scope scope, Status status, List<VerificationError> errors) { 263 this.scope = scope; 264 this.status = status; 265 this.errors = errors; 266 } 267 268 @Override 269 public Scope getScope() { 270 return scope; 271 } 272 273 @Override 274 public Status getStatus() { 275 return status; 276 } 277 278 @Override 279 public List<VerificationError> getErrors() { 280 return errors; 281 } 282 } 283}