Source Code Cross Referenced for Hibernate2GpsDevice.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 net.sf.hibernate.HibernateException;
025:        import net.sf.hibernate.Query;
026:        import net.sf.hibernate.Session;
027:        import net.sf.hibernate.SessionFactory;
028:        import net.sf.hibernate.Transaction;
029:        import net.sf.hibernate.cfg.Configuration;
030:        import org.compass.core.mapping.ResourceMapping;
031:        import org.compass.gps.CompassGpsException;
032:        import org.compass.gps.device.hibernate.HibernateGpsDeviceException;
033:
034:        /**
035:         * A {@link org.compass.gps.device.hibernate.HibernateGpsDevice} which works with hibernate 2.
036:         * <p/>
037:         * You must either set the Hibernate <code>Configuration</code> or the
038:         * <code>SessionFactory</code> to be used by the device. Note that if the
039:         * <code>Configuration</code> is supplied, when the device <code>start</code>
040:         * is called, a new <code>SessionFactory</code> will be built.
041:         * <p/>
042:         * Note: Only provides the <code>index()</code> operation. No real time index
043:         * updated are performed since the Hiberante <code>Interceptor</code> provides
044:         * null values for newly created objects with generated ids.
045:         *
046:         * @author kimchy
047:         */
048:        public class Hibernate2GpsDevice extends AbstractHibernateGpsDevice {
049:
050:            private SessionFactory sessionFactory;
051:
052:            private Configuration configuration;
053:
054:            private class Hibernate2SessionWrapper implements 
055:                    HibernateSessionWrapper {
056:
057:                private SessionFactory sessionFactory;
058:
059:                private Session session;
060:
061:                private Transaction tr;
062:
063:                public Hibernate2SessionWrapper(SessionFactory sessionFactory) {
064:                    this .sessionFactory = sessionFactory;
065:                }
066:
067:                public Session getSession() {
068:                    return session;
069:                }
070:
071:                public void open() throws HibernateGpsDeviceException {
072:                    try {
073:                        session = sessionFactory.openSession();
074:                    } catch (HibernateException e) {
075:                        throw new HibernateGpsDeviceException(
076:                                buildMessage("Failed to open session to fetch data"),
077:                                e);
078:                    }
079:                    try {
080:                        tr = session.beginTransaction();
081:                    } catch (HibernateException e) {
082:                        throw new HibernateGpsDeviceException(
083:                                buildMessage("Failed to begin transaction to fetch data"),
084:                                e);
085:                    }
086:                }
087:
088:                public void close() {
089:                    if (tr != null) {
090:                        try {
091:                            tr.commit();
092:                        } catch (HibernateException e1) {
093:                            log
094:                                    .error(
095:                                            buildMessage("Failed to rollback hibernate transaction"),
096:                                            e1);
097:                        }
098:                    }
099:                    try {
100:                        session.close();
101:                    } catch (HibernateException e) {
102:                        log
103:                                .error(
104:                                        buildMessage("Failed to close Hibernate session"),
105:                                        e);
106:                    }
107:                }
108:
109:                public void closeOnError() {
110:                    if (tr != null) {
111:                        try {
112:                            tr.rollback();
113:                        } catch (HibernateException e1) {
114:                            log
115:                                    .error(
116:                                            buildMessage("Failed to rollback hibernate transaction"),
117:                                            e1);
118:                        }
119:                    }
120:                    try {
121:                        session.close();
122:                    } catch (HibernateException e) {
123:                        log
124:                                .error(
125:                                        buildMessage("Failed to close Hibernate session"),
126:                                        e);
127:                    }
128:                }
129:            }
130:
131:            public Hibernate2GpsDevice() {
132:
133:            }
134:
135:            public Hibernate2GpsDevice(String name,
136:                    SessionFactory sessionFactory) {
137:                setName(name);
138:                this .sessionFactory = sessionFactory;
139:            }
140:
141:            public Hibernate2GpsDevice(String name, Configuration configuration) {
142:                setName(name);
143:                this .configuration = configuration;
144:            }
145:
146:            public void setSessionFactory(SessionFactory sessionFactory) {
147:                this .sessionFactory = sessionFactory;
148:            }
149:
150:            public void setConfiguration(Configuration configuration) {
151:                this .configuration = configuration;
152:            }
153:
154:            protected void doStart() throws CompassGpsException {
155:                super .doStart();
156:                if (sessionFactory == null) {
157:                    if (configuration == null) {
158:                        throw new HibernateGpsDeviceException(
159:                                buildMessage("Must set configuration or sessionFactory"));
160:                    }
161:                    try {
162:                        sessionFactory = configuration.buildSessionFactory();
163:                    } catch (HibernateException e) {
164:                        throw new HibernateGpsDeviceException(
165:                                buildMessage("Failed to create session factory"),
166:                                e);
167:                    }
168:                }
169:            }
170:
171:            protected void doStop() throws CompassGpsException {
172:            }
173:
174:            protected HibernateSessionWrapper doGetHibernateSessionWrapper() {
175:                return new Hibernate2SessionWrapper(sessionFactory);
176:            }
177:
178:            protected HibernateEntityInfo[] doGetHibernateEntitiesInfo()
179:                    throws HibernateGpsDeviceException {
180:                ArrayList infos = new ArrayList();
181:                try {
182:                    Map allClassMetaData = sessionFactory.getAllClassMetadata();
183:                    for (Iterator it = allClassMetaData.keySet().iterator(); it
184:                            .hasNext();) {
185:                        Class clazz = (Class) it.next();
186:                        if (isFilteredForIndex(clazz.getName())) {
187:                            continue;
188:                        }
189:                        if (compassGps.hasMappingForEntityForIndex(clazz)) {
190:                            ResourceMapping resourceMapping = compassGps
191:                                    .getMappingForEntityForIndex(clazz);
192:                            HibernateEntityInfo info = new HibernateEntityInfo(
193:                                    clazz.getName(), "from " + clazz.getName(),
194:                                    resourceMapping.getSubIndexHash()
195:                                            .getSubIndexes());
196:                            infos.add(info);
197:                        }
198:                    }
199:                } catch (Exception e) {
200:                    throw new HibernateGpsDeviceException(
201:                            buildMessage("Failed to fetch all class meta data"),
202:                            e);
203:                }
204:
205:                return (HibernateEntityInfo[]) infos
206:                        .toArray(new HibernateEntityInfo[infos.size()]);
207:            }
208:
209:            protected List doGetObjects(HibernateEntityInfo info, int from,
210:                    int count, HibernateSessionWrapper sessionWrapper)
211:                    throws HibernateGpsDeviceException {
212:                List values;
213:                Session session = ((Hibernate2SessionWrapper) sessionWrapper)
214:                        .getSession();
215:                try {
216:                    Query query = session.createQuery(info.getSelectQuery())
217:                            .setFirstResult(from).setMaxResults(count);
218:                    values = query.list();
219:                } catch (Exception e) {
220:                    throw new HibernateGpsDeviceException(
221:                            buildMessage("Failed to open session to fetch data for class ["
222:                                    + info.getName() + "]"), e);
223:                }
224:                return values;
225:            }
226:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.