Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.nacos.spring.util;
import com.alibaba.nacos.api.annotation.NacosProperties;
import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.config.annotation.NacosConfigurationProperties;
import com.alibaba.nacos.api.config.annotation.NacosIgnore;
import com.alibaba.nacos.api.config.annotation.NacosProperty;
import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.spring.enums.FileTypeEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.MutablePropertyValues;
import org.springframework.beans.PropertyValues;
import org.springframework.beans.factory.config.BeanExpressionContext;
import org.springframework.beans.factory.config.BeanExpressionResolver;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.expression.EnvironmentAccessor;
import org.springframework.context.expression.StandardBeanExpressionResolver;
import org.springframework.core.env.Environment;
import org.springframework.core.env.PropertyResolver;
import org.springframework.expression.Expression;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.common.TemplateParserContext;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ReflectionUtils;
import org.springframework.util.StringUtils;
import java.lang.annotation.Annotation;
import java.lang.reflect.*;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.regex.Pattern;
import static com.alibaba.nacos.api.PropertyKeyConst.*;
import static org.springframework.core.annotation.AnnotationUtils.getAnnotation;
import static org.springframework.core.annotation.AnnotationUtils.getAnnotationAttributes;
import static org.springframework.util.StringUtils.hasText;
/**
* Nacos Utilities class
*
* @author Mercy
* @since 0.1.0
*/
public abstract class NacosUtils {
/**
* Default value of {@link String} attribute for {@link Annotation}
*/
public static final String DEFAULT_STRING_ATTRIBUTE_VALUE = "";
/**
* Default value of {@link String} attribute for {@link Annotation}
*/
public static final String DEFAULT_CONFIG_TYPE_VALUE = "properties";
/**
* Default value of boolean attribute for {@link Annotation}
*/
public static final boolean DEFAULT_BOOLEAN_ATTRIBUTE_VALUE = false;
/**
* The separator
*/
public static final String SEPARATOR = "|";
/**
* Default timeout for getting Nacos configuration
*/
public static final long DEFAULT_TIMEOUT = Long.getLong("nacos.default.timeout",
5000L);
private static final Set> NON_BEAN_CLASSES = Collections.unmodifiableSet(
new HashSet>(Arrays.asList(Object.class, Class.class)));
private static ExpressionParser parser = new SpelExpressionParser();
private static BeanExpressionResolver resolver = new StandardBeanExpressionResolver();
private static ConcurrentHashMap expressionCache
= new ConcurrentHashMap();
private static ConcurrentHashMap environmentContextCache
= new ConcurrentHashMap();
private static final Logger logger = LoggerFactory.getLogger(NacosUtils.class);
/**
* Build The default name of {@link NacosConfigurationProperties @NacosPropertySource}
*
* @param dataId data Id
* @param groupId group Id
* @param properties Nacos Properties
* @return non-null
*/
public static String buildDefaultPropertySourceName(String dataId, String groupId,
Map, ?> properties) {
return build(dataId, groupId, identify(properties));
}
/**
* Generate Id of {@link NacosProperties Nacos Properties annotation}
*
* @param nacosProperties {@link NacosProperties Nacos Properties annotation}
* @return Id
*/
public static String identify(NacosProperties nacosProperties) {
return identify(getAnnotationAttributes(nacosProperties));
}
/**
* Generate Id of {@link NacosProperties Nacos Properties}
*
* @param properties {@link Properties Nacos Properties}
* @return Id
*/
public static String identify(Map, ?> properties) {
String namespace = (String) properties.get(NAMESPACE);
String serverAddress = (String) properties.get(SERVER_ADDR);
String contextPath = (String) properties.get(CONTEXT_PATH);
String clusterName = (String) properties.get(CLUSTER_NAME);
String endpoint = (String) properties.get(ENDPOINT);
String accessKey = (String) properties.get(ACCESS_KEY);
String secretKey = (String) properties.get(SECRET_KEY);
String encode = (String) properties.get(ENCODE);
return build(namespace, clusterName, serverAddress, contextPath, endpoint,
accessKey, secretKey, encode);
}
private static String build(Object... values) {
StringBuilder stringBuilder = new StringBuilder();
for (Object value : values) {
String stringValue = value == null ? null : String.valueOf(value);
if (StringUtils.hasText(stringValue)) {
stringBuilder.append(stringValue);
}
stringBuilder.append(SEPARATOR);
}
return stringBuilder.toString();
}
/**
* Is {@link NacosProperties @NacosProperties} with default attribute values.
*
* @param nacosProperties {@link NacosProperties @NacosProperties}
* @return If default values , return true,or false
*/
public static boolean isDefault(final NacosProperties nacosProperties) {
final List