Java Code Examples for org.springframework.expression.common.LiteralExpression

The following examples show how to use org.springframework.expression.common.LiteralExpression. These examples are extracted from open source projects. 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 Project: spring-analysis-note   Source File: LiteralExpressionTests.java    License: 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 Project: java-technology-stack   Source File: LiteralExpressionTests.java    License: 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 3
@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 Project: spring-analysis-note   Source File: LiteralExpressionTests.java    License: 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 Project: java-technology-stack   Source File: LiteralExpressionTests.java    License: 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 6
Source Project: eclair   Source File: ExpressionEvaluator.java    License: 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 7
@Override
protected ExtendedProducerProperties<KinesisProducerProperties> createProducerProperties() {
	ExtendedProducerProperties<KinesisProducerProperties> producerProperties = new ExtendedProducerProperties<>(
			new KinesisProducerProperties());
	producerProperties.setPartitionKeyExpression(new LiteralExpression("1"));
	producerProperties.getExtension().setSync(true);
	return producerProperties;
}
 
Example 8
@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 9
Source Project: spring-cloud-gcp   Source File: SqlSpannerQuery.java    License: 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 10
Source Project: spring-cloud-gcp   Source File: SqlSpannerQuery.java    License: 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 11
@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 12
@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 13
@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 14
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 15
@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 16
Source Project: heroku-metrics-spring   Source File: HerokuReplayApplication.java    License: 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 17
Source Project: spring-kafka   Source File: ProducingChannelConfig.java    License: 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 18
@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 19
Source Project: spring-cloud-gcp   Source File: PubSubMessageHandler.java    License: 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 20
Source Project: spring-cloud-gcp   Source File: PubSubMessageHandler.java    License: 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
private Expression detectExpression(String collectionName) {
  Expression expression = EXPRESSION_PARSER.parseExpression(collectionName, ParserContext.TEMPLATE_EXPRESSION);
  return expression instanceof LiteralExpression ? null : expression;
}
 
Example 22
private Expression detectExpression(String collectionName) {
  Expression expression = EXPRESSION_PARSER.parseExpression(collectionName, ParserContext.TEMPLATE_EXPRESSION);
  return expression instanceof LiteralExpression ? null : expression;
}
 
Example 23
public Expression getComputedNameExpression() {
	return (nameExpression != null ? nameExpression : new LiteralExpression(getName()));
}
 
Example 24
public Expression getComputedNameExpression() {
	return (nameExpression != null ? nameExpression : new LiteralExpression(getName()));
}
 
Example 25
public Expression getComputedNameExpression() {
	return (nameExpression != null ? nameExpression : new LiteralExpression(getName()));
}
 
Example 26
public Expression getUrlExpression() {
	return urlExpression != null ? urlExpression
			: new LiteralExpression(this.url);
}
 
Example 27
public Expression keyExpression() {
	return key != null ? new LiteralExpression(key) : keyExpression;
}
 
Example 28
public Expression queueExpression() {
	return queue != null ? new LiteralExpression(queue) : queueExpression;
}
 
Example 29
public Expression topicExpression() {
	return topic != null ? new LiteralExpression(topic) : topicExpression;
}
 
Example 30
Source Project: spring-vault   Source File: BasicVaultPersistentEntity.java    License: 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 can be {@literal null}
 * @return
 */
@Nullable
private static Expression detectExpression(String potentialExpression) {

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