- /*
- * Copyright 2002-2004 the original author or authors.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- package org.springframework.orm.jdo;
- import java.io.IOException;
- import java.io.InputStream;
- import java.util.Properties;
- import javax.jdo.JDOException;
- import javax.jdo.JDOHelper;
- import javax.jdo.PersistenceManagerFactory;
- import javax.sql.DataSource;
- import org.apache.commons.logging.Log;
- import org.apache.commons.logging.LogFactory;
- import org.springframework.beans.factory.DisposableBean;
- import org.springframework.beans.factory.FactoryBean;
- import org.springframework.beans.factory.InitializingBean;
- import org.springframework.core.io.Resource;
- /**
- * FactoryBean that creates a local JDO PersistenceManager instance.
- * Behaves like a PersistenceManagerFactory instance when used as bean
- * reference, e.g. for JdoTemplate's persistenceManagerFactory property.
- * Note that switching to JndiObjectFactoryBean is just a matter of
- * configuration!
- *
- * <p>The typical usage will be to register this as singleton factory
- * (for a certain underlying data source) in an application context,
- * and give bean references to application services that need it.
- *
- * <p>Configuration settings can either be read from a properties file,
- * specified as "configLocation", or completely via this class. Properties
- * specified as "jdoProperties" here will override any settings in a file.
- *
- * <p>This PersistenceManager handling strategy is most appropriate for
- * applications that solely use JDO for data access. In this case,
- * JdoTransactionManager is much more convenient than setting up your
- * JDO implementation for JTA transactions (which might involve JCA).
- *
- * @author Juergen Hoeller
- * @since 03.06.2003
- * @see JdoTemplate#setPersistenceManagerFactory
- * @see JdoTransactionManager#setPersistenceManagerFactory
- * @see org.springframework.jndi.JndiObjectFactoryBean
- */
- public class LocalPersistenceManagerFactoryBean implements FactoryBean, InitializingBean, DisposableBean {
- protected final Log logger = LogFactory.getLog(getClass());
- private Resource configLocation;
- private Properties jdoProperties;
- private DataSource dataSource;
- private PersistenceManagerFactory persistenceManagerFactory;
- /**
- * Set the location of the JDO properties config file, for example
- * as classpath resource "classpath:kodo.properties".
- * <p>Note: Can be omitted when all necessary properties are
- * specified locally via this bean.
- */
- public void setConfigLocation(Resource configLocation) {
- this.configLocation = configLocation;
- }
- /**
- * Set JDO properties, like "javax.jdo.PersistenceManagerFactoryClass".
- * <p>Can be used to override values in a JDO properties config file,
- * or to specify all necessary properties locally.
- */
- public void setJdoProperties(Properties jdoProperties) {
- this.jdoProperties = jdoProperties;
- }
- /**
- * Set the DataSource to be used by the PersistenceManagerFactory.
- * If set, this will override corresponding settings in JDO properties.
- * <p>Note: If this is set, the JDO settings should not define
- * a connection factory to avoid meaningless double configuration.
- */
- public void setDataSource(DataSource dataSource) {
- this.dataSource = dataSource;
- }
- /**
- * Initialize the PersistenceManagerFactory for the given location.
- * @throws IllegalArgumentException in case of illegal property values
- * @throws IOException if the properties could not be loaded from the given location
- * @throws JDOException in case of JDO initialization errors
- */
- public void afterPropertiesSet() throws IllegalArgumentException, IOException, JDOException {
- if (this.configLocation == null && this.jdoProperties == null) {
- throw new IllegalArgumentException("Either configLocation (e.g. '/kodo.properties') or jdoProperties must be set");
- }
- Properties props = new Properties();
- if (this.configLocation != null) {
- // load JDO properties from given location
- InputStream is = this.configLocation.getInputStream();
- try {
- props.load(is);
- }
- finally {
- is.close();
- }
- }
- if (this.jdoProperties != null) {
- // add given JDO properties
- props.putAll(this.jdoProperties);
- }
- // build factory instance
- this.persistenceManagerFactory = newPersistenceManagerFactory(props);
- if (this.dataSource != null) {
- // use given DataSource as JDO connection factory
- this.persistenceManagerFactory.setConnectionFactory(this.dataSource);
- }
- }
- /**
- * Subclasses can override this to perform custom initialization of the
- * PersistenceManagerFactory instance, creating it via the given Properties
- * that got prepared by this LocalPersistenceManagerFactoryBean
- * <p>The default implementation invokes JDOHelper's getPersistenceManagerFactory.
- * A custom implementation could prepare the instance in a specific way,
- * or use a custom PersistenceManagerFactory implementation.
- * @param prop Properties prepared by this LocalPersistenceManagerFactoryBean
- * @return the PersistenceManagerFactory instance
- * @see javax.jdo.JDOHelper#getPersistenceManagerFactory
- */
- protected PersistenceManagerFactory newPersistenceManagerFactory(Properties prop) {
- return JDOHelper.getPersistenceManagerFactory(prop);
- }
- /**
- * Return the singleton PersistenceManagerFactory.
- */
- public Object getObject() {
- return this.persistenceManagerFactory;
- }
- public Class getObjectType() {
- return (this.persistenceManagerFactory != null) ?
- this.persistenceManagerFactory.getClass() : PersistenceManagerFactory.class;
- }
- public boolean isSingleton() {
- return true;
- }
- /**
- * Close the PersistenceManagerFactory on context shutdown.
- */
- public void destroy() {
- logger.info("Closing JDO PersistenceManagerFactory");
- this.persistenceManagerFactory.close();
- }
- }