|
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 |
| |
|
45 |
| |
|
46 |
| |
|
47 |
| |
|
48 |
| |
|
49 |
| |
|
50 |
| public abstract class AbstractHibernateSessionFactory |
|
51 |
| implements HibernateSessionFactory |
|
52 |
| { |
|
53 |
| protected SessionFactory sessionFactory; |
|
54 |
| protected Interceptor interceptor; |
|
55 |
| |
|
56 |
0
| public AbstractHibernateSessionFactory(Logger logger, final FileSystem fs, HibernateConfigurator configurator,
|
|
57 |
| InterceptorFactory interceptorFactory) throws ConfigurationException |
|
58 |
| { |
|
59 |
0
| logger.info("HibernateConfig starting...");
|
|
60 |
0
| org.hibernate.cfg.Configuration cfg = new org.hibernate.cfg.Configuration();
|
|
61 |
0
| cfg.setEntityResolver(new EntityResolver()
|
|
62 |
| { |
|
63 |
| private HashMap<String, String> mapping = new HashMap<String, String>(); |
|
64 |
| { |
|
65 |
0
| mapping.put("-//Hibernate/Hibernate Configuration DTD 3.0//EN",
|
|
66 |
| "hibernate-configuration-3.0.dtd"); |
|
67 |
0
| mapping.put("-//Hibernate/Hibernate Mapping DTD 3.0//EN",
|
|
68 |
| "hibernate-mapping-3.0.dtd"); |
|
69 |
| } |
|
70 |
| |
|
71 |
0
| public InputSource resolveEntity(String publicId, String systemId)
|
|
72 |
| throws SAXException, IOException |
|
73 |
| { |
|
74 |
0
| InputSource is = new InputSource(
|
|
75 |
| fs.getInputStream("/org/hibernate/"+mapping.get(publicId))); |
|
76 |
0
| is.setPublicId(publicId);
|
|
77 |
| |
|
78 |
0
| return is;
|
|
79 |
| } |
|
80 |
| }); |
|
81 |
| |
|
82 |
0
| configurator.configure(cfg);
|
|
83 |
| |
|
84 |
0
| sessionFactory = cfg.buildSessionFactory();
|
|
85 |
| |
|
86 |
0
| if(interceptorFactory != null)
|
|
87 |
| { |
|
88 |
0
| this.interceptor = interceptorFactory.createInterceptor(sessionFactory);
|
|
89 |
| } |
|
90 |
0
| logger.info("HibernateConfig started");
|
|
91 |
| } |
|
92 |
| |
|
93 |
0
| public AbstractHibernateSessionFactory(Logger logger, FileSystem fs,
|
|
94 |
| HibernateConfigurator configurator) |
|
95 |
| throws ConfigurationException |
|
96 |
| { |
|
97 |
0
| this(logger, fs, configurator, null);
|
|
98 |
| } |
|
99 |
| |
|
100 |
| |
|
101 |
| |
|
102 |
| |
|
103 |
| |
|
104 |
| |
|
105 |
0
| public Session openHibernateSession()
|
|
106 |
| { |
|
107 |
0
| if(interceptor != null)
|
|
108 |
| { |
|
109 |
0
| return sessionFactory.openSession(interceptor);
|
|
110 |
| } |
|
111 |
0
| return sessionFactory.openSession();
|
|
112 |
| } |
|
113 |
| |
|
114 |
0
| public void start()
|
|
115 |
| { |
|
116 |
| |
|
117 |
| } |
|
118 |
| |
|
119 |
0
| public void stop()
|
|
120 |
| { |
|
121 |
| |
|
122 |
| } |
|
123 |
| |
|
124 |
| |
|
125 |
| |
|
126 |
| |
|
127 |
| |
|
128 |
| protected interface HibernateConfigurator |
|
129 |
| { |
|
130 |
| |
|
131 |
| |
|
132 |
| |
|
133 |
| |
|
134 |
| |
|
135 |
| |
|
136 |
| |
|
137 |
| public void configure(org.hibernate.cfg.Configuration cfg) throws ConfigurationException; |
|
138 |
| } |
|
139 |
| } |