Example usage for org.hibernate.internal.util ConfigHelper getConfigStream

List of usage examples for org.hibernate.internal.util ConfigHelper getConfigStream

Introduction

In this page you can find the example usage for org.hibernate.internal.util ConfigHelper getConfigStream.

Prototype

public static InputStream getConfigStream(final String path) throws HibernateException 

Source Link

Document

Open an InputStream to the URL represented by the incoming path.

Usage

From source file:org.unitime.commons.hibernate.util.HibernateUtil.java

License:Open Source License

public static void configureHibernateFromRootDAO(String cfgName, Configuration cfg) {
    try {//w w w.  j a  va2s . c o  m
        EntityResolver entityResolver = 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;
            }
        };

        cfg.setEntityResolver(entityResolver);
        sLog.debug("  -- added entity resolver");

        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        sLog.debug("  -- document factory created");
        DocumentBuilder builder = factory.newDocumentBuilder();
        builder.setEntityResolver(entityResolver);
        sLog.debug("  -- document builder created");
        Document document = builder
                .parse(ConfigHelper.getConfigStream(cfgName == null ? "hibernate.cfg.xml" : cfgName));

        String dialect = ApplicationProperty.DatabaseDialect.value();
        if (dialect != null)
            setProperty(document, "dialect", dialect);

        String default_schema = ApplicationProperty.DatabaseSchema.value();
        if (default_schema != null)
            setProperty(document, "default_schema", default_schema);

        String idgen = ApplicationProperty.DatabaseUniqueIdGenerator.value();
        if (idgen != null)
            setProperty(document, "tmtbl.uniqueid.generator", idgen);

        if (ApplicationProperty.HibernateClusterEnabled.isFalse())
            setProperty(document, "net.sf.ehcache.configurationResourceName", "ehcache-nocluster.xml");

        for (Enumeration e = ApplicationProperties.getProperties().propertyNames(); e.hasMoreElements();) {
            String name = (String) e.nextElement();
            if (name.startsWith("hibernate.") || name.startsWith("connection.")
                    || name.startsWith("tmtbl.hibernate.")) {
                String value = ApplicationProperties.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 + ": *****");
            }
        }

        cfg.configure(document);
        sLog.debug("  -- hibernate configured");

        HibernateUtil.fixSchemaInFormulas(cfg);
        sLog.debug("  -- %SCHEMA% in formulas changed to " + cfg.getProperty("default_schema"));

        UniqueIdGenerator.configure(cfg);
        sLog.debug("  -- UniquId generator configured");
    } catch (Exception e) {
        sLog.error("Unable to configure hibernate, reason: " + e.getMessage(), e);
    }
}