001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019: package org.apache.openjpa.event;
020:
021: import org.apache.openjpa.lib.util.concurrent.AbstractConcurrentEventManager;
022:
023: /**
024: * Manager that can be used to track and notify transaction listeners
025: * of transaction-related events.
026: *
027: * @author Patrick Linskey
028: * @author Abe White
029: * @since 0.3.0
030: * @nojavadoc
031: */
032: public class TransactionEventManager extends
033: AbstractConcurrentEventManager {
034:
035: private int _begin = 0;
036: private int _flush = 0;
037: private int _end = 0;
038:
039: public void addListener(Object listener) {
040: super .addListener(listener);
041: if (listener instanceof BeginTransactionListener)
042: _begin++;
043: if (listener instanceof FlushTransactionListener)
044: _flush++;
045: if (listener instanceof EndTransactionListener)
046: _end++;
047: }
048:
049: public boolean removeListener(Object listener) {
050: if (!super .removeListener(listener))
051: return false;
052:
053: if (listener instanceof BeginTransactionListener)
054: _begin--;
055: if (listener instanceof FlushTransactionListener)
056: _flush--;
057: if (listener instanceof EndTransactionListener)
058: _end--;
059: return true;
060: }
061:
062: /**
063: * Whether there are any begin transaction listeners.
064: */
065: public boolean hasBeginListeners() {
066: return _begin > 0;
067: }
068:
069: /**
070: * Whether there are any flush transaction listeners.
071: */
072: public boolean hasFlushListeners() {
073: return _flush > 0;
074: }
075:
076: /**
077: * Whether there are any end transaction listeners.
078: */
079: public boolean hasEndListeners() {
080: return _end > 0;
081: }
082:
083: /**
084: * Fire the given event to all registered listeners.
085: */
086: protected void fireEvent(Object event, Object listener) {
087: TransactionEvent ev = (TransactionEvent) event;
088: switch (ev.getType()) {
089: case TransactionEvent.AFTER_BEGIN:
090: if (listener instanceof BeginTransactionListener)
091: ((BeginTransactionListener) listener).afterBegin(ev);
092: break;
093: case TransactionEvent.BEFORE_FLUSH:
094: if (listener instanceof FlushTransactionListener)
095: ((FlushTransactionListener) listener).beforeFlush(ev);
096: break;
097: case TransactionEvent.AFTER_FLUSH:
098: if (listener instanceof FlushTransactionListener)
099: ((FlushTransactionListener) listener).afterFlush(ev);
100: break;
101: case TransactionEvent.BEFORE_COMMIT:
102: if (listener instanceof EndTransactionListener)
103: ((EndTransactionListener) listener).beforeCommit(ev);
104: break;
105: case TransactionEvent.AFTER_COMMIT:
106: if (listener instanceof EndTransactionListener)
107: ((EndTransactionListener) listener).afterCommit(ev);
108: break;
109: case TransactionEvent.AFTER_ROLLBACK:
110: if (listener instanceof EndTransactionListener)
111: ((EndTransactionListener) listener).afterRollback(ev);
112: break;
113: case TransactionEvent.AFTER_STATE_TRANSITIONS:
114: if (listener instanceof EndTransactionListener)
115: ((EndTransactionListener) listener)
116: .afterStateTransitions(ev);
117: break;
118: case TransactionEvent.AFTER_COMMIT_COMPLETE:
119: if (listener instanceof EndTransactionListener)
120: ((EndTransactionListener) listener)
121: .afterCommitComplete(ev);
122: break;
123: case TransactionEvent.AFTER_ROLLBACK_COMPLETE:
124: if (listener instanceof EndTransactionListener)
125: ((EndTransactionListener) listener)
126: .afterRollbackComplete(ev);
127: break;
128: }
129: }
130: }
|