Mercurial > dive4elements > river
comparison flys-backend/src/main/java/org/dive4elements/river/backend/SessionFactoryProvider.java @ 5828:dfb26b03b179
Moved directories to org.dive4elements.river
author | Sascha L. Teichmann <teichmann@intevation.de> |
---|---|
date | Thu, 25 Apr 2013 11:53:11 +0200 |
parents | flys-backend/src/main/java/de/intevation/flys/backend/SessionFactoryProvider.java@a2da78fdbec0 |
children | 18619c1e7c2a |
comparison
equal
deleted
inserted
replaced
5827:e308d4ecd35a | 5828:dfb26b03b179 |
---|---|
1 package de.intevation.flys.backend; | |
2 | |
3 import java.util.Properties; | |
4 | |
5 import org.apache.log4j.Logger; | |
6 | |
7 import org.hibernate.SessionFactory; | |
8 | |
9 import org.hibernate.cfg.Configuration; | |
10 import org.hibernate.cfg.Environment; | |
11 | |
12 import org.hibernate.impl.SessionFactoryImpl; | |
13 | |
14 public final class SessionFactoryProvider | |
15 { | |
16 private static Logger log = Logger.getLogger(SessionFactoryProvider.class); | |
17 | |
18 //public static final boolean ENABLE_JMX = | |
19 // Boolean.getBoolean("flys.backend.enablejmx"); | |
20 | |
21 private static SessionFactory flysSessionFactory; | |
22 private static SessionFactory sedDBSessionFactory; | |
23 | |
24 private SessionFactoryProvider() { | |
25 } | |
26 | |
27 public static synchronized SessionFactory getSessionFactory() { | |
28 if (flysSessionFactory == null) { | |
29 flysSessionFactory = | |
30 createSessionFactory(FLYSCredentials.getInstance()); | |
31 } | |
32 return flysSessionFactory; | |
33 } | |
34 | |
35 public static SessionFactory createSessionFactory() { | |
36 return createSessionFactory(FLYSCredentials.getDefault()); | |
37 } | |
38 | |
39 public static synchronized SessionFactory getSedDBSessionFactory() { | |
40 if (sedDBSessionFactory == null) { | |
41 sedDBSessionFactory = | |
42 createSessionFactory(SedDBCredentials.getInstance()); | |
43 } | |
44 return sedDBSessionFactory; | |
45 } | |
46 | |
47 public static SessionFactory createSedDBSessionFactory() { | |
48 return createSessionFactory(SedDBCredentials.getDefault()); | |
49 } | |
50 | |
51 public static SessionFactory createSessionFactory( | |
52 Credentials credentials | |
53 ) { | |
54 Configuration cfg = createConfiguration(credentials); | |
55 | |
56 SessionFactory factory = cfg.buildSessionFactory(); | |
57 | |
58 /* | |
59 if (ENABLE_JMX) { | |
60 registerAsMBean(factory); | |
61 } | |
62 else { | |
63 log.info("No JMX support for hibernate."); | |
64 } | |
65 */ | |
66 | |
67 return factory; | |
68 } | |
69 | |
70 /** XXX: Commented out till it is configured correctly. | |
71 public static void registerAsMBean(SessionFactory factory) { | |
72 | |
73 // | |
74 | |
75 StatisticsService statsMBean = new StatisticsService(); | |
76 statsMBean.setSessionFactory(factory); | |
77 statsMBean.setStatisticsEnabled(true); | |
78 | |
79 try { | |
80 MBeanServer mbs = ManagementFactory.getPlatformMBeanServer(); | |
81 mbs.registerMBean( | |
82 statsMBean, | |
83 new ObjectName("Hibernate:application=Statistics")); | |
84 | |
85 log.info("Enabled JMX support for hibernate."); | |
86 } | |
87 catch (MalformedObjectNameException mone) { | |
88 log.warn(mone, mone); | |
89 } | |
90 catch (InstanceAlreadyExistsException iaee) { | |
91 log.warn(iaee, iaee); | |
92 } | |
93 catch (MBeanRegistrationException mbre) { | |
94 log.warn(mbre, mbre); | |
95 } | |
96 catch (NotCompliantMBeanException ncmbe) { | |
97 log.warn(ncmbe, ncmbe); | |
98 } | |
99 } | |
100 */ | |
101 | |
102 public static Configuration createConfiguration() { | |
103 return createConfiguration(FLYSCredentials.getInstance()); | |
104 } | |
105 | |
106 public static Configuration createConfiguration( | |
107 Credentials credentials | |
108 ) { | |
109 Configuration cfg = new Configuration(); | |
110 | |
111 for (Class<?> clazz: credentials.getClasses()) { | |
112 cfg.addAnnotatedClass(clazz); | |
113 } | |
114 | |
115 if (log.isDebugEnabled()) { | |
116 log.debug("user: " + credentials.getUser()); | |
117 log.debug("dialect: " + credentials.getDialect()); | |
118 log.debug("driver: " + credentials.getDriver()); | |
119 log.debug("url: " + credentials.getUrl()); | |
120 } | |
121 | |
122 Properties props = new Properties(); | |
123 | |
124 // We rely on our own connection pool | |
125 props.setProperty( | |
126 "hibernate.connection.provider_class", | |
127 "de.intevation.flys.utils.DBCPConnectionProvider"); | |
128 | |
129 props.setProperty(Environment.DIALECT, credentials.getDialect()); | |
130 props.setProperty(Environment.USER, credentials.getUser()); | |
131 props.setProperty(Environment.PASS, credentials.getPassword()); | |
132 props.setProperty(Environment.DRIVER, credentials.getDriver()); | |
133 props.setProperty(Environment.URL, credentials.getUrl()); | |
134 | |
135 String connectionInitSqls = credentials.getConnectionInitSqls(); | |
136 if (connectionInitSqls != null) { | |
137 props.setProperty("connectionInitSqls", connectionInitSqls); | |
138 } | |
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 : |