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    public String getComponentName() {
064        return name;
065    }
066
067    public String getState() {
068        // must use String type to be sure remote JMX can read the attribute without requiring Camel classes.
069        if (component instanceof StatefulService) {
070            ServiceStatus status = ((StatefulService) component).getStatus();
071            return status.name();
072        }
073
074        // assume started if not a ServiceSupport instance
075        return ServiceStatus.Started.name();
076    }
077
078    public String getCamelId() {
079        return component.getCamelContext().getName();
080    }
081
082    public String getCamelManagementName() {
083        return component.getCamelContext().getManagementName();
084    }
085
086    public Object getInstance() {
087        return component;
088    }
089
090    @Override
091    public boolean isVerifySupported() {
092        return component.getExtension(org.apache.camel.component.extension.ComponentVerifierExtension.class).isPresent();
093    }
094
095    @Override
096    public ComponentVerifierExtension.Result verify(String scope, Map<String, String> options) {
097        try {
098            org.apache.camel.component.extension.ComponentVerifierExtension.Scope scopeEnum = org.apache.camel.component.extension.ComponentVerifierExtension.Scope.fromString(scope);
099            Optional<org.apache.camel.component.extension.ComponentVerifierExtension> verifier = component.getExtension(org.apache.camel.component.extension.ComponentVerifierExtension.class);
100            if (verifier.isPresent()) {
101                org.apache.camel.component.extension.ComponentVerifierExtension.Result result = verifier.get().verify(scopeEnum, CastUtils.cast(options));
102                String rstatus = result.getStatus().toString();
103                String rscope = result.getScope().toString();
104                return new ResultImpl(Scope.valueOf(rscope), Status.valueOf(rstatus),
105                        result.getErrors().stream().map(this::translate).collect(Collectors.toList()));
106
107            } else {
108                return new ResultImpl(Scope.PARAMETERS, Status.UNSUPPORTED, Collections.emptyList());
109            }
110        } catch (IllegalArgumentException e) {
111            return new ResultImpl(Scope.PARAMETERS, Status.UNSUPPORTED, Collections.singletonList(
112                    new VerificationErrorImpl(StandardCode.UNSUPPORTED_SCOPE, "Unsupported scope: " + scope)));
113        }
114    }
115
116    private VerificationError translate(org.apache.camel.component.extension.ComponentVerifierExtension.VerificationError error) {
117        return new VerificationErrorImpl(translate(error.getCode()), error.getDescription(),
118                error.getParameterKeys(), translate(error.getDetails()));
119    }
120
121    private Map<VerificationError.Attribute, Object> translate(Map<org.apache.camel.component.extension.ComponentVerifierExtension.VerificationError.Attribute, Object> details) {
122        return details.entrySet().stream().collect(Collectors.toMap(e -> translate(e.getKey()), Entry::getValue));
123    }
124
125    private VerificationError.Attribute translate(org.apache.camel.component.extension.ComponentVerifierExtension.VerificationError.Attribute attribute) {
126        if (attribute == org.apache.camel.component.extension.ComponentVerifierExtension.VerificationError.GroupAttribute.GROUP_NAME) {
127            return GroupAttribute.GROUP_NAME;
128        } else if (attribute == org.apache.camel.component.extension.ComponentVerifierExtension.VerificationError.GroupAttribute.GROUP_OPTIONS) {
129            return GroupAttribute.GROUP_OPTIONS;
130        } else if (attribute == org.apache.camel.component.extension.ComponentVerifierExtension.VerificationError.HttpAttribute.HTTP_CODE) {
131            return HttpAttribute.HTTP_CODE;
132        } else if (attribute == org.apache.camel.component.extension.ComponentVerifierExtension.VerificationError.HttpAttribute.HTTP_REDIRECT) {
133            return HttpAttribute.HTTP_REDIRECT;
134        } else if (attribute == org.apache.camel.component.extension.ComponentVerifierExtension.VerificationError.HttpAttribute.HTTP_TEXT) {
135            return HttpAttribute.HTTP_TEXT;
136        } else if (attribute == org.apache.camel.component.extension.ComponentVerifierExtension.VerificationError.ExceptionAttribute.EXCEPTION_CLASS) {
137            return ExceptionAttribute.EXCEPTION_CLASS;
138        } else if (attribute == org.apache.camel.component.extension.ComponentVerifierExtension.VerificationError.ExceptionAttribute.EXCEPTION_INSTANCE) {
139            return ExceptionAttribute.EXCEPTION_INSTANCE;
140        }  else if (attribute != null) {
141            return VerificationError.asAttribute(attribute.getName());
142        } else {
143            return null;
144        }
145    }
146
147    private VerificationError.Code translate(org.apache.camel.component.extension.ComponentVerifierExtension.VerificationError.Code code) {
148        if (code == org.apache.camel.component.extension.ComponentVerifierExtension.VerificationError.StandardCode.AUTHENTICATION) {
149            return StandardCode.AUTHENTICATION;
150        } else if (code == org.apache.camel.component.extension.ComponentVerifierExtension.VerificationError.StandardCode.EXCEPTION) {
151            return StandardCode.EXCEPTION;
152        } else if (code == org.apache.camel.component.extension.ComponentVerifierExtension.VerificationError.StandardCode.INTERNAL) {
153            return StandardCode.INTERNAL;
154        } else if (code == org.apache.camel.component.extension.ComponentVerifierExtension.VerificationError.StandardCode.MISSING_PARAMETER) {
155            return StandardCode.MISSING_PARAMETER;
156        } else if (code == org.apache.camel.component.extension.ComponentVerifierExtension.VerificationError.StandardCode.UNKNOWN_PARAMETER) {
157            return StandardCode.UNKNOWN_PARAMETER;
158        } else if (code == org.apache.camel.component.extension.ComponentVerifierExtension.VerificationError.StandardCode.ILLEGAL_PARAMETER) {
159            return StandardCode.ILLEGAL_PARAMETER;
160        } else if (code == org.apache.camel.component.extension.ComponentVerifierExtension.VerificationError.StandardCode.ILLEGAL_PARAMETER_GROUP_COMBINATION) {
161            return StandardCode.ILLEGAL_PARAMETER_GROUP_COMBINATION;
162        } else if (code == org.apache.camel.component.extension.ComponentVerifierExtension.VerificationError.StandardCode.ILLEGAL_PARAMETER_VALUE) {
163            return StandardCode.ILLEGAL_PARAMETER_VALUE;
164        } else if (code == org.apache.camel.component.extension.ComponentVerifierExtension.VerificationError.StandardCode.INCOMPLETE_PARAMETER_GROUP) {
165            return StandardCode.ILLEGAL_PARAMETER_GROUP_COMBINATION;
166        } else if (code == org.apache.camel.component.extension.ComponentVerifierExtension.VerificationError.StandardCode.UNSUPPORTED) {
167            return StandardCode.UNSUPPORTED;
168        } else if (code == org.apache.camel.component.extension.ComponentVerifierExtension.VerificationError.StandardCode.UNSUPPORTED_SCOPE) {
169            return StandardCode.UNSUPPORTED_SCOPE;
170        } else if (code == org.apache.camel.component.extension.ComponentVerifierExtension.VerificationError.StandardCode.UNSUPPORTED_COMPONENT) {
171            return StandardCode.UNSUPPORTED_COMPONENT;
172        } else if (code == org.apache.camel.component.extension.ComponentVerifierExtension.VerificationError.StandardCode.GENERIC) {
173            return StandardCode.GENERIC;
174        } else if (code != null) {
175            return VerificationError.asCode(code.getName());
176        } else {
177            return null;
178        }
179    }
180
181    public static class VerificationErrorImpl implements VerificationError {
182        private final Code code;
183        private final String description;
184        private final Set<String> parameterKeys;
185        private final Map<Attribute, Object> details;
186
187        public VerificationErrorImpl(Code code, String description) {
188            this.code = code;
189            this.description = description;
190            this.parameterKeys = Collections.emptySet();
191            this.details = Collections.emptyMap();
192        }
193
194        public VerificationErrorImpl(Code code, String description, Set<String> parameterKeys, Map<Attribute, Object> details) {
195            this.code = code;
196            this.description = description;
197            this.parameterKeys = parameterKeys;
198            this.details = details;
199        }
200
201        @Override
202        public Code getCode() {
203            return code;
204        }
205
206        @Override
207        public String getDescription() {
208            return description;
209        }
210
211        @Override
212        public Set<String> getParameterKeys() {
213            return parameterKeys;
214        }
215
216        @Override
217        public Map<Attribute, Object> getDetails() {
218            return details;
219        }
220    }
221
222    public static class ResultImpl implements Result {
223        private final Scope scope;
224        private final Status status;
225        private final List<VerificationError> errors;
226
227        public ResultImpl(Scope scope, Status status, List<VerificationError> errors) {
228            this.scope = scope;
229            this.status = status;
230            this.errors = errors;
231        }
232
233        @Override
234        public Scope getScope() {
235            return scope;
236        }
237
238        @Override
239        public Status getStatus() {
240            return status;
241        }
242
243        @Override
244        public List<VerificationError> getErrors() {
245            return errors;
246        }
247    }
248}