List of usage examples for javax.naming.spi NamingManager setInitialContextFactoryBuilder
public static synchronized void setInitialContextFactoryBuilder(InitialContextFactoryBuilder builder) throws NamingException
From source file:org.unitime.commons.hibernate.util.HibernateUtil.java
public static void configureHibernate(Properties properties) throws Exception { if (sSessionFactory != null) { sSessionFactory.close();// ww w . j a v a 2s . c o m sSessionFactory = null; } if (!NamingManager.hasInitialContextFactoryBuilder()) NamingManager.setInitialContextFactoryBuilder(new LocalContext(null)); sLog.info("Connecting to " + getProperty(properties, "connection.url")); ClassLoader classLoader = HibernateUtil.class.getClassLoader(); sLog.debug(" -- class loader retrieved"); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); sLog.debug(" -- document factory created"); DocumentBuilder builder = factory.newDocumentBuilder(); builder.setEntityResolver(new EntityResolver() { public InputSource resolveEntity(String publicId, String systemId) { if (publicId.equals("-//Hibernate/Hibernate Mapping DTD 3.0//EN")) { return new InputSource(HibernateUtil.class.getClassLoader() .getResourceAsStream("org/hibernate/hibernate-mapping-3.0.dtd")); } else if (publicId.equals("-//Hibernate/Hibernate Mapping DTD//EN")) { return new InputSource(HibernateUtil.class.getClassLoader() .getResourceAsStream("org/hibernate/hibernate-mapping-3.0.dtd")); } else if (publicId.equals("-//Hibernate/Hibernate Configuration DTD 3.0//EN")) { return new InputSource(HibernateUtil.class.getClassLoader() .getResourceAsStream("org/hibernate/hibernate-configuration-3.0.dtd")); } else if (publicId.equals("-//Hibernate/Hibernate Configuration DTD//EN")) { return new InputSource(HibernateUtil.class.getClassLoader() .getResourceAsStream("org/hibernate/hibernate-configuration-3.0.dtd")); } return null; } }); sLog.debug(" -- document builder created"); Document document = builder.parse(classLoader.getResource("hibernate.cfg.xml").openStream()); sLog.debug(" -- hibernate.cfg.xml parsed"); String dialect = getProperty(properties, "dialect"); if (dialect != null) setProperty(document, "dialect", dialect); String idgen = getProperty(properties, "tmtbl.uniqueid.generator"); if (idgen != null) setProperty(document, "tmtbl.uniqueid.generator", idgen); if (ApplicationProperty.HibernateClusterEnabled.isFalse()) setProperty(document, "net.sf.ehcache.configurationResourceName", "ehcache-nocluster.xml"); // Remove second level cache setProperty(document, "hibernate.cache.use_second_level_cache", "false"); setProperty(document, "hibernate.cache.use_query_cache", "false"); removeProperty(document, "hibernate.cache.region.factory_class"); for (Enumeration e = properties.propertyNames(); e.hasMoreElements();) { String name = (String) e.nextElement(); if (name.startsWith("hibernate.") || name.startsWith("connection.") || name.startsWith("tmtbl.hibernate.")) { String value = properties.getProperty(name); if ("NULL".equals(value)) removeProperty(document, name); else setProperty(document, name, value); if (!name.equals("connection.password")) sLog.debug(" -- set " + name + ": " + value); else sLog.debug(" -- set " + name + ": *****"); } } String default_schema = getProperty(properties, "default_schema"); if (default_schema != null) setProperty(document, "default_schema", default_schema); sLog.debug(" -- hibernate.cfg.xml altered"); Configuration cfg = new Configuration(); sLog.debug(" -- configuration object created"); cfg.setEntityResolver(new EntityResolver() { public InputSource resolveEntity(String publicId, String systemId) { if (publicId.equals("-//Hibernate/Hibernate Mapping DTD 3.0//EN")) { return new InputSource(HibernateUtil.class.getClassLoader() .getResourceAsStream("org/hibernate/hibernate-mapping-3.0.dtd")); } else if (publicId.equals("-//Hibernate/Hibernate Mapping DTD//EN")) { return new InputSource(HibernateUtil.class.getClassLoader() .getResourceAsStream("org/hibernate/hibernate-mapping-3.0.dtd")); } else if (publicId.equals("-//Hibernate/Hibernate Configuration DTD 3.0//EN")) { return new InputSource(HibernateUtil.class.getClassLoader() .getResourceAsStream("org/hibernate/hibernate-configuration-3.0.dtd")); } else if (publicId.equals("-//Hibernate/Hibernate Configuration DTD//EN")) { return new InputSource(HibernateUtil.class.getClassLoader() .getResourceAsStream("org/hibernate/hibernate-configuration-3.0.dtd")); } return null; } }); sLog.debug(" -- added entity resolver"); cfg.configure(document); sLog.debug(" -- hibernate configured"); fixSchemaInFormulas(cfg); UniqueIdGenerator.configure(cfg); (new _BaseRootDAO() { void setConf(Configuration cfg) { _BaseRootDAO.sConfiguration = cfg; } protected Class getReferenceClass() { return null; } }).setConf(cfg); sLog.debug(" -- configuration set to _BaseRootDAO"); sSessionFactory = cfg.buildSessionFactory(); sLog.debug(" -- session factory created"); (new _BaseRootDAO() { void setSF(SessionFactory fact) { _BaseRootDAO.sSessionFactory = fact; } protected Class getReferenceClass() { return null; } }).setSF(sSessionFactory); sLog.debug(" -- session factory set to _BaseRootDAO"); addBitwiseOperationsToDialect(); sLog.debug(" -- bitwise operation added to the dialect if needed"); DatabaseUpdate.update(); }