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.work;
023:
024: import java.util.EventObject;
025: import java.io.ObjectStreamField;
026: import java.io.ObjectInputStream;
027: import java.io.IOException;
028: import java.io.ObjectOutputStream;
029:
030: import org.jboss.util.id.SerialVersion;
031:
032: /**
033: * Events that occur on work
034: * @version $Revision: 57196 $
035: */
036: public class WorkEvent extends EventObject {
037: /** @since 4.0.2 */
038: static final long serialVersionUID;
039:
040: // Constants -----------------------------------------------------
041: /**
042: * These field names match the j2ee 1.4.1 ri version names
043: */
044: private static final ObjectStreamField[] serialPersistentFields;
045: private static final int TYPE_IDX = 0;
046: private static final int WORK_IDX = 1;
047: private static final int EXCPEPTION_IDX = 2;
048: private static final int DURATION_IDX = 2;
049:
050: /** The WORK_ACCEPTED value */
051: public static final int WORK_ACCEPTED = 1;
052: /** The WORK_REJECTED value */
053: public static final int WORK_REJECTED = 2;
054: /** The WORK_STARTED value */
055: public static final int WORK_STARTED = 3;
056: /** The WORK_COMPLETED value */
057: public static final int WORK_COMPLETED = 4;
058:
059: static {
060: if (SerialVersion.version == SerialVersion.LEGACY) {
061: serialVersionUID = 6971276136970053051L;
062: serialPersistentFields = new ObjectStreamField[] {
063: /** @serialField type int */
064: new ObjectStreamField("type", int.class),
065: /** @serialField work Work */
066: new ObjectStreamField("work", Work.class),
067: /** @serialField exception WorkException */
068: new ObjectStreamField("e", WorkException.class),
069: /** @serialField startDuration long */
070: new ObjectStreamField("startDuration", long.class) };
071: } else {
072: // j2ee 1.4.1 RI values
073: serialVersionUID = -3063612635015047218L;
074: serialPersistentFields = new ObjectStreamField[] {
075: /** @serialField type int */
076: new ObjectStreamField("type", int.class),
077: /** @serialField work Work */
078: new ObjectStreamField("work", Work.class),
079: /** @serialField exception WorkException */
080: new ObjectStreamField("exception", WorkException.class),
081: /** @serialField startDuration long */
082: new ObjectStreamField("startDuration", long.class) };
083: }
084: }
085:
086: /** The event type */
087: private int type;
088: /** The work */
089: private Work work;
090: /** The exception */
091: private WorkException e;
092: /** The start delay in millis */
093: private long startDuration;
094:
095: /**
096: * Create a new WorkEvent
097: *
098: * @param source the source of the event
099: * @param type the type
100: * @param work the work
101: * @param e the exception
102: */
103: public WorkEvent(Object source, int type, Work work, WorkException e) {
104: this (source, type, work, e, 0l);
105: }
106:
107: /**
108: * Create a new WorkEvent
109: *
110: * @param source the source of the event
111: * @param type the type
112: * @param work the work
113: * @param e the exception
114: * @param startDuration the delay in the start in milliseconds
115: */
116: public WorkEvent(Object source, int type, Work work,
117: WorkException e, long startDuration) {
118: super (source);
119: this .type = type;
120: this .work = work;
121: this .e = e;
122: this .startDuration = startDuration;
123: }
124:
125: /**
126: * Get the type
127: *
128: * @return the type
129: */
130: public int getType() {
131: return type;
132: }
133:
134: /**
135: * Get the work
136: *
137: * @return the work
138: */
139: public Work getWork() {
140: return work;
141: }
142:
143: /**
144: * Get the exception
145: *
146: * @return the work exception
147: */
148: public WorkException getException() {
149: return e;
150: }
151:
152: /**
153: * Get the start duration
154: *
155: * @return the start duration
156: */
157: public long getStartDuration() {
158: return startDuration;
159: }
160:
161: // Private -------------------------------------------------------
162: private void readObject(ObjectInputStream ois)
163: throws ClassNotFoundException, IOException {
164: ObjectInputStream.GetField fields = ois.readFields();
165: String name = serialPersistentFields[TYPE_IDX].getName();
166: this .type = fields.get(name, 0);
167: name = serialPersistentFields[WORK_IDX].getName();
168: this .work = (Work) fields.get(name, null);
169: name = serialPersistentFields[EXCPEPTION_IDX].getName();
170: this .e = (WorkException) fields.get(name, null);
171: name = serialPersistentFields[DURATION_IDX].getName();
172: this .startDuration = fields.get(name, 0L);
173: }
174:
175: private void writeObject(ObjectOutputStream oos) throws IOException {
176: // Write j2ee 1.4.1 RI field names
177: ObjectOutputStream.PutField fields = oos.putFields();
178: String name = serialPersistentFields[TYPE_IDX].getName();
179: fields.put(name, type);
180: name = serialPersistentFields[WORK_IDX].getName();
181: fields.put(name, work);
182: name = serialPersistentFields[EXCPEPTION_IDX].getName();
183: fields.put(name, e);
184: name = serialPersistentFields[DURATION_IDX].getName();
185: fields.put(name, startDuration);
186: oos.writeFields();
187: }
188: }
|