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;
  17. import java.io.IOException;
  18. import java.io.InputStream;
  19. import java.util.Properties;
  20. import javax.jdo.JDOException;
  21. import javax.jdo.JDOHelper;
  22. import javax.jdo.PersistenceManagerFactory;
  23. import javax.sql.DataSource;
  24. import org.apache.commons.logging.Log;
  25. import org.apache.commons.logging.LogFactory;
  26. import org.springframework.beans.factory.DisposableBean;
  27. import org.springframework.beans.factory.FactoryBean;
  28. import org.springframework.beans.factory.InitializingBean;
  29. import org.springframework.core.io.Resource;
  30. /**
  31. * FactoryBean that creates a local JDO PersistenceManager instance.
  32. * Behaves like a PersistenceManagerFactory instance when used as bean
  33. * reference, e.g. for JdoTemplate's persistenceManagerFactory property.
  34. * Note that switching to JndiObjectFactoryBean is just a matter of
  35. * configuration!
  36. *
  37. * <p>The typical usage will be to register this as singleton factory
  38. * (for a certain underlying data source) in an application context,
  39. * and give bean references to application services that need it.
  40. *
  41. * <p>Configuration settings can either be read from a properties file,
  42. * specified as "configLocation", or completely via this class. Properties
  43. * specified as "jdoProperties" here will override any settings in a file.
  44. *
  45. * <p>This PersistenceManager handling strategy is most appropriate for
  46. * applications that solely use JDO for data access. In this case,
  47. * JdoTransactionManager is much more convenient than setting up your
  48. * JDO implementation for JTA transactions (which might involve JCA).
  49. *
  50. * @author Juergen Hoeller
  51. * @since 03.06.2003
  52. * @see JdoTemplate#setPersistenceManagerFactory
  53. * @see JdoTransactionManager#setPersistenceManagerFactory
  54. * @see org.springframework.jndi.JndiObjectFactoryBean
  55. */
  56. public class LocalPersistenceManagerFactoryBean implements FactoryBean, InitializingBean, DisposableBean {
  57. protected final Log logger = LogFactory.getLog(getClass());
  58. private Resource configLocation;
  59. private Properties jdoProperties;
  60. private DataSource dataSource;
  61. private PersistenceManagerFactory persistenceManagerFactory;
  62. /**
  63. * Set the location of the JDO properties config file, for example
  64. * as classpath resource "classpath:kodo.properties".
  65. * <p>Note: Can be omitted when all necessary properties are
  66. * specified locally via this bean.
  67. */
  68. public void setConfigLocation(Resource configLocation) {
  69. this.configLocation = configLocation;
  70. }
  71. /**
  72. * Set JDO properties, like "javax.jdo.PersistenceManagerFactoryClass".
  73. * <p>Can be used to override values in a JDO properties config file,
  74. * or to specify all necessary properties locally.
  75. */
  76. public void setJdoProperties(Properties jdoProperties) {
  77. this.jdoProperties = jdoProperties;
  78. }
  79. /**
  80. * Set the DataSource to be used by the PersistenceManagerFactory.
  81. * If set, this will override corresponding settings in JDO properties.
  82. * <p>Note: If this is set, the JDO settings should not define
  83. * a connection factory to avoid meaningless double configuration.
  84. */
  85. public void setDataSource(DataSource dataSource) {
  86. this.dataSource = dataSource;
  87. }
  88. /**
  89. * Initialize the PersistenceManagerFactory for the given location.
  90. * @throws IllegalArgumentException in case of illegal property values
  91. * @throws IOException if the properties could not be loaded from the given location
  92. * @throws JDOException in case of JDO initialization errors
  93. */
  94. public void afterPropertiesSet() throws IllegalArgumentException, IOException, JDOException {
  95. if (this.configLocation == null && this.jdoProperties == null) {
  96. throw new IllegalArgumentException("Either configLocation (e.g. '/kodo.properties') or jdoProperties must be set");
  97. }
  98. Properties props = new Properties();
  99. if (this.configLocation != null) {
  100. // load JDO properties from given location
  101. InputStream is = this.configLocation.getInputStream();
  102. try {
  103. props.load(is);
  104. }
  105. finally {
  106. is.close();
  107. }
  108. }
  109. if (this.jdoProperties != null) {
  110. // add given JDO properties
  111. props.putAll(this.jdoProperties);
  112. }
  113. // build factory instance
  114. this.persistenceManagerFactory = newPersistenceManagerFactory(props);
  115. if (this.dataSource != null) {
  116. // use given DataSource as JDO connection factory
  117. this.persistenceManagerFactory.setConnectionFactory(this.dataSource);
  118. }
  119. }
  120. /**
  121. * Subclasses can override this to perform custom initialization of the
  122. * PersistenceManagerFactory instance, creating it via the given Properties
  123. * that got prepared by this LocalPersistenceManagerFactoryBean
  124. * <p>The default implementation invokes JDOHelper's getPersistenceManagerFactory.
  125. * A custom implementation could prepare the instance in a specific way,
  126. * or use a custom PersistenceManagerFactory implementation.
  127. * @param prop Properties prepared by this LocalPersistenceManagerFactoryBean
  128. * @return the PersistenceManagerFactory instance
  129. * @see javax.jdo.JDOHelper#getPersistenceManagerFactory
  130. */
  131. protected PersistenceManagerFactory newPersistenceManagerFactory(Properties prop) {
  132. return JDOHelper.getPersistenceManagerFactory(prop);
  133. }
  134. /**
  135. * Return the singleton PersistenceManagerFactory.
  136. */
  137. public Object getObject() {
  138. return this.persistenceManagerFactory;
  139. }
  140. public Class getObjectType() {
  141. return (this.persistenceManagerFactory != null) ?
  142. this.persistenceManagerFactory.getClass() : PersistenceManagerFactory.class;
  143. }
  144. public boolean isSingleton() {
  145. return true;
  146. }
  147. /**
  148. * Close the PersistenceManagerFactory on context shutdown.
  149. */
  150. public void destroy() {
  151. logger.info("Closing JDO PersistenceManagerFactory");
  152. this.persistenceManagerFactory.close();
  153. }
  154. }