001: package org.apache.ojb.broker;
002:
003: /* Copyright 2003-2005 The Apache Software Foundation
004: *
005: * Licensed under the Apache License, Version 2.0 (the "License");
006: * you may not use this file except in compliance with the License.
007: * You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: import org.apache.commons.lang.builder.ToStringBuilder;
019:
020: /**
021: * The <code>PBStateEvent</code> encapsulates information about
022: * the life-cycle/transaction demarcation of the used {@link org.apache.ojb.broker.PersistenceBroker}
023: * instance.
024: *
025: * @author Armin Waibel
026: * @version $Id: PBStateEvent.java,v 1.5.2.1 2005/12/21 22:22:08 tomdz Exp $
027: */
028: public final class PBStateEvent extends PersistenceBrokerEvent {
029: /** Denotes an event that happens before the broker will be closed. */
030: public static final int KEY_BEFORE_CLOSE = 1;
031: /** Denotes an event that happens before a transaction will be started. */
032: public static final int KEY_BEFORE_BEGIN = 2;
033: /** Denotes an event that happens before a transaction will be comitted. */
034: public static final int KEY_BEFORE_COMMIT = 3;
035: /** Denotes an event that happens before a transaction will be rolled back. */
036: public static final int KEY_BEFORE_ROLLBACK = 4;
037: /** Denotes an event that happens after a transaction was started. */
038: public static final int KEY_AFTER_BEGIN = 5;
039: /** Denotes an event that happens after a transaction was comitted. */
040: public static final int KEY_AFTER_COMMIT = 6;
041: /** Denotes an event that happens after a broker was opened. */
042: public static final int KEY_AFTER_OPEN = 7;
043: /** Denotes an event that happens after a transaction was rolled back. */
044: public static final int KEY_AFTER_ROLLBACK = 8;
045:
046: private Type eventType;
047:
048: /**
049: * Creates a new event instance.
050: *
051: * @param broker The broker
052: * @param eventType The type of the event
053: */
054: public PBStateEvent(PersistenceBroker broker, Type eventType) {
055: super (broker);
056: this .eventType = eventType;
057: }
058:
059: /**
060: * {@inheritDoc}
061: */
062: public String toString() {
063: ToStringBuilder buf = new ToStringBuilder(this );
064: buf.append("type", eventType.toString()).append(
065: "source object", getSource());
066: return buf.toString();
067: }
068:
069: /**
070: * Returns the event type.
071: *
072: * @return The event type
073: */
074: public Type getEventType() {
075: return eventType;
076: }
077:
078: /**
079: * Enum-like class for the event types.
080: */
081: public static class Type {
082: /** Denotes an event that happens before a transaction will be started. */
083: public static final Type BEFORE_BEGIN = new Type(
084: KEY_BEFORE_BEGIN);
085: /** Denotes an event that happens after a transaction was started. */
086: public static final Type AFTER_BEGIN = new Type(KEY_AFTER_BEGIN);
087: /** Denotes an event that happens before a transaction will be comitted. */
088: public static final Type BEFORE_COMMIT = new Type(
089: KEY_BEFORE_COMMIT);
090: /** Denotes an event that happens after a transaction was comitted. */
091: public static final Type AFTER_COMMIT = new Type(
092: KEY_AFTER_COMMIT);
093: /** Denotes an event that happens before a transaction will be rolled back. */
094: public static final Type BEFORE_ROLLBACK = new Type(
095: KEY_BEFORE_ROLLBACK);
096: /** Denotes an event that happens after a transaction was rolled back. */
097: public static final Type AFTER_ROLLBACK = new Type(
098: KEY_AFTER_ROLLBACK);
099: /** Denotes an event that happens after a broker was opened. */
100: public static final Type AFTER_OPEN = new Type(KEY_AFTER_OPEN);
101: /** Denotes an event that happens before the broker will be closed. */
102: public static final Type BEFORE_CLOSE = new Type(
103: KEY_BEFORE_CLOSE);
104:
105: private int type;
106:
107: /**
108: * Creates a new instance.
109: *
110: * @param type The type value
111: */
112: protected Type(int type) {
113: this .type = type;
114: }
115:
116: /**
117: * {@inheritDoc}
118: */
119: public final boolean equals(Object obj) {
120: if (obj == this ) {
121: return true;
122: }
123: if (!(obj instanceof PBStateEvent)) {
124: return false;
125: }
126:
127: return type == ((Type) obj).type;
128: }
129:
130: /**
131: * {@inheritDoc}
132: */
133: public final int hashCode() {
134: return type;
135: }
136:
137: /**
138: * Returns the type id.
139: *
140: * @return The type id
141: */
142: public final int typeId() {
143: return type;
144: }
145:
146: /**
147: * {@inheritDoc}
148: */
149: public String toString() {
150: return this .getClass().getName() + " [type= "
151: + typeAsName(type) + "]";
152: }
153:
154: private String typeAsName(int aType) {
155: if (aType == KEY_AFTER_BEGIN) {
156: return "AFTER_BEGIN";
157: } else if (aType == KEY_AFTER_COMMIT) {
158: return "AFTER_COMMIT";
159: } else if (aType == KEY_AFTER_OPEN) {
160: return "AFTER_OPEN";
161: } else if (aType == KEY_AFTER_ROLLBACK) {
162: return "AFTER_ROLLBACK";
163: } else if (aType == KEY_BEFORE_BEGIN) {
164: return "BEFORE_BEGIN";
165: } else if (aType == KEY_BEFORE_CLOSE) {
166: return "BEFORE_CLOSE";
167: } else if (aType == KEY_BEFORE_COMMIT) {
168: return "BEFORE_COMMIT";
169: } else if (aType == KEY_BEFORE_ROLLBACK) {
170: return "BEFORE_ROLLBACK";
171: } else {
172: throw new OJBRuntimeException("Could not find type "
173: + aType);
174: }
175: }
176: }
177: }
|