Mercurial > dive4elements > river
comparison flys-backend/src/main/java/de/intevation/flys/backend/SessionFactoryProvider.java @ 2386:cb7ebcadb214 2.6
merged flys-backend/2.6
author | Thomas Arendsen Hein <thomas@intevation.de> |
---|---|
date | Fri, 28 Sep 2012 12:14:23 +0200 |
parents | efe19e669092 |
children | 501f00e355eb |
comparison
equal
deleted
inserted
replaced
2333:66946e278e66 | 2386:cb7ebcadb214 |
---|---|
1 package de.intevation.flys.backend; | |
2 | |
3 import de.intevation.artifacts.common.utils.Config; | |
4 | |
5 import java.lang.management.ManagementFactory; | |
6 import java.util.Properties; | |
7 | |
8 import javax.management.InstanceAlreadyExistsException; | |
9 import javax.management.MalformedObjectNameException; | |
10 import javax.management.MBeanRegistrationException; | |
11 import javax.management.MBeanServer; | |
12 import javax.management.NotCompliantMBeanException; | |
13 import javax.management.ObjectName; | |
14 | |
15 import org.hibernate.SessionFactory; | |
16 | |
17 import org.hibernate.impl.SessionFactoryImpl; | |
18 | |
19 import org.hibernate.jmx.StatisticsService; | |
20 | |
21 import org.hibernate.cfg.Configuration; | |
22 import org.hibernate.cfg.Environment; | |
23 | |
24 import de.intevation.flys.model.Annotation; | |
25 import de.intevation.flys.model.AnnotationType; | |
26 import de.intevation.flys.model.Attribute; | |
27 import de.intevation.flys.model.Building; | |
28 import de.intevation.flys.model.Catchment; | |
29 import de.intevation.flys.model.CrossSection; | |
30 import de.intevation.flys.model.CrossSectionLine; | |
31 import de.intevation.flys.model.CrossSectionPoint; | |
32 import de.intevation.flys.model.CrossSectionTrack; | |
33 import de.intevation.flys.model.DGM; | |
34 import de.intevation.flys.model.DischargeTable; | |
35 import de.intevation.flys.model.DischargeTableValue; | |
36 import de.intevation.flys.model.Edge; | |
37 import de.intevation.flys.model.Fixpoint; | |
38 import de.intevation.flys.model.Floodplain; | |
39 import de.intevation.flys.model.Gauge; | |
40 import de.intevation.flys.model.Hws; | |
41 import de.intevation.flys.model.HYK; | |
42 import de.intevation.flys.model.HYKEntry; | |
43 import de.intevation.flys.model.HYKFormation; | |
44 import de.intevation.flys.model.HYKFlowZoneType; | |
45 import de.intevation.flys.model.HYKFlowZone; | |
46 import de.intevation.flys.model.Line; | |
47 import de.intevation.flys.model.MainValueType; | |
48 import de.intevation.flys.model.NamedMainValue; | |
49 import de.intevation.flys.model.MainValue; | |
50 import de.intevation.flys.model.Position; | |
51 import de.intevation.flys.model.Range; | |
52 import de.intevation.flys.model.River; | |
53 import de.intevation.flys.model.RiverAxis; | |
54 import de.intevation.flys.model.RiverAxisKm; | |
55 import de.intevation.flys.model.TimeInterval; | |
56 import de.intevation.flys.model.Unit; | |
57 import de.intevation.flys.model.WstColumn; | |
58 import de.intevation.flys.model.WstColumnQRange; | |
59 import de.intevation.flys.model.WstColumnValue; | |
60 import de.intevation.flys.model.Wst; | |
61 import de.intevation.flys.model.WstQRange; | |
62 | |
63 import org.apache.log4j.Logger; | |
64 | |
65 public final class SessionFactoryProvider | |
66 { | |
67 private static Logger log = Logger.getLogger(SessionFactoryProvider.class); | |
68 | |
69 public static final String XPATH_USER = | |
70 "/artifact-database/backend-database/user/text()"; | |
71 | |
72 public static final String XPATH_PASSWORD = | |
73 "/artifact-database/backend-database/password/text()"; | |
74 | |
75 public static final String XPATH_DIALECT = | |
76 "/artifact-database/backend-database/dialect/text()"; | |
77 | |
78 public static final String XPATH_DRIVER = | |
79 "/artifact-database/backend-database/driver/text()"; | |
80 | |
81 public static final String XPATH_URL = | |
82 "/artifact-database/backend-database/url/text()"; | |
83 | |
84 public static final String DEFAULT_USER = | |
85 System.getProperty("flys.backend.user", "flys"); | |
86 | |
87 public static final String DEFAULT_PASSWORD = | |
88 System.getProperty("flys.backend.password", "flys"); | |
89 | |
90 public static final String DEFAULT_DIALECT = | |
91 System.getProperty( | |
92 "flys.backend.dialect", | |
93 "org.hibernate.dialect.PostgreSQLDialect"); | |
94 | |
95 public static final String DEFAULT_DRIVER = | |
96 System.getProperty( | |
97 "flys.backend.driver", | |
98 "org.postgresql.Driver"); | |
99 | |
100 public static final String DEFAULT_URL = | |
101 System.getProperty( | |
102 "flys.backend.url", | |
103 "jdbc:postgresql://localhost:5432/flys"); | |
104 | |
105 public static final boolean ENABLE_JMX = | |
106 Boolean.getBoolean("flys.backend.enablejmx"); | |
107 | |
108 private static SessionFactory sessionFactory; | |
109 | |
110 private SessionFactoryProvider() { | |
111 } | |
112 | |
113 public static synchronized SessionFactory getSessionFactory() { | |
114 if (sessionFactory == null) { | |
115 String user = | |
116 Config.getStringXPath(XPATH_USER, DEFAULT_USER); | |
117 String password = | |
118 Config.getStringXPath(XPATH_PASSWORD, DEFAULT_PASSWORD); | |
119 String dialect = | |
120 Config.getStringXPath(XPATH_DIALECT, DEFAULT_DIALECT); | |
121 String driver = | |
122 Config.getStringXPath(XPATH_DRIVER, DEFAULT_DRIVER); | |
123 String url = | |
124 Config.getStringXPath(XPATH_URL, DEFAULT_URL); | |
125 | |
126 sessionFactory = createSessionFactory( | |
127 user, password, dialect, driver, url); | |
128 } | |
129 return sessionFactory; | |
130 } | |
131 | |
132 public static SessionFactory createSessionFactory() { | |
133 return createSessionFactory( | |
134 DEFAULT_USER, | |
135 DEFAULT_PASSWORD, | |
136 DEFAULT_DIALECT, | |
137 DEFAULT_DRIVER, | |
138 DEFAULT_URL); | |
139 } | |
140 | |
141 public static SessionFactory createSessionFactory( | |
142 String user, | |
143 String password, | |
144 String dialect, | |
145 String driver, | |
146 String url | |
147 ) { | |
148 Configuration cfg = createConfiguration( | |
149 user, password, dialect, driver, url); | |
150 | |
151 SessionFactory factory = cfg.buildSessionFactory(); | |
152 | |
153 if (ENABLE_JMX) { | |
154 registerAsMBean(factory); | |
155 } | |
156 else { | |
157 log.info("No JMX support for hibernate."); | |
158 } | |
159 | |
160 return factory; | |
161 } | |
162 | |
163 | |
164 public static void registerAsMBean(SessionFactory factory) { | |
165 | |
166 StatisticsService statsMBean = new StatisticsService(); | |
167 statsMBean.setSessionFactory(factory); | |
168 statsMBean.setStatisticsEnabled(true); | |
169 | |
170 try { | |
171 MBeanServer mbs = ManagementFactory.getPlatformMBeanServer(); | |
172 mbs.registerMBean( | |
173 statsMBean, | |
174 new ObjectName("Hibernate:application=Statistics")); | |
175 | |
176 log.info("Enabled JMX support for hibernate."); | |
177 } | |
178 catch (MalformedObjectNameException mone) { | |
179 log.warn(mone, mone); | |
180 } | |
181 catch (InstanceAlreadyExistsException iaee) { | |
182 log.warn(iaee, iaee); | |
183 } | |
184 catch (MBeanRegistrationException mbre) { | |
185 log.warn(mbre, mbre); | |
186 } | |
187 catch (NotCompliantMBeanException ncmbe) { | |
188 log.warn(ncmbe, ncmbe); | |
189 } | |
190 } | |
191 | |
192 | |
193 public static Configuration createConfiguration() { | |
194 return createConfiguration( | |
195 DEFAULT_USER, | |
196 DEFAULT_PASSWORD, | |
197 DEFAULT_DIALECT, | |
198 DEFAULT_DRIVER, | |
199 DEFAULT_URL); | |
200 } | |
201 | |
202 public static Configuration createConfiguration( | |
203 String user, | |
204 String password, | |
205 String dialect, | |
206 String driver, | |
207 String url | |
208 ) { | |
209 Configuration cfg = new Configuration(); | |
210 | |
211 // TODO: Use package reflection here. | |
212 cfg.addAnnotatedClass(Annotation.class); | |
213 cfg.addAnnotatedClass(AnnotationType.class); | |
214 cfg.addAnnotatedClass(Attribute.class); | |
215 cfg.addAnnotatedClass(Building.class); | |
216 cfg.addAnnotatedClass(Catchment.class); | |
217 cfg.addAnnotatedClass(CrossSection.class); | |
218 cfg.addAnnotatedClass(CrossSectionLine.class); | |
219 cfg.addAnnotatedClass(CrossSectionPoint.class); | |
220 cfg.addAnnotatedClass(CrossSectionTrack.class); | |
221 cfg.addAnnotatedClass(DGM.class); | |
222 cfg.addAnnotatedClass(DischargeTable.class); | |
223 cfg.addAnnotatedClass(DischargeTableValue.class); | |
224 cfg.addAnnotatedClass(Edge.class); | |
225 cfg.addAnnotatedClass(Fixpoint.class); | |
226 cfg.addAnnotatedClass(Floodplain.class); | |
227 cfg.addAnnotatedClass(Gauge.class); | |
228 cfg.addAnnotatedClass(Hws.class); | |
229 cfg.addAnnotatedClass(HYK.class); | |
230 cfg.addAnnotatedClass(HYKEntry.class); | |
231 cfg.addAnnotatedClass(HYKFormation.class); | |
232 cfg.addAnnotatedClass(HYKFlowZoneType.class); | |
233 cfg.addAnnotatedClass(HYKFlowZone.class); | |
234 cfg.addAnnotatedClass(Line.class); | |
235 cfg.addAnnotatedClass(MainValueType.class); | |
236 cfg.addAnnotatedClass(NamedMainValue.class); | |
237 cfg.addAnnotatedClass(MainValue.class); | |
238 cfg.addAnnotatedClass(Position.class); | |
239 cfg.addAnnotatedClass(Range.class); | |
240 cfg.addAnnotatedClass(River.class); | |
241 cfg.addAnnotatedClass(RiverAxis.class); | |
242 cfg.addAnnotatedClass(RiverAxisKm.class); | |
243 cfg.addAnnotatedClass(TimeInterval.class); | |
244 cfg.addAnnotatedClass(Unit.class); | |
245 cfg.addAnnotatedClass(WstColumn.class); | |
246 cfg.addAnnotatedClass(WstColumnQRange.class); | |
247 cfg.addAnnotatedClass(WstColumnValue.class); | |
248 cfg.addAnnotatedClass(Wst.class); | |
249 cfg.addAnnotatedClass(WstQRange.class); | |
250 | |
251 if (log.isDebugEnabled()) { | |
252 log.debug("user: " + user); | |
253 log.debug("dialect: " + dialect); | |
254 log.debug("driver: " + driver); | |
255 log.debug("url: " + url); | |
256 } | |
257 | |
258 Properties props = new Properties(); | |
259 | |
260 // We rely on our own connection pool | |
261 props.setProperty( | |
262 "hibernate.connection.provider_class", | |
263 "org.hibernate.connection.DBCPConnectionProvider"); | |
264 | |
265 props.setProperty(Environment.DIALECT, dialect); | |
266 props.setProperty(Environment.USER, user); | |
267 props.setProperty(Environment.PASS, password); | |
268 props.setProperty(Environment.DRIVER, driver); | |
269 props.setProperty(Environment.URL, url); | |
270 | |
271 cfg.mergeProperties(props); | |
272 | |
273 return cfg; | |
274 } | |
275 | |
276 | |
277 public static String getProperty(SessionFactoryImpl factory, String key) { | |
278 Properties props = factory.getProperties(); | |
279 return props.getProperty(key); | |
280 } | |
281 | |
282 public static String getUser(SessionFactoryImpl factory) { | |
283 return getProperty(factory, Environment.USER); | |
284 } | |
285 | |
286 | |
287 public static String getPass(SessionFactoryImpl factory) { | |
288 return getProperty(factory, Environment.PASS); | |
289 } | |
290 | |
291 | |
292 public static String getURL(SessionFactoryImpl factory) { | |
293 return getProperty(factory, Environment.URL); | |
294 } | |
295 | |
296 | |
297 public static String getDriver(SessionFactoryImpl factory) { | |
298 return getProperty(factory, Environment.DRIVER); | |
299 } | |
300 } | |
301 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |