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