org.zodiac.actuate.nacos.confcenter.NacosConfCenterEndpoint Maven / Gradle / Ivy
The newest version!
package org.zodiac.actuate.nacos.confcenter;
import java.lang.reflect.Method;
import java.util.Map;
import java.util.Properties;
import com.alibaba.nacos.api.config.annotation.NacosConfigListener;
import com.alibaba.nacos.api.config.annotation.NacosConfigurationProperties;
import com.alibaba.nacos.common.utils.JacksonUtils;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.w3c.dom.Element;
import org.zodiac.commons.util.Classes;
import org.zodiac.nacos.base.constants.NacosBeanConstants;
import org.zodiac.nacos.base.context.event.config.NacosConfigMetadataEvent;
import org.zodiac.nacos.base.util.NacosUtil;
import org.zodiac.sdk.toolkit.constants.StringPool;
import org.zodiac.sdk.toolkit.util.collection.CollUtil;
import org.zodiac.sdk.toolkit.util.lang.StrUtil;
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.ApplicationListener;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.type.AnnotationMetadata;
@Endpoint(id = NacosConfCenterEndpoint.ENDPOINT_ID)
public class NacosConfCenterEndpoint implements ApplicationListener {
static final String ENDPOINT_ID = "nacos-config";
private ApplicationContext applicationContext;
private Map nacosConfigMetadataMap = CollUtil.map(8);
public NacosConfCenterEndpoint(ApplicationContext applicationContext) {
this.applicationContext = applicationContext;
}
@ReadOperation
public Map invoke() {
Map result = CollUtil.map(8);
if (!(Classes.isAssignable(applicationContext.getEnvironment().getClass(), ConfigurableEnvironment.class))) {
result.put("error", "environment type not match ConfigurableEnvironment: "
+ applicationContext.getEnvironment().getClass().getName());
} else {
result.put("nacosConfigMetadata", nacosConfigMetadataMap.values());
result.put("nacosConfigGlobalProperties", NacosUtil.extractSafeProperties(
applicationContext.getBean(NacosBeanConstants.CONFIG_GLOBAL_NACOS_PROPERTIES_BEAN_NAME, Properties.class)));
}
return result;
}
@Override
public void onApplicationEvent(NacosConfigMetadataEvent event) {
String key = buildMetadataKey(event);
if (StrUtil.isNotEmpty(key) && !nacosConfigMetadataMap.containsKey(key)) {
ObjectNode jsonNode = JacksonUtils.createEmptyJsonNode();
jsonNode.put("groupId", event.getGroupId());
jsonNode.put("dataId", event.getDataId());
if (Classes.isAssignable(event.getSource().getClass(), AnnotationMetadata.class)) {
jsonNode.put("origin", "NacosPropertySource");
jsonNode.put("target", ((AnnotationMetadata)event.getSource()).getClassName());
} else if (Classes.isAssignable(event.getSource().getClass(), NacosConfigListener.class)) {
jsonNode.put("origin", "NacosConfigListener");
Method configListenerMethod = (Method)event.getAnnotatedElement();
jsonNode.put("target",
configListenerMethod.getDeclaringClass().getName() + ":" + configListenerMethod.toString());
} else if (Classes.isAssignable(event.getSource().getClass(), NacosConfigurationProperties.class)) {
jsonNode.put("origin", "NacosConfigurationProperties");
jsonNode.put("target", event.getBeanType().getName());
} else if (Classes.isAssignable(event.getSource().getClass(), Element.class)) {
jsonNode.put("origin", "NacosPropertySource");
jsonNode.put("target", event.getXmlResource().toString());
} else {
throw new RuntimeException("unknown NacosConfigMetadataEvent");
}
nacosConfigMetadataMap.put(key, jsonNode);
}
}
private String buildMetadataKey(NacosConfigMetadataEvent event) {
if (event.getXmlResource() != null) {
return event.getGroupId() + NacosUtil.SEPARATOR + event.getDataId() + NacosUtil.SEPARATOR
+ event.getXmlResource();
} else {
if (event.getBeanType() == null && event.getAnnotatedElement() == null) {
return StringPool.EMPTY;
}
return event.getGroupId() + NacosUtil.SEPARATOR + event.getDataId() + NacosUtil.SEPARATOR
+ event.getBeanType() + NacosUtil.SEPARATOR + event.getAnnotatedElement();
}
}
}