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.persistence;
020:
021: import java.lang.reflect.InvocationTargetException;
022: import java.util.Collection;
023:
024: import org.apache.openjpa.event.DeleteListener;
025: import org.apache.openjpa.event.LifecycleCallbacks;
026: import org.apache.openjpa.event.LifecycleEvent;
027: import org.apache.openjpa.event.LifecycleEventManager;
028: import org.apache.openjpa.event.LoadListener;
029: import org.apache.openjpa.event.PersistListener;
030: import org.apache.openjpa.event.PostPersistListener;
031: import org.apache.openjpa.event.UpdateListener;
032: import org.apache.openjpa.event.PostDeleteListener;
033: import org.apache.openjpa.lib.util.Localizer;
034: import org.apache.openjpa.util.CallbackException;
035:
036: class PersistenceListenerAdapter implements
037: LifecycleEventManager.ListenerAdapter, PersistListener,
038: PostPersistListener, LoadListener, UpdateListener,
039: DeleteListener, PostDeleteListener {
040:
041: private static final Localizer _loc = Localizer
042: .forPackage(PersistenceListenerAdapter.class);
043:
044: private final LifecycleCallbacks[][] _callbacks;
045:
046: public PersistenceListenerAdapter(LifecycleCallbacks[][] callbacks) {
047: _callbacks = callbacks;
048: }
049:
050: public PersistenceListenerAdapter(
051: Collection<LifecycleCallbacks>[] calls) {
052: _callbacks = new LifecycleCallbacks[LifecycleEvent.ALL_EVENTS.length][];
053: for (int i = 0; i < LifecycleEvent.ALL_EVENTS.length; i++) {
054: if (calls[i] == null)
055: continue;
056: _callbacks[i] = calls[i]
057: .toArray(new LifecycleCallbacks[calls[i].size()]);
058: }
059: }
060:
061: public boolean respondsTo(int eventType) {
062: return _callbacks[eventType] != null;
063: }
064:
065: private void makeCallback(LifecycleEvent ev) {
066: int eventType = ev.getType();
067: if (_callbacks[eventType] == null)
068: return;
069: Object src = ev.getSource();
070: for (LifecycleCallbacks callback : _callbacks[eventType]) {
071: try {
072: callback.makeCallback(src, ev.getRelated(), eventType);
073: } catch (Throwable t) {
074: if (t instanceof InvocationTargetException)
075: t = t.getCause();
076: if (t instanceof RuntimeException)
077: throw (RuntimeException) t;
078:
079: throw new CallbackException(_loc.get(
080: "system-listener-err", src)).setCause(t)
081: .setFatal(true);
082: }
083: }
084: }
085:
086: public void beforePersist(LifecycleEvent event) {
087: makeCallback(event);
088: }
089:
090: public void afterPersist(LifecycleEvent event) {
091: throw new UnsupportedOperationException();
092: }
093:
094: public void afterPersistPerformed(LifecycleEvent event) {
095: makeCallback(event);
096: }
097:
098: public void afterLoad(LifecycleEvent event) {
099: makeCallback(event);
100: }
101:
102: public void afterRefresh(LifecycleEvent event) {
103: makeCallback(event);
104: }
105:
106: public void beforeUpdate(LifecycleEvent event) {
107: makeCallback(event);
108: }
109:
110: public void afterUpdatePerformed(LifecycleEvent event) {
111: makeCallback(event);
112: }
113:
114: public void beforeDelete(LifecycleEvent event) {
115: makeCallback(event);
116: }
117:
118: public void afterDelete(LifecycleEvent event) {
119: throw new UnsupportedOperationException();
120: }
121:
122: public void afterDeletePerformed(LifecycleEvent event) {
123: makeCallback(event);
124: }
125: }
|