001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package javax.resource.spi;
023:
024: import java.io.Serializable;
025: import java.io.ObjectStreamField;
026: import java.io.ObjectInputStream;
027: import java.io.IOException;
028: import java.io.ObjectOutputStream;
029: import java.util.EventObject;
030:
031: import org.jboss.util.id.SerialVersion;
032:
033: /**
034: * The ConnectionEvent class provides information about the source of a
035: * Connection related event. A ConnectionEvent contains:
036: * <ul>
037: *
038: * <li>Type of connection event.</li>
039: * <li>Managed connection instance that generated the event.</li>
040: * <li>Connection handle associated with the managed connection.</li>
041: * <li>Optionally an exception indicating an error.</li>
042: * </ul>
043: * <p>
044: * This class is used for the following types of notifications:
045: * <ul>
046: *
047: * <li>Connection closed</li>
048: * <li>Local transaction started</li>
049: * <li>Local transaction commited</li>
050: * <li>Local transaction rolled back</li>
051: * <li>Connection error occurred</li>
052: * </ul>
053: * @version $Revision: 57196 $
054: */
055: public class ConnectionEvent extends EventObject {
056: /** @since 4.0.2 */
057: static final long serialVersionUID;
058:
059: // Constants -----------------------------------------------------
060: /**
061: * These field names match the j2ee 1.4.1 ri version names
062: */
063: private static final ObjectStreamField[] serialPersistentFields;
064: private static final int ID_IDX = 0;
065: private static final int EXCEPTION_IDX = 1;
066: private static final int CONN_HANDLE_IDX = 2;
067:
068: static {
069: if (SerialVersion.version == SerialVersion.LEGACY) {
070: serialVersionUID = 2776168349823367611L;
071: serialPersistentFields = new ObjectStreamField[] {
072: /** @serialField id int */
073: new ObjectStreamField("id", int.class),
074: /** @serialField e Exception */
075: new ObjectStreamField("e", Exception.class),
076: /** @serialField connectionHandle Object */
077: new ObjectStreamField("connectionHandle", Object.class) };
078: } else {
079: // j2ee 1.4.1 RI values
080: serialVersionUID = 5611772461379563249L;
081: serialPersistentFields = new ObjectStreamField[] {
082: /** @serialField id int */
083: new ObjectStreamField("id", int.class),
084: /** @serialField exception Exception */
085: new ObjectStreamField("exception", Exception.class),
086: /** @serialField connectionHandle Object */
087: new ObjectStreamField("connectionHandle", Object.class) };
088: }
089: }
090:
091: /**
092: * Connection has been closed
093: */
094: public static final int CONNECTION_CLOSED = 1;
095:
096: /**
097: * Local transaction has been started
098: */
099: public static final int LOCAL_TRANSACTION_STARTED = 2;
100:
101: /**
102: * Local transaction has been committed
103: */
104: public static final int LOCAL_TRANSACTION_COMMITTED = 3;
105:
106: /**
107: * Local transaction has been rolled back
108: */
109: public static final int LOCAL_TRANSACTION_ROLLEDBACK = 4;
110:
111: /**
112: * Connection error has occurred
113: */
114: public static final int CONNECTION_ERROR_OCCURRED = 5;
115:
116: /** Type of event */
117: protected int id;
118:
119: /** The exception */
120: private Exception e = null;
121: /** The connectionHandle */
122: private Object connectionHandle = null;
123:
124: /**
125: * Create a new ConnectionEvent
126: *
127: * @param source the source of the event
128: * @param eid the event id
129: */
130: public ConnectionEvent(ManagedConnection source, int eid) {
131: super (source);
132: id = eid;
133: }
134:
135: /**
136: * Create a new ConnectionEvent
137: *
138: * @param source the source of the event
139: * @param eid the event id
140: * @param exception the exception associated with the event
141: */
142: public ConnectionEvent(ManagedConnection source, int eid,
143: Exception exception) {
144: super (source);
145: id = eid;
146: e = exception;
147: }
148:
149: /**
150: * Get the event type
151: *
152: * @return the event id
153: */
154: public int getId() {
155: return id;
156: }
157:
158: /**
159: * Get the exception
160: *
161: * @return the exception
162: */
163: public Exception getException() {
164: return e;
165: }
166:
167: /**
168: * Set the ConnectionHandle
169: *
170: * @param connectionHandle the connection handle
171: */
172: public void setConnectionHandle(Object connectionHandle) {
173: this .connectionHandle = connectionHandle;
174: }
175:
176: /**
177: * Get the ConnectionHandle
178: *
179: * @return the connection handle
180: */
181: public Object getConnectionHandle() {
182: return connectionHandle;
183: }
184:
185: // Private -------------------------------------------------------
186: private void readObject(ObjectInputStream ois)
187: throws ClassNotFoundException, IOException {
188: ObjectInputStream.GetField fields = ois.readFields();
189: String name = serialPersistentFields[ID_IDX].getName();
190: this .id = fields.get(name, CONNECTION_ERROR_OCCURRED);
191: name = serialPersistentFields[EXCEPTION_IDX].getName();
192: this .e = (Exception) fields.get(name, null);
193: name = serialPersistentFields[CONN_HANDLE_IDX].getName();
194: this .connectionHandle = fields.get(name, null);
195: }
196:
197: private void writeObject(ObjectOutputStream oos) throws IOException {
198: // Write j2ee 1.4.1 RI field names
199: ObjectOutputStream.PutField fields = oos.putFields();
200: String name = serialPersistentFields[ID_IDX].getName();
201: fields.put(name, id);
202: name = serialPersistentFields[EXCEPTION_IDX].getName();
203: fields.put(name, e);
204: name = serialPersistentFields[CONN_HANDLE_IDX].getName();
205: fields.put(name, connectionHandle);
206: oos.writeFields();
207: }
208:
209: }
|