org.zodiac.actuate.feature.AppFeaturesEndpoint Maven / Gradle / Ivy
The newest version!
package org.zodiac.actuate.feature;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
@Endpoint(id = AppFeaturesEndpoint.ENDPOINT_NAME)
public class AppFeaturesEndpoint implements ApplicationContextAware {
public static final String ENDPOINT_NAME = "app-features";
private final List hasFeaturesList;
private ApplicationContext context;
public AppFeaturesEndpoint(List hasFeaturesList) {
this.hasFeaturesList = hasFeaturesList;
}
@Override
public void setApplicationContext(ApplicationContext context) throws BeansException {
this.context = context;
}
@ReadOperation
public Features features() {
Features features = new Features();
for (HasFeatures hasFeatures : this.hasFeaturesList) {
List> abstractFeatures = hasFeatures.getAbstractFeatures();
if (abstractFeatures != null) {
for (Class> clazz : abstractFeatures) {
addAbstractFeature(features, clazz);
}
}
List namedFeatures = hasFeatures.getNamedFeatures();
if (namedFeatures != null) {
for (NamedFeature namedFeature : namedFeatures) {
addFeature(features, namedFeature);
}
}
}
return features;
}
private void addAbstractFeature(Features features, Class> type) {
String featureName = type.getSimpleName();
try {
Object bean = this.context.getBean(type);
Class> beanClass = bean.getClass();
addFeature(features, new NamedFeature(featureName, beanClass));
} catch (NoSuchBeanDefinitionException e) {
features.getDisabled().add(featureName);
}
}
private void addFeature(Features features, NamedFeature feature) {
Class> type = feature.getType();
features.getEnabled().add(new Feature(feature.getName(), type.getCanonicalName(),
type.getPackage().getImplementationVersion(), type.getPackage().getImplementationVendor()));
}
static class Features {
final List enabled = new ArrayList<>();
final List disabled = new ArrayList<>();
public List getEnabled() {
return this.enabled;
}
public List getDisabled() {
return this.disabled;
}
}
static class Feature {
final String type;
final String name;
final String version;
final String vendor;
Feature(String name, String type, String version, String vendor) {
this.type = type;
this.name = name;
this.version = version;
this.vendor = vendor;
}
public String getType() {
return this.type;
}
public String getName() {
return this.name;
}
public String getVersion() {
return this.version;
}
public String getVendor() {
return this.vendor;
}
@Override
public String toString() {
return "Feature{" + "type='" + this.type + '\'' + ", name='" + this.name + '\'' + ", version='"
+ this.version + '\'' + ", vendor='" + this.vendor + '\'' + '}';
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
Feature feature = (Feature)o;
if (this.type != null ? !this.type.equals(feature.type) : feature.type != null) {
return false;
}
if (this.name != null ? !this.name.equals(feature.name) : feature.name != null) {
return false;
}
if (this.version != null ? !this.version.equals(feature.version) : feature.version != null) {
return false;
}
return this.vendor != null ? this.vendor.equals(feature.vendor) : feature.vendor == null;
}
@Override
public int hashCode() {
int result = this.type != null ? this.type.hashCode() : 0;
result = 31 * result + (this.name != null ? this.name.hashCode() : 0);
result = 31 * result + (this.version != null ? this.version.hashCode() : 0);
result = 31 * result + (this.vendor != null ? this.vendor.hashCode() : 0);
return result;
}
}
}