Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2006-2007 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.batch.repeat.interceptor;
 
A org.aopalliance.intercept.MethodInterceptor that can be used to automatically repeat calls to a method on a service. The injected org.springframework.batch.repeat.RepeatOperations is used to control the completion of the loop. Independent of the completion policy in the org.springframework.batch.repeat.RepeatOperations the loop will repeat until the target method returns null or false. Be careful when injecting a bespoke org.springframework.batch.repeat.RepeatOperations that the loop will actually terminate, because the default policy for a vanilla org.springframework.batch.repeat.support.RepeatTemplate will never complete if the return type of the target method is void (the value returned is always not-null, representing the java.lang.Void.TYPE).

Author(s):
Dave Syer
 
 public class RepeatOperationsInterceptor implements MethodInterceptor {
 
Setter for the org.springframework.batch.repeat.RepeatOperations.

Parameters:
batchTempate
Throws:
java.lang.IllegalArgumentException if the argument is null.
 
 	public void setRepeatOperations(RepeatOperations batchTempate) {
 		Assert.notNull(batchTempate"'repeatOperations' cannot be null.");
 		this. = batchTempate;
 	}

Invoke the proceeding method call repeatedly, according to the properties of the injected org.springframework.batch.repeat.RepeatOperations.

 
 	public Object invoke(final MethodInvocation invocationthrows Throwable {
 
 		final ResultHolder result = new ResultHolder();
 		// Cache void return value if intercepted method returns void
 		final boolean voidReturnType = ..equals(invocation.getMethod().getReturnType());
 		if (voidReturnType) {
 			// This will be ignored anyway, but we want it to be non-null for
 			// convenience of checking that there is a result.
 			result.setValue(new Object());
 		}
 
 		try {
 
 				public RepeatStatus doInIteration(RepeatContext contextthrows Exception {
 					try {
 
 						MethodInvocation clone = invocation;
 						if (invocation instanceof ProxyMethodInvocation) {
 							clone = ((ProxyMethodInvocationinvocation).invocableClone();
 						}
 						else {
 							throw new IllegalStateException(
 									"MethodInvocation of the wrong type detected - this should not happen with Spring AOP, so please raise an issue if you see this exception");
 						}
 
 						Object value = clone.proceed();
 						if (voidReturnType) {
 						}
 						if (!isComplete(value)) {
 							// Save the last result
 							result.setValue(value);
 						}
 						else {
							result.setFinalValue(value);
						}
					}
					catch (Throwable e) {
						if (e instanceof Exception) {
							throw (Exceptione;
						}
						else {
							throw new RepeatOperationsInterceptorException("Unexpected error in batch interceptor"e);
						}
					}
				}
			});
		}
		catch (Throwable t) {
			// The repeat exception should be unwrapped by the template
			throw t;
		}
		if (result.isReady()) {
			return result.getValue();
		}
		// No result means something weird happened
		throw new IllegalStateException("No result available for attempted repeat call to " + invocation
".  The invocation was never called, so maybe there is a problem with the completion policy?");
	}

Parameters:
result
Returns:
	private boolean isComplete(Object result) {
		return result == null || (result instanceof Boolean) && !((Booleanresult).booleanValue();
	}

Simple wrapper exception class to enable nasty errors to be passed out of the scope of the repeat operations and handled by the caller.

Author(s):
Dave Syer
	private static class RepeatOperationsInterceptorException extends RepeatException {

Parameters:
message
e
			super(messagee);
		}
	}

Simple wrapper object for the result from a method invocation.

Author(s):
Dave Syer
	private static class ResultHolder {
		private Object value = null;
		private boolean ready = false;

Public setter for the Object.

Parameters:
value the value to set
		public void setValue(Object value) {
			this. = true;
			this. = value;
		}

Parameters:
value
		public void setFinalValue(Object value) {
			if () {
				// Only set the value the last time if the last time was also
				// the first time
				return;
			}
			setValue(value);
		}

Public getter for the Object.

Returns:
the value
		public Object getValue() {
			return ;
		}

Returns:
true if a value has been set
		public boolean isReady() {
			return ;
		}
	}
New to GrepCode? Check out our FAQ X