001/*
002 * (C) Copyright 2006-2008 Nuxeo SAS (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     "Stephane Lacoin (aka matic) <slacoin@nuxeo.org>"
016 */
017package org.nuxeo.ecm.core.persistence;
018
019import java.lang.reflect.Field;
020import java.util.HashMap;
021import java.util.Map;
022import java.util.Properties;
023
024import org.apache.commons.logging.Log;
025import org.apache.commons.logging.LogFactory;
026import org.hibernate.dialect.Oracle10gDialect;
027import org.hibernate.dialect.resolver.BasicDialectResolver;
028import org.hibernate.dialect.resolver.DialectFactory;
029import org.hibernate.dialect.resolver.DialectResolverSet;
030import org.nuxeo.runtime.api.Framework;
031import org.nuxeo.runtime.model.ComponentContext;
032import org.nuxeo.runtime.model.ComponentInstance;
033import org.nuxeo.runtime.model.ComponentName;
034import org.nuxeo.runtime.model.DefaultComponent;
035
036/**
037 * @author "Stephane Lacoin (aka matic) <slacoin@nuxeo.org>"
038 */
039public class PersistenceComponent extends DefaultComponent implements HibernateConfigurator, PersistenceProviderFactory {
040
041    private static final Log log = LogFactory.getLog(PersistenceComponent.class);
042
043    protected final Map<String, HibernateConfiguration> registry = new HashMap<String, HibernateConfiguration>();
044
045    {
046        // do this statically once, as we're patching a static variable
047        registerOracle12DialectResolver();
048    }
049
050    /**
051     * Registers with Hibernate a dialect resolver to recognize Oracle 12.
052     *
053     * @since 7.2
054     */
055    // We can't easily use DialectFactory's Environment.DIALECT_RESOLVERS because we would have
056    // to set it before Hibernate's classloading which we can't control.
057    protected static void registerOracle12DialectResolver() {
058        try {
059            Field f = DialectFactory.class.getDeclaredField("DIALECT_RESOLVERS");
060            f.setAccessible(true);
061            DialectResolverSet resolvers = (DialectResolverSet) f.get(null);
062            resolvers.addResolverAtFirst(new BasicDialectResolver("Oracle", 12, Oracle10gDialect.class));
063        } catch (ReflectiveOperationException | SecurityException e) {
064            log.error("Cannot patch Hibernate to support Oracle 12", e);
065        }
066    }
067
068    @Override
069    public int getApplicationStartedOrder() {
070        return 50; // even before repository init
071    }
072
073    @Override
074    public void applicationStarted(ComponentContext context) {
075        /*
076         * Initialize all the persistence units synchronously at startup, otherwise init may end up being called during
077         * the first asynchronous event, which means hibernate init may happen in parallel with the main Nuxeo startup
078         * thread which may be doing the hibernate init for someone else (JBPM for instance).
079         */
080        for (String name : registry.keySet()) {
081            PersistenceProvider pp = newProvider(name);
082            pp.openPersistenceUnit(); // creates tables etc.
083            pp.closePersistenceUnit();
084        }
085    }
086
087    @Override
088    public void registerContribution(Object contribution, String extensionPoint, ComponentInstance contributor) {
089        if ("hibernate".equals(extensionPoint)) {
090            registerHibernateContribution((HibernateConfiguration) contribution, contributor.getName());
091        }
092    }
093
094    protected void registerHibernateContribution(HibernateConfiguration contribution, ComponentName contributorName) {
095        if (contribution.name == null) {
096            throw new PersistenceError(contributorName + " should set the 'name' attribute of hibernate configurations");
097        }
098        if (contribution.hibernateProperties != null) {
099            doPatchForTests(contribution.hibernateProperties);
100        }
101        if (!registry.containsKey(contribution.name)) {
102            registry.put(contribution.name, contribution);
103        } else {
104            registry.get(contribution.name).merge(contribution);
105        }
106    }
107
108    protected void doPatchForTests(Properties hibernateProperties) {
109        String url = hibernateProperties.getProperty("hibernate.connection.url");
110        if (url != null) {
111            url = Framework.expandVars(url);
112            hibernateProperties.put("hibernate.connection.url", url);
113        }
114    }
115
116    @Override
117    public PersistenceProvider newProvider(String name) {
118        EntityManagerFactoryProvider emfProvider = registry.get(name);
119        if (emfProvider == null) {
120            throw new PersistenceError("no hibernate configuration identified by '" + name + "' is available");
121        }
122        return new PersistenceProvider(emfProvider);
123    }
124
125    @Override
126    public HibernateConfiguration getHibernateConfiguration(String name) {
127        HibernateConfiguration config = registry.get(name);
128        if (config == null) {
129            throw new PersistenceError("no hibernate configuration identified by '" + name + "' is available");
130        }
131        return config;
132    }
133
134}