Remove twitter SpEL converter in favor of config-common

This commit is contained in:
Christian Tzolov
2020-06-30 00:40:11 +02:00
parent cbeabd371e
commit 321c753c93
3 changed files with 4 additions and 88 deletions

View File

@@ -38,9 +38,11 @@
<artifactId>spring-integration-ip</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-integration</artifactId>
<groupId>org.springframework.cloud.fn</groupId>
<artifactId>config-common</artifactId>
<version>${spring-cloud-fn.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>

View File

@@ -1,61 +0,0 @@
/*
* Copyright 2020-2020 the original author or authors.
*
* Licensed 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
*
* https://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 org.springframework.cloud.fn.common.twitter;
import java.util.function.Function;
import org.springframework.expression.EvaluationContext;
import org.springframework.expression.Expression;
import org.springframework.expression.ParseException;
import org.springframework.expression.spel.standard.SpelExpression;
import org.springframework.expression.spel.standard.SpelExpressionParser;
/**
* Converter from String to Spring Expression.
* <p>
* TODO: This could be a top level project.
*/
public class StringToSpelConversionFunction implements Function<String, Expression> {
private final SpelExpressionParser parser;
private final EvaluationContext evaluationContext;
public StringToSpelConversionFunction(EvaluationContext evaluationContext) {
this(new SpelExpressionParser(), evaluationContext);
}
public StringToSpelConversionFunction(SpelExpressionParser parser, EvaluationContext evaluationContext) {
this.evaluationContext = evaluationContext;
this.parser = parser;
}
@Override
public Expression apply(String source) {
try {
Expression expression = parser.parseExpression(source);
if (expression instanceof SpelExpression) {
((SpelExpression) expression).setEvaluationContext(evaluationContext);
}
return expression;
}
catch (ParseException e) {
throw new IllegalArgumentException(String.format(
"Could not convert '%s' into a SpEL expression", source), e);
}
}
}

View File

@@ -31,16 +31,9 @@ import twitter4j.TwitterStream;
import twitter4j.TwitterStreamFactory;
import twitter4j.conf.ConfigurationBuilder;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationPropertiesBinding;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;
import org.springframework.core.convert.converter.Converter;
import org.springframework.expression.EvaluationContext;
import org.springframework.expression.Expression;
import org.springframework.integration.context.IntegrationContextUtils;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageHeaders;
import org.springframework.messaging.support.MessageBuilder;
@@ -131,22 +124,4 @@ public class TwitterConnectionConfiguration {
Function<Object, Object> rawJsonExtractor, Function<Object, Message<byte[]>> json) {
return list -> (properties.isRawJson()) ? rawJsonExtractor.andThen(json).apply(list) : json.apply(list);
}
@Bean
public Function<String, Expression> stringToSpelFunction(
@Qualifier(IntegrationContextUtils.INTEGRATION_EVALUATION_CONTEXT_BEAN_NAME)
@Lazy EvaluationContext evaluationContext) {
return new StringToSpelConversionFunction(evaluationContext);
}
@Bean
@ConfigurationPropertiesBinding
public Converter<String, Expression> propertiesSpelConverter(Function<String, Expression> stringToSpelFunction) {
return new Converter<String, Expression>() { // NOTE Using lambda causes Java Generics issues.
@Override
public Expression convert(String source) {
return stringToSpelFunction.apply(source);
}
};
}
}