PageRenderTime 43ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/jboss-as-7.1.1.Final/jpa/core/src/main/java/org/jboss/as/jpa/service/PersistenceUnitServiceImpl.java

#
Java | 173 lines | 102 code | 23 blank | 48 comment | 2 complexity | 2d33004732bcc07c8322c2ea4048f0e8 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.service;
  23. import java.util.Map;
  24. import javax.persistence.EntityManagerFactory;
  25. import javax.persistence.spi.PersistenceProvider;
  26. import javax.sql.DataSource;
  27. import org.jboss.as.jpa.classloader.TempClassLoaderFactoryImpl;
  28. import org.jboss.as.jpa.spi.PersistenceProviderAdaptor;
  29. import org.jboss.as.jpa.spi.PersistenceUnitMetadata;
  30. import org.jboss.as.jpa.spi.PersistenceUnitService;
  31. import org.jboss.as.jpa.util.JPAServiceNames;
  32. import org.jboss.as.naming.WritableServiceBasedNamingStore;
  33. import org.jboss.msc.inject.Injector;
  34. import org.jboss.msc.service.Service;
  35. import org.jboss.msc.service.ServiceName;
  36. import org.jboss.msc.service.StartContext;
  37. import org.jboss.msc.service.StartException;
  38. import org.jboss.msc.service.StopContext;
  39. import org.jboss.msc.value.InjectedValue;
  40. import static org.jboss.as.jpa.JpaLogger.JPA_LOGGER;
  41. /**
  42. * Persistence Unit service that is created for each deployed persistence unit that will be referenced by the
  43. * persistence context/unit injector.
  44. * <p/>
  45. * The persistence unit scoped
  46. *
  47. * @author Scott Marlow
  48. */
  49. public class PersistenceUnitServiceImpl implements Service<PersistenceUnitServiceImpl>, PersistenceUnitService {
  50. private final InjectedValue<Map> properties = new InjectedValue<Map>();
  51. private final InjectedValue<DataSource> jtaDataSource = new InjectedValue<DataSource>();
  52. private final InjectedValue<DataSource> nonJtaDataSource = new InjectedValue<DataSource>();
  53. private final PersistenceProviderAdaptor persistenceProviderAdaptor;
  54. private final PersistenceProvider persistenceProvider;
  55. private final PersistenceUnitMetadata pu;
  56. private final ClassLoader classLoader;
  57. private volatile EntityManagerFactory entityManagerFactory;
  58. public PersistenceUnitServiceImpl(final ClassLoader classLoader, final PersistenceUnitMetadata pu, final PersistenceProviderAdaptor persistenceProviderAdaptor, final PersistenceProvider persistenceProvider) {
  59. this.pu = pu;
  60. this.persistenceProviderAdaptor = persistenceProviderAdaptor;
  61. this.persistenceProvider = persistenceProvider;
  62. this.classLoader = classLoader;
  63. }
  64. @Override
  65. public void start(StartContext context) throws StartException {
  66. try {
  67. JPA_LOGGER.startingService("Persistence Unit", pu.getScopedPersistenceUnitName());
  68. pu.setTempClassLoaderFactory(new TempClassLoaderFactoryImpl(classLoader));
  69. pu.setJtaDataSource(jtaDataSource.getOptionalValue());
  70. pu.setNonJtaDataSource(nonJtaDataSource.getOptionalValue());
  71. WritableServiceBasedNamingStore.pushOwner(context.getController().getServiceContainer().subTarget());
  72. this.entityManagerFactory = createContainerEntityManagerFactory();
  73. } finally {
  74. pu.setTempClassLoaderFactory(null); // release the temp classloader factory (only needed when creating the EMF)
  75. WritableServiceBasedNamingStore.popOwner();
  76. }
  77. }
  78. @Override
  79. public void stop(StopContext context) {
  80. JPA_LOGGER.stoppingService("Persistence Unit", pu.getScopedPersistenceUnitName());
  81. if (entityManagerFactory != null) {
  82. WritableServiceBasedNamingStore.pushOwner(context.getController().getServiceContainer().subTarget());
  83. try {
  84. entityManagerFactory.close();
  85. } finally {
  86. entityManagerFactory = null;
  87. pu.setTempClassLoaderFactory(null);
  88. WritableServiceBasedNamingStore.popOwner();
  89. }
  90. }
  91. }
  92. @Override
  93. public PersistenceUnitServiceImpl getValue() throws IllegalStateException, IllegalArgumentException {
  94. return this;
  95. }
  96. /**
  97. * Get the entity manager factory
  98. *
  99. * @return the entity manager factory
  100. */
  101. @Override
  102. public EntityManagerFactory getEntityManagerFactory() {
  103. return entityManagerFactory;
  104. }
  105. @Override
  106. public String getScopedPersistenceUnitName() {
  107. return pu.getScopedPersistenceUnitName();
  108. }
  109. public Injector<Map> getPropertiesInjector() {
  110. return properties;
  111. }
  112. public Injector<DataSource> getJtaDataSourceInjector() {
  113. return jtaDataSource;
  114. }
  115. public Injector<DataSource> getNonJtaDataSourceInjector() {
  116. return nonJtaDataSource;
  117. }
  118. /**
  119. * Returns the Persistence Unit service name used for creation or lookup.
  120. * The service name contains the unique fully scoped persistence unit name
  121. *
  122. * @param pu persistence unit definition
  123. * @return
  124. */
  125. public static ServiceName getPUServiceName(PersistenceUnitMetadata pu) {
  126. return JPAServiceNames.getPUServiceName(pu.getScopedPersistenceUnitName());
  127. }
  128. public static ServiceName getPUServiceName(String scopedPersistenceUnitName) {
  129. return JPAServiceNames.getPUServiceName(scopedPersistenceUnitName);
  130. }
  131. /**
  132. * Create EE container entity manager factory
  133. *
  134. * @return EntityManagerFactory
  135. */
  136. private EntityManagerFactory createContainerEntityManagerFactory() {
  137. persistenceProviderAdaptor.beforeCreateContainerEntityManagerFactory(pu);
  138. try {
  139. return persistenceProvider.createContainerEntityManagerFactory(pu, properties.getValue());
  140. } finally {
  141. try {
  142. persistenceProviderAdaptor.afterCreateContainerEntityManagerFactory(pu);
  143. } finally {
  144. pu.setAnnotationIndex(null); // close reference to Annotation Index (only needed during call to createContainerEntityManagerFactory)
  145. //This is needed if the datasource is restarted
  146. //pu.setTempClassLoaderFactory(null); // close reference to temp classloader factory (only needed during call to createEntityManagerFactory)
  147. }
  148. }
  149. }
  150. }