org.springframework.expression.common.LiteralExpression Java Examples

The following examples show how to use org.springframework.expression.common.LiteralExpression. 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: LiteralExpressionTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testGetValue() throws Exception {
	LiteralExpression lEx = new LiteralExpression("somevalue");
	checkString("somevalue", lEx.getValue());
	checkString("somevalue", lEx.getValue(String.class));
	EvaluationContext ctx = new StandardEvaluationContext();
	checkString("somevalue", lEx.getValue(ctx));
	checkString("somevalue", lEx.getValue(ctx, String.class));
	checkString("somevalue", lEx.getValue(new Rooty()));
	checkString("somevalue", lEx.getValue(new Rooty(), String.class));
	checkString("somevalue", lEx.getValue(ctx, new Rooty()));
	checkString("somevalue", lEx.getValue(ctx, new Rooty(),String.class));
	assertEquals("somevalue", lEx.getExpressionString());
	assertFalse(lEx.isWritable(new StandardEvaluationContext()));
	assertFalse(lEx.isWritable(new Rooty()));
	assertFalse(lEx.isWritable(new StandardEvaluationContext(), new Rooty()));
}
 
Example #2
Source File: LiteralExpressionTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetValue() throws Exception {
	LiteralExpression lEx = new LiteralExpression("somevalue");
	checkString("somevalue", lEx.getValue());
	checkString("somevalue", lEx.getValue(String.class));
	EvaluationContext ctx = new StandardEvaluationContext();
	checkString("somevalue", lEx.getValue(ctx));
	checkString("somevalue", lEx.getValue(ctx, String.class));
	checkString("somevalue", lEx.getValue(new Rooty()));
	checkString("somevalue", lEx.getValue(new Rooty(), String.class));
	checkString("somevalue", lEx.getValue(ctx, new Rooty()));
	checkString("somevalue", lEx.getValue(ctx, new Rooty(),String.class));
	assertEquals("somevalue", lEx.getExpressionString());
	assertFalse(lEx.isWritable(new StandardEvaluationContext()));
	assertFalse(lEx.isWritable(new Rooty()));
	assertFalse(lEx.isWritable(new StandardEvaluationContext(), new Rooty()));
}
 
Example #3
Source File: LiteralExpressionTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testGetValue() throws Exception {
	LiteralExpression lEx = new LiteralExpression("somevalue");
	checkString("somevalue", lEx.getValue());
	checkString("somevalue", lEx.getValue(String.class));
	EvaluationContext ctx = new StandardEvaluationContext();
	checkString("somevalue", lEx.getValue(ctx));
	checkString("somevalue", lEx.getValue(ctx, String.class));
	checkString("somevalue", lEx.getValue(new Rooty()));
	checkString("somevalue", lEx.getValue(new Rooty(), String.class));
	checkString("somevalue", lEx.getValue(ctx, new Rooty()));
	checkString("somevalue", lEx.getValue(ctx, new Rooty(),String.class));
	assertEquals("somevalue", lEx.getExpressionString());
	assertFalse(lEx.isWritable(new StandardEvaluationContext()));
	assertFalse(lEx.isWritable(new Rooty()));
	assertFalse(lEx.isWritable(new StandardEvaluationContext(), new Rooty()));
}
 
Example #4
Source File: LiteralExpressionTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void testGetValueType() throws Exception {
	LiteralExpression lEx = new LiteralExpression("somevalue");
	assertEquals(String.class, lEx.getValueType());
	assertEquals(String.class, lEx.getValueType(new StandardEvaluationContext()));
	assertEquals(String.class, lEx.getValueType(new Rooty()));
	assertEquals(String.class, lEx.getValueType(new StandardEvaluationContext(), new Rooty()));
	assertEquals(String.class, lEx.getValueTypeDescriptor().getType());
	assertEquals(String.class, lEx.getValueTypeDescriptor(new StandardEvaluationContext()).getType());
	assertEquals(String.class, lEx.getValueTypeDescriptor(new Rooty()).getType());
	assertEquals(String.class, lEx.getValueTypeDescriptor(new StandardEvaluationContext(), new Rooty()).getType());
}
 
Example #5
Source File: ProducingChannelConfig.java    From spring-kafka with MIT License 5 votes vote down vote up
@Bean
@ServiceActivator(inputChannel = "producingChannel")
public MessageHandler kafkaMessageHandler() {
  KafkaProducerMessageHandler<String, String> handler =
      new KafkaProducerMessageHandler<>(kafkaTemplate());
  handler.setMessageKeyExpression(new LiteralExpression("kafka-integration"));

  return handler;
}
 
Example #6
Source File: HerokuReplayApplication.java    From heroku-metrics-spring with MIT License 5 votes vote down vote up
@ServiceActivator(inputChannel = "toKafka")
@Bean
public MessageHandler kafkaHandler() throws Exception {
  KafkaProducerMessageHandler<String, String> handler =
      new KafkaProducerMessageHandler<>(kafkaTemplate());
  handler.setTopicExpression(new LiteralExpression(KafkaConfig.getTopic()));
  handler.setMessageKeyExpression(new LiteralExpression(KafkaConfig.getMessageKey()));
  return handler;
}
 
Example #7
Source File: KafkaBinderTests.java    From spring-cloud-stream-binder-kafka with Apache License 2.0 5 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void testCustomPartitionCountOverridesDefaultIfLarger() throws Exception {
	byte[] testPayload = new byte[2048];
	Arrays.fill(testPayload, (byte) 65);
	KafkaBinderConfigurationProperties binderConfiguration = createConfigurationProperties();
	binderConfiguration.setMinPartitionCount(10);
	Binder binder = getBinder(binderConfiguration);
	QueueChannel moduleInputChannel = new QueueChannel();
	ExtendedProducerProperties<KafkaProducerProperties> producerProperties = createProducerProperties();
	producerProperties.setPartitionCount(10);
	producerProperties.setPartitionKeyExpression(new LiteralExpression("foo"));

	DirectChannel moduleOutputChannel = createBindableChannel("output",
			createProducerBindingProperties(producerProperties));

	ExtendedConsumerProperties<KafkaConsumerProperties> consumerProperties = createConsumerProperties();
	long uniqueBindingId = System.currentTimeMillis();
	Binding<MessageChannel> producerBinding = binder.bindProducer(
			"foo" + uniqueBindingId + ".0", moduleOutputChannel, producerProperties);
	Binding<MessageChannel> consumerBinding = binder.bindConsumer(
			"foo" + uniqueBindingId + ".0", null, moduleInputChannel,
			consumerProperties);
	Message<?> message = org.springframework.integration.support.MessageBuilder
			.withPayload(testPayload).build();
	// Let the consumer actually bind to the producer before sending a msg
	binderBindUnbindLatency();
	moduleOutputChannel.send(message);
	Message<?> inbound = receive(moduleInputChannel);
	assertThat(inbound).isNotNull();
	assertThat((byte[]) inbound.getPayload()).containsExactly(testPayload);

	assertThat(partitionSize("foo" + uniqueBindingId + ".0")).isEqualTo(10);
	producerBinding.unbind();
	consumerBinding.unbind();
}
 
Example #8
Source File: KafkaMessageChannelBinder.java    From spring-cloud-stream-binder-kafka with Apache License 2.0 5 votes vote down vote up
ProducerConfigurationMessageHandler(KafkaTemplate<byte[], byte[]> kafkaTemplate,
		String topic,
		ExtendedProducerProperties<KafkaProducerProperties> producerProperties,
		ProducerFactory<byte[], byte[]> producerFactory) {

	super(kafkaTemplate);
	if (producerProperties.getExtension().isUseTopicHeader()) {
		setTopicExpression(PARSER.parseExpression("headers['" + KafkaHeaders.TOPIC + "'] ?: '" + topic + "'"));
	}
	else {
		setTopicExpression(new LiteralExpression(topic));
	}
	Expression messageKeyExpression = producerProperties.getExtension().getMessageKeyExpression();
	if (expressionInterceptorNeeded(producerProperties)) {
		messageKeyExpression = PARSER.parseExpression("headers['"
				+ KafkaExpressionEvaluatingInterceptor.MESSAGE_KEY_HEADER
				+ "']");
	}
	setMessageKeyExpression(messageKeyExpression);
	setBeanFactory(KafkaMessageChannelBinder.this.getBeanFactory());
	if (producerProperties.isPartitioned()) {
		setPartitionIdExpression(PARSER.parseExpression(
				"headers['" + BinderHeaders.PARTITION_HEADER + "']"));
	}
	if (producerProperties.getExtension().isSync()) {
		setSync(true);
	}
	if (producerProperties.getExtension().getSendTimeoutExpression() != null) {
		setSendTimeoutExpression(producerProperties.getExtension().getSendTimeoutExpression());
	}
	this.producerFactory = producerFactory;
}
 
Example #9
Source File: MongodbSourceConfiguration.java    From spring-cloud-stream-app-starters with Apache License 2.0 5 votes vote down vote up
@Bean
protected MessageSource<Object> mongoSource() {
    MongoDbMessageSource ms = new MongoDbMessageSource(mongoTemplate, new LiteralExpression(config.getQuery()));
    ms.setCollectionNameExpression(new LiteralExpression(config.getCollection()));
    ms.setEntityClass(String.class);
    return ms;
}
 
Example #10
Source File: LiteralExpressionTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetValueType() throws Exception {
	LiteralExpression lEx = new LiteralExpression("somevalue");
	assertEquals(String.class, lEx.getValueType());
	assertEquals(String.class, lEx.getValueType(new StandardEvaluationContext()));
	assertEquals(String.class, lEx.getValueType(new Rooty()));
	assertEquals(String.class, lEx.getValueType(new StandardEvaluationContext(), new Rooty()));
	assertEquals(String.class, lEx.getValueTypeDescriptor().getType());
	assertEquals(String.class, lEx.getValueTypeDescriptor(new StandardEvaluationContext()).getType());
	assertEquals(String.class, lEx.getValueTypeDescriptor(new Rooty()).getType());
	assertEquals(String.class, lEx.getValueTypeDescriptor(new StandardEvaluationContext(), new Rooty()).getType());
}
 
Example #11
Source File: PubSubMessageHandlerTests.java    From spring-cloud-gcp with Apache License 2.0 5 votes vote down vote up
@Test
public void testTopic() {
	String topic = "pubsub";
	this.adapter.setTopic(topic);

	Expression exp = this.adapter.getTopicExpression();

	assertThat(exp.getClass()).isEqualTo(LiteralExpression.class);
	assertThat(exp.getValue()).isEqualTo(topic);
}
 
Example #12
Source File: SpannerPersistentEntityImpl.java    From spring-cloud-gcp with Apache License 2.0 5 votes vote down vote up
@Nullable
private Expression detectExpression() {
	if (!hasAnnotatedTableName()) {
		return null;
	}

	Expression expression = PARSER.parseExpression(this.table.name(),
			ParserContext.TEMPLATE_EXPRESSION);

	return (expression instanceof LiteralExpression) ? null : expression;
}
 
Example #13
Source File: SqlSpannerQuery.java    From spring-cloud-gcp with Apache License 2.0 5 votes vote down vote up
private Expression[] detectExpressions(String sql) {
	Expression expression = this.expressionParser.parseExpression(sql,
			ParserContext.TEMPLATE_EXPRESSION);
	if (expression instanceof LiteralExpression) {
		return new Expression[] { expression };
	}
	else if (expression instanceof CompositeStringExpression) {
		return ((CompositeStringExpression) expression).getExpressions();
	}
	else {
		throw new SpannerDataException("Unexpected expression type. "
				+ "Query can either contain no SpEL expressions or have SpEL expressions in the SQL.");
	}
}
 
Example #14
Source File: SqlSpannerQuery.java    From spring-cloud-gcp with Apache License 2.0 5 votes vote down vote up
private void resolveSpELTags(QueryTagValue queryTagValue) {
	Expression[] expressions = detectExpressions(queryTagValue.sql);
	StringBuilder sb = new StringBuilder();
	Map<Object, String> valueToTag = new HashMap<>();
	int tagNum = 0;
	EvaluationContext evaluationContext = this.evaluationContextProvider
			.getEvaluationContext(this.queryMethod.getParameters(),
					queryTagValue.rawParams);
	for (Expression expression : expressions) {
		if (expression instanceof LiteralExpression) {
			sb.append(expression.getValue(String.class));
		}
		else if (expression instanceof SpelExpression) {
			Object value = expression.getValue(evaluationContext);
			if (valueToTag.containsKey(value)) {
				sb.append("@").append(valueToTag.get(value));
			}
			else {
				String newTag;
				do {
					tagNum++;
					newTag = "SpELtag" + tagNum;
				}
				while (queryTagValue.initialTags.contains(newTag));
				valueToTag.put(value, newTag);
				queryTagValue.params.add(value);
				queryTagValue.tags.add(newTag);
				sb.append("@").append(newTag);
			}
		}
		else {
			throw new SpannerDataException(
					"Unexpected expression type. SQL queries are expected to be "
							+ "concatenation of Literal and SpEL expressions.");
		}
	}
	queryTagValue.sql = sb.toString();
}
 
Example #15
Source File: DatastorePersistentEntityImpl.java    From spring-cloud-gcp with Apache License 2.0 5 votes vote down vote up
@Nullable
private Expression detectExpression() {
	if (!hasTableName()) {
		return null;
	}

	Expression expression = PARSER.parseExpression(this.kind.name(),
			ParserContext.TEMPLATE_EXPRESSION);

	return (expression instanceof LiteralExpression) ? null : expression;
}
 
Example #16
Source File: KinesisBinderTests.java    From spring-cloud-stream-binder-aws-kinesis with Apache License 2.0 5 votes vote down vote up
@Override
protected ExtendedProducerProperties<KinesisProducerProperties> createProducerProperties() {
	ExtendedProducerProperties<KinesisProducerProperties> producerProperties = new ExtendedProducerProperties<>(
			new KinesisProducerProperties());
	producerProperties.setPartitionKeyExpression(new LiteralExpression("1"));
	producerProperties.getExtension().setSync(true);
	return producerProperties;
}
 
Example #17
Source File: ExpressionEvaluator.java    From eclair with Apache License 2.0 5 votes vote down vote up
private Expression parse(String expressionString) {
    try {
        return expressionParser.parseExpression(expressionString, parserContext);
    } catch (ParseException e) {
        return new LiteralExpression(expressionString);
    }
}
 
Example #18
Source File: LiteralExpressionTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void testGetValueType() throws Exception {
	LiteralExpression lEx = new LiteralExpression("somevalue");
	assertEquals(String.class, lEx.getValueType());
	assertEquals(String.class, lEx.getValueType(new StandardEvaluationContext()));
	assertEquals(String.class, lEx.getValueType(new Rooty()));
	assertEquals(String.class, lEx.getValueType(new StandardEvaluationContext(), new Rooty()));
	assertEquals(String.class, lEx.getValueTypeDescriptor().getType());
	assertEquals(String.class, lEx.getValueTypeDescriptor(new StandardEvaluationContext()).getType());
	assertEquals(String.class, lEx.getValueTypeDescriptor(new Rooty()).getType());
	assertEquals(String.class, lEx.getValueTypeDescriptor(new StandardEvaluationContext(), new Rooty()).getType());
}
 
Example #19
Source File: RedisSinkProperties.java    From spring-cloud-stream-app-starters with Apache License 2.0 4 votes vote down vote up
public Expression topicExpression() {
	return topic != null ? new LiteralExpression(topic) : topicExpression;
}
 
Example #20
Source File: PubSubMessageHandler.java    From spring-cloud-gcp with Apache License 2.0 4 votes vote down vote up
/**
 * Set the topic where this adapter sends messages to.
 * @param topic topic name
 */
public void setTopic(String topic) {
	Assert.hasText(topic, "The topic can't be null or empty");
	this.topicExpression = new LiteralExpression(topic);
}
 
Example #21
Source File: RedisSinkProperties.java    From spring-cloud-stream-app-starters with Apache License 2.0 4 votes vote down vote up
public Expression queueExpression() {
	return queue != null ? new LiteralExpression(queue) : queueExpression;
}
 
Example #22
Source File: RedisSinkProperties.java    From spring-cloud-stream-app-starters with Apache License 2.0 4 votes vote down vote up
public Expression keyExpression() {
	return key != null ? new LiteralExpression(key) : keyExpression;
}
 
Example #23
Source File: HttpclientProcessorProperties.java    From spring-cloud-stream-app-starters with Apache License 2.0 4 votes vote down vote up
public Expression getUrlExpression() {
	return urlExpression != null ? urlExpression
			: new LiteralExpression(this.url);
}
 
Example #24
Source File: CounterProperties.java    From spring-cloud-stream-app-starters with Apache License 2.0 4 votes vote down vote up
public Expression getComputedNameExpression() {
	return (nameExpression != null ? nameExpression : new LiteralExpression(getName()));
}
 
Example #25
Source File: AggregateCounterSinkProperties.java    From spring-cloud-stream-app-starters with Apache License 2.0 4 votes vote down vote up
public Expression getComputedNameExpression() {
	return (nameExpression != null ? nameExpression : new LiteralExpression(getName()));
}
 
Example #26
Source File: FieldValueCounterSinkProperties.java    From spring-cloud-stream-app-starters with Apache License 2.0 4 votes vote down vote up
public Expression getComputedNameExpression() {
	return (nameExpression != null ? nameExpression : new LiteralExpression(getName()));
}
 
Example #27
Source File: AggregateQueryProvider.java    From mongodb-aggregate-query-support with Apache License 2.0 4 votes vote down vote up
private Expression detectExpression(String collectionName) {
  Expression expression = EXPRESSION_PARSER.parseExpression(collectionName, ParserContext.TEMPLATE_EXPRESSION);
  return expression instanceof LiteralExpression ? null : expression;
}
 
Example #28
Source File: NonReactiveAggregateQueryProvider.java    From mongodb-aggregate-query-support with Apache License 2.0 4 votes vote down vote up
private Expression detectExpression(String collectionName) {
  Expression expression = EXPRESSION_PARSER.parseExpression(collectionName, ParserContext.TEMPLATE_EXPRESSION);
  return expression instanceof LiteralExpression ? null : expression;
}
 
Example #29
Source File: PubSubMessageHandler.java    From spring-cloud-gcp with Apache License 2.0 4 votes vote down vote up
public PubSubMessageHandler(PubSubPublisherOperations pubSubPublisherOperations, String topic) {
	Assert.notNull(pubSubPublisherOperations, "Pub/Sub publisher template can't be null.");
	Assert.hasText(topic, "Pub/Sub topic can't be null or empty.");
	this.pubSubPublisherOperations = pubSubPublisherOperations;
	this.topicExpression = new LiteralExpression(topic);
}
 
Example #30
Source File: BasicKeyValuePersistentEntity.java    From spring-data-keyvalue with Apache License 2.0 3 votes vote down vote up
/**
 * Returns a SpEL {@link Expression} if the given {@link String} is actually an expression that does not evaluate to a
 * {@link LiteralExpression} (indicating that no subsequent evaluation is necessary).
 *
 * @param potentialExpression must not be {@literal null}
 * @return the parsed {@link Expression} or {@literal null}.
 */
@Nullable
private static Expression detectExpression(String potentialExpression) {

	Expression expression = PARSER.parseExpression(potentialExpression, ParserContext.TEMPLATE_EXPRESSION);
	return expression instanceof LiteralExpression ? null : expression;
}