| 1 | /* |
|---|
| 2 | * $Id$ |
|---|
| 3 | * |
|---|
| 4 | * Copyright 2006, The jCoderZ.org Project. All rights reserved. |
|---|
| 5 | * |
|---|
| 6 | * Redistribution and use in source and binary forms, with or without |
|---|
| 7 | * modification, are permitted provided that the following conditions are |
|---|
| 8 | * met: |
|---|
| 9 | * |
|---|
| 10 | * * Redistributions of source code must retain the above copyright |
|---|
| 11 | * notice, this list of conditions and the following disclaimer. |
|---|
| 12 | * * Redistributions in binary form must reproduce the above |
|---|
| 13 | * copyright notice, this list of conditions and the following |
|---|
| 14 | * disclaimer in the documentation and/or other materials |
|---|
| 15 | * provided with the distribution. |
|---|
| 16 | * * Neither the name of the jCoderZ.org Project nor the names of |
|---|
| 17 | * its contributors may be used to endorse or promote products |
|---|
| 18 | * derived from this software without specific prior written |
|---|
| 19 | * permission. |
|---|
| 20 | * |
|---|
| 21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS" AND |
|---|
| 22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
|---|
| 23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
|---|
| 24 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS |
|---|
| 25 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
|---|
| 26 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
|---|
| 27 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
|---|
| 28 | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
|---|
| 29 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
|---|
| 30 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
|---|
| 31 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|---|
| 32 | */ |
|---|
| 33 | package org.jcoderz.commons.connector; |
|---|
| 34 | |
|---|
| 35 | import javax.resource.ResourceException; |
|---|
| 36 | |
|---|
| 37 | |
|---|
| 38 | /** |
|---|
| 39 | * This abstract class provides a set of common methods that used by all |
|---|
| 40 | * derived connection's implementations. |
|---|
| 41 | * |
|---|
| 42 | */ |
|---|
| 43 | public abstract class ConnectionBase |
|---|
| 44 | implements ConnectionHandle |
|---|
| 45 | { |
|---|
| 46 | /** Indicates whether this connection has already been closed.*/ |
|---|
| 47 | private boolean mIsClosed = false; |
|---|
| 48 | /** Indicates whether this connection has already been cleaned up.*/ |
|---|
| 49 | private boolean mIsCleanedUp = false; |
|---|
| 50 | /** The listener to be used. */ |
|---|
| 51 | private ConnectionNotificationListener mCnl; |
|---|
| 52 | |
|---|
| 53 | /** |
|---|
| 54 | * Constructor. |
|---|
| 55 | * |
|---|
| 56 | * @param cnl The ConnectionNotificationListener to be used. |
|---|
| 57 | */ |
|---|
| 58 | protected ConnectionBase (final ConnectionNotificationListener cnl) |
|---|
| 59 | { |
|---|
| 60 | mCnl = cnl; |
|---|
| 61 | } |
|---|
| 62 | |
|---|
| 63 | /** |
|---|
| 64 | * Throws the {@link ResourceException} if this connection has already |
|---|
| 65 | * been closed. |
|---|
| 66 | * |
|---|
| 67 | * @throws javax.resource.spi.IllegalStateException if this connection has |
|---|
| 68 | * already been closed. |
|---|
| 69 | */ |
|---|
| 70 | protected void assertNotClosed () |
|---|
| 71 | throws javax.resource.spi.IllegalStateException |
|---|
| 72 | { |
|---|
| 73 | if (mIsClosed) |
|---|
| 74 | { |
|---|
| 75 | throw new javax.resource.spi.IllegalStateException( |
|---|
| 76 | "Connection has already been closed."); |
|---|
| 77 | } |
|---|
| 78 | } |
|---|
| 79 | |
|---|
| 80 | /** |
|---|
| 81 | * Throws the {@link ResourceException} if this connection has already been |
|---|
| 82 | * cleaned up. |
|---|
| 83 | * |
|---|
| 84 | * @throws javax.resource.spi.IllegalStateException if this connection has |
|---|
| 85 | * already been cleaned up. |
|---|
| 86 | */ |
|---|
| 87 | protected void assertNotCleanedUp () |
|---|
| 88 | throws javax.resource.spi.IllegalStateException |
|---|
| 89 | { |
|---|
| 90 | if (mIsCleanedUp) |
|---|
| 91 | { |
|---|
| 92 | throw new javax.resource.spi.IllegalStateException( |
|---|
| 93 | "Connection has already been cleaned up."); |
|---|
| 94 | } |
|---|
| 95 | } |
|---|
| 96 | |
|---|
| 97 | /** |
|---|
| 98 | * Throws the {@link ResourceException} if this connection has already been |
|---|
| 99 | * closed or cleaned up. |
|---|
| 100 | * |
|---|
| 101 | * @throws ResourceException if this connection has already been closed or |
|---|
| 102 | * cleaned up. |
|---|
| 103 | */ |
|---|
| 104 | protected void assertValid () |
|---|
| 105 | throws ResourceException |
|---|
| 106 | { |
|---|
| 107 | assertNotClosed(); |
|---|
| 108 | assertNotCleanedUp(); |
|---|
| 109 | } |
|---|
| 110 | |
|---|
| 111 | /** |
|---|
| 112 | * Sets this connection to the state closed. All futher calls on the public |
|---|
| 113 | * methods of this connection will throw a ResourceException. |
|---|
| 114 | * |
|---|
| 115 | * @throws ResourceException thrown if this connection has already been |
|---|
| 116 | * closed. |
|---|
| 117 | */ |
|---|
| 118 | protected void setClosed () |
|---|
| 119 | throws ResourceException |
|---|
| 120 | { |
|---|
| 121 | assertValid(); |
|---|
| 122 | |
|---|
| 123 | mIsClosed = true; |
|---|
| 124 | mCnl.notifyConnectionClosed(this); |
|---|
| 125 | } |
|---|
| 126 | |
|---|
| 127 | /** |
|---|
| 128 | * Sets this connection to the state cleaned up. All futher calls on the |
|---|
| 129 | * public methods of this connection will throw a ResourceException. |
|---|
| 130 | */ |
|---|
| 131 | public void cleanUp () |
|---|
| 132 | { |
|---|
| 133 | mIsCleanedUp = true; |
|---|
| 134 | } |
|---|
| 135 | |
|---|
| 136 | /** {@inheritDoc} */ |
|---|
| 137 | public void changeAssociation (final ConnectionNotificationListener newCnl) |
|---|
| 138 | { |
|---|
| 139 | mCnl.notifyConnectionDissociated(this); |
|---|
| 140 | mCnl = newCnl; |
|---|
| 141 | } |
|---|
| 142 | } |
|---|