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.support.transaction;
 
 import java.io.Writer;
 
Wrapper for a java.io.Writer that delays actually writing to or closing the buffer if a transaction is active. If a transaction is detected on the call to java.io.Writer.write(java.lang.String) the parameter is buffered and passed on to the underlying writer only when the transaction is committed.

Author(s):
Dave Syer
 
 public class TransactionAwareBufferedWriter extends Writer {
 
 	private static final String BUFFER_KEY_PREFIX = TransactionAwareBufferedWriter.class.getName() + ".BUFFER_KEY";
 
 	private static final String CLOSE_KEY_PREFIX = TransactionAwareBufferedWriter.class.getName() + ".CLOSE_KEY";
 
 	private final String bufferKey;
 
 	private final String closeKey;
 
 	private Writer writer;
 
 	private final Runnable closeCallback;

Create a new instance with the underlying writer provided, and a callback to execute on close. The callback should clean up related resources like output streams or channels.

Parameters:
writer actually writes to output
closeCallback callback to execute on close
 
 	public TransactionAwareBufferedWriter(Writer writerRunnable closeCallback) {
 		super();
 		this. = writer;
 		this. = closeCallback;
 		this. =  + "." + hashCode();
 		this. =  + "." + hashCode();
 	}

Returns:
 
 
 		if (!TransactionSynchronizationManager.hasResource()) {
 
 			TransactionSynchronizationManager.bindResource(new StringBuffer());
 
 			TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronizationAdapter() {
 				public void afterCompletion(int status) {
 					if (status == ) {
 					}
 				}
 
 				private void complete() {
 					StringBuffer buffer = (StringBuffer) TransactionSynchronizationManager.getResource();
 					if (buffer != null) {
 						try {
 							.write(buffer.toString());
 							if (TransactionSynchronizationManager.hasResource()) {
 							}
 						}
 						catch (IOException e) {
 							throw new FlushFailedException("Could not write to output buffer"e);
 						}
 						finally {
 							TransactionSynchronizationManager.unbindResource();
 							if (TransactionSynchronizationManager.hasResource()) {
 								TransactionSynchronizationManager.unbindResource();
 							}
 						}
					}
				}
			});
		}
		return (StringBuffer) TransactionSynchronizationManager.getResource();
	}

Convenience method for clients to determine if there is any unflushed data.

Returns:
the current size of unflushed buffered data
	public long getBufferSize() {
			return 0L;
		}
	}

Returns:
	private boolean transactionActive() {
		return TransactionSynchronizationManager.isActualTransactionActive();
	}
	/*
	 * (non-Javadoc)
	 * 
	 * @see java.io.Writer#close()
	 */
	public void close() throws IOException {
			if (getCurrentBuffer().length() > 0) {
				TransactionSynchronizationManager.bindResource(.);
			}
			return;
		}
	}
	/*
	 * (non-Javadoc)
	 * 
	 * @see java.io.Writer#flush()
	 */
	public void flush() throws IOException {
		}
	}
	/*
	 * (non-Javadoc)
	 * 
	 * @see java.io.Writer#write(char[], int, int)
	 */
	public void write(char[] cbufint offint lenthrows IOException {
			.write(cbufofflen);
			return;
		}
		buffer.append(cbufofflen);
	}
New to GrepCode? Check out our FAQ X