PageRenderTime 47ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/jboss-as-7.1.1.Final/jpa/hibernate3/src/main/java/org/jboss/as/jpa/hibernate3/HibernatePersistenceProviderAdaptor.java

#
Java | 156 lines | 102 code | 17 blank | 37 comment | 11 complexity | fb503de48a668723bf9a7b6f46753bc4 MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0
  1. /*
  2. * JBoss, Home of Professional Open Source.
  3. * Copyright 2011, Red Hat, Inc., and individual contributors
  4. * as indicated by the @author tags. See the copyright.txt file in the
  5. * distribution for a full listing of individual contributors.
  6. *
  7. * This is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU Lesser General Public License as
  9. * published by the Free Software Foundation; either version 2.1 of
  10. * the License, or (at your option) any later version.
  11. *
  12. * This software is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this software; if not, write to the Free
  19. * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  20. * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  21. */
  22. package org.jboss.as.jpa.hibernate3;
  23. import java.lang.reflect.Method;
  24. import java.util.Map;
  25. import java.util.Properties;
  26. import org.hibernate.cfg.Configuration;
  27. import org.hibernate.cfg.Environment;
  28. import org.jboss.as.clustering.infinispan.subsystem.CacheConfigurationService;
  29. import org.jboss.as.jpa.hibernate3.infinispan.InfinispanRegionFactory;
  30. import org.jboss.as.jpa.hibernate3.infinispan.SharedInfinispanRegionFactory;
  31. import org.jboss.as.jpa.spi.JtaManager;
  32. import org.jboss.as.jpa.spi.ManagementAdaptor;
  33. import org.jboss.as.jpa.spi.PersistenceProviderAdaptor;
  34. import org.jboss.as.jpa.spi.PersistenceUnitMetadata;
  35. import org.jboss.msc.service.ServiceBuilder;
  36. import org.jboss.msc.service.ServiceRegistry;
  37. import org.jboss.msc.service.ServiceTarget;
  38. /**
  39. * Implements the PersistenceProviderAdaptor for Hibernate 3.3.x or higher 3.x
  40. *
  41. * @author Scott Marlow
  42. */
  43. public class HibernatePersistenceProviderAdaptor implements PersistenceProviderAdaptor {
  44. private static final String DEFAULT_REGION_FACTORY = SharedInfinispanRegionFactory.class.getName();
  45. public static final String SCANNER = "hibernate.ejb.resource_scanner";
  46. private static final String HIBERNATE_ANNOTATION_SCANNER_CLASS = "org.jboss.as.jpa.hibernate3.HibernateAnnotationScanner";
  47. @Override
  48. public void injectJtaManager(JtaManager jtaManager) {
  49. JBossAppServerJtaPlatform.initJBossAppServerJtaPlatform(jtaManager);
  50. }
  51. @Override
  52. public void addProviderProperties(Map properties, PersistenceUnitMetadata pu) {
  53. putPropertyIfAbsent(pu, properties, Environment.TRANSACTION_MANAGER_STRATEGY, JBossAppServerJtaPlatform.class.getName());
  54. putPropertyIfAbsent(pu, properties, Configuration.USE_NEW_ID_GENERATOR_MAPPINGS, "true");
  55. addAnnotationScanner(pu);
  56. }
  57. /**
  58. * Use reflection to see if we are using Hibernate 3.3.x or older (which doesn't have the
  59. * org.hibernate.ejb.packaging.Scanner class)
  60. *
  61. * @param pu
  62. */
  63. private void addAnnotationScanner(PersistenceUnitMetadata pu) {
  64. try {
  65. Configuration.class.getClassLoader().loadClass(HIBERNATE_ANNOTATION_SCANNER_CLASS);
  66. pu.getProperties().put(SCANNER, HIBERNATE_ANNOTATION_SCANNER_CLASS);
  67. } catch (Throwable ignore) {
  68. }
  69. }
  70. @Override
  71. public void addProviderDependencies(ServiceRegistry registry, ServiceTarget target, ServiceBuilder<?> builder, PersistenceUnitMetadata pu) {
  72. Properties properties = pu.getProperties();
  73. if (Boolean.parseBoolean(properties.getProperty(Environment.USE_SECOND_LEVEL_CACHE))) {
  74. if (properties.getProperty(Environment.CACHE_REGION_PREFIX) == null) {
  75. // cache entries for this PU will be identified by scoped pu name + Entity class name
  76. properties.put(Environment.CACHE_REGION_PREFIX, pu.getScopedPersistenceUnitName());
  77. }
  78. String regionFactory = properties.getProperty(Environment.CACHE_REGION_FACTORY);
  79. if (regionFactory == null) {
  80. regionFactory = DEFAULT_REGION_FACTORY;
  81. properties.setProperty(Environment.CACHE_REGION_FACTORY, regionFactory);
  82. }
  83. if (regionFactory.equals(DEFAULT_REGION_FACTORY)) {
  84. // Set infinispan defaults
  85. String container = properties.getProperty(InfinispanRegionFactory.CACHE_CONTAINER);
  86. if (container == null) {
  87. container = InfinispanRegionFactory.DEFAULT_CACHE_CONTAINER;
  88. properties.setProperty(InfinispanRegionFactory.CACHE_CONTAINER, container);
  89. }
  90. String entity = properties.getProperty(InfinispanRegionFactory.ENTITY_CACHE_RESOURCE_PROP, InfinispanRegionFactory.DEF_ENTITY_RESOURCE);
  91. String collection = properties.getProperty(InfinispanRegionFactory.COLLECTION_CACHE_RESOURCE_PROP, InfinispanRegionFactory.DEF_ENTITY_RESOURCE);
  92. String query = properties.getProperty(InfinispanRegionFactory.QUERY_CACHE_RESOURCE_PROP, InfinispanRegionFactory.DEF_QUERY_RESOURCE);
  93. String timestamps = properties.getProperty(InfinispanRegionFactory.TIMESTAMPS_CACHE_RESOURCE_PROP, InfinispanRegionFactory.DEF_QUERY_RESOURCE);
  94. builder.addDependency(CacheConfigurationService.getServiceName(container, entity));
  95. builder.addDependency(CacheConfigurationService.getServiceName(container, collection));
  96. builder.addDependency(CacheConfigurationService.getServiceName(container, timestamps));
  97. builder.addDependency(CacheConfigurationService.getServiceName(container, query));
  98. }
  99. }
  100. }
  101. private void putPropertyIfAbsent(PersistenceUnitMetadata pu, Map properties, String property, Object value) {
  102. if (!pu.getProperties().containsKey(property)) {
  103. properties.put(property, value);
  104. }
  105. }
  106. @Override
  107. public void beforeCreateContainerEntityManagerFactory(PersistenceUnitMetadata pu) {
  108. if (pu.getProperties().containsKey(SCANNER)) {
  109. try {
  110. Class<?> scanner = Configuration.class.getClassLoader().loadClass(HIBERNATE_ANNOTATION_SCANNER_CLASS);
  111. // get method for public static void setThreadLocalPersistenceUnitMetadata(final PersistenceUnitMetadata pu) {
  112. Method setThreadLocalPersistenceUnitMetadata = scanner.getMethod("setThreadLocalPersistenceUnitMetadata", PersistenceUnitMetadata.class);
  113. setThreadLocalPersistenceUnitMetadata.invoke(null, pu);
  114. } catch (Throwable ignore) {
  115. }
  116. }
  117. }
  118. @Override
  119. public void afterCreateContainerEntityManagerFactory(PersistenceUnitMetadata pu) {
  120. if (pu.getProperties().containsKey(SCANNER)) {
  121. // clear backdoor annotation scanner access to pu
  122. try {
  123. Class<?> scanner = Configuration.class.getClassLoader().loadClass(HIBERNATE_ANNOTATION_SCANNER_CLASS);
  124. // get method for public static void clearThreadLocalPersistenceUnitMetadata() {
  125. Method clearThreadLocalPersistenceUnitMetadata = scanner.getMethod("clearThreadLocalPersistenceUnitMetadata");
  126. clearThreadLocalPersistenceUnitMetadata.invoke(null);
  127. } catch (Throwable ignore) {
  128. }
  129. }
  130. }
  131. @Override
  132. public ManagementAdaptor getManagementAdaptor() {
  133. return null;
  134. }
  135. @Override
  136. public void cleanup(PersistenceUnitMetadata pu) {
  137. HibernateAnnotationScanner.cleanup(pu);
  138. }
  139. }