Skip to content

Fix read type from data #248

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ Complete the following steps to enable Nacos for your Spring project.
<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-spring-context</artifactId>
<version>1.0.0</version>
<version>1.0.1</version>
</dependency>

...
Expand Down
4 changes: 2 additions & 2 deletions nacos-spring-context/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
<parent>
<artifactId>nacos-spring-parent</artifactId>
<groupId>com.alibaba.nacos</groupId>
<version>1.0.0</version>
<version>1.0.1</version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>

<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-spring-context</artifactId>
<version>1.0.0</version>
<version>1.0.1</version>
<name>Alibaba Nacos :: Spring :: Context</name>
<packaging>jar</packaging>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,6 @@
@Import(NacosBeanDefinitionRegistrar.class)
public @interface EnableNacos {

/**
* Whether to get the file type from dataId
*
* @return read config-type from dataId, default value is {@link Boolean#TRUE}
*/
boolean readConfigTypeFromDataId() default true;

/**
* Global {@link NacosProperties Nacos Properties}
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@
import org.springframework.core.env.Environment;
import org.springframework.core.type.AnnotationMetadata;

import com.alibaba.nacos.spring.util.NacosUtils;

/**
* Nacos Properties {@link ImportBeanDefinitionRegistrar BeanDefinition Registrar}
*
Expand Down Expand Up @@ -66,9 +64,6 @@ public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata,
.fromMap(importingClassMetadata
.getAnnotationAttributes(EnableNacos.class.getName()));

boolean readTypeFromDataId = attributes.getBoolean("readConfigTypeFromDataId");
NacosUtils.setReadTypeFromDataIdIfNull(readTypeFromDataId);

// Register Global Nacos Properties Bean
registerGlobalNacosProperties(attributes, registry, environment,
GLOBAL_NACOS_PROPERTIES_BEAN_NAME);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,13 +141,6 @@
String PASSWORD_PLACEHOLDER = "${" + CONFIG_PREFIX + PASSWORD + ":"
+ NacosProperties.PASSWORD_PLACEHOLDER + "}";

/**
* Whether to get the file type from dataId
*
* @return read config-type from dataId, default value is {@link Boolean#TRUE}
*/
boolean readConfigTypeFromDataId() default true;

/**
* Global {@link NacosProperties Nacos Properties}
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
import org.springframework.core.type.AnnotationMetadata;

import com.alibaba.nacos.spring.util.NacosBeanUtils;
import com.alibaba.nacos.spring.util.NacosUtils;

/**
* Nacos Config {@link ImportBeanDefinitionRegistrar BeanDefinition Registrar}
Expand All @@ -62,9 +61,6 @@ public void registerBeanDefinitions(AnnotationMetadata metadata,
AnnotationAttributes attributes = fromMap(
metadata.getAnnotationAttributes(EnableNacosConfig.class.getName()));

boolean readTypeFromDataId = attributes.getBoolean("readConfigTypeFromDataId");
NacosUtils.setReadTypeFromDataIdIfNull(readTypeFromDataId);

// Register Global Nacos Properties Bean
registerGlobalNacosProperties(attributes, registry, environment,
CONFIG_GLOBAL_NACOS_PROPERTIES_BEAN_NAME);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.util.Map;
import java.util.Properties;

import com.alibaba.nacos.api.config.ConfigType;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanCreationException;
import org.springframework.context.ApplicationContext;
Expand Down Expand Up @@ -97,14 +98,14 @@ protected void processListenerMethod(String beanName, final Object bean,
environment);
final String groupId = NacosUtils.readFromEnvironment(listener.groupId(),
environment);

final String type;

if (NacosUtils.isReadTypeFromDataId()) {

ConfigType typeEunm = listener.type();
if (ConfigType.UNSET.equals(typeEunm)) {
type = NacosUtils.readFileExtension(dataId);
}
else {
type = listener.type().getType();
type = typeEunm.getType();
}

long timeout = listener.timeout();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@
*
* @return the type of config
*/
ConfigType type() default ConfigType.PROPERTIES;
ConfigType type() default ConfigType.UNSET;

/**
* The {@link NacosProperties} attribute, If not specified, it will use
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import org.w3c.dom.Element;

import com.alibaba.nacos.spring.context.annotation.NacosBeanDefinitionRegistrar;
import com.alibaba.nacos.spring.util.NacosUtils;

/**
* Nacos Annotation Driven {@link BeanDefinitionParser} for XML element
Expand All @@ -39,9 +38,6 @@ public class NacosAnnotationDrivenBeanDefinitionParser implements BeanDefinition
@Override
public BeanDefinition parse(Element element, ParserContext parserContext) {

String val = element.getAttribute("read-type-from-daraId");
NacosUtils.setReadTypeFromDataIdIfNull("true".equalsIgnoreCase(val));

// Get Environment
Environment environment = parserContext.getDelegate().getReaderContext()
.getReader().getEnvironment();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,15 @@ public boolean publishConfig(String dataId, String group, String content)
published));
return published;
}


@Override
public boolean publishConfig(String dataId, String group, String content, String type) throws NacosException {
boolean published = configService.publishConfig(dataId, group, content, type);
publishEvent(new NacosConfigPublishedEvent(configService, dataId, group, content,
published));
return published;
}

@Override
public boolean removeConfig(String dataId, String group) throws NacosException {
boolean removed = configService.removeConfig(dataId, group);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,13 +102,13 @@ protected void bind(final Object bean, final String beanName,
final String groupId = NacosUtils.readFromEnvironment(properties.groupId(),
environment);
final String type;

if (NacosUtils.isReadTypeFromDataId()) {

ConfigType typeEunm = properties.yaml() ? ConfigType.YAML : properties.type();
if (ConfigType.UNSET.equals(typeEunm)) {
type = NacosUtils.readFileExtension(dataId);
}
else {
type = (properties.yaml() ? ConfigType.YAML.getType()
: properties.type().getType());
type = typeEunm.getType();
}

final ConfigService configService = configServiceBeanBuilder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,15 +164,15 @@ protected NacosPropertySource doBuild(String beanName, T beanDefinition,

dataId = NacosUtils.readFromEnvironment(dataId, environment);
groupId = NacosUtils.readFromEnvironment(groupId, environment);

String type = null;

if (NacosUtils.isReadTypeFromDataId()) {

final String type;

ConfigType typeEunm = (ConfigType) runtimeAttributes.get(CONFIG_TYPE_ATTRIBUTE_NAME);
if (ConfigType.UNSET.equals(typeEunm)) {
type = NacosUtils.readFileExtension(dataId);
}
else {
type = ((ConfigType) runtimeAttributes.get(CONFIG_TYPE_ATTRIBUTE_NAME))
.getType();
type = typeEunm.getType();
}

Map<String, Object> nacosPropertiesAttributes = (Map<String, Object>) runtimeAttributes
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/*
* 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.enums;

import com.alibaba.nacos.common.utils.StringUtils;

/**
* Config file type enum.
*
*/
public enum FileTypeEnum {

/**
* Yaml file.
*/
YML("yaml"),

/**
* Yaml file.
*/
YAML("yaml"),

/**
* Text file.
*/
TXT("text"),

/**
* Text file.
*/
TEXT("text"),

/**
* Json file.
*/
JSON("json"),

/**
* Xml file.
*/
XML("xml"),

/**
* Html file.
*/
HTM("html"),

/**
* Html file.
*/
HTML("html"),

/**
* Properties file.
*/
PROPERTIES("properties");

/**
* File type corresponding to file extension.
*/
private String fileType;

FileTypeEnum(String fileType) {
this.fileType = fileType;
}

public String getFileType() {
return this.fileType;
}


/**
* Get the corresponding FileTypeEnum by file extension or fileType. If not found FileTypeEnum.TEXT is returned
*
* @param extOrFileType file extension or fileType
* @return
*/
public static FileTypeEnum getFileTypeEnumByFileExtensionOrFileType(String extOrFileType) {
if (StringUtils.isNotBlank(extOrFileType)) {
String upperExtName = extOrFileType.trim().toUpperCase();
for (FileTypeEnum value : VALUES) {
if (value.name().equals(upperExtName)) {
return value;
}
}
}
return FileTypeEnum.PROPERTIES;
}

private static final FileTypeEnum[] VALUES = FileTypeEnum.values();
}
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,12 @@ public void updateService(Service service, AbstractSelector selector)
throws NacosException {
delegate.updateService(service, selector);
}


@Override
public void shutDown() throws NacosException {
delegate.shutDown();
}

@Override
public Properties getProperties() {
return properties;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import java.util.Set;
import java.util.regex.Pattern;

import com.alibaba.nacos.spring.enums.FileTypeEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.MutablePropertyValues;
Expand Down Expand Up @@ -102,22 +103,6 @@ public abstract class NacosUtils {

private static final Logger logger = LoggerFactory.getLogger(NacosUtils.class);

private static Boolean readTypeFromDataId = null;

public static Boolean isReadTypeFromDataId() {
return readTypeFromDataId;
}

public static void setReadTypeFromDataIdIfNull(boolean readTypeFromDataId) {
if (NacosUtils.readTypeFromDataId == null) {
NacosUtils.readTypeFromDataId = readTypeFromDataId;
}
}

public static void resetReadTypeFromDataId() {
NacosUtils.readTypeFromDataId = null;
}

/**
* Build The default name of {@link NacosConfigurationProperties @NacosPropertySource}
*
Expand Down Expand Up @@ -220,7 +205,9 @@ public static String readFromEnvironment(String label, Environment environment)

public static String readFileExtension(String dataId) {
int lastIndex = dataId.lastIndexOf(".");
return dataId.substring(lastIndex + 1);
final String extName = dataId.substring(lastIndex + 1);
FileTypeEnum fileTypeEnum = FileTypeEnum.getFileTypeEnumByFileExtensionOrFileType(extName);
return fileTypeEnum.getFileType();
}

public static PropertyValues resolvePropertyValues(Object bean, String content,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@
@NacosConfigProperties / @NacosProperty / @NacosIgnore
]]></xsd:documentation>
</xsd:annotation>
<xsd:complexType>
<xsd:attribute name="read-type-from-daraId" default="true"/>
</xsd:complexType>
</xsd:element>

<xsd:element name="global-properties">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,16 +74,6 @@ public class AnnotationNacosInjectedBeanPostProcessorTest
@NacosInjected(properties = @NacosProperties(encode = "GBK"))
private NamingService namingService3;

@BeforeClass
public static void beforeClass() {
NacosUtils.resetReadTypeFromDataId();
}

@AfterClass
public static void afterClass() {
NacosUtils.resetReadTypeFromDataId();
}

@Bean(name = ApplicationContextHolder.BEAN_NAME)
public ApplicationContextHolder applicationContextHolder(
ApplicationContext applicationContext) {
Expand Down
Loading