1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 package org.objectledge.hibernate;
29
30 import java.io.IOException;
31 import java.util.HashMap;
32
33 import org.hibernate.Interceptor;
34 import org.hibernate.Session;
35 import org.hibernate.SessionFactory;
36 import org.jcontainer.dna.ConfigurationException;
37 import org.jcontainer.dna.Logger;
38 import org.objectledge.filesystem.FileSystem;
39 import org.xml.sax.EntityResolver;
40 import org.xml.sax.InputSource;
41 import org.xml.sax.SAXException;
42
43 /***
44 * The hibernate session factory component.
45 *
46 * @author <a href="mailto:mgolebsk@elka.pw.edu.pl">Marcin Golebski</a>
47 * @author <a href="mailto:dgajda@caltha.pl">Damian Gajda</a>
48 * @version $Id: AbstractHibernateSessionFactory.java,v 1.2 2006/03/13 18:22:50 zwierzem Exp $
49 */
50 public abstract class AbstractHibernateSessionFactory
51 implements HibernateSessionFactory
52 {
53 protected SessionFactory sessionFactory;
54 protected Interceptor interceptor;
55
56 public AbstractHibernateSessionFactory(Logger logger, final FileSystem fs, HibernateConfigurator configurator,
57 InterceptorFactory interceptorFactory) throws ConfigurationException
58 {
59 logger.info("HibernateConfig starting...");
60 org.hibernate.cfg.Configuration cfg = new org.hibernate.cfg.Configuration();
61 cfg.setEntityResolver(new EntityResolver()
62 {
63 private HashMap<String, String> mapping = new HashMap<String, String>();
64 {
65 mapping.put("-//Hibernate/Hibernate Configuration DTD 3.0//EN",
66 "hibernate-configuration-3.0.dtd");
67 mapping.put("-//Hibernate/Hibernate Mapping DTD 3.0//EN",
68 "hibernate-mapping-3.0.dtd");
69 }
70
71 public InputSource resolveEntity(String publicId, String systemId)
72 throws SAXException, IOException
73 {
74 InputSource is = new InputSource(
75 fs.getInputStream("/org/hibernate/"+mapping.get(publicId)));
76 is.setPublicId(publicId);
77
78 return is;
79 }
80 });
81
82 configurator.configure(cfg);
83
84 sessionFactory = cfg.buildSessionFactory();
85
86 if(interceptorFactory != null)
87 {
88 this.interceptor = interceptorFactory.createInterceptor(sessionFactory);
89 }
90 logger.info("HibernateConfig started");
91 }
92
93 public AbstractHibernateSessionFactory(Logger logger, FileSystem fs,
94 HibernateConfigurator configurator)
95 throws ConfigurationException
96 {
97 this(logger, fs, configurator, null);
98 }
99
100 /***
101 * Opens the hibernate session.
102 *
103 * @return the newly open hibernate session.
104 */
105 public Session openHibernateSession()
106 {
107 if(interceptor != null)
108 {
109 return sessionFactory.openSession(interceptor);
110 }
111 return sessionFactory.openSession();
112 }
113
114 public void start()
115 {
116
117 }
118
119 public void stop()
120 {
121
122 }
123
124 /***
125 * Configurator configures the hibernate session factory, to be implemented by concrete
126 * implementations.
127 */
128 protected interface HibernateConfigurator
129 {
130 /***
131 * Provides the configuration data and calls one of <code>configure()</code> methods on
132 * hibernate configuration object.
133 *
134 * @param cfg the hibernate configuration object.
135 * @throws ConfigurationException thrown on configuration problems.
136 */
137 public void configure(org.hibernate.cfg.Configuration cfg) throws ConfigurationException;
138 }
139 }