001: /*
002: * Copyright 2004-2006 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.compass.gps.device.hibernate.lifecycle;
018:
019: import java.util.ArrayList;
020:
021: import org.compass.gps.device.hibernate.HibernateGpsDevice;
022: import org.compass.gps.device.hibernate.HibernateGpsDeviceException;
023: import org.hibernate.SessionFactory;
024: import org.hibernate.event.EventListeners;
025: import org.hibernate.event.PostDeleteEventListener;
026: import org.hibernate.event.PostInsertEventListener;
027: import org.hibernate.event.PostUpdateEventListener;
028: import org.hibernate.impl.SessionFactoryImpl;
029:
030: /**
031: * Injects lifecycle listeners directly into Hibernate for mirroring operations.
032: *
033: * <p>By default, registers with plain insert/update/delete listeners, which will be triggered
034: * by Hibernate before committing (and up to Hibernate flushing logic). Also allows to be created
035: * with setting the <code>registerPostCommitListeneres</code> to <code>true</code> which will cause
036: * the insert/update/delete listeneres to be registered as post commit events.
037: *
038: * @author kimchy
039: */
040: public class DefaultHibernateEntityLifecycleInjector implements
041: HibernateEntityLifecycleInjector {
042:
043: private boolean registerPostCommitListeneres = false;
044:
045: private boolean marshallIds = false;
046:
047: public DefaultHibernateEntityLifecycleInjector() {
048: this (false);
049: }
050:
051: /**
052: * Creates a new lifecycle injector. Allows to control if the insert/update/delete
053: * even listeners will be registered with post commit listeres (flag it <code>true</code>)
054: * or with plain post events (triggered based on Hibrenate flushing logic).
055: *
056: * @param registerPostCommitListeneres <code>true</code> if post commit listeners will be
057: * registered. <code>false</code> for plain listeners.
058: */
059: public DefaultHibernateEntityLifecycleInjector(
060: boolean registerPostCommitListeneres) {
061: this .registerPostCommitListeneres = registerPostCommitListeneres;
062: }
063:
064: /**
065: * Should the listener try and marshall ids for the event listener of post insert. Some
066: * Hibernate versions won't put the generated ids in the object that is inserted. Defaults
067: * to <code>false</code>.
068: */
069: public void setMarshallIds(boolean marshallIds) {
070: this .marshallIds = marshallIds;
071: }
072:
073: public void injectLifecycle(SessionFactory sessionFactory,
074: HibernateGpsDevice device)
075: throws HibernateGpsDeviceException {
076:
077: SessionFactoryImpl sessionFactoryImpl = (SessionFactoryImpl) sessionFactory;
078: EventListeners eventListeners = sessionFactoryImpl
079: .getEventListeners();
080:
081: // TODO add the ability to extend the system and register your own event listener (which maybe performs filtering)
082: HibernateEventListener hibernateEventListener = new HibernateEventListener(
083: device, marshallIds);
084:
085: PostInsertEventListener[] postInsertEventListeners;
086: if (registerPostCommitListeneres) {
087: postInsertEventListeners = eventListeners
088: .getPostCommitInsertEventListeners();
089: } else {
090: postInsertEventListeners = eventListeners
091: .getPostInsertEventListeners();
092: }
093: PostInsertEventListener[] tempPostInsertEventListeners = new PostInsertEventListener[postInsertEventListeners.length + 1];
094: System.arraycopy(postInsertEventListeners, 0,
095: tempPostInsertEventListeners, 0,
096: postInsertEventListeners.length);
097: tempPostInsertEventListeners[postInsertEventListeners.length] = hibernateEventListener;
098: if (registerPostCommitListeneres) {
099: eventListeners
100: .setPostCommitInsertEventListeners(tempPostInsertEventListeners);
101: } else {
102: eventListeners
103: .setPostInsertEventListeners(tempPostInsertEventListeners);
104: }
105:
106: PostUpdateEventListener[] postUpdateEventListeners;
107: if (registerPostCommitListeneres) {
108: postUpdateEventListeners = eventListeners
109: .getPostCommitUpdateEventListeners();
110: } else {
111: postUpdateEventListeners = eventListeners
112: .getPostUpdateEventListeners();
113: }
114: PostUpdateEventListener[] tempPostUpdateEventListeners = new PostUpdateEventListener[postUpdateEventListeners.length + 1];
115: System.arraycopy(postUpdateEventListeners, 0,
116: tempPostUpdateEventListeners, 0,
117: postUpdateEventListeners.length);
118: tempPostUpdateEventListeners[postUpdateEventListeners.length] = hibernateEventListener;
119: if (registerPostCommitListeneres) {
120: eventListeners
121: .setPostCommitUpdateEventListeners(tempPostUpdateEventListeners);
122: } else {
123: eventListeners
124: .setPostUpdateEventListeners(tempPostUpdateEventListeners);
125: }
126:
127: PostDeleteEventListener[] postDeleteEventListeners;
128: if (registerPostCommitListeneres) {
129: postDeleteEventListeners = eventListeners
130: .getPostCommitDeleteEventListeners();
131: } else {
132: postDeleteEventListeners = eventListeners
133: .getPostDeleteEventListeners();
134: }
135: PostDeleteEventListener[] tempPostDeleteEventListeners = new PostDeleteEventListener[postDeleteEventListeners.length + 1];
136: System.arraycopy(postDeleteEventListeners, 0,
137: tempPostDeleteEventListeners, 0,
138: postDeleteEventListeners.length);
139: tempPostDeleteEventListeners[postDeleteEventListeners.length] = hibernateEventListener;
140: if (registerPostCommitListeneres) {
141: eventListeners
142: .setPostCommitDeleteEventListeners(tempPostDeleteEventListeners);
143: } else {
144: eventListeners
145: .setPostDeleteEventListeners(tempPostDeleteEventListeners);
146: }
147: }
148:
149: public void removeLifecycle(SessionFactory sessionFactory,
150: HibernateGpsDevice device)
151: throws HibernateGpsDeviceException {
152:
153: SessionFactoryImpl sessionFactoryImpl = (SessionFactoryImpl) sessionFactory;
154: EventListeners eventListeners = sessionFactoryImpl
155: .getEventListeners();
156:
157: PostInsertEventListener[] postInsertEventListeners;
158: if (registerPostCommitListeneres) {
159: postInsertEventListeners = eventListeners
160: .getPostCommitInsertEventListeners();
161: } else {
162: postInsertEventListeners = eventListeners
163: .getPostInsertEventListeners();
164: }
165: ArrayList tempPostInsertEventListeners = new ArrayList();
166: for (int i = 0; i < postInsertEventListeners.length; i++) {
167: PostInsertEventListener postInsertEventListener = postInsertEventListeners[i];
168: if (!(postInsertEventListener instanceof HibernateEventListener)) {
169: tempPostInsertEventListeners
170: .add(postInsertEventListener);
171: }
172: }
173: if (registerPostCommitListeneres) {
174: eventListeners
175: .setPostCommitInsertEventListeners((PostInsertEventListener[]) tempPostInsertEventListeners
176: .toArray(new PostInsertEventListener[tempPostInsertEventListeners
177: .size()]));
178: } else {
179: eventListeners
180: .setPostInsertEventListeners((PostInsertEventListener[]) tempPostInsertEventListeners
181: .toArray(new PostInsertEventListener[tempPostInsertEventListeners
182: .size()]));
183: }
184:
185: PostUpdateEventListener[] postUpdateEventListeners;
186: if (registerPostCommitListeneres) {
187: postUpdateEventListeners = eventListeners
188: .getPostCommitUpdateEventListeners();
189: } else {
190: postUpdateEventListeners = eventListeners
191: .getPostUpdateEventListeners();
192: }
193: ArrayList tempPostUpdateEventListeners = new ArrayList();
194: for (int i = 0; i < postUpdateEventListeners.length; i++) {
195: PostUpdateEventListener postUpdateEventListener = postUpdateEventListeners[i];
196: if (!(postUpdateEventListener instanceof HibernateEventListener)) {
197: tempPostUpdateEventListeners
198: .add(postUpdateEventListener);
199: }
200: }
201: if (registerPostCommitListeneres) {
202: eventListeners
203: .setPostCommitUpdateEventListeners((PostUpdateEventListener[]) tempPostUpdateEventListeners
204: .toArray(new PostUpdateEventListener[tempPostUpdateEventListeners
205: .size()]));
206: } else {
207: eventListeners
208: .setPostUpdateEventListeners((PostUpdateEventListener[]) tempPostUpdateEventListeners
209: .toArray(new PostUpdateEventListener[tempPostUpdateEventListeners
210: .size()]));
211: }
212:
213: PostDeleteEventListener[] postDeleteEventListeners;
214: if (registerPostCommitListeneres) {
215: postDeleteEventListeners = eventListeners
216: .getPostCommitDeleteEventListeners();
217: } else {
218: postDeleteEventListeners = eventListeners
219: .getPostDeleteEventListeners();
220: }
221: ArrayList tempPostDeleteEventListeners = new ArrayList();
222: for (int i = 0; i < postDeleteEventListeners.length; i++) {
223: PostDeleteEventListener postDeleteEventListener = postDeleteEventListeners[i];
224: if (!(postDeleteEventListener instanceof HibernateEventListener)) {
225: tempPostDeleteEventListeners
226: .add(postDeleteEventListener);
227: }
228: }
229: if (registerPostCommitListeneres) {
230: eventListeners
231: .setPostCommitDeleteEventListeners((PostDeleteEventListener[]) tempPostDeleteEventListeners
232: .toArray(new PostDeleteEventListener[tempPostDeleteEventListeners
233: .size()]));
234: } else {
235: eventListeners
236: .setPostDeleteEventListeners((PostDeleteEventListener[]) tempPostDeleteEventListeners
237: .toArray(new PostDeleteEventListener[tempPostDeleteEventListeners
238: .size()]));
239: }
240: }
241: }
|