1. /*
  2. * Copyright 2002-2004 the original author or authors.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.springframework.orm.jdo.support;
  17. import javax.jdo.JDOException;
  18. import javax.jdo.PersistenceManager;
  19. import javax.jdo.PersistenceManagerFactory;
  20. import org.apache.commons.logging.Log;
  21. import org.apache.commons.logging.LogFactory;
  22. import org.springframework.beans.factory.InitializingBean;
  23. import org.springframework.dao.CleanupFailureDataAccessException;
  24. import org.springframework.dao.DataAccessException;
  25. import org.springframework.dao.DataAccessResourceFailureException;
  26. import org.springframework.orm.jdo.JdoTemplate;
  27. import org.springframework.orm.jdo.PersistenceManagerFactoryUtils;
  28. /**
  29. * Convenient super class for JDO data access objects.
  30. * Requires a PersistenceManagerFactory to be set,
  31. * providing a JdoTemplate based on it to subclasses.
  32. *
  33. * <p>This base class is mainly intended for JdoeTemplate usage but can
  34. * also be used when working with PersistenceManagerFactoryUtils directly,
  35. * e.g. in combination with JdoInterceptor-managed PersistenceManagers.
  36. *
  37. * @author Juergen Hoeller
  38. * @since 28.07.2003
  39. * @see #setPersistenceManagerFactory
  40. * @see org.springframework.orm.jdo.JdoTemplate
  41. * @see org.springframework.orm.jdo.JdoInterceptor
  42. */
  43. public abstract class JdoDaoSupport implements InitializingBean {
  44. protected final Log logger = LogFactory.getLog(getClass());
  45. private JdoTemplate jdoTemplate;
  46. /**
  47. * Set the JDO PersistenceManagerFactory to be used by this DAO.
  48. */
  49. public final void setPersistenceManagerFactory(PersistenceManagerFactory persistenceManagerFactory) {
  50. this.jdoTemplate = new JdoTemplate(persistenceManagerFactory);
  51. }
  52. /**
  53. * Return the JDO PersistenceManagerFactory used by this DAO.
  54. */
  55. public final PersistenceManagerFactory getPersistenceManagerFactory() {
  56. return jdoTemplate.getPersistenceManagerFactory();
  57. }
  58. /**
  59. * Set the JdoTemplate for this DAO explicitly,
  60. * as an alternative to specifying a PersistenceManagerFactory.
  61. */
  62. public final void setJdoTemplate(JdoTemplate jdoTemplate) {
  63. this.jdoTemplate = jdoTemplate;
  64. }
  65. /**
  66. * Return the JdoTemplate for this DAO, pre-initialized
  67. * with the PersistenceManagerFactory or set explicitly.
  68. */
  69. public final JdoTemplate getJdoTemplate() {
  70. return jdoTemplate;
  71. }
  72. public final void afterPropertiesSet() throws Exception {
  73. if (this.jdoTemplate == null) {
  74. throw new IllegalArgumentException("persistenceManagerFactory or jdoTemplate is required");
  75. }
  76. initDao();
  77. }
  78. /**
  79. * Subclasses can override this for custom initialization behavior.
  80. * Gets called after population of this instance's bean properties.
  81. * @throws Exception if initialization fails
  82. */
  83. protected void initDao() throws Exception {
  84. }
  85. /**
  86. * Get a JDO PersistenceManager, either from the current transaction or
  87. * a new one. The latter is only allowed if the "allowCreate" setting
  88. * of this bean's JdoTemplate is true.
  89. * @return the JDO PersistenceManager
  90. * @throws DataAccessResourceFailureException if the Session couldn't be created
  91. * @throws IllegalStateException if no thread-bound Session found and allowCreate false
  92. * @see org.springframework.orm.jdo.PersistenceManagerFactoryUtils#getPersistenceManager
  93. */
  94. protected final PersistenceManager getPersistenceManager() {
  95. return getPersistenceManager(this.jdoTemplate.isAllowCreate());
  96. }
  97. /**
  98. * Get a JDO PersistenceManager, either from the current transaction or
  99. * a new one. The latter is only allowed if "allowCreate" is true.
  100. * @param allowCreate if a new PersistenceManager should be created if no thread-bound found
  101. * @return the JDO PersistenceManager
  102. * @throws DataAccessResourceFailureException if the Session couldn't be created
  103. * @throws IllegalStateException if no thread-bound Session found and allowCreate false
  104. * @see org.springframework.orm.jdo.PersistenceManagerFactoryUtils#getPersistenceManager
  105. */
  106. protected final PersistenceManager getPersistenceManager(boolean allowCreate)
  107. throws DataAccessResourceFailureException {
  108. return PersistenceManagerFactoryUtils.getPersistenceManager(getPersistenceManagerFactory(), allowCreate);
  109. }
  110. /**
  111. * Convert the given JDOException to an appropriate exception from the
  112. * org.springframework.dao hierarchy.
  113. * <p>Delegates to the convertJdoAccessException method of this DAO's JdoTemplate.
  114. * @param ex JDOException that occured
  115. * @return the corresponding DataAccessException instance
  116. * @see #setJdoTemplate
  117. * @see org.springframework.orm.jdo.JdoTemplate#convertJdoAccessException
  118. */
  119. protected final DataAccessException convertJdoAccessException(JDOException ex) {
  120. return this.jdoTemplate.convertJdoAccessException(ex);
  121. }
  122. /**
  123. * Close the given JDO PersistenceManager if necessary, created via this bean's
  124. * PersistenceManagerFactory, if it isn't bound to the thread.
  125. * @param pm PersistenceManager to close
  126. * @throws DataAccessResourceFailureException if the PersistenceManager couldn't be closed
  127. * @see org.springframework.orm.jdo.PersistenceManagerFactoryUtils#closePersistenceManagerIfNecessary
  128. */
  129. protected final void closeSessionIfNecessary(PersistenceManager pm)
  130. throws CleanupFailureDataAccessException {
  131. PersistenceManagerFactoryUtils.closePersistenceManagerIfNecessary(pm, getPersistenceManagerFactory());
  132. }
  133. }