Source Code Cross Referenced for DefaultHibernateEntityLifecycleInjector.java in  » Search-Engine » compass-2.0 » org » compass » gps » device » hibernate » lifecycle » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Search Engine » compass 2.0 » org.compass.gps.device.hibernate.lifecycle 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.