| 1 | /* |
| 2 | * Copyright 2007 brunella ltd |
| 3 | * |
| 4 | * Licensed under the LGPL Version 3 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * |
| 7 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 8 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 9 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 10 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
| 11 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 12 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 13 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 14 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 15 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 16 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
| 17 | * THE POSSIBILITY OF SUCH DAMAGE. |
| 18 | */ |
| 19 | package sf.qof.session; |
| 20 | |
| 21 | import java.sql.Connection; |
| 22 | import java.sql.SQLException; |
| 23 | |
| 24 | /** |
| 25 | * <code>DefaultSessionRunner</code> is the default implementation that runs |
| 26 | * a <code>TransactionRunnable</code> in a session context. |
| 27 | * |
| 28 | * Calling <code>execute</code> starts a new session, calls the <code>run</code> |
| 29 | * method of the <code>TransactionRunnable</code> and commits or rolls back |
| 30 | * the transaction on success or failure. |
| 31 | * |
| 32 | * <p>Typical usage is like this:</p> |
| 33 | * |
| 34 | * <p><blockquote><pre> |
| 35 | * List<Person> personList = ... |
| 36 | * PersonUpdaterRunnable runnable = new PersonUpdaterRunnable(); |
| 37 | * Integer numberOfUpdates = |
| 38 | * new DefaultSessionRunner<Integer>(runnable, "MY_CONTEXT_NAME").execute(personList); |
| 39 | * </pre></blockquote></p> |
| 40 | * |
| 41 | * @param <T> the type of the result of the <code>TransactionRunnable</code>. If |
| 42 | * no result is returned this type should be <code>Void</code> |
| 43 | * |
| 44 | * @see SessionRunner |
| 45 | */ |
| 46 | public class DefaultSessionRunner<T> extends BaseSessionRunner<T> { |
| 47 | |
| 48 | private TransactionRunnable<T> runnable; |
| 49 | |
| 50 | /** |
| 51 | * Creates a <code>DefaultSessionRunner</code> that creates a session |
| 52 | * from the default session context. |
| 53 | * |
| 54 | * @param runnable a <code>TransactionRunnable</code> |
| 55 | */ |
| 56 | public DefaultSessionRunner(TransactionRunnable<T> runnable) { |
| 57 | this(runnable, SessionContext.DEFAULT_CONTEXT_NAME); |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Creates a <code>DefaultSessionRunner</code> that creates a session |
| 62 | * from the session context with the given name. |
| 63 | * |
| 64 | * @param runnable a <code>TransactionRunnable</code> |
| 65 | * @param contextName the context name |
| 66 | */ |
| 67 | public DefaultSessionRunner(TransactionRunnable<T> runnable, String contextName) { |
| 68 | super(contextName); |
| 69 | this.runnable = runnable; |
| 70 | } |
| 71 | |
| 72 | @Override |
| 73 | protected T run(Connection connection, Object... arguments) throws SQLException { |
| 74 | return runnable.run(connection, arguments); |
| 75 | } |
| 76 | } |