Java Code Examples for com.alibaba.nacos.spring.util.NacosUtils#readTypeFromDataId()

The following examples show how to use com.alibaba.nacos.spring.util.NacosUtils#readTypeFromDataId() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: NacosConfigListenerMethodProcessor.java    From nacos-spring-project with Apache License 2.0 4 votes vote down vote up
@Override
protected void processListenerMethod(String beanName, final Object bean,
		Class<?> beanClass, final NacosConfigListener listener, final Method method,
		ApplicationContext applicationContext) {

	final String dataId = NacosUtils.readFromEnvironment(listener.dataId(),
			environment);
	final String groupId = NacosUtils.readFromEnvironment(listener.groupId(),
			environment);
	final String type = StringUtils.isEmpty(NacosUtils.readTypeFromDataId(dataId))
			? listener.type().getType()
			: NacosUtils.readTypeFromDataId(dataId);
	long timeout = listener.timeout();

	Assert.isTrue(StringUtils.hasText(dataId), "dataId must have content");
	Assert.isTrue(StringUtils.hasText(groupId), "groupId must have content");
	Assert.isTrue(timeout > 0, "timeout must be greater than zero");

	ConfigService configService = configServiceBeanBuilder
			.build(listener.properties());

	try {
		configService.addListener(dataId, groupId,
				new TimeoutNacosConfigListener(dataId, groupId, timeout) {

					@Override
					protected void onReceived(String config) {
						Class<?> targetType = method.getParameterTypes()[0];
						NacosConfigConverter configConverter = determineNacosConfigConverter(
								targetType, listener, type);
						Object parameterValue = configConverter.convert(config);
						// Execute target method
						ReflectionUtils.invokeMethod(method, bean, parameterValue);
					}
				});
	}
	catch (NacosException e) {
		logger.error("ConfigService can't add Listener for dataId : " + dataId
				+ " , groupId : " + groupId, e);
	}

	publishMetadataEvent(beanName, bean, beanClass, dataId, groupId, listener,
			method);

}
 
Example 2
Source File: NacosConfigurationPropertiesBinder.java    From nacos-spring-project with Apache License 2.0 4 votes vote down vote up
protected void bind(final Object bean, final String beanName,
		final NacosConfigurationProperties properties) {

	Assert.notNull(bean, "Bean must not be null!");

	Assert.notNull(properties, "NacosConfigurationProperties must not be null!");

	// support read data-id and group-id from spring environment
	final String dataId = NacosUtils.readFromEnvironment(properties.dataId(),
			environment);
	final String groupId = NacosUtils.readFromEnvironment(properties.groupId(),
			environment);
	String fileType = NacosUtils.readTypeFromDataId(dataId);
	final String type = StringUtils.isEmpty(fileType)
			? (properties.yaml() ? ConfigType.YAML.getType()
					: properties.type().getType())
			: fileType;

	final ConfigService configService = configServiceBeanBuilder
			.build(properties.properties());

	// Add a Listener if auto-refreshed
	if (properties.autoRefreshed()) {

		Listener listener = new AbstractListener() {
			@Override
			public void receiveConfigInfo(String config) {
				doBind(bean, beanName, dataId, groupId, type, properties, config,
						configService);
			}
		};
		try {//
			if (configService instanceof EventPublishingConfigService) {
				((EventPublishingConfigService) configService).addListener(dataId,
						groupId, type, listener);
			}
			else {
				configService.addListener(dataId, groupId, listener);
			}
		}
		catch (NacosException e) {
			if (logger.isErrorEnabled()) {
				logger.error(e.getMessage(), e);
			}
		}
	}

	String content = getContent(configService, dataId, groupId);

	if (hasText(content)) {
		doBind(bean, beanName, dataId, groupId, type, properties, content,
				configService);
	}
}
 
Example 3
Source File: AbstractNacosPropertySourceBuilder.java    From nacos-spring-project with Apache License 2.0 4 votes vote down vote up
protected NacosPropertySource doBuild(String beanName, T beanDefinition,
		Map<String, Object> runtimeAttributes) {

	// Get annotation metadata
	String name = (String) runtimeAttributes.get(NAME_ATTRIBUTE_NAME);
	String dataId = (String) runtimeAttributes.get(DATA_ID_ATTRIBUTE_NAME);
	String groupId = (String) runtimeAttributes.get(GROUP_ID_ATTRIBUTE_NAME);
	String type = ((ConfigType) runtimeAttributes.get(CONFIG_TYPE_ATTRIBUTE_NAME))
			.getType();

	dataId = NacosUtils.readFromEnvironment(dataId, environment);
	groupId = NacosUtils.readFromEnvironment(groupId, environment);
	type = StringUtils.isEmpty(NacosUtils.readTypeFromDataId(dataId)) ? type
			: NacosUtils.readTypeFromDataId(dataId);
	Map<String, Object> nacosPropertiesAttributes = (Map<String, Object>) runtimeAttributes
			.get(PROPERTIES_ATTRIBUTE_NAME);

	Properties nacosProperties = resolveProperties(nacosPropertiesAttributes,
			environment, globalNacosProperties);

	String nacosConfig = nacosConfigLoader.load(dataId, groupId, nacosProperties);

	if (!StringUtils.hasText(nacosConfig)) {
		if (logger.isWarnEnabled()) {
			logger.warn(format(
					"There is no content for NacosPropertySource from dataId[%s] , groupId[%s] , properties[%s].",
					dataId, groupId, nacosPropertiesAttributes));
		}
	}

	if (!StringUtils.hasText(name)) {
		name = buildDefaultPropertySourceName(dataId, groupId, nacosProperties);
	}

	NacosPropertySource nacosPropertySource = new NacosPropertySource(dataId, groupId,
			name, nacosConfig, type);

	nacosPropertySource.setBeanName(beanName);

	String beanClassName = beanDefinition.getBeanClassName();
	if (StringUtils.hasText(beanClassName)) {
		nacosPropertySource.setBeanType(resolveClassName(beanClassName, classLoader));
	}
	nacosPropertySource.setGroupId(groupId);
	nacosPropertySource.setDataId(dataId);
	nacosPropertySource.setProperties(nacosProperties);

	initNacosPropertySource(nacosPropertySource, beanDefinition, runtimeAttributes);

	return nacosPropertySource;

}