01: // Copyright 2007 The Apache Software Foundation
02: //
03: // Licensed under the Apache License, Version 2.0 (the "License");
04: // you may not use this file except in compliance with the License.
05: // You may obtain a copy of the License at
06: //
07: // http://www.apache.org/licenses/LICENSE-2.0
08: //
09: // Unless required by applicable law or agreed to in writing, software
10: // distributed under the License is distributed on an "AS IS" BASIS,
11: // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12: // See the License for the specific language governing permissions and
13: // limitations under the License.
14:
15: package org.apache.tapestry.internal.hibernate;
16:
17: import java.util.Collection;
18:
19: import org.apache.commons.logging.Log;
20: import org.apache.tapestry.hibernate.HibernateSessionSource;
21: import org.apache.tapestry.internal.services.ClassNameLocator;
22: import org.apache.tapestry.ioc.annotations.InjectService;
23: import org.hibernate.Session;
24: import org.hibernate.SessionFactory;
25: import org.hibernate.cfg.AnnotationConfiguration;
26:
27: public class HibernateSessionSourceImpl implements
28: HibernateSessionSource {
29: private SessionFactory _sessionFactory;
30:
31: public HibernateSessionSourceImpl(Log log,
32: Collection<String> packageNames,
33:
34: @InjectService("ClassNameLocator")
35: ClassNameLocator classNameLocator) {
36: ClassLoader contextClassLoader = Thread.currentThread()
37: .getContextClassLoader();
38: long startTime = System.currentTimeMillis();
39:
40: AnnotationConfiguration configuration = new AnnotationConfiguration();
41:
42: // Perform normal configuration.
43:
44: configuration.configure();
45:
46: for (String packageName : packageNames) {
47: configuration.addPackage(packageName);
48:
49: for (String className : classNameLocator
50: .locateClassNames(packageName)) {
51: try {
52: Class entityClass = contextClassLoader
53: .loadClass(className);
54:
55: configuration.addAnnotatedClass(entityClass);
56: } catch (ClassNotFoundException ex) {
57: throw new RuntimeException(ex);
58: }
59: }
60: }
61:
62: long configurationComplete = System.currentTimeMillis();
63:
64: _sessionFactory = configuration.buildSessionFactory();
65:
66: long factoryCreated = System.currentTimeMillis();
67:
68: log.info(HibernateMessages.startupTiming(configurationComplete
69: - startTime, factoryCreated - startTime));
70:
71: log.info(HibernateMessages.entityCatalog(_sessionFactory
72: .getAllClassMetadata().keySet()));
73:
74: }
75:
76: public Session create() {
77: return _sessionFactory.openSession();
78: }
79:
80: public SessionFactory getSessionFactory() {
81: return _sessionFactory;
82: }
83: }
|