teichmann@5844: /* Copyright (C) 2011, 2012, 2013 by Bundesanstalt für Gewässerkunde teichmann@5844: * Software engineering by Intevation GmbH teichmann@5844: * teichmann@5844: * This file is Free Software under the GNU AGPL (>=v3) teichmann@5844: * and comes with ABSOLUTELY NO WARRANTY! Check out the teichmann@5844: * documentation coming with Dive4Elements River for details. teichmann@5844: */ teichmann@5844: teichmann@5829: package org.dive4elements.river.backend; sascha@176: sascha@176: import java.util.Properties; sascha@176: sascha@3333: import org.apache.log4j.Logger; sascha@3333: sascha@176: import org.hibernate.SessionFactory; sascha@176: sascha@3333: import org.hibernate.cfg.Configuration; sascha@3333: import org.hibernate.cfg.Environment; sascha@3333: ingo@2361: import org.hibernate.impl.SessionFactoryImpl; ingo@2361: sascha@176: public final class SessionFactoryProvider sascha@176: { sascha@176: private static Logger log = Logger.getLogger(SessionFactoryProvider.class); sascha@176: teichmann@5564: //public static final boolean ENABLE_JMX = teichmann@5564: // Boolean.getBoolean("flys.backend.enablejmx"); ingo@2337: sascha@3333: private static SessionFactory flysSessionFactory; sascha@3333: private static SessionFactory sedDBSessionFactory; sascha@176: sascha@176: private SessionFactoryProvider() { sascha@176: } sascha@176: sascha@176: public static synchronized SessionFactory getSessionFactory() { sascha@3333: if (flysSessionFactory == null) { sascha@3333: flysSessionFactory = sascha@3333: createSessionFactory(FLYSCredentials.getInstance()); sascha@176: } sascha@3333: return flysSessionFactory; sascha@176: } sascha@176: sascha@179: public static SessionFactory createSessionFactory() { sascha@3333: return createSessionFactory(FLYSCredentials.getDefault()); sascha@3333: } sascha@3333: sascha@3333: public static synchronized SessionFactory getSedDBSessionFactory() { sascha@3333: if (sedDBSessionFactory == null) { sascha@3333: sedDBSessionFactory = sascha@3333: createSessionFactory(SedDBCredentials.getInstance()); sascha@3333: } sascha@3333: return sedDBSessionFactory; sascha@3333: } sascha@3333: sascha@3333: public static SessionFactory createSedDBSessionFactory() { sascha@3333: return createSessionFactory(SedDBCredentials.getDefault()); sascha@179: } sascha@179: sascha@178: public static SessionFactory createSessionFactory( sascha@3333: Credentials credentials sascha@178: ) { sascha@3333: Configuration cfg = createConfiguration(credentials); sascha@1229: ingo@2337: SessionFactory factory = cfg.buildSessionFactory(); ingo@2337: teichmann@5564: /* ingo@2337: if (ENABLE_JMX) { ingo@2337: registerAsMBean(factory); ingo@2337: } ingo@2337: else { ingo@2337: log.info("No JMX support for hibernate."); ingo@2337: } teichmann@5564: */ ingo@2337: ingo@2337: return factory; sascha@1229: } sascha@1229: teichmann@5564: /** XXX: Commented out till it is configured correctly. ingo@2337: public static void registerAsMBean(SessionFactory factory) { ingo@2337: teichmann@5564: // teichmann@5557: ingo@2337: StatisticsService statsMBean = new StatisticsService(); ingo@2337: statsMBean.setSessionFactory(factory); ingo@2337: statsMBean.setStatisticsEnabled(true); ingo@2337: ingo@2337: try { ingo@2337: MBeanServer mbs = ManagementFactory.getPlatformMBeanServer(); ingo@2337: mbs.registerMBean( ingo@2337: statsMBean, ingo@2337: new ObjectName("Hibernate:application=Statistics")); ingo@2337: ingo@2337: log.info("Enabled JMX support for hibernate."); ingo@2337: } ingo@2337: catch (MalformedObjectNameException mone) { ingo@2337: log.warn(mone, mone); ingo@2337: } ingo@2337: catch (InstanceAlreadyExistsException iaee) { ingo@2337: log.warn(iaee, iaee); ingo@2337: } ingo@2337: catch (MBeanRegistrationException mbre) { ingo@2337: log.warn(mbre, mbre); ingo@2337: } ingo@2337: catch (NotCompliantMBeanException ncmbe) { ingo@2337: log.warn(ncmbe, ncmbe); ingo@2337: } ingo@2337: } teichmann@5564: */ ingo@2337: sascha@1229: public static Configuration createConfiguration() { sascha@3333: return createConfiguration(FLYSCredentials.getInstance()); sascha@1229: } sascha@1229: sascha@1229: public static Configuration createConfiguration( sascha@3333: Credentials credentials sascha@1229: ) { sascha@176: Configuration cfg = new Configuration(); sascha@176: teichmann@5564: for (Class clazz: credentials.getClasses()) { sascha@3333: cfg.addAnnotatedClass(clazz); sascha@3333: } sascha@176: sascha@176: if (log.isDebugEnabled()) { sascha@3333: log.debug("user: " + credentials.getUser()); sascha@3333: log.debug("dialect: " + credentials.getDialect()); sascha@3333: log.debug("driver: " + credentials.getDriver()); sascha@3333: log.debug("url: " + credentials.getUrl()); sascha@176: } sascha@176: sascha@176: Properties props = new Properties(); sascha@176: sascha@176: // We rely on our own connection pool sascha@176: props.setProperty( sascha@176: "hibernate.connection.provider_class", teichmann@5829: "org.dive4elements.river.utils.DBCPConnectionProvider"); sascha@176: sascha@3333: props.setProperty(Environment.DIALECT, credentials.getDialect()); sascha@3333: props.setProperty(Environment.USER, credentials.getUser()); sascha@3333: props.setProperty(Environment.PASS, credentials.getPassword()); sascha@3333: props.setProperty(Environment.DRIVER, credentials.getDriver()); sascha@3333: props.setProperty(Environment.URL, credentials.getUrl()); sascha@176: teichmann@5193: String connectionInitSqls = credentials.getConnectionInitSqls(); teichmann@5193: if (connectionInitSqls != null) { teichmann@5193: props.setProperty("connectionInitSqls", connectionInitSqls); teichmann@5193: } teichmann@5193: sascha@176: cfg.mergeProperties(props); sascha@176: sascha@1229: return cfg; sascha@176: } ingo@2361: ingo@2361: ingo@2362: public static String getProperty(SessionFactoryImpl factory, String key) { ingo@2361: Properties props = factory.getProperties(); ingo@2362: return props.getProperty(key); ingo@2362: } ingo@2361: ingo@2362: public static String getUser(SessionFactoryImpl factory) { ingo@2362: return getProperty(factory, Environment.USER); ingo@2362: } ingo@2362: ingo@2362: ingo@2362: public static String getPass(SessionFactoryImpl factory) { ingo@2362: return getProperty(factory, Environment.PASS); ingo@2362: } ingo@2362: ingo@2362: ingo@2362: public static String getURL(SessionFactoryImpl factory) { ingo@2362: return getProperty(factory, Environment.URL); ingo@2362: } ingo@2362: ingo@2362: ingo@2362: public static String getDriver(SessionFactoryImpl factory) { ingo@2362: return getProperty(factory, Environment.DRIVER); ingo@2361: } sascha@176: } sascha@176: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :