Apply Some Java 8 Code Changes

* Make most functional interfaces as `@FunctionalInterface`
* Convert some abstract classes to `@FunctionalInterface` with `default` methods
* Apply Lambda style implementation in some places
* Remove `Function` in favor of similar in Java 8

*  Remove redundant code from `DefaultAmqpHeaderMapper` since we are already on Spring AMQP-2.0
* Add several ctors to the `ExpressionEvaluatingMessageListProcessor`
* Populate explicit `Boolean.class` `expectedType` from the `ExpressionEvaluatingReleaseStrategy`
This commit is contained in:
Artem Bilan
2016-08-26 14:52:23 -04:00
committed by Gary Russell
parent 89e0d134f5
commit 370be4853d
58 changed files with 208 additions and 270 deletions

View File

@@ -16,13 +16,11 @@
package org.springframework.integration.amqp.support;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;
import org.springframework.amqp.core.MessageDeliveryMode;
import org.springframework.amqp.core.MessageProperties;
@@ -31,9 +29,6 @@ import org.springframework.integration.IntegrationMessageHeaderAccessor;
import org.springframework.integration.mapping.AbstractHeaderMapper;
import org.springframework.integration.mapping.support.JsonHeaders;
import org.springframework.util.MimeType;
import org.springframework.util.ReflectionUtils;
import org.springframework.util.ReflectionUtils.FieldCallback;
import org.springframework.util.ReflectionUtils.FieldFilter;
import org.springframework.util.StringUtils;
/**
@@ -59,8 +54,6 @@ import org.springframework.util.StringUtils;
*/
public class DefaultAmqpHeaderMapper extends AbstractHeaderMapper<MessageProperties> implements AmqpHeaderMapper {
static final boolean CONSUMER_METADATA_PRESENT;
private static final List<String> STANDARD_HEADER_NAMES = new ArrayList<String>();
static {
@@ -90,27 +83,6 @@ public class DefaultAmqpHeaderMapper extends AbstractHeaderMapper<MessagePropert
STANDARD_HEADER_NAMES.add(JsonHeaders.KEY_TYPE_ID);
STANDARD_HEADER_NAMES.add(AmqpHeaders.SPRING_REPLY_CORRELATION);
STANDARD_HEADER_NAMES.add(AmqpHeaders.SPRING_REPLY_TO_STACK);
final AtomicBoolean consumerTagHeader = new AtomicBoolean();
try {
ReflectionUtils.doWithFields(AmqpHeaders.class, new FieldCallback() {
@Override
public void doWith(Field field) throws IllegalArgumentException, IllegalAccessException {
consumerTagHeader.set(true);
}
},
new FieldFilter() {
@Override
public boolean matches(Field field) {
return field.getName().equals("CONSUMER_TAG") && field.getType().equals(String.class);
}
});
}
catch (Exception e) {
}
CONSUMER_METADATA_PRESENT = consumerTagHeader.get();
}
protected DefaultAmqpHeaderMapper(String[] requestHeaderNames, String[] replyHeaderNames) {
@@ -395,9 +367,7 @@ public class DefaultAmqpHeaderMapper extends AbstractHeaderMapper<MessagePropert
@Override
public Map<String, Object> toHeadersFromRequest(MessageProperties source) {
Map<String, Object> headersFromRequest = super.toHeadersFromRequest(source);
if (CONSUMER_METADATA_PRESENT) {
addConsumerMetadata(source, headersFromRequest);
}
addConsumerMetadata(source, headersFromRequest);
return headersFromRequest;
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-2016 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.
@@ -25,6 +25,7 @@ import org.springframework.messaging.Message;
* @author Marius Bogoevici
* @author Iwein Fuld
*/
@FunctionalInterface
public interface CorrelationStrategy {
/**

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-2016 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.
@@ -22,29 +22,41 @@ import org.springframework.expression.Expression;
import org.springframework.expression.ParseException;
import org.springframework.integration.util.AbstractExpressionEvaluator;
import org.springframework.messaging.Message;
import org.springframework.util.Assert;
/**
* A base class for aggregators that evaluates a SpEL expression with the message list as the root object within the
* evaluation context.
*
* @author Dave Syer
* @author Artem Bilan
* @since 2.0
*/
public class ExpressionEvaluatingMessageListProcessor extends AbstractExpressionEvaluator implements MessageListProcessor {
public class ExpressionEvaluatingMessageListProcessor extends AbstractExpressionEvaluator
implements MessageListProcessor {
private final Expression expression;
private volatile Class<?> expectedType = null;
/**
* Set the result type expected from evaluation of the expression.
*
* @param expectedType The expected type.
* Construct {@link ExpressionEvaluatingMessageListProcessor} for the provided
* SpEL expression and expected result type.
* @param expression a SpEL expression to evaluate in {@link #process(Collection)}.
* @param expectedType an expected result type.
* @since 5.0
*/
public void setExpectedType(Class<?> expectedType) {
public ExpressionEvaluatingMessageListProcessor(String expression, Class<?> expectedType) {
this(expression);
this.expectedType = expectedType;
}
/**
* Construct {@link ExpressionEvaluatingMessageListProcessor} for the provided
* SpEL expression and expected result type.
* @param expression a SpEL expression to evaluate in {@link #process(Collection)}.
* @since 5.0
*/
public ExpressionEvaluatingMessageListProcessor(String expression) {
try {
this.expression = EXPRESSION_PARSER.parseExpression(expression);
@@ -54,6 +66,36 @@ public class ExpressionEvaluatingMessageListProcessor extends AbstractExpression
}
}
/**
* Construct {@link ExpressionEvaluatingMessageListProcessor} for the provided
* expression and expected result type.
* @param expression an expression to evaluate in {@link #process(Collection)}.
* @param expectedType an expected result type.
* @since 5.0
*/
public ExpressionEvaluatingMessageListProcessor(Expression expression, Class<?> expectedType) {
this(expression);
this.expectedType = expectedType;
}
/**
* Construct {@link ExpressionEvaluatingMessageListProcessor} for the provided expression.
* @param expression an expression to evaluate in {@link #process(Collection)}.
* @since 5.0
*/
public ExpressionEvaluatingMessageListProcessor(Expression expression) {
Assert.notNull(expression, "'expression' must not be null.");
this.expression = expression;
}
/**
* Set the result type expected from evaluation of the expression.
* @param expectedType The expected type.
*/
public void setExpectedType(Class<?> expectedType) {
this.expectedType = expectedType;
}
/**
* Processes the Message by evaluating the expression with that Message as the root object. The expression
* evaluation result Object will be returned.

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2011 the original author or authors.
* Copyright 2002-2016 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.
@@ -27,7 +27,7 @@ public class ExpressionEvaluatingReleaseStrategy extends ExpressionEvaluatingMes
ReleaseStrategy {
public ExpressionEvaluatingReleaseStrategy(String expression) {
super(expression);
super(expression, Boolean.class);
}
/**
@@ -35,7 +35,7 @@ public class ExpressionEvaluatingReleaseStrategy extends ExpressionEvaluatingMes
* be boolean).
*/
public boolean canRelease(MessageGroup messages) {
return ((Boolean) process(messages.getMessages())).booleanValue();
return (Boolean) process(messages.getMessages());
}
}

View File

@@ -24,6 +24,7 @@ import org.springframework.integration.store.MessageGroup;
* @author Iwein Fuld
* @see org.springframework.integration.aggregator.AbstractCorrelatingMessageHandler
*/
@FunctionalInterface
public interface MessageGroupProcessor {
/**

View File

@@ -24,6 +24,7 @@ import org.springframework.messaging.Message;
* @author Dave Syer
*
*/
@FunctionalInterface
public interface MessageListProcessor {
Object process(Collection<? extends Message<?>> messages);

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2008 the original author or authors.
* Copyright 2002-2016 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.
@@ -27,6 +27,7 @@ import org.springframework.integration.store.MessageGroup;
* @author Mark Fisher
* @author Dave Syer
*/
@FunctionalInterface
public interface ReleaseStrategy {
boolean canRelease(MessageGroup group);

View File

@@ -116,7 +116,7 @@ public abstract class ThreadStatePropagationChannelInterceptor<S>
@Override
public Message<?> decorateMessage(Message<?> message) {
return new MessageWithThreadState<S>(message, this.state);
return new MessageWithThreadState<>(message, this.state);
}
@Override

View File

@@ -28,6 +28,7 @@ import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
* @author Artem Bilan
* @since 4.0
*/
@FunctionalInterface
public interface IntegrationConfigurationInitializer {
void initialize(ConfigurableListableBeanFactory beanFactory) throws BeansException;

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2010 the original author or authors.
* Copyright 2002-2016 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.
@@ -24,6 +24,7 @@ package org.springframework.integration.core;
* @author Artem Bilan
* @since 4.0
*/
@FunctionalInterface
public interface GenericSelector<S> {
boolean accept(S source);

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2010 the original author or authors.
* Copyright 2002-2016 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.
@@ -23,6 +23,7 @@ import org.springframework.messaging.Message;
*
* @author Mark Fisher
*/
@FunctionalInterface
public interface MessageSelector extends GenericSelector<Message<?>> {
boolean accept(Message<?> message);

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-2016 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.
@@ -23,6 +23,7 @@ import org.springframework.messaging.Message;
*
* @author Mark Fisher
*/
@FunctionalInterface
public interface MessageSource<T> {
/**

View File

@@ -29,6 +29,7 @@ import org.springframework.messaging.MessageHandler;
* @author Oleg Zhurakousky
* @since 1.0.3
*/
@FunctionalInterface
public interface LoadBalancingStrategy {
Iterator<MessageHandler> getHandlerIterator(Message<?> message, Collection<MessageHandler> handlers);

View File

@@ -27,6 +27,7 @@ import org.springframework.messaging.support.MessageHandlingRunnable;
* @see UnicastingDispatcher
* @see BroadcastingDispatcher
*/
@FunctionalInterface
public interface MessageHandlingTaskDecorator {
Runnable decorate(MessageHandlingRunnable task);

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2010 the original author or authors.
* Copyright 2002-2016 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.
@@ -26,6 +26,7 @@ import org.springframework.expression.Expression;
* @author Mark Fisher
* @since 2.0
*/
@FunctionalInterface
public interface ExpressionSource {
Expression getExpression(String key, Locale locale);

View File

@@ -26,6 +26,7 @@ import org.springframework.messaging.Message;
* @author Mark Fisher
* @since 2.0
*/
@FunctionalInterface
public interface RequestReplyExchanger {
Message<?> exchange(Message<?> request);

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-2016 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.
@@ -39,6 +39,7 @@ import org.springframework.messaging.Message;
* @author Mark Fisher
* @since 2.0
*/
@FunctionalInterface
public interface MessageProcessor<T> {
/**

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2015 the original author or authors.
* Copyright 2015-2016 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.
@@ -26,6 +26,7 @@ import org.springframework.messaging.Message;
* @since 4.2
*
*/
@FunctionalInterface
public interface MessageTriggerAction {
/**

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2008 the original author or authors.
* Copyright 2002-2016 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.
@@ -23,6 +23,7 @@ import org.springframework.messaging.Message;
*
* @author Mark Fisher
*/
@FunctionalInterface
public interface InboundMessageMapper<T> {
Message<?> toMessage(T object) throws Exception;

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2008 the original author or authors.
* Copyright 2002-2016 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.
@@ -23,6 +23,7 @@ import org.springframework.messaging.Message;
*
* @author Mark Fisher
*/
@FunctionalInterface
public interface OutboundMessageMapper<T> {
T fromMessage(Message<?> message) throws Exception;

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2014 the original author or authors.
* Copyright 2014-2016 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.
@@ -27,6 +27,7 @@ import org.springframework.messaging.Message;
* @since 4.1
* @see org.springframework.integration.handler.AbstractMessageProducingHandler
*/
@FunctionalInterface
public interface RoutingSlipRouteStrategy {
/**

View File

@@ -26,7 +26,6 @@ import java.util.concurrent.atomic.AtomicInteger;
import org.springframework.integration.handler.AbstractReplyProducingMessageHandler;
import org.springframework.integration.support.AbstractIntegrationMessageBuilder;
import org.springframework.integration.util.Function;
import org.springframework.integration.util.FunctionIterator;
import org.springframework.messaging.Message;
@@ -89,7 +88,7 @@ public abstract class AbstractMessageSplitter extends AbstractReplyProducingMess
Map<String, Object> messageHeaders = message.getHeaders();
if (willAddHeaders(message)) {
messageHeaders = new HashMap<String, Object>(messageHeaders);
messageHeaders = new HashMap<>(messageHeaders);
addHeaders(message, messageHeaders);
}
final Map<String, Object> headers = messageHeaders;
@@ -97,15 +96,8 @@ public abstract class AbstractMessageSplitter extends AbstractReplyProducingMess
final AtomicInteger sequenceNumber = new AtomicInteger(1);
return new FunctionIterator<Object, AbstractIntegrationMessageBuilder<?>>(iterator,
new Function<Object, AbstractIntegrationMessageBuilder<?>>() {
@Override
public AbstractIntegrationMessageBuilder<?> apply(Object object) {
return createBuilder(object, headers, correlationId, sequenceNumber.getAndIncrement(),
sequenceSize);
}
});
object ->
createBuilder(object, headers, correlationId, sequenceNumber.getAndIncrement(), sequenceSize));
}
private AbstractIntegrationMessageBuilder<?> createBuilder(Object item, Map<String, Object> headers,

View File

@@ -27,6 +27,7 @@ import org.springframework.messaging.Message;
* @author Artem Bilan
* @since 4.2.9
*/
@FunctionalInterface
public interface MessageDecorator {
Message<?> decorateMessage(Message<?> message);

View File

@@ -25,6 +25,7 @@ import java.util.concurrent.locks.Lock;
* @author Gary Russell
* @since 2.1.1
*/
@FunctionalInterface
public interface LockRegistry {
/**

View File

@@ -23,6 +23,7 @@ package org.springframework.integration.support.management;
* @since 4.2
*
*/
@FunctionalInterface
public interface ConfigurableMetricsAware<M extends ConfigurableMetrics> {
void configureMetrics(M metrics);

View File

@@ -25,6 +25,7 @@ package org.springframework.integration.transformer;
* @author Artem Bilan
* @since 4.0
*/
@FunctionalInterface
public interface GenericTransformer<S, T> {
T transform(S source);

View File

@@ -23,6 +23,7 @@ import org.springframework.messaging.Message;
*
* @author Mark Fisher
*/
@FunctionalInterface
public interface Transformer extends GenericTransformer<Message<?>, Message<?>> {
Message<?> transform(Message<?> message);

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2011 the original author or authors.
* Copyright 2002-2016 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.
@@ -25,6 +25,7 @@ import java.util.Collection;
* @author Mark Fisher
* @since 2.1
*/
@FunctionalInterface
public interface CollectionFilter<T> {
Collection<T> filter(Collection<T> unfilteredElements);

View File

@@ -1,42 +0,0 @@
/*
* Copyright 2015-2016 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
*
* 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 org.springframework.integration.util;
/**
* Implementations of this class perform work on the given parameter
* and return a result of an optionally different type.
*
* <p>This is a copy of Java 8 {@code Function} interface.
*
* @param <T> The type of the input to the apply operation
* @param <R> The type of the result of the apply operation
*
* @author Jon Brisbin
* @author Stephane Maldini
* @since 4.0
*/
public interface Function<T, R> {
/**
* Execute the logic of the action, accepting the given parameter.
* @param t The parameter to pass to the action.
* @return result
*/
R apply(T t);
}

View File

@@ -17,6 +17,7 @@
package org.springframework.integration.util;
import java.util.Iterator;
import java.util.function.Function;
/**
* An {@link Iterator} implementation to convert each item from the target

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2010 the original author or authors.
* Copyright 2002-2016 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.
@@ -23,6 +23,7 @@ import org.springframework.messaging.Message;
*
* @author Mark Fisher
*/
@FunctionalInterface
public interface FileNameGenerator {
String generateFileName(Message<?> message);

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-2016 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.
@@ -27,6 +27,7 @@ import java.util.List;
*
* @since 1.0.0
*/
@FunctionalInterface
public interface FileListFilter<F> {
/**

View File

@@ -29,6 +29,7 @@ import org.springframework.integration.file.remote.session.Session;
* @since 4.1
*
*/
@FunctionalInterface
public interface ClientCallback<C, T> {
/**

View File

@@ -22,15 +22,17 @@ package org.springframework.integration.file.remote;
* access to lower level methods where no result is returned.
*
* @author Gary Russell
* @author Artem Bilan
*
* @param <C> The type of the underlying client object.
* @since 4.1
*
*/
public abstract class ClientCallbackWithoutResult<C> implements ClientCallback<C, Object> {
@FunctionalInterface
public interface ClientCallbackWithoutResult<C> extends ClientCallback<C, Object> {
@Override
public Object doWithClient(C client) {
default Object doWithClient(C client) {
doWithClientWithoutResult(client);
return null;
}
@@ -43,6 +45,6 @@ public abstract class ClientCallbackWithoutResult<C> implements ClientCallback<C
* operations.
* @param client The client.
*/
protected abstract void doWithClientWithoutResult(C client);
void doWithClientWithoutResult(C client);
}

View File

@@ -26,13 +26,13 @@ import java.io.InputStream;
* @since 3.0
*
*/
@FunctionalInterface
public interface InputStreamCallback {
/**
* Called with the InputStream for the remote file. The caller will
* take care of closing the stream and finalizing the file retrieval operation after
* this method exits.
*
* @param stream The InputStream.
* @throws IOException Any IOException.
*/

View File

@@ -54,6 +54,7 @@ import org.springframework.util.StringUtils;
* @author Oleg Zhurakousky
* @author David Turanski
* @author Gary Russell
* @author Artem Bilan
* @since 3.0
*
*/
@@ -139,7 +140,8 @@ public class RemoteFileTemplate<F> implements RemoteFileOperations<F>, Initializ
*/
public void setRemoteDirectoryExpression(Expression remoteDirectoryExpression) {
Assert.notNull(remoteDirectoryExpression, "remoteDirectoryExpression must not be null");
this.directoryExpressionProcessor = new ExpressionEvaluatingMessageProcessor<String>(remoteDirectoryExpression, String.class);
this.directoryExpressionProcessor =
new ExpressionEvaluatingMessageProcessor<>(remoteDirectoryExpression, String.class);
}
/**
@@ -150,7 +152,8 @@ public class RemoteFileTemplate<F> implements RemoteFileOperations<F>, Initializ
*/
public void setTemporaryRemoteDirectoryExpression(Expression temporaryRemoteDirectoryExpression) {
Assert.notNull(temporaryRemoteDirectoryExpression, "temporaryRemoteDirectoryExpression must not be null");
this.temporaryDirectoryExpressionProcessor = new ExpressionEvaluatingMessageProcessor<String>(temporaryRemoteDirectoryExpression, String.class);
this.temporaryDirectoryExpressionProcessor =
new ExpressionEvaluatingMessageProcessor<>(temporaryRemoteDirectoryExpression, String.class);
}
/**
@@ -161,7 +164,7 @@ public class RemoteFileTemplate<F> implements RemoteFileOperations<F>, Initializ
*/
public void setFileNameExpression(Expression fileNameExpression) {
Assert.notNull(fileNameExpression, "fileNameExpression must not be null");
this.fileNameProcessor = new ExpressionEvaluatingMessageProcessor<String>(fileNameExpression, String.class);
this.fileNameProcessor = new ExpressionEvaluatingMessageProcessor<>(fileNameExpression, String.class);
}
/**
@@ -243,10 +246,12 @@ public class RemoteFileTemplate<F> implements RemoteFileOperations<F>, Initializ
}
}
if (this.autoCreateDirectory) {
Assert.hasText(this.remoteFileSeparator, "'remoteFileSeparator' must not be empty when 'autoCreateDirectory' is set to 'true'");
Assert.hasText(this.remoteFileSeparator,
"'remoteFileSeparator' must not be empty when 'autoCreateDirectory' is set to 'true'");
}
if (this.hasExplicitlySetSuffix && !this.useTemporaryFileName) {
this.logger.warn("Since 'use-temporary-file-name' is set to 'false' the value of 'temporary-file-suffix' has no effect");
this.logger.warn("Since 'use-temporary-file-name' is set to 'false' " +
"the value of 'temporary-file-suffix' has no effect");
}
}
@@ -280,46 +285,42 @@ public class RemoteFileTemplate<F> implements RemoteFileOperations<F>, Initializ
final StreamHolder inputStreamHolder = this.payloadToInputStream(message);
if (inputStreamHolder != null) {
try {
return this.execute(new SessionCallback<F, String>() {
@Override
public String doInSession(Session<F> session) throws IOException {
String fileName = inputStreamHolder.getName();
try {
String remoteDirectory = RemoteFileTemplate.this.directoryExpressionProcessor
return this.execute(session -> {
String fileName = inputStreamHolder.getName();
try {
String remoteDirectory = RemoteFileTemplate.this.directoryExpressionProcessor
.processMessage(message);
remoteDirectory = RemoteFileTemplate.this.normalizeDirectoryPath(remoteDirectory);
if (StringUtils.hasText(subDirectory)) {
if (subDirectory.startsWith(RemoteFileTemplate.this.remoteFileSeparator)) {
remoteDirectory += subDirectory.substring(1);
}
else {
remoteDirectory += RemoteFileTemplate.this.normalizeDirectoryPath(subDirectory);
}
}
String temporaryRemoteDirectory = remoteDirectory;
if (RemoteFileTemplate.this.temporaryDirectoryExpressionProcessor != null) {
temporaryRemoteDirectory = RemoteFileTemplate.this.temporaryDirectoryExpressionProcessor
.processMessage(message);
remoteDirectory = RemoteFileTemplate.this.normalizeDirectoryPath(remoteDirectory);
if (StringUtils.hasText(subDirectory)) {
if (subDirectory.startsWith(RemoteFileTemplate.this.remoteFileSeparator)) {
remoteDirectory += subDirectory.substring(1);
}
else {
remoteDirectory += RemoteFileTemplate.this.normalizeDirectoryPath(subDirectory);
}
}
String temporaryRemoteDirectory = remoteDirectory;
if (RemoteFileTemplate.this.temporaryDirectoryExpressionProcessor != null) {
temporaryRemoteDirectory = RemoteFileTemplate.this.temporaryDirectoryExpressionProcessor
.processMessage(message);
}
fileName = RemoteFileTemplate.this.fileNameGenerator.generateFileName(message);
RemoteFileTemplate.this.sendFileToRemoteDirectory(inputStreamHolder.getStream(),
temporaryRemoteDirectory, remoteDirectory, fileName, session, mode);
return remoteDirectory + fileName;
}
catch (FileNotFoundException e) {
throw new MessageDeliveryException(message, "File [" + inputStreamHolder.getName()
+ "] not found in local working directory; it was moved or deleted unexpectedly.", e);
}
catch (IOException e) {
throw new MessageDeliveryException(message, "Failed to transfer file ["
+ inputStreamHolder.getName() + " -> " + fileName
+ "] from local directory to remote directory.", e);
}
catch (Exception e) {
throw new MessageDeliveryException(message, "Error handling message for file ["
+ inputStreamHolder.getName() + " -> " + fileName + "]", e);
}
fileName = RemoteFileTemplate.this.fileNameGenerator.generateFileName(message);
RemoteFileTemplate.this.sendFileToRemoteDirectory(inputStreamHolder.getStream(),
temporaryRemoteDirectory, remoteDirectory, fileName, session, mode);
return remoteDirectory + fileName;
}
catch (FileNotFoundException e) {
throw new MessageDeliveryException(message, "File [" + inputStreamHolder.getName()
+ "] not found in local working directory; it was moved or deleted unexpectedly.", e);
}
catch (IOException e) {
throw new MessageDeliveryException(message, "Failed to transfer file ["
+ inputStreamHolder.getName() + " -> " + fileName
+ "] from local directory to remote directory.", e);
}
catch (Exception e) {
throw new MessageDeliveryException(message, "Error handling message for file ["
+ inputStreamHolder.getName() + " -> " + fileName + "]", e);
}
});
}
@@ -342,24 +343,12 @@ public class RemoteFileTemplate<F> implements RemoteFileOperations<F>, Initializ
@Override
public boolean exists(final String path) {
return this.execute(new SessionCallback<F, Boolean>() {
@Override
public Boolean doInSession(Session<F> session) throws IOException {
return session.exists(path);
}
});
return execute(session -> session.exists(path));
}
@Override
public boolean remove(final String path) {
return this.execute(new SessionCallback<F, Boolean>() {
@Override
public Boolean doInSession(Session<F> session) throws IOException {
return session.remove(path);
}
});
return execute(session -> session.remove(path));
}
@Override
@@ -367,10 +356,7 @@ public class RemoteFileTemplate<F> implements RemoteFileOperations<F>, Initializ
Assert.hasText(fromPath, "Old filename cannot be null or empty");
Assert.hasText(toPath, "New filename cannot be null or empty");
this.execute(new SessionCallbackWithoutResult<F>() {
@Override
public void doInSessionWithoutResult(Session<F> session) throws IOException {
this.execute((SessionCallbackWithoutResult<F>) session -> {
int lastSeparator = toPath.lastIndexOf(RemoteFileTemplate.this.remoteFileSeparator);
if (lastSeparator > 0) {
String remoteFileDirectory = toPath.substring(0, lastSeparator + 1);
@@ -378,7 +364,6 @@ public class RemoteFileTemplate<F> implements RemoteFileOperations<F>, Initializ
RemoteFileTemplate.this.remoteFileSeparator, RemoteFileTemplate.this.logger);
}
session.rename(fromPath, toPath);
}
});
}
@@ -395,29 +380,18 @@ public class RemoteFileTemplate<F> implements RemoteFileOperations<F>, Initializ
@Override
public boolean get(final String remotePath, final InputStreamCallback callback) {
Assert.notNull(remotePath, "'remotePath' cannot be null");
return this.execute(new SessionCallback<F, Boolean>() {
@Override
public Boolean doInSession(Session<F> session) throws IOException {
InputStream inputStream = session.readRaw(remotePath);
callback.doWithInputStream(inputStream);
inputStream.close();
return session.finalizeRaw();
}
return this.execute(session -> {
InputStream inputStream = session.readRaw(remotePath);
callback.doWithInputStream(inputStream);
inputStream.close();
return session.finalizeRaw();
});
}
@Override
public F[] list(final String path) {
return this.execute(new SessionCallback<F, F[]>() {
@Override
public F[] doInSession(Session<F> session) throws IOException {
return session.list(path);
}
});
public F[] list(String path) {
return execute(session -> session.list(path));
}
@Override
@@ -425,7 +399,6 @@ public class RemoteFileTemplate<F> implements RemoteFileOperations<F>, Initializ
return this.sessionFactory.getSession();
}
@SuppressWarnings("rawtypes")
@Override
public <T> T execute(SessionCallback<F, T> callback) {
Session<F> session = null;

View File

@@ -28,6 +28,7 @@ import org.springframework.integration.file.remote.session.Session;
* @since 3.0
*
*/
@FunctionalInterface
public interface SessionCallback<F, T> {
/**

View File

@@ -25,13 +25,15 @@ import org.springframework.integration.file.remote.session.Session;
* no result is returned.
*
* @author Gary Russell
* @author Artem Bilan
* @since 3.0
*
*/
public abstract class SessionCallbackWithoutResult<F> implements SessionCallback<F, Object> {
@FunctionalInterface
public interface SessionCallbackWithoutResult<F> extends SessionCallback<F, Object> {
@Override
public Object doInSession(Session<F> session) throws IOException {
default Object doInSession(Session<F> session) throws IOException {
doInSessionWithoutResult(session);
return null;
}
@@ -40,10 +42,9 @@ public abstract class SessionCallbackWithoutResult<F> implements SessionCallback
* Called within the context of a session.
* Perform some operation(s) on the session. The caller will take
* care of closing the session after this method exits.
*
* @param session The session.
* @throws IOException Any IOException.
*/
protected abstract void doInSessionWithoutResult(Session<F> session) throws IOException;
void doInSessionWithoutResult(Session<F> session) throws IOException;
}

View File

@@ -62,7 +62,7 @@ public class FileTransferringMessageHandler<F> extends AbstractMessageHandler {
/**
* @param autoCreateDirectory true to automatically create the direcotory.
* @param autoCreateDirectory true to automatically create the directory.
* @see RemoteFileTemplate#setAutoCreateDirectory(boolean)
*/
public void setAutoCreateDirectory(boolean autoCreateDirectory) {

View File

@@ -22,6 +22,7 @@ package org.springframework.integration.file.remote.session;
* @author Mark Fisher
* @since 2.0
*/
@FunctionalInterface
public interface SessionFactory<F> {
Session<F> getSession();

View File

@@ -24,6 +24,7 @@ package org.springframework.integration.file.remote.session;
* @since 4.2
*
*/
@FunctionalInterface
public interface SessionFactoryLocator<F> {
/**

View File

@@ -23,8 +23,6 @@ import org.apache.commons.net.ftp.FTPFile;
import org.springframework.integration.file.remote.ClientCallback;
import org.springframework.integration.file.remote.RemoteFileTemplate;
import org.springframework.integration.file.remote.SessionCallback;
import org.springframework.integration.file.remote.session.Session;
import org.springframework.integration.file.remote.session.SessionFactory;
import org.springframework.messaging.MessagingException;
import org.springframework.util.Assert;
@@ -67,13 +65,7 @@ public class FtpRemoteFileTemplate extends RemoteFileTemplate<FTPFile> {
}
protected <T> T doExecuteWithClient(final ClientCallback<FTPClient, T> callback) {
return execute(new SessionCallback<FTPFile, T>() {
@Override
public T doInSession(Session<FTPFile> session) throws IOException {
return callback.doWithClient((FTPClient) session.getClientInstance());
}
});
return execute(session -> callback.doWithClient((FTPClient) session.getClientInstance()));
}
/**

View File

@@ -29,10 +29,8 @@ import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.integration.config.AbstractSimpleMessageHandlerFactoryBean;
import org.springframework.integration.groovy.GroovyCommandMessageProcessor;
import org.springframework.integration.handler.ServiceActivatingHandler;
import org.springframework.integration.scripting.ScriptVariableGenerator;
import org.springframework.integration.support.management.IntegrationManagedResource;
import org.springframework.jmx.export.annotation.ManagedResource;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageHandler;
import org.springframework.scripting.groovy.GroovyObjectCustomizer;
import org.springframework.util.CustomizableThreadCreator;
@@ -51,7 +49,8 @@ import groovy.lang.MissingPropertyException;
* @author Gary Russell
* @since 2.0
*/
public class GroovyControlBusFactoryBean extends AbstractSimpleMessageHandlerFactoryBean<MessageHandler> implements BeanClassLoaderAware {
public class GroovyControlBusFactoryBean extends AbstractSimpleMessageHandlerFactoryBean<MessageHandler>
implements BeanClassLoaderAware {
private volatile Long sendTimeout;
@@ -75,15 +74,11 @@ public class GroovyControlBusFactoryBean extends AbstractSimpleMessageHandlerFac
@Override
protected MessageHandler createHandler() {
Binding binding = new ManagedBeansBinding(this.getBeanFactory());
GroovyCommandMessageProcessor processor = new GroovyCommandMessageProcessor(binding, new ScriptVariableGenerator() {
@Override
public Map<String, Object> generateScriptVariables(Message<?> message) {
Map<String, Object> variables = new HashMap<String, Object>();
variables.put("headers", message.getHeaders());
return variables;
}
GroovyCommandMessageProcessor processor = new GroovyCommandMessageProcessor(binding,
message -> {
Map<String, Object> variables = new HashMap<>();
variables.put("headers", message.getHeaders());
return variables;
});
if (this.customizer != null) {
processor.setCustomizer(this.customizer);
@@ -147,7 +142,8 @@ public class GroovyControlBusFactoryBean extends AbstractSimpleMessageHandlerFac
(AnnotationUtils.findAnnotation(bean.getClass(), IntegrationManagedResource.class) != null)) {
return bean;
}
throw new BeanCreationNotAllowedException(name, "Only beans with @ManagedResource or beans which implement " +
throw new BeanCreationNotAllowedException(name,
"Only beans with @ManagedResource or beans which implement " +
"org.springframework.context.Lifecycle or org.springframework.util.CustomizableThreadCreator " +
"are allowed to use as ControlBus components.");
}

View File

@@ -26,6 +26,7 @@ import org.springframework.messaging.Message;
* @since 2.0
*
*/
@FunctionalInterface
public interface TcpListener {
/**

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2015 the original author or authors.
* Copyright 2015-2016 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.
@@ -34,6 +34,7 @@ import org.springframework.messaging.Message;
* @since 4.2
* @see PreparedStatementSetter
*/
@FunctionalInterface
public interface MessagePreparedStatementSetter {
void setValues(PreparedStatement ps, Message<?> requestMessage) throws SQLException;

View File

@@ -25,6 +25,7 @@ import org.springframework.jdbc.core.namedparam.SqlParameterSource;
* @author Jonas Partner
* @since 2.0
*/
@FunctionalInterface
public interface SqlParameterSourceFactory {
/**

View File

@@ -23,6 +23,7 @@ import javax.management.ObjectName;
* @since 3.0
*
*/
@FunctionalInterface
public interface MBeanAttributeFilter {
/**

View File

@@ -25,6 +25,7 @@ import javax.management.ObjectInstance;
* @since 3.0
*
*/
@FunctionalInterface
public interface MBeanObjectConverter {
/**

View File

@@ -29,6 +29,7 @@ import javax.mail.search.SearchTerm;
* @since 2.2
*
*/
@FunctionalInterface
public interface SearchTermStrategy {
/**

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2014 the original author or authors.
* Copyright 2014-2016 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.
@@ -22,6 +22,7 @@ import org.springframework.messaging.Message;
* @author Artem Bilan
* @since 4.0
*/
@FunctionalInterface
public interface ArgumentsStrategy {
Object[] resolve(String command, Message<?> message);

View File

@@ -22,15 +22,19 @@ import org.springframework.scripting.ScriptSource;
/**
* @author David Turanski
* @author Artem Bilan
* @since 2.1
*/
@FunctionalInterface
public interface ScriptExecutor {
/**
* @param scriptSource The script source.
* @return The result of the execution.
*/
Object executeScript(ScriptSource scriptSource);
default Object executeScript(ScriptSource scriptSource) {
return executeScript(scriptSource, null);
}
/**
* @param scriptSource The script source.

View File

@@ -27,6 +27,7 @@ import org.springframework.messaging.Message;
* @author Oleg Zhurakousky
* @since 2.0.2
*/
@FunctionalInterface
public interface ScriptVariableGenerator {
Map<String, Object> generateScriptVariables(Message<?> message);

View File

@@ -60,11 +60,6 @@ public abstract class AbstractScriptExecutor implements ScriptExecutor {
}
}
@Override
public Object executeScript(ScriptSource scriptSource) {
return this.executeScript(scriptSource, null);
}
@Override
public Object executeScript(ScriptSource scriptSource, Map<String, Object> variables) {
Object result;

View File

@@ -141,18 +141,13 @@ public class SftpOutboundGateway extends AbstractRemoteFileOutboundGateway<LsEnt
@Override
protected void doChmod(RemoteFileTemplate<LsEntry> remoteFileTemplate, final String path, final int chmod) {
remoteFileTemplate.executeWithClient(new ClientCallbackWithoutResult<ChannelSftp>() {
@Override
protected void doWithClientWithoutResult(ChannelSftp client) {
remoteFileTemplate.executeWithClient((ClientCallbackWithoutResult<ChannelSftp>) client -> {
try {
client.chmod(chmod, path);
}
catch (SftpException e) {
throw new GeneralSftpException("Failed to execute chmod", e);
}
}
});
}

View File

@@ -73,18 +73,13 @@ public class SftpMessageHandler extends FileTransferringMessageHandler<LsEntry>
@Override
protected void doChmod(RemoteFileTemplate<LsEntry> remoteFileTemplate, final String path, final int chmod) {
remoteFileTemplate.executeWithClient(new ClientCallbackWithoutResult<ChannelSftp>() {
@Override
protected void doWithClientWithoutResult(ChannelSftp client) {
try {
client.chmod(chmod, path);
}
catch (SftpException e) {
throw new GeneralSftpException("Failed to execute chmod", e);
}
remoteFileTemplate.executeWithClient((ClientCallbackWithoutResult<ChannelSftp>) client -> {
try {
client.chmod(chmod, path);
}
catch (SftpException e) {
throw new GeneralSftpException("Failed to execute chmod", e);
}
});
}

View File

@@ -16,12 +16,8 @@
package org.springframework.integration.sftp.session;
import java.io.IOException;
import org.springframework.integration.file.remote.ClientCallback;
import org.springframework.integration.file.remote.RemoteFileTemplate;
import org.springframework.integration.file.remote.SessionCallback;
import org.springframework.integration.file.remote.session.Session;
import org.springframework.integration.file.remote.session.SessionFactory;
import com.jcraft.jsch.ChannelSftp;
@@ -48,13 +44,7 @@ public class SftpRemoteFileTemplate extends RemoteFileTemplate<LsEntry> {
}
protected <T> T doExecuteWithClient(final ClientCallback<ChannelSftp, T> callback) {
return execute(new SessionCallback<LsEntry, T>() {
@Override
public T doInSession(Session<LsEntry> session) throws IOException {
return callback.doWithClient((ChannelSftp) session.getClientInstance());
}
});
return execute(session -> callback.doWithClient((ChannelSftp) session.getClientInstance()));
}
}

View File

@@ -26,6 +26,7 @@ import org.springframework.messaging.Message;
* @since 3.0
*
*/
@FunctionalInterface
public interface MessageConverter {
Message<?> fromSyslog(Message<?> syslog) throws Exception;

View File

@@ -41,7 +41,6 @@ import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.springframework.integration.splitter.AbstractMessageSplitter;
import org.springframework.integration.util.Function;
import org.springframework.integration.util.FunctionIterator;
import org.springframework.integration.xml.DefaultXmlPayloadConverter;
import org.springframework.integration.xml.XmlPayloadConverter;
@@ -210,20 +209,15 @@ public class XPathMessageSplitter extends AbstractMessageSplitter {
return splitStrings;
}
else {
return new FunctionIterator<Node, String>((Iterator<Node>) nodes, new Function<Node, String>() {
@Override
public String apply(Node node) {
StringResult result = new StringResult();
try {
transformer.transform(new DOMSource(node), result);
}
catch (TransformerException e) {
throw new IllegalStateException("failed to create DocumentBuilder", e);
}
return result.toString();
return new FunctionIterator<>((Iterator<Node>) nodes, node -> {
StringResult result = new StringResult();
try {
transformer.transform(new DOMSource(node), result);
}
catch (TransformerException e) {
throw new IllegalStateException("failed to create DocumentBuilder", e);
}
return result.toString();
});
}
}