comparison flys-backend/src/main/java/de/intevation/flys/backend/SessionFactoryProvider.java @ 3471:e4250c6e1538 2.8.1

merged flys-backend/2.8.1
author Thomas Arendsen Hein <thomas@intevation.de>
date Fri, 28 Sep 2012 12:14:40 +0200
parents 69f06b83b3ec
children fb9dcc68b9c2
comparison
equal deleted inserted replaced
3468:f37e7e8907cb 3471:e4250c6e1538
1 package de.intevation.flys.backend;
2
3 import java.lang.management.ManagementFactory;
4
5 import java.util.Properties;
6
7 import javax.management.InstanceAlreadyExistsException;
8 import javax.management.MBeanRegistrationException;
9 import javax.management.MBeanServer;
10 import javax.management.MalformedObjectNameException;
11 import javax.management.NotCompliantMBeanException;
12 import javax.management.ObjectName;
13
14 import org.apache.log4j.Logger;
15
16 import org.hibernate.SessionFactory;
17
18 import org.hibernate.cfg.Configuration;
19 import org.hibernate.cfg.Environment;
20
21 import org.hibernate.impl.SessionFactoryImpl;
22
23 import org.hibernate.jmx.StatisticsService;
24
25 public final class SessionFactoryProvider
26 {
27 private static Logger log = Logger.getLogger(SessionFactoryProvider.class);
28
29 public static final boolean ENABLE_JMX =
30 Boolean.getBoolean("flys.backend.enablejmx");
31
32 private static SessionFactory flysSessionFactory;
33 private static SessionFactory sedDBSessionFactory;
34
35 private SessionFactoryProvider() {
36 }
37
38 public static synchronized SessionFactory getSessionFactory() {
39 if (flysSessionFactory == null) {
40 flysSessionFactory =
41 createSessionFactory(FLYSCredentials.getInstance());
42 }
43 return flysSessionFactory;
44 }
45
46 public static SessionFactory createSessionFactory() {
47 return createSessionFactory(FLYSCredentials.getDefault());
48 }
49
50 public static synchronized SessionFactory getSedDBSessionFactory() {
51 if (sedDBSessionFactory == null) {
52 sedDBSessionFactory =
53 createSessionFactory(SedDBCredentials.getInstance());
54 }
55 return sedDBSessionFactory;
56 }
57
58 public static SessionFactory createSedDBSessionFactory() {
59 return createSessionFactory(SedDBCredentials.getDefault());
60 }
61
62 public static SessionFactory createSessionFactory(
63 Credentials credentials
64 ) {
65 Configuration cfg = createConfiguration(credentials);
66
67 SessionFactory factory = cfg.buildSessionFactory();
68
69 if (ENABLE_JMX) {
70 registerAsMBean(factory);
71 }
72 else {
73 log.info("No JMX support for hibernate.");
74 }
75
76 return factory;
77 }
78
79 public static void registerAsMBean(SessionFactory factory) {
80
81 StatisticsService statsMBean = new StatisticsService();
82 statsMBean.setSessionFactory(factory);
83 statsMBean.setStatisticsEnabled(true);
84
85 try {
86 MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
87 mbs.registerMBean(
88 statsMBean,
89 new ObjectName("Hibernate:application=Statistics"));
90
91 log.info("Enabled JMX support for hibernate.");
92 }
93 catch (MalformedObjectNameException mone) {
94 log.warn(mone, mone);
95 }
96 catch (InstanceAlreadyExistsException iaee) {
97 log.warn(iaee, iaee);
98 }
99 catch (MBeanRegistrationException mbre) {
100 log.warn(mbre, mbre);
101 }
102 catch (NotCompliantMBeanException ncmbe) {
103 log.warn(ncmbe, ncmbe);
104 }
105 }
106
107 public static Configuration createConfiguration() {
108 return createConfiguration(FLYSCredentials.getInstance());
109 }
110
111 public static Configuration createConfiguration(
112 Credentials credentials
113 ) {
114 Configuration cfg = new Configuration();
115
116 for (Class clazz: credentials.getClasses()) {
117 cfg.addAnnotatedClass(clazz);
118 }
119
120 if (log.isDebugEnabled()) {
121 log.debug("user: " + credentials.getUser());
122 log.debug("dialect: " + credentials.getDialect());
123 log.debug("driver: " + credentials.getDriver());
124 log.debug("url: " + credentials.getUrl());
125 }
126
127 Properties props = new Properties();
128
129 // We rely on our own connection pool
130 props.setProperty(
131 "hibernate.connection.provider_class",
132 "de.intevation.flys.utils.DBCPConnectionProvider");
133
134 props.setProperty(Environment.DIALECT, credentials.getDialect());
135 props.setProperty(Environment.USER, credentials.getUser());
136 props.setProperty(Environment.PASS, credentials.getPassword());
137 props.setProperty(Environment.DRIVER, credentials.getDriver());
138 props.setProperty(Environment.URL, credentials.getUrl());
139
140 cfg.mergeProperties(props);
141
142 return cfg;
143 }
144
145
146 public static String getProperty(SessionFactoryImpl factory, String key) {
147 Properties props = factory.getProperties();
148 return props.getProperty(key);
149 }
150
151 public static String getUser(SessionFactoryImpl factory) {
152 return getProperty(factory, Environment.USER);
153 }
154
155
156 public static String getPass(SessionFactoryImpl factory) {
157 return getProperty(factory, Environment.PASS);
158 }
159
160
161 public static String getURL(SessionFactoryImpl factory) {
162 return getProperty(factory, Environment.URL);
163 }
164
165
166 public static String getDriver(SessionFactoryImpl factory) {
167 return getProperty(factory, Environment.DRIVER);
168 }
169 }
170 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org