001/* 002 * (C) Copyright 2006-2017 Nuxeo (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) <[email protected]>" 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) <[email protected]>" 040 */ 041public class PersistenceComponent extends DefaultComponent 042 implements HibernateConfigurator, PersistenceProviderFactory { 043 044 private static final Log log = LogFactory.getLog(PersistenceComponent.class); 045 046 protected final Map<String, HibernateConfiguration> registry = new HashMap<>(); 047 048 static { 049 // do this statically once, as we're patching a static variable 050 registerOracle12DialectResolver(); 051 } 052 053 /** 054 * Registers with Hibernate a dialect resolver to recognize Oracle 12. 055 * 056 * @since 7.2 057 */ 058 // We can't easily use DialectFactory's Environment.DIALECT_RESOLVERS because we would have 059 // to set it before Hibernate's classloading which we can't control. 060 protected static void registerOracle12DialectResolver() { 061 try { 062 Field f = DialectFactory.class.getDeclaredField("DIALECT_RESOLVERS"); 063 f.setAccessible(true); 064 DialectResolverSet resolvers = (DialectResolverSet) f.get(null); 065 resolvers.addResolverAtFirst(new BasicDialectResolver("Oracle", 12, Oracle10gDialect.class)); 066 } catch (ReflectiveOperationException | SecurityException e) { 067 log.error("Cannot patch Hibernate to support Oracle 12", e); 068 } 069 } 070 071 @Override 072 public int getApplicationStartedOrder() { 073 return 50; // even before repository init 074 } 075 076 @Override 077 public void start(ComponentContext context) { 078 /* 079 * Initialize all the persistence units synchronously at startup, otherwise init may end up being called during 080 * the first asynchronous event, which means hibernate init may happen in parallel with the main Nuxeo startup 081 * thread which may be doing the hibernate init for someone else (JBPM for instance). 082 */ 083 for (String name : registry.keySet()) { 084 PersistenceProvider pp = newProvider(name); 085 pp.openPersistenceUnit(); // creates tables etc. 086 pp.closePersistenceUnit(); 087 } 088 } 089 090 @Override 091 public void registerContribution(Object contribution, String extensionPoint, ComponentInstance contributor) { 092 if ("hibernate".equals(extensionPoint)) { 093 registerHibernateContribution((HibernateConfiguration) contribution, contributor.getName()); 094 } 095 } 096 097 protected void registerHibernateContribution(HibernateConfiguration contribution, ComponentName contributorName) { 098 if (contribution.name == null) { 099 throw new PersistenceError( 100 contributorName + " should set the 'name' attribute of hibernate configurations"); 101 } 102 if (contribution.hibernateProperties != null) { 103 doPatchForTests(contribution.hibernateProperties); 104 } 105 if (!registry.containsKey(contribution.name)) { 106 registry.put(contribution.name, contribution); 107 } else { 108 registry.get(contribution.name).merge(contribution); 109 } 110 } 111 112 protected void doPatchForTests(Properties hibernateProperties) { 113 String url = hibernateProperties.getProperty("hibernate.connection.url"); 114 if (url != null) { 115 url = Framework.expandVars(url); 116 hibernateProperties.put("hibernate.connection.url", url); 117 } 118 } 119 120 @Override 121 public PersistenceProvider newProvider(String name) { 122 EntityManagerFactoryProvider emfProvider = registry.get(name); 123 if (emfProvider == null) { 124 throw new PersistenceError("no hibernate configuration identified by '" + name + "' is available"); 125 } 126 return new PersistenceProvider(emfProvider); 127 } 128 129 @Override 130 public HibernateConfiguration getHibernateConfiguration(String name) { 131 HibernateConfiguration config = registry.get(name); 132 if (config == null) { 133 throw new PersistenceError("no hibernate configuration identified by '" + name + "' is available"); 134 } 135 return config; 136 } 137 138}