Source Code Cross Referenced for Hibernate3GpsDevice.java in  » Search-Engine » compass-2.0 » org » compass » gps » device » hibernate » dep » 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.dep 
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.dep;
018:
019:        import java.util.ArrayList;
020:        import java.util.Iterator;
021:        import java.util.List;
022:        import java.util.Map;
023:
024:        import org.compass.core.CompassCallbackWithoutResult;
025:        import org.compass.core.CompassException;
026:        import org.compass.core.CompassSession;
027:        import org.compass.core.mapping.CascadeMapping;
028:        import org.compass.core.mapping.ResourceMapping;
029:        import org.compass.core.spi.InternalCompassSession;
030:        import org.compass.core.util.ClassUtils;
031:        import org.compass.core.util.FieldInvoker;
032:        import org.compass.core.util.MethodInvoker;
033:        import org.compass.gps.CompassGpsException;
034:        import org.compass.gps.PassiveMirrorGpsDevice;
035:        import org.compass.gps.device.hibernate.HibernateGpsDeviceException;
036:        import org.hibernate.EntityMode;
037:        import org.hibernate.FlushMode;
038:        import org.hibernate.HibernateException;
039:        import org.hibernate.Query;
040:        import org.hibernate.Session;
041:        import org.hibernate.SessionFactory;
042:        import org.hibernate.Transaction;
043:        import org.hibernate.cfg.Configuration;
044:        import org.hibernate.event.PostDeleteEvent;
045:        import org.hibernate.event.PostDeleteEventListener;
046:        import org.hibernate.event.PostInsertEvent;
047:        import org.hibernate.event.PostInsertEventListener;
048:        import org.hibernate.event.PostUpdateEvent;
049:        import org.hibernate.event.PostUpdateEventListener;
050:        import org.hibernate.impl.SessionFactoryImpl;
051:        import org.hibernate.metadata.ClassMetadata;
052:        import org.hibernate.persister.entity.AbstractEntityPersister;
053:
054:        /**
055:         * A hibernate device which works with hibernate 3.
056:         * <p/>
057:         * You must either set the Hibernate <code>Configuration</code> or the
058:         * <code>SessionFactory</code> to be used by the device. Note that if the
059:         * <code>Configuration</code> is supplied, when the device <code>start</code>
060:         * is called, a new <code>SessionFactory</code> will be built.
061:         * <p/>
062:         * Provides support for real time index updates using the new Hibernate 3 event
063:         * system. The device uses the <code>PostInsertEventListener</code>,
064:         * <code>PostUpdateEventListener</code>, and
065:         * <code>PostDeleteEventListener</code> events.
066:         *
067:         * @author kimchy
068:         * @deprecated Please use {@link org.compass.gps.device.hibernate.HibernateGpsDevice}.
069:         */
070:        public class Hibernate3GpsDevice extends AbstractHibernateGpsDevice
071:                implements  PassiveMirrorGpsDevice {
072:
073:            public class Hibernate3SessionWrapper implements 
074:                    HibernateSessionWrapper {
075:
076:                private SessionFactory sessionFactory;
077:
078:                private Session session;
079:
080:                private Transaction tr;
081:
082:                public Hibernate3SessionWrapper(SessionFactory sessionFactory) {
083:                    this .sessionFactory = sessionFactory;
084:                }
085:
086:                public Session getSession() {
087:                    return session;
088:                }
089:
090:                public void open() throws HibernateGpsDeviceException {
091:                    try {
092:                        session = sessionFactory.openSession();
093:                        session.setFlushMode(FlushMode.NEVER);
094:                    } catch (HibernateException e) {
095:                        throw new HibernateGpsDeviceException(
096:                                buildMessage("Failed to open session to fetch data"),
097:                                e);
098:                    }
099:                    try {
100:                        tr = session.beginTransaction();
101:                    } catch (HibernateException e) {
102:                        throw new HibernateGpsDeviceException(
103:                                buildMessage("Failed to begin transaction to fetch data"),
104:                                e);
105:                    }
106:                }
107:
108:                public void close() {
109:                    if (tr != null) {
110:                        try {
111:                            tr.commit();
112:                        } catch (HibernateException e) {
113:                            throw new HibernateGpsDeviceException(
114:                                    "Failed to commit hibernate transaction");
115:                        }
116:                    }
117:                    try {
118:                        session.close();
119:                    } catch (HibernateException e) {
120:                        log.error("Failed to close Hibernate session", e);
121:                    }
122:                }
123:
124:                public void closeOnError() {
125:                    if (tr != null) {
126:                        try {
127:                            tr.rollback();
128:                        } catch (HibernateException e1) {
129:                            log
130:                                    .error(
131:                                            buildMessage("Failed to rollback hibernate transaction"),
132:                                            e1);
133:                        }
134:                    }
135:                    try {
136:                        session.close();
137:                    } catch (HibernateException e) {
138:                        log
139:                                .error(
140:                                        buildMessage("Failed to close Hibernate session"),
141:                                        e);
142:                    }
143:                }
144:            }
145:
146:            private class Hibernate3GpsDevicePostInsert implements 
147:                    PostInsertEventListener {
148:                private static final long serialVersionUID = 3544677273799308593L;
149:
150:                private PostInsertEventListener postInsertEventListener;
151:
152:                private HibernateMirrorFilter mirrorFilter;
153:
154:                public Hibernate3GpsDevicePostInsert(
155:                        PostInsertEventListener postInsertEventListener,
156:                        HibernateMirrorFilter mirrorFilter) {
157:                    this .postInsertEventListener = postInsertEventListener;
158:                    this .mirrorFilter = mirrorFilter;
159:                }
160:
161:                public PostInsertEventListener getPostInsertEventListener() {
162:                    return postInsertEventListener;
163:                }
164:
165:                public void onPostInsert(final PostInsertEvent postInsertEvent) {
166:
167:                    if (this .postInsertEventListener != null) {
168:                        this .postInsertEventListener
169:                                .onPostInsert(postInsertEvent);
170:                    }
171:
172:                    if (!shouldMirrorDataChanges()
173:                            || isPerformingIndexOperation()) {
174:                        return;
175:                    }
176:
177:                    final Object entity = postInsertEvent.getEntity();
178:                    if (!compassGps.hasMappingForEntityForMirror(entity
179:                            .getClass(), CascadeMapping.Cascade.CREATE)) {
180:                        return;
181:                    }
182:
183:                    if (mirrorFilter != null) {
184:                        if (mirrorFilter.shouldFilterInsert(postInsertEvent)) {
185:                            return;
186:                        }
187:                    }
188:
189:                    try {
190:                        if (log.isDebugEnabled()) {
191:                            log
192:                                    .debug(buildMessage("Creating [" + entity
193:                                            + "]"));
194:                        }
195:                        compassGps
196:                                .executeForMirror(new CompassCallbackWithoutResult() {
197:                                    protected void doInCompassWithoutResult(
198:                                            CompassSession session)
199:                                            throws CompassException {
200:                                        ((InternalCompassSession) session)
201:                                                .getMarshallingStrategy()
202:                                                .marshallIds(entity,
203:                                                        postInsertEvent.getId());
204:                                        session.create(entity);
205:                                    }
206:                                });
207:                    } catch (Exception e) {
208:                        if (isIgnoreMirrorExceptions()) {
209:                            log.error(buildMessage("Failed while creating ["
210:                                    + entity + "]"), e);
211:                        } else {
212:                            throw new HibernateGpsDeviceException(
213:                                    buildMessage("Failed while creating ["
214:                                            + entity + "]"), e);
215:                        }
216:                    }
217:                }
218:            }
219:
220:            private class Hibernate3GpsDevicePostUpdate implements 
221:                    PostUpdateEventListener {
222:                private static final long serialVersionUID = 3833181428363113528L;
223:
224:                private PostUpdateEventListener postUpdateEventListener;
225:
226:                private HibernateMirrorFilter mirrorFilter;
227:
228:                public Hibernate3GpsDevicePostUpdate(
229:                        PostUpdateEventListener postUpdateEventListener,
230:                        HibernateMirrorFilter mirrorFilter) {
231:                    this .postUpdateEventListener = postUpdateEventListener;
232:                    this .mirrorFilter = mirrorFilter;
233:                }
234:
235:                public PostUpdateEventListener getPostUpdateEventListener() {
236:                    return postUpdateEventListener;
237:                }
238:
239:                public void onPostUpdate(final PostUpdateEvent postUpdateEvent) {
240:
241:                    if (this .postUpdateEventListener != null) {
242:                        this .postUpdateEventListener
243:                                .onPostUpdate(postUpdateEvent);
244:                    }
245:
246:                    if (!shouldMirrorDataChanges()
247:                            || isPerformingIndexOperation()) {
248:                        return;
249:                    }
250:
251:                    final Object entity = postUpdateEvent.getEntity();
252:                    if (!compassGps.hasMappingForEntityForMirror(entity
253:                            .getClass(), CascadeMapping.Cascade.SAVE)) {
254:                        return;
255:                    }
256:
257:                    if (mirrorFilter != null) {
258:                        if (mirrorFilter.shouldFilterUpdate(postUpdateEvent)) {
259:                            return;
260:                        }
261:                    }
262:
263:                    try {
264:                        if (log.isDebugEnabled()) {
265:                            log
266:                                    .debug(buildMessage("Updating [" + entity
267:                                            + "]"));
268:                        }
269:                        compassGps
270:                                .executeForMirror(new CompassCallbackWithoutResult() {
271:                                    protected void doInCompassWithoutResult(
272:                                            CompassSession session)
273:                                            throws CompassException {
274:                                        session.save(entity);
275:                                    }
276:                                });
277:                    } catch (Exception e) {
278:                        if (isIgnoreMirrorExceptions()) {
279:                            log.error(buildMessage("Failed while updating ["
280:                                    + entity + "]"), e);
281:                        } else {
282:                            throw new HibernateGpsDeviceException(
283:                                    buildMessage("Failed while updating ["
284:                                            + entity + "]"), e);
285:                        }
286:                    }
287:                }
288:            }
289:
290:            private class Hibernate3GpsDevicePostDelete implements 
291:                    PostDeleteEventListener {
292:
293:                private static final long serialVersionUID = 3258126955726385720L;
294:
295:                private PostDeleteEventListener postDeleteEventListener;
296:
297:                private HibernateMirrorFilter mirrorFilter;
298:
299:                public Hibernate3GpsDevicePostDelete(
300:                        PostDeleteEventListener postDeleteEventListener,
301:                        HibernateMirrorFilter mirrorFilter) {
302:                    this .postDeleteEventListener = postDeleteEventListener;
303:                    this .mirrorFilter = mirrorFilter;
304:                }
305:
306:                public PostDeleteEventListener getPostDeleteEventListener() {
307:                    return postDeleteEventListener;
308:                }
309:
310:                public void onPostDelete(final PostDeleteEvent postDeleteEvent) {
311:
312:                    if (this .postDeleteEventListener != null) {
313:                        this .postDeleteEventListener
314:                                .onPostDelete(postDeleteEvent);
315:                    }
316:
317:                    if (!shouldMirrorDataChanges()
318:                            || isPerformingIndexOperation()) {
319:                        return;
320:                    }
321:
322:                    final Object entity = postDeleteEvent.getEntity();
323:                    if (!compassGps.hasMappingForEntityForMirror(entity
324:                            .getClass(), CascadeMapping.Cascade.DELETE)) {
325:                        return;
326:                    }
327:
328:                    if (mirrorFilter != null) {
329:                        if (mirrorFilter.shouldFilterDelete(postDeleteEvent)) {
330:                            return;
331:                        }
332:                    }
333:
334:                    try {
335:                        if (log.isDebugEnabled()) {
336:                            log
337:                                    .debug(buildMessage("Deleting [" + entity
338:                                            + "]"));
339:                        }
340:                        compassGps
341:                                .executeForMirror(new CompassCallbackWithoutResult() {
342:                                    protected void doInCompassWithoutResult(
343:                                            CompassSession session)
344:                                            throws CompassException {
345:                                        session.delete(entity);
346:                                    }
347:                                });
348:                    } catch (Exception e) {
349:                        if (isIgnoreMirrorExceptions()) {
350:                            log.error(buildMessage("Failed while deleting ["
351:                                    + entity + "]"), e);
352:                        } else {
353:                            throw new HibernateGpsDeviceException(
354:                                    buildMessage("Failed while deleting ["
355:                                            + entity + "]"), e);
356:                        }
357:                    }
358:                }
359:            }
360:
361:            private boolean mirrorDataChanges = true;
362:
363:            private SessionFactory sessionFactory;
364:
365:            private Configuration configuration;
366:
367:            private HibernateMirrorFilter mirrorFilter;
368:
369:            private boolean ignoreMirrorExceptions;
370:
371:            public Hibernate3GpsDevice() {
372:
373:            }
374:
375:            public Hibernate3GpsDevice(String name,
376:                    SessionFactory sessionFactory) {
377:                setName(name);
378:                this .sessionFactory = sessionFactory;
379:            }
380:
381:            public Hibernate3GpsDevice(String name, Configuration configuration) {
382:                setName(name);
383:                this .configuration = configuration;
384:            }
385:
386:            public void setSessionFactory(SessionFactory sessionFactory) {
387:                this .sessionFactory = sessionFactory;
388:            }
389:
390:            public void setConfiguration(Configuration configuration) {
391:                this .configuration = configuration;
392:            }
393:
394:            /**
395:             * Sets a mirroring filter that can filter hibernate mirror events. If no mirror filter is set
396:             * no filtering will happen.
397:             *
398:             * @param mirrorFilter The mirror filter handler
399:             */
400:            public void setMirrorFilter(HibernateMirrorFilter mirrorFilter) {
401:                this .mirrorFilter = mirrorFilter;
402:            }
403:
404:            protected void doStart() throws CompassGpsException {
405:                super .doStart();
406:                if (sessionFactory == null) {
407:                    if (configuration == null) {
408:                        throw new HibernateGpsDeviceException(
409:                                buildMessage("Must set configuration or sessionFactory"));
410:                    }
411:                    try {
412:                        sessionFactory = configuration.buildSessionFactory();
413:                    } catch (HibernateException e) {
414:                        throw new HibernateGpsDeviceException(
415:                                buildMessage("Failed to create session factory"),
416:                                e);
417:                    }
418:                }
419:                if (isMirrorDataChanges()) {
420:                    SessionFactory actualSessionFactory = doGetActualSessionFactory();
421:
422:                    try {
423:                        ClassUtils
424:                                .forName(
425:                                        "org.hibernate.event.SessionEventListenerConfig",
426:                                        compassGps.getMirrorCompass()
427:                                                .getSettings().getClassLoader());
428:                        registerEventsForHibernate30(actualSessionFactory);
429:                    } catch (ClassNotFoundException e) {
430:                        registerEventsForHibernate31(actualSessionFactory);
431:                    }
432:                }
433:            }
434:
435:            protected void doStop() throws CompassGpsException {
436:                if (isMirrorDataChanges()) {
437:                    SessionFactory actualSessionFactory = doGetActualSessionFactory();
438:
439:                    try {
440:                        ClassUtils
441:                                .forName(
442:                                        "org.hibernate.event.SessionEventListenerConfig",
443:                                        compassGps.getMirrorCompass()
444:                                                .getSettings().getClassLoader());
445:                        unregisterEventsForHibernate30(actualSessionFactory);
446:                    } catch (ClassNotFoundException e) {
447:                        unregisterEventsForHibernate31(actualSessionFactory);
448:                    }
449:                }
450:            }
451:
452:            /**
453:             * A helper method that returns the actual session factory for event
454:             * registration. Can be used by subclasses if the
455:             * <code>SessionFactory</code> is proxied.
456:             */
457:            protected SessionFactory doGetActualSessionFactory() {
458:                return this .sessionFactory;
459:            }
460:
461:            public boolean isMirrorDataChanges() {
462:                return mirrorDataChanges;
463:            }
464:
465:            public void setMirrorDataChanges(boolean mirrorDataChanges) {
466:                this .mirrorDataChanges = mirrorDataChanges;
467:            }
468:
469:            /**
470:             * Should exceptions be ignored during the mirroring operations (the Hibernate event listeners).
471:             * Defaults to <code>false</code>.
472:             */
473:            public boolean isIgnoreMirrorExceptions() {
474:                return ignoreMirrorExceptions;
475:            }
476:
477:            /**
478:             * Should exceptions be ignored during the mirroring operations (the Hibernate event listeners).
479:             * Defaults to <code>false</code>.
480:             */
481:            public void setIgnoreMirrorExceptions(boolean ignoreMirrorExceptions) {
482:                this .ignoreMirrorExceptions = ignoreMirrorExceptions;
483:            }
484:
485:            protected HibernateSessionWrapper doGetHibernateSessionWrapper() {
486:                return new Hibernate3SessionWrapper(sessionFactory);
487:            }
488:
489:            protected HibernateEntityInfo[] doGetHibernateEntitiesInfo()
490:                    throws HibernateGpsDeviceException {
491:                ArrayList infos = new ArrayList();
492:                try {
493:                    Map allClassMetaData = sessionFactory.getAllClassMetadata();
494:                    for (Iterator it = allClassMetaData.keySet().iterator(); it
495:                            .hasNext();) {
496:                        String entityname = (String) it.next();
497:                        ClassMetadata classMetadata = (ClassMetadata) allClassMetaData
498:                                .get(entityname);
499:                        // if it is inherited, do not add it to the classes to index, since the "from [entity]"
500:                        // query for the base class will return results for this class as well
501:                        if (isInherited(classMetadata)) {
502:                            String super ClassEntityName = ((AbstractEntityPersister) classMetadata)
503:                                    .getMappedSuperclass();
504:                            ClassMetadata super ClassMetadata = (ClassMetadata) allClassMetaData
505:                                    .get(super ClassEntityName);
506:                            Class super Class = super ClassMetadata
507:                                    .getMappedClass(EntityMode.POJO);
508:                            // only filter out classes that their super class has compass mappings
509:                            if (super Class != null
510:                                    && compassGps
511:                                            .hasMappingForEntityForIndex(super Class)) {
512:                                if (log.isDebugEnabled()) {
513:                                    log
514:                                            .debug(buildMessage("entity ["
515:                                                    + entityname
516:                                                    + "] is inherited and super class ["
517:                                                    + super Class
518:                                                    + "] has compass mapping, filtering it out"));
519:                                }
520:                                continue;
521:                            }
522:                        }
523:                        if (isFilteredForIndex(entityname)) {
524:                            if (log.isDebugEnabled()) {
525:                                log
526:                                        .debug(buildMessage("entity ["
527:                                                + entityname
528:                                                + "] is marked to be filtered, filtering it out"));
529:                            }
530:                            continue;
531:                        }
532:                        if (compassGps
533:                                .hasMappingForEntityForIndex((entityname))) {
534:                            ResourceMapping resourceMapping = compassGps
535:                                    .getMappingForEntityForIndex(entityname);
536:                            HibernateEntityInfo info = new HibernateEntityInfo(
537:                                    entityname, "from " + entityname,
538:                                    resourceMapping.getSubIndexHash()
539:                                            .getSubIndexes());
540:                            infos.add(info);
541:                            if (log.isDebugEnabled()) {
542:                                log.debug(buildMessage("entity [" + entityname
543:                                        + "] will be indexed"));
544:                            }
545:                        } else {
546:                            if (log.isDebugEnabled()) {
547:                                log
548:                                        .debug(buildMessage("entity ["
549:                                                + entityname
550:                                                + "] does not have compass mapping, filtering it out"));
551:                            }
552:                        }
553:                    }
554:                } catch (Exception e) {
555:                    throw new HibernateGpsDeviceException(
556:                            buildMessage("Failed to fetch all class meta data"),
557:                            e);
558:                }
559:
560:                return (HibernateEntityInfo[]) infos
561:                        .toArray(new HibernateEntityInfo[infos.size()]);
562:            }
563:
564:            protected List doGetObjects(HibernateEntityInfo info, int from,
565:                    int count, HibernateSessionWrapper sessionWrapper)
566:                    throws HibernateGpsDeviceException {
567:                List values;
568:                Session session = ((Hibernate3SessionWrapper) sessionWrapper)
569:                        .getSession();
570:                try {
571:                    Query query = doGetQuery(info, session)
572:                            .setFirstResult(from).setMaxResults(count);
573:                    values = query.list();
574:                } catch (Exception e) {
575:                    throw new HibernateGpsDeviceException(
576:                            buildMessage("Failed to open session to fetch data for class ["
577:                                    + info.getName() + "]"), e);
578:                }
579:                return values;
580:            }
581:
582:            /**
583:             * Can be used to override query creation for a certain hibernate entity info.
584:             */
585:            protected Query doGetQuery(HibernateEntityInfo info, Session session) {
586:                return session.createQuery(info.getSelectQuery());
587:            }
588:
589:            private void registerEventsForHibernate30(
590:                    SessionFactory sessionFactory) {
591:                try {
592:                    Class sessionEventListenerConfigClass = ClassUtils.forName(
593:                            "org.hibernate.event.SessionEventListenerConfig",
594:                            compassGps.getMirrorCompass().getSettings()
595:                                    .getClassLoader());
596:
597:                    FieldInvoker sessionFactorySessionEventListenerConfig = new FieldInvoker(
598:                            SessionFactoryImpl.class,
599:                            "sessionEventListenerConfig").prepare();
600:                    Object sessionEventListenerConfig = sessionFactorySessionEventListenerConfig
601:                            .get(sessionFactory);
602:
603:                    FieldInvoker eventListener = new FieldInvoker(
604:                            sessionEventListenerConfigClass,
605:                            "postInsertEventListener").prepare();
606:                    eventListener.set(sessionEventListenerConfig,
607:                            new Hibernate3GpsDevicePostInsert(
608:                                    (PostInsertEventListener) eventListener
609:                                            .get(sessionEventListenerConfig),
610:                                    mirrorFilter));
611:                    eventListener = new FieldInvoker(
612:                            sessionEventListenerConfigClass,
613:                            "postUpdateEventListener").prepare();
614:                    eventListener.set(sessionEventListenerConfig,
615:                            new Hibernate3GpsDevicePostUpdate(
616:                                    (PostUpdateEventListener) eventListener
617:                                            .get(sessionEventListenerConfig),
618:                                    mirrorFilter));
619:                    eventListener = new FieldInvoker(
620:                            sessionEventListenerConfigClass,
621:                            "postDeleteEventListener").prepare();
622:                    eventListener.set(sessionEventListenerConfig,
623:                            new Hibernate3GpsDevicePostDelete(
624:                                    (PostDeleteEventListener) eventListener
625:                                            .get(sessionEventListenerConfig),
626:                                    mirrorFilter));
627:                } catch (Exception e) {
628:                    throw new HibernateGpsDeviceException(
629:                            buildMessage("Failed to inject compass gps device events into hibernate 3.0 session factory ["
630:                                    + sessionFactory.getClass().getName() + "]"),
631:                            e);
632:                }
633:            }
634:
635:            private void unregisterEventsForHibernate30(
636:                    SessionFactory sessionFactory) {
637:                try {
638:                    Class sessionEventListenerConfigClass = ClassUtils.forName(
639:                            "org.hibernate.event.SessionEventListenerConfig",
640:                            compassGps.getMirrorCompass().getSettings()
641:                                    .getClassLoader());
642:
643:                    FieldInvoker sessionFactorySessionEventListenerConfig = new FieldInvoker(
644:                            SessionFactoryImpl.class,
645:                            "sessionEventListenerConfig").prepare();
646:                    Object sessionEventListenerConfig = sessionFactorySessionEventListenerConfig
647:                            .get(sessionFactory);
648:
649:                    FieldInvoker eventListener = new FieldInvoker(
650:                            sessionEventListenerConfigClass,
651:                            "postInsertEventListener").prepare();
652:                    PostInsertEventListener postInsertEventListener = (PostInsertEventListener) eventListener
653:                            .get(sessionEventListenerConfig);
654:                    if (postInsertEventListener instanceof  Hibernate3GpsDevicePostInsert) {
655:                        eventListener
656:                                .set(
657:                                        sessionEventListenerConfig,
658:                                        ((Hibernate3GpsDevicePostInsert) postInsertEventListener)
659:                                                .getPostInsertEventListener());
660:                    }
661:
662:                    eventListener = new FieldInvoker(
663:                            sessionEventListenerConfigClass,
664:                            "postUpdateEventListener").prepare();
665:                    PostUpdateEventListener postUpdateEventListener = (PostUpdateEventListener) eventListener
666:                            .get(sessionEventListenerConfig);
667:                    if (postUpdateEventListener instanceof  Hibernate3GpsDevicePostUpdate) {
668:                        eventListener
669:                                .set(
670:                                        sessionEventListenerConfig,
671:                                        ((Hibernate3GpsDevicePostUpdate) postUpdateEventListener)
672:                                                .getPostUpdateEventListener());
673:                    }
674:
675:                    eventListener = new FieldInvoker(
676:                            sessionEventListenerConfigClass,
677:                            "postDeleteEventListener").prepare();
678:                    PostDeleteEventListener postDeleteEventListener = (PostDeleteEventListener) eventListener
679:                            .get(sessionEventListenerConfig);
680:                    if (postDeleteEventListener instanceof  Hibernate3GpsDevicePostDelete) {
681:                        eventListener
682:                                .set(
683:                                        sessionEventListenerConfig,
684:                                        ((Hibernate3GpsDevicePostDelete) postDeleteEventListener)
685:                                                .getPostDeleteEventListener());
686:                    }
687:                } catch (Exception e) {
688:                    throw new HibernateGpsDeviceException(
689:                            buildMessage("Failed to inject compass gps device events into hibernate 3.0 session factory ["
690:                                    + sessionFactory.getClass().getName() + "]"),
691:                            e);
692:                }
693:            }
694:
695:            private void registerEventsForHibernate31(
696:                    SessionFactory sessionFactory) {
697:                try {
698:                    Class eventListenersClass = ClassUtils.forName(
699:                            "org.hibernate.event.EventListeners", compassGps
700:                                    .getMirrorCompass().getSettings()
701:                                    .getClassLoader());
702:
703:                    Object eventListeners = new FieldInvoker(
704:                            SessionFactoryImpl.class, "eventListeners")
705:                            .prepare().get(sessionFactory);
706:
707:                    FieldInvoker eventListener = new FieldInvoker(
708:                            eventListenersClass, "postInsertEventListeners")
709:                            .prepare();
710:                    PostInsertEventListener[] postInsertEventListener = (PostInsertEventListener[]) eventListener
711:                            .get(eventListeners);
712:                    PostInsertEventListener[] tempPostInsertEventListener = new PostInsertEventListener[postInsertEventListener.length + 1];
713:                    System.arraycopy(postInsertEventListener, 0,
714:                            tempPostInsertEventListener, 0,
715:                            postInsertEventListener.length);
716:                    tempPostInsertEventListener[postInsertEventListener.length] = new Hibernate3GpsDevicePostInsert(
717:                            null, mirrorFilter);
718:                    eventListener.set(eventListeners,
719:                            tempPostInsertEventListener);
720:
721:                    eventListener = new FieldInvoker(eventListenersClass,
722:                            "postUpdateEventListeners").prepare();
723:                    PostUpdateEventListener[] postUpdateEventListener = (PostUpdateEventListener[]) eventListener
724:                            .get(eventListeners);
725:                    PostUpdateEventListener[] tempPostUpdateEventListener = new PostUpdateEventListener[postUpdateEventListener.length + 1];
726:                    System.arraycopy(postUpdateEventListener, 0,
727:                            tempPostUpdateEventListener, 0,
728:                            postUpdateEventListener.length);
729:                    tempPostUpdateEventListener[postUpdateEventListener.length] = new Hibernate3GpsDevicePostUpdate(
730:                            null, mirrorFilter);
731:                    eventListener.set(eventListeners,
732:                            tempPostUpdateEventListener);
733:
734:                    eventListener = new FieldInvoker(eventListenersClass,
735:                            "postDeleteEventListeners").prepare();
736:                    PostDeleteEventListener[] postDeleteEventListener = (PostDeleteEventListener[]) eventListener
737:                            .get(eventListeners);
738:                    PostDeleteEventListener[] tempPostDeleteEventListener = new PostDeleteEventListener[postDeleteEventListener.length + 1];
739:                    System.arraycopy(postDeleteEventListener, 0,
740:                            tempPostDeleteEventListener, 0,
741:                            postDeleteEventListener.length);
742:                    tempPostDeleteEventListener[postDeleteEventListener.length] = new Hibernate3GpsDevicePostDelete(
743:                            null, mirrorFilter);
744:                    eventListener.set(eventListeners,
745:                            tempPostDeleteEventListener);
746:
747:                } catch (Exception e) {
748:                    throw new HibernateGpsDeviceException(
749:                            buildMessage("Failed to inject compass gps device events into hibernate 3.1 session factory ["
750:                                    + sessionFactory.getClass().getName() + "]"),
751:                            e);
752:                }
753:            }
754:
755:            private void unregisterEventsForHibernate31(
756:                    SessionFactory sessionFactory) {
757:                try {
758:                    Class eventListenersClass = ClassUtils.forName(
759:                            "org.hibernate.event.EventListeners", compassGps
760:                                    .getMirrorCompass().getSettings()
761:                                    .getClassLoader());
762:
763:                    Object eventListeners = new FieldInvoker(
764:                            SessionFactoryImpl.class, "eventListeners")
765:                            .prepare().get(sessionFactory);
766:
767:                    FieldInvoker eventListener = new FieldInvoker(
768:                            eventListenersClass, "postInsertEventListeners")
769:                            .prepare();
770:                    PostInsertEventListener[] postInsertEventListener = (PostInsertEventListener[]) eventListener
771:                            .get(eventListeners);
772:                    ArrayList tempPostInsertEventListener = new ArrayList();
773:                    for (int i = 0; i < postInsertEventListener.length; i++) {
774:                        if (!(postInsertEventListener[i] instanceof  Hibernate3GpsDevicePostInsert)) {
775:                            tempPostInsertEventListener
776:                                    .add(postInsertEventListener[i]);
777:                        }
778:                    }
779:                    eventListener
780:                            .set(
781:                                    eventListeners,
782:                                    tempPostInsertEventListener
783:                                            .toArray(new PostInsertEventListener[tempPostInsertEventListener
784:                                                    .size()]));
785:
786:                    eventListener = new FieldInvoker(eventListenersClass,
787:                            "postUpdateEventListeners").prepare();
788:                    PostUpdateEventListener[] postUpdateEventListener = (PostUpdateEventListener[]) eventListener
789:                            .get(eventListeners);
790:                    ArrayList tempPostUpdateEventListener = new ArrayList();
791:                    for (int i = 0; i < postUpdateEventListener.length; i++) {
792:                        if (!(postUpdateEventListener[i] instanceof  Hibernate3GpsDevicePostUpdate)) {
793:                            tempPostUpdateEventListener
794:                                    .add(postUpdateEventListener[i]);
795:                        }
796:                    }
797:                    eventListener
798:                            .set(
799:                                    eventListeners,
800:                                    tempPostUpdateEventListener
801:                                            .toArray(new PostUpdateEventListener[tempPostUpdateEventListener
802:                                                    .size()]));
803:
804:                    eventListener = new FieldInvoker(eventListenersClass,
805:                            "postDeleteEventListeners").prepare();
806:                    PostDeleteEventListener[] postDeleteEventListener = (PostDeleteEventListener[]) eventListener
807:                            .get(eventListeners);
808:                    ArrayList tempPostDeleteEventListener = new ArrayList();
809:                    for (int i = 0; i < postDeleteEventListener.length; i++) {
810:                        if (!(postDeleteEventListener[i] instanceof  Hibernate3GpsDevicePostDelete)) {
811:                            tempPostDeleteEventListener
812:                                    .add(postDeleteEventListener[i]);
813:                        }
814:                    }
815:                    eventListener
816:                            .set(
817:                                    eventListeners,
818:                                    tempPostDeleteEventListener
819:                                            .toArray(new PostDeleteEventListener[tempPostDeleteEventListener
820:                                                    .size()]));
821:
822:                } catch (Exception e) {
823:                    throw new HibernateGpsDeviceException(
824:                            buildMessage("Failed to inject compass gps device events into hibernate 3.1 session factory ["
825:                                    + sessionFactory.getClass().getName() + "]"),
826:                            e);
827:                }
828:            }
829:
830:            protected boolean isInherited(ClassMetadata classMetadata)
831:                    throws HibernateGpsDeviceException {
832:                try {
833:                    // just try and invoke the Hibernate 3.2 support for this one
834:                    return classMetadata.isInherited();
835:                } catch (Throwable t) {
836:                    // do nothing
837:                }
838:                try {
839:                    ClassUtils.forName(
840:                            "org.hibernate.event.SessionEventListenerConfig",
841:                            compassGps.getMirrorCompass().getSettings()
842:                                    .getClassLoader());
843:                    return isInherited30(classMetadata);
844:                } catch (ClassNotFoundException e) {
845:                    return isInherited31(classMetadata);
846:                }
847:            }
848:
849:            private boolean isInherited30(ClassMetadata classMetadata)
850:                    throws HibernateGpsDeviceException {
851:                try {
852:                    Class basicEntityPersisterClass = ClassUtils
853:                            .forName(
854:                                    "org.hibernate.persister.entity.BasicEntityPersister",
855:                                    compassGps.getMirrorCompass().getSettings()
856:                                            .getClassLoader());
857:                    Object entityMetamodel = new FieldInvoker(
858:                            basicEntityPersisterClass, "entityMetamodel")
859:                            .prepare().get(classMetadata);
860:                    MethodInvoker isInheritedMethodInvoker = new MethodInvoker();
861:                    isInheritedMethodInvoker.setTargetObject(entityMetamodel);
862:                    isInheritedMethodInvoker.setTargetMethod("isInherited");
863:                    Boolean isInherited = (Boolean) isInheritedMethodInvoker
864:                            .prepare().invoke();
865:                    return isInherited.booleanValue();
866:                } catch (Exception e) {
867:                    throw new HibernateGpsDeviceException(
868:                            buildMessage("Failed to check for inheritence for 3.0"),
869:                            e);
870:                }
871:            }
872:
873:            private boolean isInherited31(ClassMetadata classMetadata)
874:                    throws HibernateGpsDeviceException {
875:                try {
876:                    Class abstractEntityPersisterClass = ClassUtils
877:                            .forName(
878:                                    "org.hibernate.persister.entity.AbstractEntityPersister",
879:                                    compassGps.getMirrorCompass().getSettings()
880:                                            .getClassLoader());
881:                    Object entityMetamodel = new FieldInvoker(
882:                            abstractEntityPersisterClass, "entityMetamodel")
883:                            .prepare().get(classMetadata);
884:                    MethodInvoker isInheritedMethodInvoker = new MethodInvoker();
885:                    isInheritedMethodInvoker.setTargetObject(entityMetamodel);
886:                    isInheritedMethodInvoker.setTargetMethod("isInherited");
887:                    Boolean isInherited = (Boolean) isInheritedMethodInvoker
888:                            .prepare().invoke();
889:                    return isInherited.booleanValue();
890:                } catch (Exception e) {
891:                    throw new HibernateGpsDeviceException(
892:                            buildMessage("Failed to check for inheritence for 3.1"),
893:                            e);
894:                }
895:            }
896:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.